Skip to content

Instantly share code, notes, and snippets.

@bsless

bsless/mk.scm Secret

Last active December 17, 2021 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsless/ef07a7ab21a614720a36c698b5121d6e to your computer and use it in GitHub Desktop.
Save bsless/ef07a7ab21a614720a36c698b5121d6e to your computer and use it in GitHub Desktop.
(define-syntax Zzz
(syntax-rules ()
((_ g) (lambda (s/c) (lambda () (g s/c))))))
(define-syntax conj+
(syntax-rules ()
((_ g) (Zzz g))
((_ g0 g ...) (conj (Zzz g0) (conj+ g ...)))))
(define-syntax disj+
(syntax-rules ()
((_ g) (Zzz g))
((_ g0 g ...) (disj (Zzz g0) (disj+ g ...)))))
(define-syntax fresh
(syntax-rules ()
((_ () g0 g ...) (conj+ g0 g ...))
((_ (x0 x ...) g0 g ...)
(call/fresh
(lambda (x0)
(fresh (x ...) g0 g ...))))))
(define-syntax conde
(syntax-rules ()
((_ (g0 g ...) ...) (disj+ (conj+ g0 g ...) ...))))
(defmacro define-syntax-rules
[name & patterns]
`(do
(def ~name
(fn ~name [~'&form ~'&env & body#]
(m/rewrite ~'&form ~@patterns)))
(. (var ~name) (setMacro))
(var ~name)))
(define-syntax-rules Zzz
(_ ?g) (fn [s] (fn [] (?g s))))
(define-syntax-rules conj+
(_ ?g) (Zzz ?g)
(_ ?g0 . !g ...) (conj (Zzz ?g0) (conj+ . !g ...)))
(define-syntax-rules disj+
(_ ?g) (Zzz ?g)
(_ ?g0 . !g ...) (disj (Zzz ?g0) (disj+ . !g ...)))
(define-syntax-rules fresh
(_ [] ?g0 . !g ...) (conj+ g0 . !g ...)
(_ [?x0 . !x ...] ?g0 . !g ...)
(call-fresh
(fn [?x0]
(fresh [. !x ...] g0 . !g ...))))
(define-syntax-rules conde
(_ . [!g ..!n] ... ) (disj+ . (conj+ . !g ..!n) ... ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment