diff --git a/demos/mk-workshop-2024/01-example.rkt b/demos/mk-workshop-2024/01-example.rkt index 8e23c3b..1c7d786 100644 --- a/demos/mk-workshop-2024/01-example.rkt +++ b/demos/mk-workshop-2024/01-example.rkt @@ -1,6 +1,6 @@ #lang racket/base -(require "mk.rkt") +(require "08-mk-compiled.rkt") (defrel (appendo l1 l2 l3) (conde diff --git a/demos/mk-workshop-2024/02-example-core.rkt b/demos/mk-workshop-2024/02-example-core.rkt index cb61caf..9f5d9f4 100644 --- a/demos/mk-workshop-2024/02-example-core.rkt +++ b/demos/mk-workshop-2024/02-example-core.rkt @@ -23,8 +23,14 @@ + + ;; What if I make a grammar mistake? #;(run 1 (q) (== q - (== q q))) \ No newline at end of file + (== q q))) + +#;(run 1 (q) + (fresh1 (x) + (+ 1 2))) \ No newline at end of file diff --git a/demos/mk-workshop-2024/05-example-with-binding.rkt b/demos/mk-workshop-2024/05-example-with-binding.rkt index 160de71..bb6862e 100644 --- a/demos/mk-workshop-2024/05-example-with-binding.rkt +++ b/demos/mk-workshop-2024/05-example-with-binding.rkt @@ -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)) @@ -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) diff --git a/demos/mk-workshop-2024/07-example-with-sugar.rkt b/demos/mk-workshop-2024/07-example-with-sugar.rkt index 1cc263f..99bf3d5 100644 --- a/demos/mk-workshop-2024/07-example-with-sugar.rkt +++ b/demos/mk-workshop-2024/07-example-with-sugar.rkt @@ -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)] diff --git a/demos/mk-workshop-2024/09-example-compiled.rkt b/demos/mk-workshop-2024/09-example-compiled.rkt index 11fe4f8..6a959f9 100644 --- a/demos/mk-workshop-2024/09-example-compiled.rkt +++ b/demos/mk-workshop-2024/09-example-compiled.rkt @@ -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))) \ No newline at end of file +(run* (l1 l2) + (appendo l1 (list 1 2 3 4))) \ No newline at end of file diff --git a/demos/mk-workshop-2024/11-example-with-check.rkt b/demos/mk-workshop-2024/11-example-with-check.rkt index 1ea5a03..335962d 100644 --- a/demos/mk-workshop-2024/11-example-with-check.rkt +++ b/demos/mk-workshop-2024/11-example-with-check.rkt @@ -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) \ No newline at end of file diff --git a/demos/mk-workshop-2024/12-example-matche.rkt b/demos/mk-workshop-2024/12-example-matche.rkt index 178bf50..bce8502 100644 --- a/demos/mk-workshop-2024/12-example-matche.rkt +++ b/demos/mk-workshop-2024/12-example-matche.rkt @@ -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)) \ No newline at end of file diff --git a/demos/mk-workshop-2024/13-routes.rkt b/demos/mk-workshop-2024/13-routes.rkt new file mode 100644 index 0000000..9b1c0da --- /dev/null +++ b/demos/mk-workshop-2024/13-routes.rkt @@ -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)) diff --git a/demos/mk-workshop-2024/14-foreign.rkt b/demos/mk-workshop-2024/14-foreign.rkt new file mode 100644 index 0000000..46c7715 Binary files /dev/null and b/demos/mk-workshop-2024/14-foreign.rkt differ diff --git a/demos/mk-workshop-2024/15-foreign-extension.rkt b/demos/mk-workshop-2024/15-foreign-extension.rkt new file mode 100644 index 0000000..de8dae2 Binary files /dev/null and b/demos/mk-workshop-2024/15-foreign-extension.rkt differ diff --git a/demos/mk-workshop-2024/16-occurs-check.rkt b/demos/mk-workshop-2024/16-occurs-check.rkt new file mode 100644 index 0000000..a300c8b --- /dev/null +++ b/demos/mk-workshop-2024/16-occurs-check.rkt @@ -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) + \ No newline at end of file diff --git a/demos/mk-workshop-2024/flights-data.rkt b/demos/mk-workshop-2024/flights-data.rkt new file mode 100644 index 0000000..088a1f8 --- /dev/null +++ b/demos/mk-workshop-2024/flights-data.rkt @@ -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))) \ No newline at end of file