Skip to content

Commit

Permalink
more demo
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelballantyne committed Sep 6, 2024
1 parent 362ea3d commit f9f71e5
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 17 deletions.
2 changes: 1 addition & 1 deletion demos/mk-workshop-2024/01-example.rkt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#lang racket/base

(require "mk.rkt")
(require "08-mk-compiled.rkt")

(defrel (appendo l1 l2 l3)
(conde
Expand Down
8 changes: 7 additions & 1 deletion demos/mk-workshop-2024/02-example-core.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@





;; What if I make a grammar mistake?

#;(run 1 (q)
(== q
(== q q)))
(== q q)))

#;(run 1 (q)
(fresh1 (x)
(+ 1 2)))
13 changes: 11 additions & 2 deletions demos/mk-workshop-2024/05-example-with-binding.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

(require "04-mk-with-binding.rkt")

;; DrRacket understands binding structure now

(defrel (appendo l1 l2 l3)
(disj2
(conj2 (== l1 '()) (== l2 l3))
Expand All @@ -21,8 +23,15 @@
(appendo l1 l2 (cons 1 (cons 2 (cons 3 (cons 4 '())))))))))


;; DrRacket understands binding structure now,
;; and unbound references are errors.
;; Unbound or incorrect references are an error now.

#;(run 1 (q)
(+ 1 2))



;; The compiler receives alphatized syntax.

(run 1 (q)
(fresh1 (x)
(fresh1 (x)
Expand Down
3 changes: 3 additions & 0 deletions demos/mk-workshop-2024/07-example-with-sugar.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

(require "06-mk-with-sugar.rkt")

;; The example uses syntactic sugar, but the compiler receives
;; core-language syntax.

(defrel (appendo l1 l2 l3)
(conde
[(== l1 '()) (== l2 l3)]
Expand Down
4 changes: 2 additions & 2 deletions demos/mk-workshop-2024/09-example-compiled.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
(appendo l1 l2 (list 1 2 3 4)))

;; What if I call a relation with the wrong number of arguments?
#;(run* (l1 l2)
(appendo l1 (list 1 2 3 4)))
(run* (l1 l2)
(appendo l1 (list 1 2 3 4)))
2 changes: 1 addition & 1 deletion demos/mk-workshop-2024/11-example-with-check.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
(submod ".." def))

(run* (l1 l2)
;; What if I make an arity mistake?
;; Now arity mistakes get friendly errors.
(appendo l1 (list 1 2 3 4))))

(require 'use)
18 changes: 8 additions & 10 deletions demos/mk-workshop-2024/12-example-matche.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
(== l3 (cons first res))
(appendo rest l2 res)))))

;; We can define the same relation using a pattern matching extension.

(defrel/matche (appendo/m l1 l2 l3)
[('() l l)]
[((cons first rest)
l2
(cons first res))
(appendo rest l2 res)])


#;(run 3 (n)
(peano n))

[((cons first rest) l2 (cons first res))
(appendo/m rest l2 res)])

(begin
(print-relation-code appendo/m)
;; Shows the macro-expanded code that the miniKanren compiler receives.
#;(print-relation-code appendo/m)

;; Shows the optimized code after constant prop and dead code elim.
(print-relation-code/after-dead-code appendo/m))
23 changes: 23 additions & 0 deletions demos/mk-workshop-2024/13-routes.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#lang racket

(require hosted-minikanren)

(defrel (route origin end path)
(conde
[(== origin end) (== path '())]
[(fresh (hop remainder)
(== path (cons (list origin hop) remainder))
(absento origin remainder)
(direct origin hop)
(route hop end remainder))]))

(defrel (direct a b)
(conde
[(== a "BOS") (== b "SEA")]
[(== a "HOU") (== b "SLC")]
[(== a "SEA") (== b "DEN")]
[(== a "SEA") (== b "BOS")]
[(== a "DEN") (== b "HOU")]
[(== a "SLC") (== b "SFO")]))

(run* (q) (route "SEA" "HOU" q))
Binary file added demos/mk-workshop-2024/14-foreign.rkt
Binary file not shown.
Binary file added demos/mk-workshop-2024/15-foreign-extension.rkt
Binary file not shown.
27 changes: 27 additions & 0 deletions demos/mk-workshop-2024/16-occurs-check.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#lang racket/base

(require hosted-minikanren
hosted-minikanren/inspect)

(defrel (r1 a b)
(fresh (x y)
(== a (list 1 x))
(== b (list 1 y))
(== x y)))

#;(print-relation-code/after-occurs-check r1)





#;(defrel (r2 x y)
(fresh (a b)
(== x (cons '5 '6))
(goal-from-expression
(void #| unknown racket code |#))
(== a b)
(== y x)))

#;(print-relation-code/after-occurs-check r2)

35 changes: 35 additions & 0 deletions demos/mk-workshop-2024/flights-data.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#lang racket

(provide download-flights-csv create-flights-table query-flight-rows)

(require hosted-minikanren
hosted-minikanren/demos/icfp2024/facts
csv-reading
racket/list
net/url
net/url-string
db sql)

(define SOURCE
"https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat")

;; () -> [Listof [Listof String String]]
;; Should produce a list of flights, each flight a list matching the data schema
(define (download-flights-csv)
(for/list ([row (csv->list (get-pure-port (string->url SOURCE)))])
(list (third row) (fifth row))))

(define (create-flights-table v)
(void))

(define-facts-table flights [flightfrom flightto]
#:initial-data (download-flights-csv))

(defrel (direct a b)
(query-facts flights a b))

(define (query-flight-rows conn a-arg b-arg)
(run* (a b)
(== a a-arg)
(== b b-arg)
(direct a b)))

0 comments on commit f9f71e5

Please sign in to comment.