Skip to content

Instantly share code, notes, and snippets.

View frenchy64's full-sized avatar

Ambrose Bonnaire-Sergeant frenchy64

  • Madison, Wisconsin
View GitHub Profile
(ns logic-introduction.core
(:refer-clojure :exclude [inc reify ==])
(:use [clojure.core.logic minikanren prelude nonrel match disequality]))
(defn geto [key env value]
"Succeed if type association [key :- value] is found in vector env."
(matche [env]
([[[key :- value] . _]])
([[_ . ?rest]] (geto key ?rest value))))
(def pm2 (pattern-matrix [(pattern-row [wildcard (pattern false) (pattern true)] :a1)
(pattern-row [(pattern false) (pattern true) wildcard] :a2)
(pattern-row [wildcard wildcard (pattern false)] :a3)
(pattern-row [wildcard wildcard (pattern true)] :a4)]
'[x y z]))
(compile pm2)
;#match.core.SwitchNode[
(def pm2 (pattern-matrix [(pattern-row [wildcard (pattern false) (pattern true)] :a1)
(pattern-row [(pattern false) (pattern true) wildcard] :a2)
(pattern-row [wildcard wildcard (pattern false)] :a3)
(pattern-row [wildcard wildcard (pattern true)] :a4)]
'[x y z]))
(compile pm2)
;; (match [x y z]
;; [_ f# t#] 1
;; [f# t# _ ] 2
;; [_ _ f#] 3
;; [_ _ t#] 4)
(def pm2 (pattern-matrix [(pattern-row [wildcard (pattern false) (pattern true)] :a1)
(pattern-row [(pattern false) (pattern true) wildcard] :a2)
(pattern-row [wildcard wildcard (pattern false)] :a3)
(pattern-row [wildcard wildcard (pattern true)] :a4)]
match.test.core=> (-> (build-matrix [x]
[[1]] 1
[(isa? Object)] 2)
compile
to-clj)
(clojure.core/cond
(<TypePattern: class java.lang.Object > x) 2
(#match.core.VectorPattern[[1], 0] x) (clojure.core/cond
(<LiteralPattern: 1> x0) (clojure.core/cond
(deftyped
addInteger
[Integer :> [Integer :> Integer]]
[x y]
(+ x y))
(deftyped
addDouble
[Double :> [Double :> Double]]
match.core=> (-> (build-matrix [x]
[1] 2
[(a)] 1)
compile
to-clj
source-pprint)
(cond
(sequential? x) (cond
(= x nil) 1
:else (throw
(defmacro expand-pattern [vars & clauses]
`(-> (build-matrix ~vars ~@clauses)
compile
to-clj
source-pprint))
(match [x y]
([1 2] |
[2 3] |
[3 4]) :a0
[10 20] :a1)
(match x
(1 |
2 |
3) :a0
1 :a1)
(match [x y]
([1 2] |
[2 3] |
[3 4]) :a0