This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathedcg.pl
75 lines (62 loc) · 1.55 KB
/
edcg.pl
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
:- use_module(library(edcg)).
% Playing around with microKanren implemented using extended DCG syntax
edcg:acc_info(stream,St,In,Out,Out=[St|In]).
edcg:acc_info(subs,Sub,In,Out,Out=[Sub|In]).
edcg:acc_info(counter,In,In,Out,succ(In,Out)).
edcg:pred_info(call_fresh,3,[counter,subs]).
edcg:pred_info(conj,3,[counter,subs]).
edcg:pred_info(disj,3,[counter,subs]).
edcg:pred_info(lookup,2,[subs]).
edcg:pred_info(state,1,[counter,subs]).
edcg:pred_info(unify,2,[counter,subs,stream]).
edcg:pred_info(unification,2,[subs]).
edcg:pred_info(unification_,2,[subs]).
edcg:pred_info(walk,2,[subs]).
edcg:pred_info(var,1,[counter]).
var(var(N)) -->>
[N]:counter.
walk(U,V) -->>
U=var(_),
lookup(U,V0),
!,
walk(V0,V).
walk(U,U) -->>
[].
lookup(U,V) -->>
Subs/subs,
memberchk(U-V,Subs).
state(state(Subs,C)) -->>
C/counter,
Subs/subs.
unify(U0,V0) -->>
walk(U0,U),
walk(V0,V),
unification_(U,V),
!,
C/counter,
Subs/subs,
[state(Subs,C)]:stream.
unify(_,_) -->>
[]:stream.
unification_(var(N),var(N)) -->> [].
unification_(var(N),T) -->> [var(N)-T]:subs.
unification_(T,var(N)) -->> [var(N)-T]:subs.
unification_(Ua-Ub,Va-Vb) -->>
unification(Ua,Va),
unification(Ub,Vb).
unification_(U,V) -->>
U == V.
call_fresh(X,Goal,Str) -->>
var(X),
state(St),
call(Goal,St,Str).
disj(Goal1,Goal2,Str) -->>
state(St),
call(Goal1,St,Str1),
call(Goal2,St,Str2),
mplus(Str1,Str2,Str).
conj(Goal1,Goal2,Str) -->>
state(St),
call(Goal1,St,Str0),
bind(Str0,Goal2,Str).
% todo.