-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRemyR.pro
76 lines (63 loc) · 1.68 KB
/
RemyR.pro
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
% random binary tree with N internal nodes
% (built with ->/2) and variables in Vs as leaves
remy(N,Tree,Vs):-remyR(N,(->),Tree,Vs).
remy(N,Tree):-remyR(N,(*),Tree,Vs),maplist(=(x),Vs).
remy_sk(N,Tree):-remyR(N,(*),Tree,Vs),maplist(ran_sk,Vs).
ran_sk(X):-0=:=random(2)->X=s;X=k.
remyExpr(N,FunList,Tree,Vs):-
Funs=..[funs|FunList],
Size is 2*N+1,
functor(Links,x,Size),
set(Links,0,0),
remyStep(0,N,Links),
get(Links,0,Root),
ranlinks2bin(Root,Funs,Links,Tree,Leaves,[]),
Vs=Leaves.
ranlinks2bin(K,Funs,Links,Tree,Ts1,Ts3):-
( K mod 2 =:= 0 -> Ts1=[Tree|Ts3]
; get(Links,K,A),
K1 is K+1,
get(Links,K1,B),
ranlinks2bin(A,Funs,Links,X,Ts1,Ts2),
ranlinks2bin(B,Funs,Links,Y,Ts2,Ts3),
functor(Funs,_,OpCount),
I0 is random(OpCount),I is I0+1,arg(I,Funs,Fun),
functor(Tree,Fun,2),
arg(1,Tree,X),
arg(2,Tree,Y)
).
remyR(N,Fun,Tree,Vs):-
Size is 2*N+1,
functor(Links,x,Size),
set(Links,0,0),
remyStep(0,N,Links),
get(Links,0,Root),
links2bin(Root,Fun,Links,Tree,Leaves,[]),
Vs=Leaves.
links2bin(K,Fun,Links,Tree,Ts1,Ts3):-
( K mod 2 =:= 0 -> Ts1=[Tree|Ts3]
; get(Links,K,A),
K1 is K+1,
get(Links,K1,B),
links2bin(A,Fun,Links,X,Ts1,Ts2),
links2bin(B,Fun,Links,Y,Ts2,Ts3),
functor(Tree,Fun,2),
arg(1,Tree,X),
arg(2,Tree,Y)
).
set(Links,I0,Tree):-I is I0+1,nb_linkarg(I,Links,Tree).
get(Links,I0,Tree):-I is I0+1,arg(I,Links,Tree).
remyStep(N,N,_).
remyStep(I0,N,Links):-I0<N,
X is random(4*I0+2),
I is I0+1,
B is X mod 2,
K is X // 2,
J is I*2,
J1 is J-B,
J2 is J-1+B,
set(Links,J1,J),
get(Links,K,Y),set(Links,J2,Y),
J3 is J-1,
set(Links,K,J3),
remyStep(I,N,Links).