-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleM.hs
More file actions
59 lines (50 loc) · 1.55 KB
/
ExampleM.hs
File metadata and controls
59 lines (50 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module MicroKanren.ExampleMonadic where
import Control.Monad
import Control.Monad.Trans.State
import MicroKanren
import MicroKanren.Plain (LVar(..), CanUnify)
import MicroKanren.Cons
aANDb ∷ Logic (LVar Int)
aANDb = do
a ← fresh
b ← fresh
a === LVal 7
mplus (b === LVal 5)
(b === LVal 6)
runEx1, runEx2, runEx3 ∷ [LVar Int]
runEx1 = run $ fresh >>= (=== LVal 5)
runEx2 = run aANDb
runEx3 = run $ do
q ← fresh
q === LVal 5
LVal 6 === q
fives, sixes ∷ LVar Int → Logic (LVar Int)
fives x = mplus (x === LVal 5) (fives x)
sixes x = mplus (x === LVal 6) (sixes x)
runFives, run5and6 ∷ [LVar Int]
runFives = run $ fresh >>= fives
run5and6 = run $ do x ← fresh
fives x `mplus` sixes x
appendo ∷ (CanUnify α, Eq α) ⇒ LVar (LCons α) → LVar (LCons α) → LVar (LCons α) → Logic (LVar (LCons α))
appendo l s out = mplus
(do l === LVal empty
s === out)
(do h ← fresh
t ← fresh
l === LVal (cons h t)
res ← fresh
out === LVal (cons h res)
appendo t s res)
runAppendo ∷ [LVar (LCons Int)]
runAppendo = run $ do
q ← fresh
appendo q q (LVal $ fromList [1, 1])
--membero ∷ Eq α ⇒ LVar (LCons α) → LVar (LCons α) → Logic (LVar (LCons α))
--membero x l = conde
-- [do t ← fresh
-- x === LVal (LCons x t)
-- return t
-- ,do t ← fresh
-- h ← fresh
-- l === LVal (LCons h t)
-- membero x t]