-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern-match
More file actions
253 lines (245 loc) · 16.3 KB
/
pattern-match
File metadata and controls
253 lines (245 loc) · 16.3 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
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
252
load(basic);
/*------------------------------------------------------------------------------------------------------*/
load("C:\\Users\\joker\\Documents\\Maxima-Docs\\segment-new.lisp")$
/* Get segment from http://www.cs.berkeley.edu/~fateman/lisp/segment-new.lisp */
/*------------------------------------------------------------------------------------------------------*/
use_segment(true);
/*------------------------------------------------------------------------------------------------------*/
load("C:/Users/joker/Documents/Maxima-Docs/positions-trials.mac")$
/*------------------------------------------------------------------------------------------------------*/
listcomplementt(k,l,[n]):=block([],if(n=[]) then map(lambda([x],k:delete(x,k)),[l]) else map(lambda([x],k:delete(x,k,n)),[l]),k)$
/*------------------------------------------------------------------------------------------------------*/
listcomplement(k,l,[n]):=block([temp:l,s:k],if(n=[]) then for i in temp do s:listcomplementt(s,i) else for i in temp do s:listcomplementt(s,i,n),s)$
/*------------------------------------------------------------------------------------------------------*/
take(x,m,n):=block([temp:[]],part(x,makelist(i,i,m,n)))$
/*------------------------------------------------------------------------------------------------------*/
init(lis):=block([lis:lis],take(lis,1,length(lis)-1))$
/*------------------------------------------------------------------------------------------------------*/
prefix_p(seq1,seq2):=block(if(slength(string(seq1))<= slength(string(seq2)))
then if(seq1="___") then (is(part(charlist(seq1),[1,3])=part(charlist(seq2),[1,3])))
elseif(seq1="__") then
(is(part(charlist(seq2),[1,2])=["_","_"]) and (if(slength(string(seq2))>2) then part(charlist(seq2),[3])#["_"]))
elseif(seq1="_") then is(first(charlist(seq2))="_") and (if(slength(string(seq2))>=2) then part(charlist(seq2),[2])#["_"]))$
/*------------------------------------------------------------------------------------------------------*/
getsymbol_name(sym):=substring(symbol_name(sym),2)$
/*------------------------------------------------------------------------------------------------------*/
:lisp(defun $symbol_name(sym)(symbol-name sym))
/*------------------------------------------------------------------------------------------------------*/
propagate(pop1_stack1,pat,item):=block([final,pop_temp,pop_return:[],pop1_stack1:reverse(pop1_stack1),loc1_t,pat:pat,item:item],
if(length(pop1_stack1)>1) then
(loc1_t:copylist(first(init(pop1_stack1))),
for i:1 thru length(pop1_stack1)-1 do
pop1_stack1[i][2]:[last(last(pop1_stack1[i+1]))] ,
pop_return:append(reverse(pop1_stack1),pop_return),
pop_return:append(pop_return,[[pat[item],[last(last(loc1_t))]]]),
pop_return:substinpart(piece:[first(piece),second(piece),init(last(piece))],pop_return,1),
final:pop_return)
else
( pop_temp:pop(pop1_stack1),
if(length(last(pop_temp))>1) then
(push([pat[item],[last(last(pop_temp))]],pop_return),
push(append(init(pop_temp),[init(last(pop_temp))]),pop_return),
final:pop_return)
else (push([pat[item],last(pop_temp)],pop_return),
push(append(init(pop_temp)),pop_return),
final:pop_return)),
(final))$
/*------------------------------------------------------------------------------------------------------*/
propagate2(pop1_stack1,pat,item):=block([atom_type,all_lasts:[],last_lis_col:[],coll_rest,final,pop_temp,pop_return:[],pop1_stack1:pop1_stack1,
loc1_t,pat:pat,item:item,last_lis,temp1],
if(length(sublist(first(pop1_stack1),listp))>1) then atom_type:false else atom_type:true,
if(atom_type) then pop1_stack1:map(lambda([x],[first(x),rest(x)]),pop1_stack1),
all_lasts:flatten(map(last,rest(pop1_stack1))),
pop1_stack1:substinpart(piece:flatten(cons(piece,all_lasts)),pop1_stack1,1,3),
last_lis_col:[],
if(length(pop1_stack1)>1) then
(loc1_t:copylist(first((pop1_stack1))),
for i:2 thru length(pop1_stack1) do pop1_stack1[i][2]:[last(loc1_t)[i-1]],
col_rest:listcomplement(last(loc1_t),flatten(map(last,rest(pop1_stack1)))),
pop1_stack1:substinpart(piece:[],pop1_stack1,1,3),
pop_return:append(pop_return,[[pat[item],[first(col_rest)],rest(col_rest)]]),
pop_return:append(pop1_stack1,pop_return),
final:pop_return)
else
(
pop_temp:pop(pop1_stack1),
push([pat[item],[first(last(pop_temp))],rest(last(pop_temp))],pop_return),
push([first(pop_temp),second(pop_temp)],pop_return),
final:pop_return),final)$
/*------------------------------------------------------------------------------------------------------*/
propagate_3(pop1_stack1,pat,item):=block([last_lis_col:[],last_lis_col3:[],help_col3:[],final,pop_temp,pop_return:[],
pop1_stack1:pop1_stack1,loc1_t,pat:pat,item:item,last_lis,temp1],
if(length(pop1_stack1)>1) then
(loc1_t:copylist(first(init(pop1_stack1))),
last_lis:(map(last,pop1_stack1)),
for item in reverse(last_lis) do last_lis_col:append(item,last_lis_col),
for i:2 thru length(pop1_stack1) do
if(atom(last_lis_col[i-1])=false and length(last_lis_col[i-1])=1) then pop1_stack1[i][2]:last(last_lis_col[i-1]) else
pop1_stack1[i][2]:[(last_lis_col[i-1])],
pop_return:append([first(loc1_t),second(loc1_t)],pop_return),
pop_return:append(reverse(rest(pop1_stack1)),[pop_return]),
for item in (map(last,(pop_return))) do
last_lis_col3:append(last_lis_col3,[apply(segment,item)]),
for item in reverse(last_lis_col3) do if(atom(item)) then help_col3:append([item],help_col3) else help_col3:append(item,help_col3),
temp1:listcomplement(last_lis_col,help_col3),
pop_return:append(pop_return,[[pat[item],[],(temp1)]]),
final:pop_return)
else
( pop_temp:pop(pop1_stack1),
push([pat[item],[],(last(pop_temp))],pop_return),
push([first(pop_temp),second(pop_temp)],pop_return),
final:pop_return),final)$
/*------------------------------------------------------------------------------------------------------*/
disect(pat,input):=block([chk_patt,inp_map,pat:pat,temp_container:[],pat_subl,temp:[],loc1,loc2,loc3,container,loc1_t,contain1,
loc2_t,loc1_t1,loc1_t2,pop1,pop1_stack:[],pop1_stack1:[]],
chk_patt:sublist_indices(pat,lambda([x],prefix_p("_",getsymbol_name(x)) or prefix_p("__",getsymbol_name(x)))),
if(length(chk_patt)<=length(input)) then (
pat:cons(___pivot,pat),
inp_map:reverse(generate_mappings(input)),
input:map(last,inp_map),
container:sublist_indices(pat,lambda([x],prefix_p("_",getsymbol_name(x)))),
container1:sublist_indices(pat,lambda([x],prefix_p("__",getsymbol_name(x)) or prefix_p("___",getsymbol_name(x)))),
if(container#[] and container1=[] and last(container)=length(input)) then (
(for i:1 thru length(input) do temp:append(temp,[[pat[i],input[i]]])))
else (
for item:1 thru length(pat) do
if(prefix_p("_",getsymbol_name(pat[item]))) then
(
( container:sublist_indices(pat,lambda([x],prefix_p("_",getsymbol_name(x)))),
container:last(container),
contain1:sublist_indices(pat,lambda([x],prefix_p("__",getsymbol_name(x)) or prefix_p("___",getsymbol_name(x)))),
if(length(input)=container+1 and contain1=[] ) then
(for i:1 thru length(input) do temp:append(temp,[[pat[i],input[i]]]))
else (
pat_subl:take(pat,1,item-1),
container:sublist_indices(pat_subl,lambda([x],prefix_p("__",getsymbol_name(x))or
prefix_p("___",getsymbol_name(x)))),
if(container#[]) then (container:last(container),
pop1_stack1:[],
loc1_t:sublist_indices(temp,lambda([x],prefix_p("__",getsymbol_name(first(x)))or
prefix_p("___",getsymbol_name(first(x))))),
pop1_stack1:take(temp,last(loc1_t),length(temp)),
temp:take(temp,1,last(loc1_t-1)),
pop1_stack1:propagate(pop1_stack1,pat,item),
temp:append(temp,(pop1_stack1)))
) ))
elseif(prefix_p("__",getsymbol_name(pat[item]))) then
(if(item=1) then
push([pat[item],[first(input)],rest(input)],temp)
else (
pat_subl:take(pat,1,item-1),
container:sublist_indices(pat_subl,lambda([x],prefix_p("__",getsymbol_name(x))or prefix_p("___",getsymbol_name(x)))),
if(container#[]) then (
container:last(container),
pop1_stack1:[],
temp:sublist(temp,lambda([x],x#[parse_string("___pivot"),[]])),
loc2_t:sublist_indices(temp,lambda([x],prefix_p("__",getsymbol_name(first(x)))or
prefix_p("___",getsymbol_name(first(x))))),
pop1_stack1:take(temp,last(loc2_t),length(temp)),
temp:take(temp,1,last(loc2_t-1)),
pop1_stack1:propagate2(pop1_stack1,pat,item),
pop1_stack1:sublist(pop1_stack1,lambda([x],x#[parse_string("___pivot"),[]])),
temp:append(temp,pop1_stack1))
else (loc2_t:pop(temp),
push([first(loc2_t),second(loc2_t)],temp),
temp:append(temp,[[pat[item],[first(last(loc2_t))],rest(last(loc2_t))]]))
))
elseif(prefix_p("___",getsymbol_name(pat[item]))) then
if(item=1) then
push([pat[item],[],input],temp)
else (
pat_subl:take(pat,1,item-1),
container:sublist_indices(pat_subl,lambda([x],prefix_p("__",getsymbol_name(x))or prefix_p("___",getsymbol_name(x)))),
if(container#[]) then (
container:last(container),
pop1_stack1:[],
temp:sublist(temp,lambda([x],x#[parse_string("___pivot"),[]])),
loc2_t:sublist_indices(temp,lambda([x],prefix_p("__",getsymbol_name(first(x)))or
prefix_p("___",getsymbol_name(first(x))))),
pop1_stack1:take(temp,last(loc2_t),length(temp)),
temp:take(temp,1,last(loc2_t-1)),
pop1_stack1:propagate_3(pop1_stack1,pat,item),
temp:append(temp,(pop1_stack1)))
else (loc2_t:pop(temp),
push([first(loc2_t),second(loc2_t)],temp),
temp:append(temp,[[pat[item],[],(last(loc2_t))]]))
)),
pop1_stack1:revert_mappings(inp_map,input),
for item in pop1_stack1 do temp:subst(last(item),first(item),temp),
if length(temp)+1>= length(pat) then temp:temp else temp:[]
),flatend(temp) )$
/*------------------------------------------------------------------------------------------------------*/
flatend(temp):=sublist(map(lambda([x],if(length(x)>2) then
[first(x),append(second(x),(last(x)))] else x),temp),lambda([x],first(x)#parse_string("___pivot")))$
/*------------------------------------------------------------------------------------------------------*/
arrange(pat,temp):=block([final:[],pat:rest(pat)],for item in pat do if(item#[]) then
(push(first(sublist(temp,lambda([x],first(x)=item))),final),temp:delete(item,temp)),(reverse(final)))$
/*------------------------------------------------------------------------------------------------------*/
generate_mappings(lis):=block([temp:[]],for item in lis do push([item,gensym()],temp),temp)$
/*------------------------------------------------------------------------------------------------------*/
revert_mappings(lis,inp):=block([temp:[],col_sym:[]],col_sym:map(first,lis),
for item:1 thru length(input) do push([input[item],col_sym[item]],temp),temp)$
/*------------------------------------------------------------------------------------------------------*/
pat_lists(patt,input):=block([out:[],final:[],symb,patt_temp1,patt_temp2,patt_sym:[],inpt_sym:[],inpt_temp1,
inpt_temp2,patt_st1:[],patt:patt,input:input,lis_pat],
lis_pat:sublist(patt,listp),
if length(lis_pat)=0 then disect(patt,input)
else ( patt_temp1:enum_positions(patt),
patt_temp1:reverse(sublist(patt_temp1,lambda([x],atom(first(x))=false))),
patt_temp2:sublist(patt_temp1,lambda([x],test_subl(x))),
patt_temp2:map(first,patt_temp2),
for i in patt_temp2 do
for item:1 thru length(i) do
if(atom(i[item])=false) then (symb:parse_string(concat("_",gensym())),push([i[item],symb],patt_sym)),
for item in patt_sym do patt_temp2:subst(last(item),first(item),patt_temp2),
inpt_temp1:enum_positions(input),
inpt_temp1:reverse(sublist(inpt_temp1,lambda([x],atom(first(x))=false))),
inpt_temp2:sublist(inpt_temp1,lambda([x],test_subl(x))),
inpt_temp2:map(first,inpt_temp2),
for i in inpt_temp2 do
for item:1 thru length(i) do
if(atom(i[item])=false) then (symb:gensym(),push([i[item],symb],inpt_sym)),
for item in inpt_sym do inpt_temp2:subst(last(item),first(item),inpt_temp2),
patt_sym:reverse(patt_sym),inpt_sym:reverse(inpt_sym),
if(sort(map(last,patt_temp1))=sort(map(last,inpt_temp1)))
then (
for item:1 thru length(patt_sym) do (
push(disect(first(patt_sym[item]),first(inpt_sym[item])),final)),
for item:1 thru length(patt_temp2) do (
push(disect(patt_temp2[item],inpt_temp2[item]),final)),
for item in inpt_sym do inpt_temp2:subst(first(item),last(item),inpt_temp2))
),
for item in patt_sym do final:subst(first(item),last(item),final),
for item in inpt_sym do final:subst(first(item),last(item),final),
for item:1 thru length(patt) do if(listp(patt[item])) then (
symb:parse_string(concat("_",gensym())),push([patt[item],symb],patt_sym),patt[item]:symb),
/*print("patt_sym:",patt_sym,"inpt_sym:",inpt_sym), */
push(disect(patt,input),final),
for item in patt_sym do final:subst(first(item),last(item),final),
for item in final do out:append(item,out),out:sublist(out,lambda([x],atom(first(x)))),out)$
/*------------------------------------------------------------------------------------------------------*/
mapss(ss):=block([temp:[],symb],for item in ss do (symb:gensym(),(push([first(item),symb:apply(segment,(last(item)))],temp))),temp)$
/*------------------------------------------------------------------------------------------------------*/
rearrange(kk,lis):=block([lis:copylist(lis),rep:[],pat_map:[],sym,symb],
kk:rem_prefix(kk),
for item:1 thru length(kk) do
(sym:gensym(),symb:apply(segment,last(kk[item])),
lis:repl_inl(lis,first(kk[item]))),
for item in kk do lis:subst(last(item)[1],first(item),lis),lis)$
/*------------------------------------------------------------------------------------------------------*/
repl_inl(lis,pref):=block([lis:lis],if(listp(lis)) then
(for item:1 thru length(lis) do
if(listp(lis[item])) then repl_inl(lis[item],pref) else if(lis[item]=pref) then (lis[item]:symb))
else (if(atom(lis)) then if(lis=pref) then lis:symb),ev(lis))$
/*------------------------------------------------------------------------------------------------------*/
rem_prefix(ss):=map(lambda([x],[first(map(parse_string,split(string(first(x)),"_"))),last(x)]),ss)$
/*------------------------------------------------------------------------------------------------------*/
dissect(patt,lis):=block([final:[]],rec_patlist(patt,lis),final:rem_prefix(final),
if(member([],flatten(final)) or member(INVALID,flatten(final))) then lis else final)$
/*------------------------------------------------------------------------------------------------------*/
rec_patlist(patt,lis):=block([temp:[]],if(length(sublist(patt,listp))=0) then (final:append(disect(patt,lis),final))
else for item:1 thru length(patt) do
if(listp(patt[item])) then (rec_patlist(patt[item],first(sublist(lis,listp))),
(lis:substinpart(piece:INVALID,lis,first(last(last(positionsof(first(sublist(lis,listp)),lis))))))),
if(length(sublist(patt,listp))>0) then (final:append(pat_lists(patt,lis),final)))$
/*------------------------------------------------------------------------------------------------------*/