Random Combination Algorithm Writer

__TOC__Back to Computer Science (with texttobr(2) algorithms)
== rcaw.pl ==
:- dynamic debug/1.
/**See intructions for running texttobr becore using texttobr.
[listprologinterpreter1listrecursion4].[texttobr].
leash(-all),visible(+all).
protocol("./file.txt"),  trace,  caw00(off,f,[[append,2,1],[delete,2,1],[head,1,1],[tail,1,1],[member,1,1]],2,8,_InputVarList,_OutputVarList,[],_Program2,Ps).
notrace,noprotocol,texttobr2.
texttobr2,texttobr2,texttobr2,texttobr2,texttobr2,texttobr2,texttobr2,texttobr2.
texttobr,texttobr,texttobr,texttobr,texttobr,texttobr,texttobr,texttobr.
caw00(off,f,[[append,2,1],[delete,2,1],[head,1,1],[tail,1,1],[member,1,1]],3,8,_InputVarList,_OutputVarList,[],_Program2,Ps).
Random Combination Algorithm Writer
- Writes random algorithms
predicates may have different combinations of modes
caw00(off,f,[[append,2,1],[delete,2,1],[head,1,1],[tail,1,1],[member,2,0]],50,6,a,1,d,1,[],Program).
caw00(off,f,[[append,2,1],[delete,2,1],[head,1,1],[tail,1,1],[member,1,1]],50,7,a,1,b,1,[],Program).Program = [f,[1,b]],[[f,[a,b],:-,[[=,[b,a]]]]],b,1
caw00(off,f,[[append,2,1],[delete,2,1],[head,1,1],[tail,1,1],[member,1,1]],50,7,[[a,[1,2]]],[[b,[1,2]]],[],Program).Program = [f,[[1,2],b]],[[f,[a,b],:-,[[=,[b,a]]]]],[[b,[1,2]]]
caw00(off,f,[[append,2,1],[delete,2,1],[head,1,1],[tail,1,1],[member,1,1]],50,4,[[a,1],[b,2]],[[c,[1,2]]],[],Program).Program = interpret(off,[f,[1,2,c]],[[f,[a,b,c],:-,[[append,[a,b,g]],[=,[c,g]]]]],[[c,[1,2]]])
caw00(off,f,[[append,2,1],[delete,2,1],[head,1,1],[tail,1,1],[member,1,1]],50,6,[[a,1],[b,2],[c,3]],[[d,[1,2,3]]],[],Program).Program = [interpret(off,[f,[1,2,3,d]],[[f,[a,b,c,d],:-,[[append,[a,b,e]],[append,[e,c,f]],[=,[d,f]]]]],[[d,[1,2,3]]])]
edit out repeatstest rule,varbetter arg finders
caw00(off,f,[[append,2,1],[delete,2,1],[head,1,1],[tail,1,1],[member,1,1]],50,7,[[a,[4,1,2,3]],[b,1]],[[d,[4,2,3]]],[],Program).
[debug]  ?- optimise([[append,[a,b,c]],[append,[c,d,e]]],[a,b,d],[a,b,c,d],[e],P).                           P = [[append, [a, b, c]], [append, [c, d, e]]] ;%% choose latest var as output, remove duplicate args in rules x (max 2), reverse optimise input, new rules have old outputs as args v[debug]  ?- optimise([[append,[a,b,c]],[append,[a,b,c]]],[a,b],[a,b,c],[c],P). P = [[append, [a, b, c]]] ;
[debug]  ?- optimise([[append,[a,b,c]],[delete,[a,b,c]]],[a,b],[a,b,c],[c],P).P = [[append, [a, b, c]], [delete, [a, b, c]]] ;
[debug]  ?- optimise([[append,[a,b,c]],[delete,[a,b,x]]],[a,b],[a,b,c,x],[c],P).P = [[append, [a, b, c]]] ;
[debug]  ?- optimise([[append,[a,b,c]],[delete,[c,b,d]],[member,[c,d]]],[a,b],[a,b,c,d],[d],P).P = [[append, [a, b, c]], [delete, [c, b, d]], [member, [c, d]]] ;
[debug]  ?- optimise([[append,[a,b,c]],[delete,[c,b,d]],[member,[a,d]]],[a,b],[a,b,c,d],[d],P).P = [[append, [a, b, c]], [delete, [c, b, d]], [member, [a, d]]] ;
[debug]  ?- optimise([[append,[a,b,c]],[delete,[c,b,d]],[member,[a,x]]],[a,b],[a,b,c,d],[d],P).P = [[append, [a, b, c]], [delete, [c, b, d]]] ;
[debug]  ?- optimise([[append,[a,b,c]],[delete,[c,b,d]],[member,[c,x]]],[a,b],[a,b,c,d],[d],P).P = [[append, [a, b, c]], [delete, [c, b, d]]] ;
Wantedly fail:optimise([[append,[a,e,c]]],[a],[a,e,c],[c],P).optimise([[append,[a,b,c]],[append,[c,d,e]]],[a,d],[a,b,c,d],[e],P).optimise([[append,[a,b,c]],[append,[c,d,e]]],[a,b],[a,b,c,d],[e],P).
optimise([[delete,[a,e,c]],[append,[a,a,e]]],[a,e],[a,e,c],[c],P).P = [[delete, [a, e, c]], [append, [a, a, e]]] .
optimise([[append,[a,a,e]],[delete,[a,e,c]]],[a,e],[a,e,c],[c],P).P = [[append, [a, a, e]], [delete, [a, e, c]]] .
optimise([[append,[a,e,c]]],[a,e,c],[a,e,c],[c],P).P = [[append, [a, e, c]]] .
findrulesflowingtopv1([[append,[a,e,c]],[member,[a,e]]],[a],[a,e,c],[c],[],R,F).R = [[append, [a, e, c]], [member, [a, e]]]
check optimise works with member ef vmember ef and member in inputvars2 (cde) voptimise - aeg (remove e), vdoes optimise work with multiple rules with same output vdelete returning progs in optimise vcut rule -
aea cant point to itself in optimise - needs iterative deepeningaec where e not in iv1 or another pred, try first or second in prog vdon't pass rule to lower predicates v
don't choose outputs from non new var vdon't run repeat preds
make predicate, clause writermember predicates returning no output
**/
%% ML max 25
caw00(Debug,PredicateName,Rules,MaxLength,TotalVars,_InputVarList,_OutputVarList,Program1,_Program2,Ps1) :- %%MaxLength2 is MaxLength + 1, %%TotalVars = MaxLength, randvars(MaxLength,MaxLength,[],RandVars), populatevars(RandVars,MaxLength,[],PV), Code is MaxLength + 1 + 97, char_code(Char,Code), OutputVarList=Char,1, retractall(debug(_)),      assertz(debug(Debug)), retractall(totalvars(_)),      assertz(totalvars(TotalVars)), caw0(PredicateName,Rules,MaxLength,PV,OutputVarList,Program1,_Program3,Ps), sort(Ps,Ps1),!.
randvars(0,_,V,V) :- !.randvars(N,L,V1,V2) :- random(N1), N2A is round(97+(N1*L)), char_code(V3,N2A), ((member(V3,V1))->randvars(N,L,V1,V2); (append(V1,[V3],V4), NA is N-1, randvars(NA,L,V4,V2))).randvars2(0,L,V,V) :- !.randvars2(N,L,V1,V2) :- random(N1), N2A is round(97+(N1*L)), char_code(V3,N2A), atom_string(V3,V4), ((member(V4,V1))->randvars2(N,L,V1,V2); (append(V1,[V4],V5), NA is N-1, randvars2(NA,L,V5,V2))).populatevars([],_,PV,PV) :- !.populatevars([RV1|RVs],MaxLength2,PV1,PV2) :- randvars2(MaxLength2,MaxLength2,[],RV2), append(PV1,RV1,RV2,PV3), populatevars(RVs,MaxLength2,PV3,PV2).
caw0(PredicateName,Rules,MaxLength,InputVarList,OutputVarList,Program1,Program2,Ps2) :- varnames(InputVarList,[],InputVars,[],InputValues), varnames(OutputVarList,[],OutputVars,[],_OutputValues), retractall(outputvars(_)),      assertz(outputvars(OutputVars)), append(InputVars,OutputVars,Vars11), %%Vars11=InputVars, %%Vars12=InputVars, append(InputValues,OutputVars,Vars2), %%append(InputValues,OutputValues,Values), Query=[PredicateName,Vars2], caw1(Query,PredicateName,Rules,MaxLength,Vars11,InputVars,InputVars,_,OutputVarList,OutputVars,Program1,Program2,[],Ps2).
caw1(_,_,_,0,_,_,_,_,_,_,_,_,Ps,Ps) :- !.caw1(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program2,Programs2,Ps1) :- MaxLength2 is MaxLength - 1, addrules0(InputVars2,OutputVars,OutputVars,[],Program3), %%writeln([addrules(InputVars2,OutputVars,OutputVars,[],PenultimateVars,[],Program3)]),     %%optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program4), %% IV2->3 %%writeln([optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program4)]), append(Program1,Program3,Program5), append(InputVars1,OutputVars,Vars2), Program2=[      [PredicateName,Vars2,(:-),              Program5      ]      ],debug(Debug),
      %%writeln([interpret(Debug,Query,Program2,OutputVarList2)]),
 interpret(Debug,Query,Program2,OutputVarList2),      writeln([interpret(Debug,Query,Program2,OutputVarList2)]), append(Programs2,Query,Program2,OutputVarList2,Programs3), caw1a(Query,PredicateName,Rules,MaxLength2,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,[],_Program2,Programs3,Ps1). caw1a(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program4,Ps1,Ps2) :-  %%writeln([caw(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,OutputVarList,OutputVars,Program1,Program4)]), %%MaxLength2 is MaxLength - 1, %%writeln(["ml",MaxLength2]), reverse(InputVars2,InputVars5), random_member([RuleName,NumInputs,NumOutputs],Rules), %%writeln([member([RuleName,NumInputs,NumOutputs],Rules)]), %%writeln([rule(RuleName,NumInputs,NumOutputs,VarList,VarList2,Rule)]), rule(RuleName,NumInputs,NumOutputs,InputVars5,InputVars4,VarList,VarList2,Rule),%%writeln([inputVars1,InputVars1]),%%    writeln([rule(RuleName,NumInputs,NumOutputs,InputVars5,InputVars4,VarList,VarList2,Rule)]), append(Program1,[Rule],Program3), %%writeln([inputVars3,InputVars3]), %%InputVars2=InputVars3, %%writeln([program4,Program4]), caw(Query,PredicateName,Rules,MaxLength,VarList2,InputVars1,InputVars4,InputVars3,OutputVarList,OutputVars,Program3,Program4,Ps1,Ps2).
caw(_,_,_,0,_,_,_,_,_,_,_,_,Ps,Ps) :- !.caw(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program2,Programs1,Programs2) :- MaxLength2 is MaxLength - 1, addrules(InputVars2,OutputVars,OutputVars,[],_PenultimateVars,[],Program3), %%writeln([addrules(InputVars2,OutputVars,OutputVars,[],PenultimateVars,[],Program3)]),     %%optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program4), %% IV2->3 %%writeln([optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program4)]), append(Program1,Program3,Program5), append(InputVars1,OutputVars,Vars2), Program2=[      [PredicateName,Vars2,(:-),              Program5      ]      ],debug(Debug),%%***%% () choose iv1 as args during caw, () eliminate e problem, could move forward in optimiser but don't need it v%% should have a leading edge of 1 immediately previous (new output) as an arg in latest rule v, go backwards to choose latest possible args x, 3 x rules can have same as previous rule's output as an output x: at a time%% chunks will solve having at least 1 rule that connects to last output%% can optimise number of inputs
%% test member, = in caw
 %%writeln([interpret(Debug,Query,Program2,OutputVarList2)]),
 interpret(Debug,Query,Program2,OutputVarList2), writeln([interpret(Debug,Query,Program2,OutputVarList2)]), append(Programs1,Query,Program2,OutputVarList2,Programs3), cawa(Query,PredicateName,Rules,MaxLength2,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,[],_Program2,Programs3,Programs2). cawa(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program4,Ps1,Ps2) :- %%writeln([caw(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,OutputVarList,OutputVars,Program1,Program4)]), %%MaxLength2 is MaxLength - 1, %%writeln(["ml",MaxLength2]), random_member([RuleName,NumInputs,NumOutputs],Rules), %%writeln([member([RuleName,NumInputs,NumOutputs],Rules)]), %%writeln([rule(RuleName,NumInputs,NumOutputs,VarList,VarList2,Rule)]), rule(RuleName,NumInputs,NumOutputs,InputVars2,InputVars4,VarList,VarList2,Rule),%%writeln([inputVars1,InputVars1]),%%    writeln([rule(RuleName,NumInputs,NumOutputs,InputVars2,InputVars4,VarList,VarList2,Rule)]), append(Program1,[Rule],Program3), %%writeln([inputVars3,InputVars3]), %%InputVars2=InputVars3, %%writeln([program4,Program4]), caw(Query,PredicateName,Rules,MaxLength,VarList2,InputVars1,InputVars4,InputVars3,OutputVarList,OutputVars,Program3,Program4,Ps1,Ps2).
varnames([],Vars,Vars,Values,Values) :- !.varnames(VarList,Vars1,Vars2,Values1,Values2) :- VarList=[Var|Vars3], Var=[VarName,Value], append(Vars1,[VarName],Vars4), append(Values1,[Value],Values3), varnames(Vars3,Vars4,Vars2,Values3,Values2),!.
addrules0(_,_,[],Program,Program).addrules0(VarList,OutputVars1,OutputVars2,Program1,Program2) :- OutputVars2=[OutputVar|OutputVars3], random_member(Var,VarList), random_member(OutputVar,OutputVars1), append(Program1,[[=,[OutputVar,Var]]],Program3), addrules0(VarList,OutputVars1,OutputVars3,Program3,Program2).
addrules(_,_,[],PV,PV,Program,Program) :- !.addrules(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2) :- restlast(VarList,[],_,Var), %%OutputVars2=[OutputVar|OutputVars3], random_member(OutputVar,OutputVars2), delete(OutputVars1,OutputVar,OutputVars3),%%    member(Var,VarList), random_member(OutputVar,OutputVars1), append(Program1,[[=,[OutputVar,Var]]],Program3), append(PenultimateVars1,[Var],PenultimateVars3), addrules2(VarList,OutputVars1,OutputVars3,PenultimateVars3,PenultimateVars2,Program3,Program2).
addrules2(_,_,[],PV,PV,Program,Program) :- !.addrules2(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2) :-%%    restlast(VarList,[],_,Var), OutputVars2=[OutputVar|OutputVars3], random_member(Var,VarList), not(member(Var,PenultimateVars1)), random_member(OutputVar,OutputVars1), append(Program1,[[=,[OutputVar,Var]]],Program3), append(PenultimateVars1,[Var],PenultimateVars3), addrules2(VarList,OutputVars1,OutputVars3,PenultimateVars3,PenultimateVars2,Program3,Program2).
%% optimise([[append,[a,a,d]],[append,[a,a,e]],[append,[a,a,f]],[append,[a,b,g]]],[g],P)./**optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program2) :- reverse(Program1,Program4), findrulesflowingtopv1(Program4,InputVars1,InputVars2,PenultimateVars,[],Rules,true), %%findrulesflowingtopv1a(Program1,_Program32,InputVars1,InputVars2,PenultimateVars,[],_Rules1), intersection(Program1,Rules,Program3), unique1(Program3,[],Program2).findrulesflowingtopv1(_,_,_,[],Rules,Rules,false).findrulesflowingtopv1(Program0,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :- (atom(Var);length(Var,1)), findrulesflowingtopv20(Program0,Program0,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1).findrulesflowingtopv1(Program0,InputVars1,InputVars2,Vars1,Rules1,Rules2,IV1Flag1) :- Vars1=[Var|Vars2], findrulesflowingtopv20(Program0,Program0,InputVars1,InputVars2,Var,Rules1,Rules3,IV1Flag2),  findrulesflowingtopv1(Program0,InputVars1,InputVars2,Vars2,Rules3,Rules2,IV1Flag3), iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
%%findrulesflowingtopv2([],Program,Program,_,_,Rules,Rules).findrulesflowingtopv20(_,[],_InputVars1,_InputVars2,_Var,Rules,Rules,false).findrulesflowingtopv20(Program0,Rules4,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :- Rules4=[Rule|Rules], (findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules3,IV1Flag2)->true;(Rules3=Rules1,IV1Flag2=false)), %%delete(Program0,Rule,Program1), findrulesflowingtopv20(Program0,Rules,InputVars1,InputVars2,Var,Rules3,Rules2,IV1Flag3),%%p1->0 iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).%%findrulesflowingtopv2(_,[],[],_,_,_,Rules,Rules).findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :- Rule=[_PredicateName,Vars], restlast(Vars,[],Rest,Var), %%delete(Program1,[PredicateName,Vars],Program2), %%Program2=Program1, %%(not(intersection(Rulesx,Rules1))-> x %% append, append, unique1 %%append(Rules1,[Rule],Rules3);Rules3=Rules1),
 %%member(Var2,Rest), %%member(Var2,InputVars1),
 length(Rest,Length1), Length1>=1, subtract(Rest,InputVars1,IV3s), length(IV3s,Length3), subtract(Rest,IV3s,IV1s), length(IV1s,Length2), Length2>=1, subtract(IV3s,InputVars2,[]),
 IV1Flag2=true,
 %%delete(Program0,Rule,Program1),
 %%(delete(Program0,Rule,Program3), %%iv3s1(IV3s,Program3,IV3s,[]), (Length3>=1-> (findrulesflowingtopv1(Program0,InputVars1,InputVars2,IV3s,[],Rules5,IV1Flag3),not(Rules5=[])); (Rules5=[],IV1Flag3=false)), iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag4), %%->true; Rules5=[],IV1Flag1=IV1Flag4),  ((findrulesflowingtopv1(Program0,InputVars1,InputVars2,IV1s,[],Rules6,IV1Flag5), %%iv1s->rest, etc iv1flagdisjunction(IV1Flag4,IV1Flag5,IV1Flag1))->true;(Rules6=[],IV1Flag1=IV1Flag4)),
 append([Rule],Rules1,Rules9), append(Rules9,Rules5,Rules7), append(Rules7,Rules6,Rules8), unique1(Rules8,[],Rules2).
/**findrulesflowingtopv2(_Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :- Rule=[_PredicateName,Vars], restlast(Vars,[],Rest,Var), %%delete(Program1,[PredicateName,Vars],Program2), %%Program2=Program1, (not(member(Rule,Rules1))-> append(Rules1,[Rule],Rules2);Rules2=Rules1), subset(Rest,InputVars2),
 intersection(Rest,InputVars1,Intersection), length(Intersection,0),
%%    not((member(Var2,Rest),%%    member(Var2,InputVars1))),
 IV1Flag1=false.**//**findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :- Rule=[_PredicateName,Vars], restlast(Vars,[],Rest,Var),     %%delete(Program1,[PredicateName,Vars],Program3), %%Program3=Program1, %%append(Rules1,[Rule],Rules3), subset(Rest,InputVars2),  intersection(Rest,InputVars1,Intersection), length(Intersection,0),
%%    not((member(Var2,Rest),%%    member(Var2,InputVars1))),
%%    delete(Program0,Rule,Program1),
 IV1Flag2=false, findrulesflowingtopv1(Program0,InputVars1,InputVars2,Rest,[],Rules4,IV1Flag3), %%not(Rules4=[]), iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1),
 append(Rules1,[Rule],Rules7), append(Rules7,Rules4,Rules8), unique1(Rules8,[],Rules2).**//**%%->true;(Program2=Program1,Rules2=Rules1)).findrulesflowingtopv2(Rule,Program0,Program1,_Program2,InputVars1,InputVars,Var,Rules1,Rules2,IV1Flag1) :- Rule=[PredicateName,Vars], restlast(Vars,[],Rest,Var), %%delete(Program1,[PredicateName,Vars],Program4), %%Program4=Program1, append(Rules1,PredicateName,Vars,Rules3), findrulesflowingtopv1(Program0,Program1,_Program2,InputVars1,InputVars,Rest,Rules3,Rules2,IV1Flag3), iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
 %%findrulesflowingtopv2(Program5,Program2,Rest,Rules3,Rules2).
**/iv1flagdisjunction(A,B,true) :- (A=true); (B=true).iv1flagdisjunction(_,_,false)./**iv3s0([],_,IV3s1,IV3s2).iv3s0(IV3s,Program0,IV3s1,IV3s2). IV3s=[IV3|IV3s3], iv3s1(IV3,Program0,IV3s1,IV3s4), iv3s0(IV3s3,Program0,IV3s4,IV3s2).iv3s1(_,[],IV3s,IV3s).    iv3s1(IV3,Program0,IV3s1,IV3s2) :- Program0=[Rule|Rules], iv3s2(IV3,Rule,IV3s1,IV3s3), iv3s1(IV3,Rules,IV3s3,IV3s2).iv3s2(IV3,Rule,IV3s,IV3s1,IV3s2). Rule=[_PredicateName,Vars], restlast(Vars,[],_Rest,IV3),     delete(IV3s1,IV3,IV3s2).
findrulesflowingtopv1a(_,_,_,_,[],Rules,Rules).findrulesflowingtopv1a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2) :- atom(Var), findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2).findrulesflowingtopv1a(Program1,Program2,InputVars1,InputVars2,Vars1,Rules1,Rules2) :- Vars1=[Var|Vars2], findrulesflowingtopv2(Program1,Program3,InputVars1,InputVars2,Var,Rules1,Rules3), findrulesflowingtopv1a(Program3,Program2,InputVars1,InputVars2,Vars2,Rules3,Rules2).%%findrulesflowingtopv2([],Program,Program,_,_,Rules,Rules).findrulesflowingtopv2a([],[],_,_,_,Rules,Rules).findrulesflowingtopv2a(Program1,Program2,_InputVars1,InputVars2,Var,Rules1,Rules2) :- member([PredicateName,Vars],Program1), restlast(Vars,[],Rest,Var), (%%delete(Program1,[PredicateName,Vars],Program2),Program2=Program1, append(Rules1,PredicateName,Vars,Rules2), subset(Rest,InputVars2)).findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2) :- member([PredicateName,Vars],Program1), restlast(Vars,[],Rest,Var), (%%delete(Program1,[PredicateName,Vars],Program3),Program3=Program1, append(Rules1,PredicateName,Vars,Rules3), subset(Rest,InputVars2)), findrulesflowingtopv1a(Program3,Program2,InputVars1,InputVars2,Rest,Rules3,Rules2).
%%->true;(Program2=Program1,Rules2=Rules1)).findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars,Var,Rules1,Rules2) :- member([PredicateName,Vars],Program1), restlast(Vars,[],Rest,Var), %%delete(Program1,[PredicateName,Vars],Program4), Program4=Program1, append(Rules1,PredicateName,Vars,Rules3), findrulesflowingtopv1a(Program4,Program2,InputVars1,InputVars,Rest,Rules3,Rules2). %%findrulesflowingtopv2(Program5,Program2,Rest,Rules3,Rules2). **/**/restlast([],_,_,_) :- fail, !.    restlast([Last],Rest,Rest,Last) :- atom(Last),!.restlast(Last,Rest,Rest,Last) :- length(Last,1),!.restlast(Vars1,Rest1,Rest2,Last) :- Vars1=[Var|Vars2], append(Rest1,[Var],Rest3), restlast(Vars2,Rest3,Rest2,Last),!.
rule(RuleName,1,1,InputVars1,InputVars2,VarList,VarList2,Rule) :- random_member(Var,InputVars1), rule2(RuleName,Var,VarList,VarList2,Rule,Var1), restlast(InputVars1,[],_,Last), %% Last should be outputs - 2nd last rule? (Var=Last;Last=Var1), append(InputVars1,[Var1],InputVars2).rule2(RuleName,Var,VarList,VarList2,Rule,Var1) :- var(VarList,Var1,VarList2), Rule=[RuleName,[Var,Var1]].%%, %%member(Var,!.
rule(RuleName,1,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-      random_member(Var,InputVars1),      rule3(RuleName,Var,VarList,VarList2,Rule,Var1,Var2), restlast(InputVars1,[],_,Last), (Var=Last; (Last=Var1;Last=Var2)), append(InputVars1,[Var1,Var2],InputVars2).rule3(RuleName,Var,VarList,VarList3,Rule,Var1,Var2) :-      var(VarList,Var1,VarList2),      var(VarList2,Var2,VarList3),      Rule=[RuleName,[Var,Var1,Var2]],!.
rule(RuleName,2,0,InputVars,InputVars,VarList,VarList,Rule) :-%%writeln([rule(RuleName,2,1,InputVars1,InputVars2,VarList,VarList2,Rule)]),      random_member(Var,InputVars),      random_member(Vara,InputVars),      rule6(RuleName,Var,Vara,_VarList,_VarList2,Rule), restlast(InputVars,[],_,Last),%%writeln([last,Last]), (Var=Last;Vara=Last).rule6(RuleName,Var,Vara,_VarList,_VarList2,Rule) :-      Rule=[RuleName,[Var,Vara]],!.
rule(RuleName,2,1,InputVars1,InputVars2,VarList,VarList2,Rule) :-%%writeln([rule(RuleName,2,1,InputVars1,InputVars2,VarList,VarList2,Rule)]),      random_member(Var,InputVars1),      random_member(Vara,InputVars1),      rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1), restlast(InputVars1,[],_,Last),%%writeln([last,Last]), ((Var=Last;Vara=Last); (Last=Var1)),%%writeln([var,Var,vara,Vara]), append(InputVars1,[Var1],InputVars2).rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1) :-      var(VarList,Var1,VarList2),      Rule=[RuleName,[Var,Vara,Var1]],!.
%%ae be with predicate support alsorule(RuleName,2,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-      random_member(Var,InputVars),      random_member(Vara,InputVars),      rule5(RuleName,Var,Vara,VarList,VarList2,Rule,Var1,Var2), restlast(InputVars1,[],_,Last), ((Var=Last;Vara=Last); (Last=Var1;Last=Var2)), %% make last var2, use different inputs from previous rule, make this line usea version of previous line as well (args from rule before that) - redo rules based on past programming append(InputVars1,[Var1,Var2],InputVars2).rule5(RuleName,Var,Vara,VarList,VarList3,Rule,Var1,Var2) :-      var(VarList,Var1,VarList2),      var(VarList2,Var2,VarList3),      Rule=[RuleName,[Var,Vara,Var1,Var2]],!.
%%var(Item,Var,Vars,Vars) :-%%    member([Item,Var],Vars).var(Vars1,Var1,Vars2) :- length(Vars1,Vars1Length1), Vars1Length2 is Vars1Length1-1, length(Vars3,Vars1Length2), append(Vars3,[Var2],Vars1), char_code(Var2,Var2Code1), Var2Code2 is Var2Code1 + 1, var2(Var2Code2,Var1), append(Vars1,[Var1],Vars2),!.
var2(Code,Var1) :- outputvars(OutputVars), totalvars(TotalVars), Code2 is 96+TotalVars, Code =< Code2, %% 122 char_code(Var1,Code), not(member(Var1,OutputVars)),!.var2(Var2Code,Code3) :- Var2Code2 is Var2Code + 1,     totalvars(TotalVars), Code2 is 96+TotalVars, Var2Code2 =< Code2, var2(Var2Code2,Code3),!.
%% this goes from e.g. c not a to TotalVars%% skip over vars, start from a x reassign vars to abc etc
%% if returning 12 from 12345 remove 345 args
%% try lowest possible number of vars first (return shortest program first), then keep on adding number of vars%% can elimininate same program both here and in assessment verification
== 
listprologinterpreter1listrecursion4.pl ==
:- include('listprologinterpreter3preds5.pl').:- include('lpiverify4.pl').:- include('rcaw.pl').:- include('texttobr2.pl').
%%:- include('caw4.pl').
:- dynamic debug/1.
/** List Prolog Interpreter **/
interpret(Debug,Query,Functions,Result) :-%%writeln([i1]), interpret1(Debug,Query,Functions,Functions,Result), !.interpret1(Debug,Query,Functions1,Functions2,Result) :-%%writeln([i11]), retractall(debug(_)),      assertz(debug(Debug)),      retractall(cut(_)),      assertz(cut(off)), member1(Query,Functions1,Functions2,Result).%%member1([_,R],_,[],R).%%member1(_,_,[],[]).member1(_,_,[],_) :- fail.member1(Query,Functions,Functions2,Vars8) :-%%writeln([m1]), cut(off)->(      (Query=[Function,Arguments1], (Functions2=[[Function,Arguments2,(:-),Body]|_Functions3]), length(Arguments1,Length), length(Arguments2,Length),      checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!, %%->ca2 %%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),              (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true), interpretbody(Functions,Functions2,Vars1,Vars2,Body,true), updatevars(FirstArgs,Vars2,[],Result),      %%reverse(Result,[],Vars7), ((not(Result=[])->      %%Result=[Var71|Vars72],      unique1(Result,[],Vars8),%%writeln(["FirstArgs",FirstArgs,"Vars",Vars2,"Result",Result,"Vars7",Vars7,"Vars72",Vars72,"Var71",Var71,"Vars8",Vars8]),%%writeln(["Vars8",Vars8]), findresult3(Arguments1,Vars8,[],Result2)%%writeln([findresult3,"Arguments1",Arguments1,"Vars8",Vars8,"Result2",Result2]) );(%%writeln(here1), Vars8=[],Result2=[])),%%writeln(["Arguments1",Arguments1,"Vars2",Vars2,"Result",Result]),     (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true)) ->true; (%%Query=[Function,_Arguments1], %%Functions2=[[Function,_Arguments2,(:-),_Body]|Functions3], %% make like previous trunk? member11(Query,Functions,Functions2,Vars8)) );(turncut(off)%%,Result=[] ).member11(Query,Functions,Functions2,Result) :-%%writeln([m11]),%%writeln(["Query",Query,"Functions",Functions,"Functions2",Functions2,"Result",Result]), cut(off)->(      (Query=[Function],      (Functions2=[[Function,(:-),Body]|_Functions3]),              (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true), Result=[],      interpretbody(Functions,Functions2,[],_Vars2,Body,true),!,          (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true) )->true; (%%Query=[Function], %%Functions2=[[Function]|Functions3], member12(Query,Functions,Functions2,Result)) );(turncut(off)).member12(Query,Functions,Functions2,Vars8) :-%%writeln([m12]), cut(off)->(      (Query=[Function,Arguments1],      (Functions2=[[Function,Arguments2]|_Functions3]),      length(Arguments1,Length),      length(Arguments2,Length),      checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!, %% ->ca2%%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]), updatevars(FirstArgs,Vars1,[],Result),      %%reverse(Result,[],Vars7),      ((not(Result=[])->      %%Result=[Var71|Vars72],      unique1(Result,[],Vars8),      findresult3(Arguments1,Vars8,[],Result2)      );(%%writeln(here2), Vars8=[],Result2=[])),          (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true) )->true; (%%Query=[Function,_Arguments1], %%Functions2=[[Function,_Arguments2]|Functions3], member13(Query,Functions,Functions2,Vars8)) );(turncut(off)).member13(Query,Functions,Functions2,Result) :-%%writeln([m13]), cut(off)->(      (Query=[Function],!,      (Functions2=[[Function]|_Functions3]),          (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true), Result=[],      %%interpretbody(Functions,[],_Vars2,Body,true),          (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true) )->true; (%%Query=[Function], Functions2=[_Function|Functions3], member1(Query,Functions,Functions3,Result)) );(turncut(off)).interpret2(Query,Functions1,Functions2,Result) :-%%writeln(i2),%%writeln(["%%interpret2 Query",Query,"Functions1",Functions1,"Functions2",Functions2]),      member2(Query,Functions1,Functions2,Result).%%member2([_,R],_,[],R).%%member2(_,_,[],[]).member2(_,_,[],_) :- fail.member2(Query,Functions,Functions2,Vars8) :-%%writeln([m2]), cut(off)->(      (Query=[Function,Arguments1],      (Functions2=[[Function,Arguments2,(:-),Body]|_Functions3]),      length(Arguments1,Length),      length(Arguments2,Length),      checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!,%%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),              (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),      interpretbody(Functions,Functions2,Vars1,Vars2,Body,true), %%**arg2 change%%writeln(["Functions",Functions,"Functions2",Functions2,"Vars1",Vars1,"Vars2",Vars2,"Body",Body]),      updatevars(FirstArgs,Vars2,[],Result),      %%reverse(Result,[],Vars7),      ((not(Result=[])->      %%Result=[Var71|Vars72],      unique1(Result,[],Vars8),      findresult3(Arguments1,Vars8,[],Result2)%%writeln(["Vars2",Vars2,"Result",Result]),      );( writeln(here3), Vars8=[],Result2=[])),          (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true) )->true; (%%Query=[Function,_Arguments1], %%Functions2=[[Function,_Arguments2,(:-),_Body]|Functions3], member21(Query,Functions,Functions2,Vars8)) );(turncut(off)).member21(Query,Functions,Functions2,Result) :-%%writeln([m21]), cut(off)->(      (Query=[Function],      (Functions2=[[Function,(:-),Body]|_Functions3]),      Vars1=[],              (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true),      interpretbody(Functions,Functions2,Vars1,_Vars2,Body,true),!, %%**arg2 change          (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true) )->true; (%%Query=[Function], %%Functions2=[[Function]|Functions3], member22(Query,Functions,Functions2,Result)) );(turncut(off)).member22(Query,Functions,Functions2,Vars8) :-%%writeln([m22]), cut(off)->(      (Query=[Function,Arguments1],      (Functions2=[[Function,Arguments2]|_Functions3]),      length(Arguments1,Length),      length(Arguments2,Length),      checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!,%%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),      updatevars(FirstArgs,Vars1,[],Result),      %%reverse(Result,[],Vars7),      ((not(Result=[])->      %%Result=[Var71|Vars72],      unique1(Result,[],Vars8),      findresult3(Arguments1,Vars8,[],Result2)      );(%%writeln(here4), Vars8=[],Result2=[])),          (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true) )->true; (%%Query=[Function,_Arguments1], %%Functions2=[[Function,_Arguments2]|Functions3], member23(Query,Functions,Functions2,Vars8)) );(turncut(off)).member23(Query,Functions,Functions2,Vars8) :-%%writeln([m23]), cut(off)->(      (Query=[Function],!,      (Functions2=[[Function]|_Functions3]),          (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true), Vars8=[],          (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true) )->true; (%%Query=[Function], Functions2=[_Function|Functions3], member2(Query,Functions,Functions3,Vars8)) );(turncut(off)).checkarguments([],[],Vars,Vars,FirstArgs,FirstArgs). checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :- %%%%writeln(1), Arguments1=[Value|Arguments3], %% Value may be a number, string, list or tree expressionnotatom(Value), Arguments2=[Variable2|Arguments4], isvar(Variable2), putvalue(Variable2,Value,Vars1,Vars3), checkarguments(Arguments3,Arguments4,Vars3,Vars2,FirstArgs1,FirstArgs2).checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :- %%A%%writeln(2),      Arguments1=[Variable|Arguments3], %% Value may be a number, string, list or tree      isvar(Variable),      Arguments2=[Value|Arguments4],      expressionnotatom(Value),      putvalue(Variable,Value,Vars1,Vars3), append(FirstArgs1,Variable,_,FirstArgs3),      checkarguments(Arguments3,Arguments4,Vars3,Vars2,FirstArgs3,FirstArgs2).checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :-%%writeln(3),      Arguments1=[Variable1|Arguments3], isvar(Variable1),      Arguments2=[Variable2|Arguments4], isvar(Variable2), (getvalue(Variable2,Value,Vars1)->true;Value=empty),      putvalue(Variable2,Value,Vars1,Vars3),      append(FirstArgs1,Variable1,Variable2,FirstArgs3),      checkarguments(Arguments3,Arguments4,Vars3,Vars2,FirstArgs3,FirstArgs2).checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :-%%writeln(4),      Arguments1=[Value1|Arguments3],      expressionnotatom(Value1),      Arguments2=[Value1|Arguments4],      expressionnotatom(Value1),      checkarguments(Arguments3,Arguments4,Vars1,Vars2,FirstArgs1,FirstArgs2).
interpretbody(_Functions1,_Functions2,Vars,Vars,[],true) :- !.
interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-      Body=[Statements1|Statements2],      interpretbody(Functions0,Functions,Vars1,Vars3,Statements1,Result2),      interpretbody(Functions0,Functions,Vars3,Vars2,Statements2,Result3),      %%((Result3=cut)->!;true),      logicalconjunction(Result1,Result2,Result3),!.
interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-      Body=[[not,[Statements]]|Statements2], interpretbody(Functions0,Functions,Vars1,Vars3,Statements,Result2),      %%((Result2=cut)->!;true),      interpretbody(Functions0,Functions,Vars3,Vars2,Statements2,Result3),      %%((Result3=cut)->!;true),      logicalnot(Result2,Result4), (logicalconjunction(Result1,Result4,Result3)->true;(Result1=false)),!.interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-      Body=[[Statements1],or,[Statements2]],      (interpretbody(Functions0,Functions,Vars1,Vars2,Statements1,Result1); %%,((Value1=cut)->!;true));      interpretbody(Functions0,Functions,Vars1,Vars2,Statements2,Result1)),!. %%,((Value=cut)->!;true)). %%(logicaldisjunction(Result1,Value1,Value2)->true;(Result1=false)).
interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :- Body=[Statement|Statements],%%writeln(["Functions0",Functions0,"Functions",Functions,"Statement",Statement,"Vars1",Vars1,"Vars3",Vars3,"Result2",Result2,"Cut",Cut]), interpretstatement1(Functions0,Functions,Statement,Vars1,Vars3,Result2,Cut),%%writeln(["here1"]), ((not(Cut=cut))->(Functions2=Functions);(turncut(on))), %% cut to interpret1/2 (assertz)%%writeln(["here3"]), interpretbody(Functions0,Functions2,Vars3,Vars2,Statements,Result3), %%((Result3=cut)->!;true),%%writeln(["here4"]), logicalconjunction(Result1,Result2,Result3),!.%%writeln([Result1,Result2,Result3]).turncut(State1) :- cut(State2), retract(cut(State2)), assertz(cut(State1)).logicaldisjunction(true,Result2,Result3) :-      true(Result2);true(Result3).logicalconjunction(true,Result2,Result3) :- true(Result2),true(Result3).logicalnot(Result1,Result2) :- true(Result1),false(Result2).logicalnot(Result1,Result2) :-      false(Result1),true(Result2).true(true).false(false).
%%interpretstatement1(_F0,[],_,Vars,Vars,true,nocut) :- !%%writeln("AND HERE!")%%    .
interpretstatement1(_F0,_Functions,[cut,[]],Vars,Vars,true,cut) :- !.
interpretstatement1(_F0,_Functions,[atom,[Variable]],Vars,Vars,true,nocut) :- getvalue(Variable,Value,Vars),              (debug(on)->(writeln([call,[atom,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true), atom(Value),              (debug(on)->(writeln([exit,[atom,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true).
interpretstatement1(_F0,_Functions,[string,[Variable]],Vars,Vars,true,nocut) :-      getvalue(Variable,Value,Vars),              (debug(on)->(writeln([call,[string,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true),      string(Value),              (debug(on)->(writeln([exit,[string,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true).
interpretstatement1(_F0,_Functions,[number,[Variable]],Vars,Vars,true,nocut) :-      getvalue(Variable,Value,Vars),              (debug(on)->(writeln([call,[number,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true),      number(Value),              (debug(on)->(writeln([exit,[number,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true).
interpretstatement1(_F0,_Functions,[variable,[Variable]],Vars,Vars,true,nocut) :-      var(Variable),              (debug(on)->(writeln([call,[variable,[Variable]],"Press c."]),(not(get_single_char(97))->true;abort));true),              (debug(on)->(writeln([exit,[variable,[Variable]],"Press c."]),(not(get_single_char(97))->true;abort));true).
interpretstatement1(_F0,_Functions,[Operator,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :- isop(Operator), interpretpart(is,Variable1,Variable2,Vars1,Vars2).
interpretstatement1(_F0,_Functions,[Operator,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-%%writeln(31),      isop(Operator),      interpretpart(is,Variable2,Variable1,Vars1,Vars2). interpretstatement1(_F0,_Functions,[+,[Variable2,Variable3,Variable1]],Vars1,Vars2,true,nocut) :-%%writeln(4),      interpretpart(isplus,Variable1,Variable2,Variable3,Vars1,Vars2).
%%interpretstatement1(_F0,_Functions,[Variable2+Variable3,is,Variable1],Vars1,Vars2,true,nocut) :-%%writeln(41),      %%interpretpart(isplus,Variable1,Variable2,Variable3,Vars1,Vars2).
interpretstatement1(_F0,_Functions,[=,[Variable1,[Variable2,Variable3]]],Vars1,Vars2,true,nocut) :-%%writeln(5),      interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2).
%%interpretstatement1(_F0,_Functions,[[Variable2,Variable3]=Variable1],Vars1,Vars2,true,nocut) :-%%writeln(51),%%        interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2).
interpretstatement1(_F0,_Functions,[wrap,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-%%writeln(52), wrap      interpretpart(bracket1,Variable1,Variable2,Vars1,Vars2).
interpretstatement1(_F0,_Functions,[unwrap,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-%%writeln(53), unwrap      interpretpart(bracket2,Variable1,Variable2,Vars1,Vars2).
interpretstatement1(_F0,_Functions,[head,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-%%writeln(6),      interpretpart(head,Variable1,Variable2,Vars1,Vars2).
interpretstatement1(_F0,_Functions,[tail,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-%%writeln(61),      interpretpart(tail,Variable1,Variable2,Vars1,Vars2).
interpretstatement1(_F0,_Functions,[member,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-%%writeln(8),      interpretpart(member,Variable1,Variable2,Vars1,Vars2).
interpretstatement1(_F0,_Functions,[delete,[Variable1,Variable2,Variable3]],Vars1,Vars2,true,nocut) :-%%writeln(),      interpretpart(delete,Variable1,Variable2,Variable3,Vars1,Vars2).%%** all in form f,[1,1,etc], including + with 0,1
interpretstatement1(_F0,_Functions,[append,[Variable1,Variable2,Variable3]],Vars1,Vars2,true,nocut) :-%%writeln(9),      interpretpart(append,Variable1,Variable2,Variable3,Vars1,Vars2).
interpretstatement1(Functions0,_Functions,Query1,Vars1,Vars8,true,nocut) :-%%writeln("h1/10"),      Query1=[Function,Arguments],%%writeln(["Arguments",Arguments,"Vars1",Vars1]),      substitutevarsA1(Arguments,Vars1,[],Vars3,[],FirstArgs), %%% var to value, after updatevars:  more vars to values, and select argument vars from latest vars%%writeln([substitutevarsA1,arguments,Arguments,vars1,Vars1,vars3,Vars3,firstargs,FirstArgs]),      Query2=[Function,Vars3], %% Bodyvars2?%%            debug(on)->writeln([call,[Function,[Vars3]]]),%%writeln(["Query2",Query2,"Functions0",Functions0]),      interpret2(Query2,Functions0,Functions0,Result1),  updatevars2(FirstArgs,Result1,[],Vars5), updatevars3(Vars1,Vars5,Vars6), reverse(Vars6,[],Vars7), ((not(Vars7=[])-> %%Vars7=[Var71|Vars72], unique1(Vars7,[],Vars8));(writeln(here1), Vars8=[])).%%**** reverse and take first instance of each variable. %%findresult3(Arguments,Vars6,[],Result2)%%writeln(["FirstArgs",FirstArgs,"Result1",Result1,"Vars5",Vars5,"Vars4",Vars4]),%%writeln(["Vars1:",Vars1,"Vars4:",Vars4]),%%        debug(on)->writeln([exit,[Function,[Result2]]]).interpretstatement1(Functions0,_Functions,Query,Vars,Vars,true) :- Query=[_Function],          debug(on)->writeln([call,[Function]]),      interpret2(Query,Functions0,Functions0,_Result1),          debug(on)->writeln([exit,[Function]]).
interpretstatement1(_Functions0, _Functions,_Query,_Vars1,_Vars2,false) :- writeln([false]).
interpretstatement2(Value,_Vars,Value) :- (number(Value);atom(Value)).interpretstatement2(Variable,Vars1,Value) :- getvalue(Variable,Value,Vars1).interpretstatement3(A + B,Vars,Value1) :-      interpretstatement2(A,Vars,Value2),      interpretstatement2(B,Vars,Value3),      Value1 = Value2 + Value3.interpretstatement3(Value,_Vars,Value) :- (number(Value);atom(Value)).interpretstatement3(Variable,Vars,Value) :-      getvalue(Variable,Value,Vars).getvalue(Variable,Value,Vars) :-      ((not(isvar(Variable)),isvalstrorundef(Value),Variable=Value);      (isvar(Variable),isvalstrorundef(Value),getvar(Variable,Value,Vars))).putvalue(Variable,Value,Vars1,Vars2) :-      ((not(isvar(Variable)),isvalstrorundef(Value),Variable=Value);      (isvar(Variable),isvalstrorundef(Value),updatevar(Variable,Value,Vars1,Vars2))),!. getvar(Variable,Value,Vars) :-      member([Variable,Value],Vars), not(Value=empty).getvar(undef,undef,_Vars) :- !.getvar(Variable,empty,Vars) :-      not(member([Variable,_Value],Vars)); member([Variable,empty],Vars).updatevar(undef,_Value,Vars,Vars) :- !.updatevar(Variable,Value,Vars1,Vars2) :- ((((member([Variable,empty],Vars1), delete(Vars1,[Variable,empty],Vars3), append(Vars3,Variable,Value,Vars2)); ((not(member([Variable,Value1],Vars1)), ((Value1=empty)->true;(Value1=Value)))),      append(Vars1,Variable,Value,Vars2)); (member([Variable,Value],Vars1),Vars2=Vars1)); (undef(Variable), append(Vars1,Variable,Value,Vars2))).updatevars(_FirstArgs,[],Vars,Vars).updatevars(FirstArgs,Vars1,Vars2,Vars3) :-      Vars1=[[Variable1,Value]|Vars4], ((member([Variable2,Variable1],FirstArgs), %% removed brackets around firstargs here and 2 line below append(Vars2,Variable2,Value,Vars5)); (member([Variable1,_Variable2],FirstArgs), append(Vars2,Variable1,Value,Vars5))), updatevars(FirstArgs,Vars4,Vars5,Vars3), !.updatevars(FirstArgs,Vars1,Vars2,Vars3) :- Vars1=[_Vars4|Vars5], updatevars(FirstArgs,Vars5,Vars2,Vars3).updatevars2(_FirstArgs,[],Vars,Vars).updatevars2(FirstArgs,Vars1,Vars2,Vars3) :-      Vars1=[[Variable,Value]|Vars4],      (member(Variable,FirstArgs), %% removed brackets around firstargs here and 2 line below, ** vars1 into arg in (10), check cond      append(Vars2,Variable,Value,Vars5)),      updatevars2(FirstArgs,Vars4,Vars5,Vars3).updatevars3(Vars1,[],Vars1).updatevars3(Vars1,Vars2,Vars4) :- Vars2=[[Variable,Value]|Vars5], delete(Vars1,[Variable,empty],Vars6), append(Vars6,Variable,Value,Vars7), updatevars3(Vars7,Vars5,Vars4), !.updatevars3(Vars1,Vars2,Vars4) :- Vars2=[[Variable,Value]|Vars5], append(Vars1,Variable,Value,Vars6),      updatevars3(Vars6,Vars5,Vars4).reverse([],List,List).reverse(List1,List2,List3) :- List1=[Head|Tail], append([Head],List2,List4), reverse(Tail,List4,List3).unique1([],Items,Items).unique1([Item|Items1],Items2,Items3) :- delete(Items1,Item,Items4), append(Items2,[Item],Items5), unique1(Items4,Items5,Items3).isvar(Variable) :- atom(Variable).isval(Value) :- number(Value).isvalstr(N) :- isval(N);string(N).isvalempty(N) :- isval(N);(N=empty)./**isvalstrempty(N) :- isval(N);(string(N);N=empty).**/isvalstrempty(N) :- isval(N).isvalstrempty(N) :- string(N).isvalstrempty(empty)./**isvalstrempty(N) :- atom(N),fail,!.**/isvalstrorundef(N) :-  var(N),!.isvalstrorundef(N) :-  not(var(N)),isval(N),!.isvalstrorundef(N) :-  not(var(N)),expression(N),!.undef(N) :- var(N)./**expression(N) :- isval(N);(string(N);atom(N)),!.expression([]).expression(empty).expression([N]) :- expression(N).expression([N|Ns]):- expression(N),expression(Ns).**/
expression(empty) :- !.expression(N) :- isval(N),!.expression(N) :- string(N),!.expression(N) :- atom(N),!.expression([]) :- !.expression(N) :- not(atom(N)), length(N,L),L>=1, expression2(N).expression2([]).expression2([N|Ns]) :- expression3(N), expression2(Ns).expression3(N) :- isval(N),!.expression3(N) :- string(N),!.expression3(N) :- atom(N),!.
expressionnotatom(N) :- isvalstrempty(N),!.expressionnotatom(N) :- not(atom(N)), length(N,L),L>=1, expressionnotatom2(N).expressionnotatom2([]).expressionnotatom2([N|Ns]) :- isvalstrempty(N),     expressionnotatom2(Ns).
substitutevarsA1(Arguments,Vars1,Vars2,Vars3,FirstArgs1,FirstArgs2) :- substitutevarsA2(Arguments,Vars1,Vars2,Vars3,FirstArgs1,FirstArgs2).substitutevarsA2([],_Vars1,Vars2,Vars2,FirstArgs,FirstArgs).substitutevarsA2(Arguments,Vars1,Vars2,Vars3,FirstArgs1,FirstArgs2) :- Arguments=[Variable|Variables], ((getvalue(Variable,Value,Vars1), Value=empty)-> ((append(Vars2,[Variable],Vars4)), (isvar(Variable)->append(FirstArgs1,[Variable], FirstArgs3);FirstArgs3=FirstArgs1)); (getvalue(Variable,Value,Vars1), append(Vars2,[Value],Vars4)), FirstArgs3=FirstArgs1),      substitutevarsA2(Variables,Vars1,Vars4,Vars3,FirstArgs3,FirstArgs2).
findresult3([],_Result,Result2,Result2).findresult3(Arguments1,Result1,Result2,Result3) :- Arguments1=[Value|Arguments2], expressionnotatom(Value), append(Result2,[Value],Result4),      findresult3(Arguments2,Result1,Result4,Result3).findresult3(Arguments1,Result1,Result2,Result3) :-      Arguments1=[Variable|Arguments2],      isvar(Variable), member([Variable,Value],Result1),      append(Result2,[Value],Result4),      findresult3(Arguments2,Result1,Result4,Result3).
== listprologinterpreter3preds5.pl ==
interpretpart(is,Variable1,Variable2,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Value1,Value2,Vars1),
      %%getvalue(Value1,Value1A,Vars1), %%isvalstr(Value1), %%isvalstr(Value1A), expression(Value1), expression(Value2),      val1emptyorvalsequal(Value1,Value2), %%isval(Value2),      putvalue(Variable1,Value2,Vars1,Vars2),     (debug(on)->(writeln([call,[variable,is,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true),     (debug(on)->(writeln([exit,[Variable1,is,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true).interpretpart(isplus,Variable1,Variable2,Variable3,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1), isvalempty(Value1), isval(Value2), isval(Value3),      Value1A is Value2 + Value3,      val1emptyorvalsequal(Value1,Value1A),      putvalue(Variable1,Value1A,Vars1,Vars2),          (debug(on)->(writeln([call,[variable,is,Value2+Value3],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[Value1A,is,Value2+Value3],"Press c."]),(not(get_single_char(97))->true;abort));true).interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),      Value1 = [Value2A, Value3A],      val1emptyorvalsequal(Value2,Value2A),      val1emptyorvalsequal(Value3,Value3A),      putvalue(Variable2,Value2A,Vars1,Vars3),      putvalue(Variable3,Value3A,Vars3,Vars2),          (debug(on)->(writeln([call,[[Value2A, Value3A],=,[variable1,variable2]],"Press c."]),(not(get_single_char(97))->true;abort));true),                     (debug(on)->(writeln([exit,[[Value2A, Value3A],=,[Value2A, Value3A]],"Press c."]),(not(get_single_char(97))->true;abort));true).interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),      Value1A = [Value2, Value3],      val1emptyorvalsequal(Value1,Value1A),      putvalue(Variable1,Value1A,Vars1,Vars2),          (debug(on)->(writeln([call,[variable,=,[Value2,Value3]],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[[Value2,Value3],=,[Value2,Value3]],"Press c."]),(not(get_single_char(97))->true;abort));true).interpretpart(bracket1,Variable1,Variable2,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Value1,Value2,Vars1), Value1A = [Value2],      val1emptyorvalsequal(Value1,Value1A),      %%val1emptyorvalsequal(Value1A,Value2),      putvalue(Variable1,Value1A,Vars1,Vars2),          (debug(on)->(writeln([call,[variable,=,[Value2]],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[Variable2,=,[Value2]],"Press c."]),(not(get_single_char(97))->true;abort));true).interpretpart(bracket2,Variable1,Variable2,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Value1,Value2,Vars1),      Value1A = Value2,      val1emptyorvalsequal(Value1,Value1A),      %%val1emptyorvalsequal(Value2A,Value1),      putvalue(Variable1,Value1A,Vars1,Vars2),          (debug(on)->(writeln([call,[[variable],=,[Value2]],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[[Value2],=,[Value2]],"Press c."]),(not(get_single_char(97))->true;abort));true).interpretpart(head,Variable1,Variable2,Vars1,Vars2) :- getvalues(Variable1,Variable2,Value1,Value2,Vars1), Value1=[Value1A|_Rest],      val1emptyorvalsequal(Value2,Value1A),      putvalue(Variable2,Value1A,Vars1,Vars2),          (debug(on)->(writeln([call,[head,Value1,variable],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[head,Value1,Value1A],"Press c."]),(not(get_single_char(97))->true;abort));true),    !.interpretpart(tail,Variable1,Variable2,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Value1,Value2,Vars1),      Value1=[_Head|Value1A], %%removebrackets(Value1A,Value1B),       val1emptyorvalsequal(Value2,Value1A),      putvalue(Variable2,Value1A,Vars1,Vars2),          (debug(on)->(writeln([call,[tail,Value1,variable],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[tail,Value1,Value1A],"Press c."]),(not(get_single_char(97))->true;abort));true).interpretpart(member,Variable1,Variable2,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Value1,Value2,Vars1), (not(Value1=empty)->(member(Value1,Value2),Vars2=Vars1,          (debug(on)->(writeln([call,[member,Value1,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[member,Value1,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true)); (member(Value3,Value2), putvalue(Variable1,Value3,Vars1,Vars2),              (debug(on)->(writeln([call,[member,variable1,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true),              (debug(on)->(writeln([exit,[member,Value3,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true))).
interpretpart(delete,Variable1,Variable2,Variable3,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),      delete(Value1,Value2,Value3A),      val1emptyorvalsequal(Value3,Value3A),      putvalue(Variable3,Value3A,Vars1,Vars2),          (debug(on)->(writeln([call,[delete,Value1,Value2,variable3],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[delete,Value1,Value2,Value3A],"Press c."]),(not(get_single_char(97))->true;abort));true).
interpretpart(append,Variable1,Variable2,Variable3,Vars1,Vars2) :-      getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),      append1(Value1,Value2,Value3A),      val1emptyorvalsequal(Value3,Value3A),      putvalue(Variable3,Value3A,Vars1,Vars2),          (debug(on)->(writeln([call,[append,Value1,Value2,variable3],"Press c."]),(not(get_single_char(97))->true;abort));true),          (debug(on)->(writeln([exit,[append,Value1,Value2,Value3A],"Press c."]),(not(get_single_char(97))->true;abort));true).getvalues(Variable1,Variable2,Value1,Value2,Vars) :-      getvalue(Variable1,Value1,Vars),      getvalue(Variable2,Value2,Vars).getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars) :-      getvalue(Variable1,Value1,Vars),      getvalue(Variable2,Value2,Vars),      getvalue(Variable3,Value3,Vars).val1emptyorvalsequal(empty,_Value) :- !.val1emptyorvalsequal(Value,Value) :- not(Value=empty).isop(is).isop(=).append1([],Item,Item) :- !.append1(Item1,Item2,Item3) :- ((isvalstr(Item1),Item1A=[Item1]);(not(isvalstr(Item1)),Item1A=Item1)),      ((isvalstr(Item2),Item2A=[Item2]);(not(isvalstr(Item2)),Item2A=Item2)),      %%((isvalstr(Item3),Item3A=[Item3]);(not(isvalstr(Item3)),Item3A=Item3)), append(Item1A,Item2A,Item3)./**delete1(Item1,Item2,Item3) :- ((isvalstr(Item1),Item1A=[Item1]);(not(isvalstr(Item1)),Item1A=Item1)),      ((isvalstr(Item2),Item2A=[Item2]);(not(isvalstr(Item2)),Item2A=Item2)),      %%((isvalstr(Item3),Item3A=[Item3]);(not(isvalstr(Item3)),Item3A=Item3)), delete(Item1A,Item2A,Item3).**/
removebrackets(Value,Value) :-!.removebrackets(Value,Value).
== lpiverify4.pl ==
%% test(Debug[on/off],Total,Score).
test(Debug,N29,S30) :- N22 is 0,S23 is 0,N23 is N22+1,%%writeln([eg1]),((interpret(Debug,[function,[1,1,c]],[
      [function,[a,b,c],(:-),      [              [c,is,a+b]      ]      ]]
,c, 2) ,S24 is S23 +1)->true;S24 is S23),N24 is N23+1,%%writeln([eg2]),((interpret(Debug,[function,[1,1,c]],[
      [function,[a,b,c],(:-),      [              [d,is,a+b],              [c,is,d+1]      ]      ]]
,c, 3), S25 is S24 +1)->true;S25 is S24),N25 is N24+1,%%writeln([eg3]),((interpret(Debug,[function,[1,1,c]],[
      [function,[a,b,c],(:-),      [              [function2,[d,f]],              [e,is,a+b],              [g,is,e+f],              [c,is,g+d]      ]      ],      [function2,[a,f],(:-),      [              [a,is,2],              [f,is,1]      ]      ]]
,c, 5), S26 is S25 +1)->true;S26 is S25),N26 is N25+1,%%writeln([eg4]),((interpret(Debug,[append1,[a]],[
      [append1,[a],(:-),      [              [b,[b]],              [c,[c]],              [append,b,c,a]      ]      ],      [b,["b"]],      [c,["c"]]]
,[[a, ["b", "c"]]]), S27 is S26 +1)->true;S27 is S26),N27 is N26+1,%%writeln([eg5]),((interpret(Debug,[count,[1,n]],[
      [count,[1,2]],      [count,[n,p],(:-),      [              [m,is,n+1],              [count,[m,p]]      ]      ]] ,n, 2),S28 is S27 +1)->true;S28 is S27),N28 is N27+1,%%writeln([eg6]),((interpret(Debug,[count,[0,n]],[
      [count,[1,2]],      [count,[n,p],(:-),      [              [m,is,n+1],              [count,[m,p]]      ]      ]] ,n, 2),S29 is S28 +1)->true;S29 is S28),N29 is N28+1,%%writeln([eg7]),((interpret(Debug,[reverse,[[1,2,3],[],l]],[
      [reverse,[[],l,l]],      [reverse,[l,m,n],(:-),      [       [head,l,h],              [tail,l,t],              [append,h,m,o],              [reverse,[t,o,n]]      ]      ]],[[l, [3, 2, 1]]]),S30 is S29 +1)->true;S30 is S29).
/**
**/