/
ikanren.idr
251 lines (188 loc) · 6.63 KB
/
ikanren.idr
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
data LVar = MkLVar Int
Eq LVar where
(MkLVar x) == (MkLVar y) = x == y
Show LVar where
show (MkLVar x) = "LVar_" ++ show x
data Term a = LVarTerm LVar | Data a | (::) (Term a) (Term a) | Nil
Eq a => Eq (Term a) where
(LVarTerm x) == (LVarTerm y) = x == y
(Data x) == (Data y) = x == y
(x :: xs) == (y :: ys) = (x == y) && (xs == ys)
Nil == Nil = True
_ == _ = False
Show a => Show (Term a) where
show (LVarTerm lv) = show lv
show (Data x) = show x
show (x :: xs) = (show x) ++ "::" ++ (show xs)
show Nil = "Nil"
SMap : Type -> Type
SMap a = List (LVar, Term a)
total lookup : SMap a -> LVar -> Maybe (Term a)
lookup [] var = Nothing
lookup ((entry_k, entry_v) :: smap) var =
if entry_k == var
then Just entry_v
else lookup smap var
total addSubstitution: SMap a -> LVar -> Term a -> SMap a
addSubstitution s v t = (v, t) :: s
-- LookupTheorem1 : (v: LVar) -> (t: Term) -> (s: SMap) -> lookup (addSubstitution s v t) v = Just t
-- LookupTheorem1 v t s = ?P1_rhs1
walk : SMap a -> Term a -> Term a
walk s (LVarTerm v) = case (lookup s v) of
Just t => walk s t
Nothing => LVarTerm v
walk s x = x
unify : Eq a => Term a -> Term a -> SMap a -> Maybe (SMap a)
unify t u smap =
let t = walk smap t
u = walk smap u in
-- Terms that walk to equal values always unify, but add nothing
-- to the substitution map
if t == u
then Just smap
else case (t, u) of
(LVarTerm lv, _) => Just (addSubstitution smap lv u)
(_, LVarTerm lv) => Just (addSubstitution smap lv t)
((x :: xs), (y :: ys)) => (unify x y smap) >>= (unify xs ys)
(Nil, Nil) => Just smap
(Data x, Data y) => if x == y
then Just smap
else Nothing
(_, _) => Nothing
-- UnifyTransitive : (s : SMap) -> (t: Term) -> (u : Term) -> unify s t u = unify s u t
-- Lazy streams
data LazyStream a = EmptyStream | MatureStream a (LazyStream a) | ImmatureStream (Inf (LazyStream a))
Semigroup (LazyStream a) where
EmptyStream <+> y = y
(MatureStream head next) <+> y = MatureStream head (next <+> y)
(ImmatureStream x) <+> y = ImmatureStream (y <+> x)
Monoid (LazyStream a) where
neutral = EmptyStream
Functor LazyStream where
map func EmptyStream = EmptyStream
map func (MatureStream head next) = MatureStream (func head) (map func next)
map func (ImmatureStream x) = ImmatureStream (map func x)
Applicative LazyStream where
pure a = MatureStream a EmptyStream
_ <*> EmptyStream = EmptyStream
EmptyStream <*> y = EmptyStream
(MatureStream func funcs) <*> (MatureStream y ys) = MatureStream (func y) (funcs <*> ys)
(ImmatureStream funcs) <*> ys = ImmatureStream (funcs <*> ys)
funcs <*> (ImmatureStream ys) = ImmatureStream (funcs <*> ys)
Monad LazyStream where
EmptyStream >>= _ = EmptyStream
(MatureStream head next) >>= func = (func head) <+> (next >>= func)
(ImmatureStream x) >>= func = ImmatureStream (x >>= func)
realizeStreamHead : LazyStream a -> LazyStream a
realizeStreamHead (ImmatureStream s) = realizeStreamHead s
realizeStreamHead s = s
take : Nat -> LazyStream a -> List a
take Z _ = []
take (S n) s = case realizeStreamHead s of
MatureStream x xs => x :: take n xs
_ => []
realizeAll : LazyStream a -> List a
realizeAll EmptyStream = []
realizeAll (MatureStream x xs) = x :: realizeAll xs
realizeAll (ImmatureStream s) = realizeAll s
-- fours : LazyStream Nat
-- fours = MatureStream 4 (ImmatureStream fours)
-- fives : LazyStream Nat
-- fives = MatureStream 5 (ImmatureStream fives)
-- diverge : LazyStream Nat
-- diverge = ImmatureStream diverge
-- take 4 (fours <+> fives) = [4, 5, 4, 5]
-- take 4 (fours <+> diverge) = take 4 (diverge <+> fours) = [4, 4, 4, 4]
-- Interpreter State
record State a where
constructor MkState
smap : SMap a
nextId : Int
emptyState : State a
emptyState = MkState [] 0
-- Goal functions
Goal : Type -> Type
Goal a = State a -> LazyStream (State a)
succeed : Goal a
succeed = pure
fail : Goal a
fail _ = neutral
infixr 10 ===
(===) : Eq a => Term a -> Term a -> Goal a
(===) u v state =
case unify u v (smap state) of
Just smap' => pure ( record { smap = smap' } state )
Nothing => neutral
callFresh : (LVar -> Goal a) -> Goal a
callFresh f state =
let goal = f (MkLVar (nextId state))
state' = record { nextId $= (+ 1) } state in
goal state'
delay : Goal a -> Goal a
delay g state = ImmatureStream (g state)
disj : Goal a -> Goal a -> Goal a
disj g1 g2 state = ((delay g1) state) <+> ((delay g2) state)
conj : Goal a -> Goal a -> Goal a
conj g1 g2 state = (g1 state) >>= g2
-- Sugar
implicit lvarTerm : LVar -> Term a
lvarTerm lv = LVarTerm lv
implicit dataTerm : Eq a => a -> Term a
dataTerm s = Data s
term syntax fresh {x} "in" [body] = callFresh (\x => body)
conjList : List (Goal a) -> Goal a
conjList = foldr conj succeed
conde : List (List (Goal a)) -> Goal a
conde conjClauses = foldr disj fail
(map (foldr conj succeed)
conjClauses)
(&&) : Goal a -> Goal a -> Goal a
g1 && g2 = conj g1 g2
(||) : Goal a -> Goal a -> Goal a
g1 || g2 = disj g1 g2
run : Nat -> Goal a -> List (SMap a)
run n g = map smap (take n (g emptyState))
runComplete : Goal a -> List (SMap a)
runComplete g = map smap (realizeAll (g emptyState))
-- List relations
conso : Eq a => Term a -> Term a -> Term a -> Goal a
conso a d l = (a :: d) === l
firsto : Eq a => Term a -> Term a -> Goal a
firsto a l = fresh d in (a :: d) === l
resto : Eq a => Term a -> Term a -> Goal a
resto d l = fresh a in (a :: d) === l
nilo : Eq a => Term a -> Goal a
nilo l = l === Nil
appendo : Eq a => Term a -> Term a -> Term a -> Goal a
appendo front back l =
fresh a in
fresh d in
fresh rec in
disj
(nilo front && back === l)
((a :: d) === front &&
appendo d back rec &&
(a :: rec) === l)
-- foobar : Goal String
-- foobar =
-- fresh a in
-- disj (a === "foo")
-- (a === "bar")
-- foos : Term -> Goal String
-- foos a =
-- disj (a === "foo")
-- (foos a)
-- bars : Term -> Goal String
-- bars a =
-- disj (a === "bar")
-- (bars a)
-- foobars : Term -> Goal String
-- foobars a = disj (foos a) (bars a)
-- condeTest : Goal String
-- condeTest =
-- fresh a in
-- fresh b in
-- conde [[a === "One", b === "Two"],
-- [a === "Alpha", b === "Beta"]]
-- main: IO ()
-- main = putStrLn(show (runComplete condeTest))