luciangreen commited on
Commit
557a17a
1 Parent(s): 1026e0d

Upload 1424 files

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. Algorithm-Writer-with-Lists/.DS_Store +0 -0
  2. Algorithm-Writer-with-Lists/LICENSE +29 -0
  3. Algorithm-Writer-with-Lists/README.md +195 -0
  4. Algorithm-Writer-with-Lists/algwriter-lists-mr.pl +283 -0
  5. Algorithm-Writer-with-Lists/algwriter-lists-random.pl +295 -0
  6. Algorithm-Writer-with-Lists/brdict3.txt +1 -0
  7. Algorithm-Writer-with-Lists/combophil.pl +33 -0
  8. Algorithm-Writer-with-Lists/combophil2qadb.pl +165 -0
  9. Algorithm-Writer-with-Lists/combophil_grammar_logic_to_alg.pl +208 -0
  10. Algorithm-Writer-with-Lists/combophil_grammar_logic_to_alg_vps.pl +241 -0
  11. Algorithm-Writer-with-Lists/four_crosses.pl +12 -0
  12. Algorithm-Writer-with-Lists/gl-api.pl +63 -0
  13. Algorithm-Writer-with-Lists/grammar_logic_to_alg.pl +277 -0
  14. Algorithm-Writer-with-Lists/grammar_logic_to_alg2.pl +265 -0
  15. Algorithm-Writer-with-Lists/grammar_logic_to_alg_mr.pl +275 -0
  16. Algorithm-Writer-with-Lists/grammar_logic_to_alg_random.pl +197 -0
  17. Algorithm-Writer-with-Lists/grammar_logic_to_alg_sh.txt +9 -0
  18. Algorithm-Writer-with-Lists/midnight_algorithm_input_list.pl +68 -0
  19. Algorithm-Writer-with-Lists/mr_alg.pl +57 -0
  20. Algorithm-Writer-with-Lists/random_dependencies.pl +17 -0
  21. Algorithm-Writer-with-Lists/ya_phil_to_alg.pl +45 -0
  22. Combination-Algorithm-Writer-Multiple-Predicates/.DS_Store +0 -0
  23. Combination-Algorithm-Writer-Multiple-Predicates/LICENSE +29 -0
  24. Combination-Algorithm-Writer-Multiple-Predicates/LPCAWMP_docs.md +20 -0
  25. Combination-Algorithm-Writer-Multiple-Predicates/README.md +135 -0
  26. Combination-Algorithm-Writer-Multiple-Predicates/caw5 copy 12.pl +1252 -0
  27. Combination-Algorithm-Writer-Multiple-Predicates/cawmp.bib +7 -0
  28. Combination-Algorithm-Writer-Multiple-Predicates/cawplistprolog.pl +43 -0
  29. Combination-Algorithm-Writer-Multiple-Predicates/cawpverify.pl +429 -0
  30. Combination-Algorithm-Writer-Multiple-Predicates/cawpverifya.pl +129 -0
  31. Combination-Algorithm-Writer-Multiple-Predicates/convert_lp_to_caw0000.pl +68 -0
  32. Combination-Algorithm-Writer-Multiple-Predicates/lpi_caw_commands.pl +173 -0
  33. Combination-Algorithm-Writer-Multiple-Predicates/rcaw.pl +298 -0
  34. Combination-Algorithm-Writer-Multiple-Predicates/remove_duplicate_predicates.pl +14 -0
  35. Combination-Algorithm-Writer-Stable/LICENSE +29 -0
  36. Combination-Algorithm-Writer-Stable/LPI CAW commands workaround.txt +9 -0
  37. Combination-Algorithm-Writer-Stable/README.md +71 -0
  38. Combination-Algorithm-Writer-Stable/caw5copy11.pl +386 -0
  39. Combination-Algorithm-Writer-Stable/cawplistprolog.pl +5 -0
  40. Combination-Algorithm-Writer-Stable/cawps-api.pl +103 -0
  41. Combination-Algorithm-Writer-Stable/cawpverify.pl +51 -0
  42. Combination-Algorithm-Writer-Stable/listprologinterpreter1listrecursion4copy52.pl +584 -0
  43. Combination-Algorithm-Writer-Stable/listprologinterpreter3preds5copy52.pl +118 -0
  44. Combination-Algorithm-Writer-Stable/lpi_caw_commands.pl +39 -0
  45. Combination-Algorithm-Writer-with-Predicates/LICENSE +29 -0
  46. Combination-Algorithm-Writer-with-Predicates/LPI CAW commands workaround.txt +9 -0
  47. Combination-Algorithm-Writer-with-Predicates/README.md +73 -0
  48. Combination-Algorithm-Writer-with-Predicates/caw5copy11.pl +351 -0
  49. Combination-Algorithm-Writer-with-Predicates/cawps-api.pl +103 -0
  50. Combination-Algorithm-Writer-with-Predicates/listprologinterpreter1listrecursion4copy52.pl +576 -0
Algorithm-Writer-with-Lists/.DS_Store ADDED
Binary file (6.15 kB). View file
 
Algorithm-Writer-with-Lists/LICENSE ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2020, Lucian Green
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Algorithm-Writer-with-Lists/README.md ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <a href="https://github.com/luciangreen/Essay-Helper">Essay Helper</a> | <a href="https://github.com/luciangreen/Text-to-Breasonings">Text-to-Breasonings (Helps Earn High Distinctions)</a> | Grammar Logic (Helps Mind Map Details)
2
+
3
+ # Algorithm Writer with Lists
4
+
5
+ Generates random algorithms in List Prolog, which can be converted to a simple form similar to Prolog.
6
+
7
+ * algwriter-lists-mr.pl - generates algorithms with combinations of other algorithms with vague mind reading
8
+ * algwriter-lists-random.pl - generates algorithms with combinations of other algorithms at random
9
+ * grammar_logic_to_alg.pl - takes text, randomly generates details and appropriate algorithms for A (agreement), B (disagreement) and solution to B based on approximate parts of speech and the Text to Breasonings dictionary.
10
+ * grammar_logic_to_alg_random.pl - like the previous algorithm but randomly selects up to 10 sentences from the file to process.
11
+ * random_dependencies.pl - generates random (meaningless) algorithm specifications.
12
+ * combophil.pl - finds combinations of lines to write philosophies about.
13
+ * combophil_grammar_logic_to_alg.pl - finds all combinations in lines to write philosophies and does what grammar_logic_to_alg.pl (above) does.
14
+ * four_crosses.pl - finds 4 crosses to transition to PhD refinement.
15
+ * combophil2qadb.pl - generates algorithms from dictionary.
16
+ * mr_alg.pl and ya_phil_to_alg.pl - Yet Another Philosophy to Algorithm and Mind Read Algorithm (randomly produces 5 line algorithms with type checking, possibly about Lucian Academy philosophies).
17
+
18
+ Please contact Lucian Green at <a href="mailto:luciangreen@lucianacademy.com">luciangreen@lucianacademy.com</a> with questions, comments and feedback about Algorithm Writer with Lists.
19
+
20
+ # Getting Started
21
+
22
+ Please read the following instructions on how to install the project on your computer for generating algorithms.
23
+
24
+ # Prerequisites
25
+
26
+ * Please download and install SWI-Prolog for your machine at `https://www.swi-prolog.org/build/`.
27
+
28
+ # 1. Install manually
29
+
30
+ Download <a href="http://github.com/luciangreen/Algorithm-Writer-with-Lists/">this repository</a>, the <a href="https://github.com/luciangreen/listprologinterpreter">List Prolog Interpreter Repository</a> and the <a href="https://github.com/luciangreen/Text-to-Breasonings">Text to Breasonings Repository</a>.
31
+
32
+ # 2. Or Install from List Prolog Package Manager (LPPM)
33
+
34
+ * Download the <a href="https://github.com/luciangreen/List-Prolog-Package-Manager">LPPM Repository</a>:
35
+
36
+ ```
37
+ mkdir GitHub
38
+ cd GitHub/
39
+ git clone https://github.com/luciangreen/List-Prolog-Package-Manager.git
40
+ cd List-Prolog-Package-Manager
41
+ swipl
42
+ ['lppm'].
43
+ lppm_install("luciangreen","Algorithm-Writer-with-Lists").
44
+ halt
45
+ ```
46
+
47
+ # Caution:
48
+
49
+ follow instructions in <a href="https://github.com/luciangreen/Text-to-Breasonings/blob/master/Instructions_for_Using_texttobr(2).pl.txt">Instructions for Using texttobr(2)</a> when using texttobr, texttobr2 or mind reader to avoid medical problems.
50
+
51
+ # Running Algorithm Writer with Lists with Mind Reader
52
+
53
+ * In Shell:
54
+ `cd Algorithm-Writer-with-Lists`
55
+ `swipl`
56
+ `['algwriter-lists-mr.pl'].`
57
+
58
+ * Enter `algwriter(Algorithm),writeln1(Algorithm).`
59
+
60
+ # Installing and Running Algorithm Writer with Lists at Random
61
+
62
+ * Download this repository.
63
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
64
+ ```
65
+ ['algwriter-lists-random.pl'].
66
+ ```
67
+ * Enter `algwriter(Algorithm),writeln1(Algorithm).`
68
+
69
+ # Installing and Running Grammar and Logic to Algorithm
70
+
71
+ * Download this repository.
72
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
73
+ ```
74
+ ['grammar_logic_to_alg.pl'].
75
+ ```
76
+ * Enter sentences and lines to randomly process to give details and algorithms to into `"file.txt"` in the `"Text-to-Breasonings"` folder. The output or the `gla.txt` file, depending on whether you run using (1) or (2) in the following will contain the sentences and algorithms with instructions about how to run them.
77
+
78
+ * Enter `grammar_logic_to_alg1.` (1) or paste the contents of `grammar_logic_to_alg_sh.txt` into the Terminal window on Mac (2).
79
+
80
+ # Installing and Running Grammar and Logic to Algorithm at Random
81
+
82
+ * Download this repository.
83
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
84
+ ```
85
+ ['grammar_logic_to_alg_random.pl'].
86
+ ```
87
+ * As stated above, rather than processing the whole file, this algorithm processes up to 10 random sentences.
88
+ * Enter many sentences and lines to randomly process to give details and algorithms to into `"file.txt"` in the `"Algorithm-Writer-with-Lists"` folder. The output or the `gla.txt` file, depending on whether you run using (1) or (2) in the following will contain the sentences and algorithms with instructions about how to run them.
89
+
90
+ * Enter `grammar_logic_to_alg1.` (1) or paste the contents of `grammar_logic_to_alg_sh.txt` into the Terminal window on Mac (2).
91
+
92
+ # Installing and Running Random Dependencies
93
+
94
+ * Download this repository.
95
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
96
+ ```
97
+ ['random_dependencies.pl'].
98
+ ```
99
+ * As stated above, the value of this algorithm's input is found by mentally inserting not and transformations.
100
+
101
+ * Enter `random_dependencies(A),writeln1(A).`
102
+
103
+ ```
104
+ [substring,[[duplicates,[[]]],[findall,[[add_number_to_list,[[split_into_sentences,[[sort,[[list_head,[[reverse,[[findall,[[string_to_list,[[or,[[and,[[delete,[[map,[[]]]]],[]]]]]]]]]]]]],[map,[[length,[[maximum,[[length,[[reverse,[[split_into_sentences,[[member,[[map,[[]]]]]]],[]]]]]]],[delete,[[get_item_n,[[],[and,[[]]]]],[reverse,[[]]]]]]],[]]]]]]]]]]]]]
105
+ ```
106
+
107
+ # Installing and Running Combinations of Lines of Philosophy
108
+
109
+ * Download this repository.
110
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
111
+ ```
112
+ ['combophil.pl'].
113
+ ```
114
+ * Finds combinations of lines to write philosophies about with e.g. a combination of 2 lines:
115
+
116
+ * Enter `combophil(2).`
117
+
118
+ # Installing and Running Combinations of Lines of Philosophy with Grammar and Logic to Algorithm
119
+
120
+ * Download this repository.
121
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
122
+ ```
123
+ ['combophil_grammar_logic_to_alg.pl'].
124
+ ```
125
+ * Enter sentences and lines to intermix (warning: takes all combinations of verbs and non verbs in file) and randomly process to give details and algorithms to into `"file.txt"` in the `"Text-to-Breasonings"` folder. The output in the following will contain the sentences and algorithms with instructions about how to run them.
126
+
127
+ * Enter `combophil_grammar_logic_to_alg1.`
128
+
129
+ # Installing and Running Four Crosses
130
+ * Download this repository.
131
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
132
+ ```
133
+ ['four_crosses.pl'].
134
+ ```
135
+ * Enter four levels of crosses of ideas to make a PhD-level connection.
136
+
137
+ * Enter `four_crosses.`
138
+
139
+ # grammar_logic_to_alg2.pl
140
+ * Converts files from e.g. grammar_logic_to_alg.pl to simplified algorithms with more specific command names. Breason out file.txt using <a href="https://github.com/luciangreen/Text-to-Breasonings">Text-to-Breasonings</a>, run grammar_logic_to_alg.pl, run
141
+ grammar_logic_to_alg2.pl, then run <a href="https://github.com/luciangreen/Text-to-Breasonings">Text-to-Breasonings Reading Algorithm</a> on the grammar_logic_to_alg2.pl output.
142
+ * Download this repository.
143
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
144
+ ```
145
+ ['grammar_logic_to_alg2.pl'].
146
+ ```
147
+ * Enter the number of words in file.txt to replace with computational language (it learns as it goes).
148
+
149
+ * Enter `grammar_logic_to_alg2(200).`
150
+
151
+ # combophil2qadb.pl
152
+ * Generates algorithms in qadb format from word dictionary.
153
+ * Download this repository.
154
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
155
+ ```
156
+ ['combophil2qadb.pl'].
157
+ ```
158
+ * Enter `.` or `\n` delimited algorithm ideas in `Text-to-Breasonings/file.txt` and enter:
159
+ `combophil2qadb.`
160
+ * Produces algorithms in `file-o.txt`.
161
+
162
+ # Installing and Running Yet Another Philosophy to Algorithm and Mind Read Algorithm
163
+
164
+ * Download this repository.
165
+ * In SWI-Prolog, in the `"Algorithm-Writer-with-Lists"` folder, enter:
166
+ ```
167
+ ['ya_phil_to_alg.pl.pl'].
168
+
169
+ ```
170
+ * As stated above, Yet Another Philosophy to Algorithm randomly produces 5 line algorithms with type checking about all sentences in `Lucian-Academy/`.
171
+
172
+
173
+ * Enter `ya_phil_to_alg.`
174
+
175
+ * Produces the file `yet_more_phil_algs.txt`, containing, for example:
176
+ ```
177
+ ...n,length,list,number,n,round,number,number,n,sqrt,number,number,n,sqrt,number,number,n,round,number,number,n,sqrt,number,number...
178
+ ```
179
+
180
+ * To produce one five-line algorithm, enter `mr_alg(Commands).`, producing:
181
+
182
+ * `Commands = [[[n,member],[[list],[_3386]]],[[n,member],[[list],[list]]],[[n,intersection],[[list],[list]]],[[n,member],[[list],[_5928]]],[[n,atom_string],[[atom],[string]]],[[n,string_length],[[string],[number]]]]`,
183
+
184
+ * where this algorithm means, "Take a member (the second item, the first output of member) of a list, take a member list of this list, etc.".
185
+
186
+ * Note: member(List,Item) is now member(Item,List) in List Prolog, which is different from the above.
187
+
188
+ # Authors
189
+
190
+ Lucian Green - Initial programmer - <a href="https://www.lucianacademy.com/">Lucian Academy</a>
191
+
192
+ # License
193
+
194
+ I licensed this project under the BSD3 License - see the <a href="LICENSE">LICENSE.md</a> file for details
195
+
Algorithm-Writer-with-Lists/algwriter-lists-mr.pl ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %% algwriter(A),write1(A).
2
+ %% write data,fns in exec format
3
+
4
+ :-include('../Text-to-Breasonings/text_to_breasonings.pl').
5
+
6
+ multiply(A,B,C) :-
7
+ C is A*B.
8
+
9
+ subtract(A,B,C) :-
10
+ C is A-B.
11
+
12
+ head(A,B) :- A=[B|_].
13
+
14
+ tail(A,B) :- A=[_|B].
15
+
16
+ wrap(A,B) :- B=[A].
17
+
18
+ algwriter(Na) :-
19
+ notrace,
20
+ phrase_from_file_s(string(BrDict0), "../Text-to-Breasonings/brdict1.txt"),
21
+ %%SepandPad="&#@~%`$?-+*^,()|.:;=_/[]<>{}\n\r\s\t\\\"!'0123456789",
22
+ splitfurther(BrDict0,BrDict01),
23
+ %%trace,
24
+ sort(BrDict01,BrDict02),
25
+ makerandomlist(BrDict02,5,[],R0),makerandomlist(R0,3,[],R1),makerandomlist(R0,3,[],R2),wrap(R1,Nb1),wrap(R2,Nb2),append(Nb1,Nb2,Nb3),trialy2A([3,4,5,6,7,8],NFns),randomfns(NFns,Nb3,Na),!.
26
+ makerandomlist(_,0,A,A).
27
+ makerandomlist(List,A,C1,C) :-
28
+ not((=(A,0))),trialy2A(List,N1),%%random(R),multiply(R,L,R1),ceiling(R1,N1),
29
+ wrap(N1,N2),append(C1,N2,Nb3),subtract(A,1,D),makerandomlist(List,D,Nb3,C).
30
+ randomfns(1,B,Nb3):-randomlist(B,Na1),randomlist(B,Na2),randomfn(Na1,Na2,Nb),wrap(Nb,Nb2),append(B,Nb2,Nb3).%%write1(".").
31
+ randomfns(A,B,C) :- not((=(A,1))),randomlist(B,Na1),randomlist(B,Na2),randomfn(Na1,Na2,Nb),wrap(Nb,Nb2),append(B,Nb2,Nb3),%%tail(B,T),
32
+ subtract(A,1,D),write(,),
33
+ randomfns(D,Nb3,C).
34
+ randomlist(B,Na) :- %%List1=[1,2,3,4,5,6,7,9,10],
35
+ length(B,L),length(C,L),append(C,_,B),trialy2A(C,Na).%%,random(R),length(B,Bl),multiply(R,Bl,N),ceiling(N,N1),getitemn(N1,B,Na).
36
+ getitemn(0,_A,[]).
37
+ getitemn(1,B,C) :- head(B,C).
38
+ getitemn(A,B,C) :- not((=(A,1))),tail(B,T),subtract(A,1,D),getitemn(D,T,C).
39
+ randomfn(A1,A2,B) :- repeat,trialy2A([1,3,4,5,7],N1),%%random(R),multiply(R,8,N),ceiling(N,N1),
40
+ fna(N1,A1,A2,B).
41
+ fna(1,A1,_A2,B) :- reverse(A1,[],B),write1([[n,reverse],[A1,[],B]]).
42
+ %%fna(2,A1,_A2,B) :- sort0(A1,B),write1(sort0(A1,B)).
43
+ fna(3,A1,A2,B) :- append1(A1,A2,B),write1([[n,append1],[A1,A2,B]]).
44
+ fna(4,A1,A2,B) :- minus1(A1,A2,B),write1([[n,minus1],[A1,A2,B]]).
45
+ fna(5,A1,A2,B) :- intersection1(A1,A2,[],B),write1([[n,intersection1],[A1,A2,[],B]]).
46
+ %%fna(6,A1,A2,A2) :- mutually_exclusive(A1,A2),write1([[n,mutually_exclusive],[A1,A2]]).
47
+ fna(7,A1,A2,B) :- duplicates(A1,A2,[],B),write1([[n,duplicates],[A1,A2,[],B]]).
48
+ %%fna(8,A1,A2,A1) :- substring(A1,A2),write1([[n,substring],[A1,A2]]).
49
+ reverse([],L,L).
50
+ reverse(L,M,N) :- head(L,H),tail(L,T),wrap(H,H1),append(H1,M,O),reverse(T,O,N).
51
+ sort0(L,N) :- sort1(L,[],N).
52
+ sort1([],L,L).
53
+ sort1(L,M1,N) :- not((=(L,[]))),head(L,H),tail(L,T),maximum(T,H,M2,[],R),wrap(M2,M3),append(M1,M3,M4),sort1(R,M4,N).
54
+ maximum([],L,L,R,R).
55
+ maximum(L,M1,N,R1,R2) :- not((=(L,[]))),head(L,H),tail(L,T),(>=(M1,H)->(=(M2,M1),wrap(H,H2),append(R1,H2,R3));(=(M2,H),wrap(M1,M12),append(R1,M12,R3))),maximum(T,M2,N,R3,R2).
56
+ map(_F,[],L,L).
57
+ map(F,L,M1,N) :- not((=(L,[]))),head(L,H),tail(L,T),functor(A,F,3),arg(1,A,M1),arg(2,A,H),arg(3,A,M2),A,map(F,T,M2,N).
58
+ findall(_F,[],L,L).
59
+ findall(F,L,M1,N) :- not((=(L,[]))),head(L,H),tail(L,T),functor(A,F,2),arg(1,A,H),arg(2,A,M2),(A->((wrap(M2,M3),append(M1,M3,M4)));(=(M1,M4))),findall(F,T,M4,N).
60
+ intersection1([],_A,L,L).
61
+ intersection1(L1,L2,L3a,L3) :- head(L1,I1),tail(L1,L4),intersection2(I1,L2,[],L5),append(L3a,L5,L6),intersection1(L4,L2,L6,L3).
62
+ intersection2(_A,[],L,L).
63
+ intersection2(I1,L1,L2,L3) :- head(L1,I1),tail(L1,L4),wrap(I1,I11),append(L2,I11,L5),intersection2(I1,L4,L5,L3).
64
+ intersection2(I1,L1,L2,L3) :- head(L1,I2),tail(L1,L4),not((=(I1,I2))),intersection2(I1,L4,L2,L3).
65
+ append1(B,C,A) :- append(B,C,A).
66
+ minus1(L,[],L).
67
+ minus1(L1,L2,L3) :- head(L2,I1),tail(L2,L5),delete2(L1,I1,[],L6),minus1(L6,L5,L3).
68
+ delete2([],_A,L,L).
69
+ delete2(L1,I1,L2,L3) :- head(L1,I1),tail(L1,L5),delete2(L5,I1,L2,L3).
70
+ delete2(L1,I1,L2,L3) :- head(L1,I2),tail(L1,L5),not((=(I1,I2))),wrap(I2,I21),append(L2,I21,L6),delete2(L5,I1,L6,L3).
71
+
72
+ mutually_exclusive([],_L):-!.
73
+ mutually_exclusive(L,M):-head(L,H),tail(L,T),membera3(M,H),mutually_exclusive(T,M),!.
74
+ membera3([],_L):-!.
75
+ membera3(L,M):-head(L,H),tail(L,T),not((=(M,H))),membera3(T,M),!.
76
+
77
+ duplicates([],_L,S,S).
78
+ duplicates(L,M,S1,S2):-head(L,H),tail(L,T),member(H,M),(deletea2(M,H,M1)->(true);(=(M,M1))),wrap(H,H1),append(S1,H1,S3),duplicates(T,M1,S3,S2),!.
79
+ duplicates(L,M,S1,S2):-head(L,H),tail(L,T),not((membera4(M,H))),duplicates(T,M,S1,S2).
80
+ deletea2([],_L,_M1):-fail.
81
+ deletea2(L,M,T):-head(L,H),tail(L,T),=(M,H).
82
+ deletea2(L,M,M1):-head(L,H),tail(L,T),not((=(M,H))),deletea2(T,M,M1).
83
+ membera4([],_L):-fail.
84
+ membera4(L,H):-head(L,H).
85
+ membera4(L,M):-head(L,H),tail(L,T),not(M=H),membera4(T,M).
86
+
87
+ substring([],[]).
88
+ substring([],B):-not((=(B,[]))),fail.
89
+ substring(A,B):-tail(A,At),(listhead(A,B)->(true);(substring(At,B))).
90
+ listhead(_L,[]).
91
+ listhead(A,B):-head(A,Ah),tail(A,At),head(B,Ah),tail(B,Bt),listhead(At,Bt).
92
+
93
+
94
+ %%%%%%%%%%%%
95
+
96
+ %%findbest(R,R) :-!.
97
+ findbest2(R,Item):-
98
+ sort(R,RA),
99
+ reverse(RA,RB),
100
+ RB=[[_,Item]|_Rest].
101
+
102
+ trialy2A([],R) :-
103
+ R=[[_,'A']].
104
+ trialy2A(List,R) :-
105
+ notrace,trialy2B(List,R).%%,trace.
106
+ trialy2B(List,R) :-
107
+ length(List,Length),
108
+ ((Length=<9->
109
+ findr4(R4),
110
+ number_string(R4,R4A),
111
+ formr5([R4A],9,Length,R5),
112
+ findr(R5,List,R));
113
+ (Length=<99->
114
+ findr4(R41),
115
+ findr4(R42),
116
+ formr5([R41,R42],99,Length,R5),
117
+ findr(R5,List,R));
118
+ (Length=<999->
119
+ findr4(R41),
120
+ findr4(R42),
121
+ findr4(R43),
122
+ formr5([R41,R42,R43],999,Length,R5),
123
+ findr(R5,List,R));
124
+ (Length=<9999->
125
+ findr4(R41),
126
+ findr4(R42),
127
+ findr4(R43),
128
+ findr4(R44),
129
+ formr5([R41,R42,R43,R44],9999,Length,R5),
130
+ findr(R5,List,R));
131
+ (Length=<99999->
132
+ findr4(R41),
133
+ findr4(R42),
134
+ findr4(R43),
135
+ findr4(R44),
136
+ findr4(R45),
137
+ formr5([R41,R42,R43,R44,R45],99999,Length,R5),
138
+ findr(R5,List,R));
139
+ fail),
140
+ %%write1([r,R]),trace.
141
+ true.
142
+
143
+ findr4(R4) :-
144
+ List1=[0,1,2,3,4,5,6,7,8,9],
145
+ Trials is 30,
146
+ trialy22(List1,Trials,[],R1),
147
+ findbest2(R1,R4).
148
+ %%number_string(R3,R2),
149
+ formr5(RList,Upper,Length,R5) :-
150
+ %%findall(D,(member(C,RList),floor(C,D)),RList2),
151
+ concat_list2A(RList,R5A),
152
+ number_string(R5B,R5A),
153
+ R51 is floor((R5B/Upper)*Length),
154
+ (R5B=Upper->R5 is R51-1;R5=R51).
155
+ findr(R4,List,R) :-
156
+ %%floor(R4,R4A),
157
+ length(A,R4),
158
+ append(A,[R|_],List).
159
+
160
+ %%random_member(A,List),
161
+ %%R=[[_,A]].
162
+
163
+ /**
164
+ length(List,L),
165
+ Trials is L*3,
166
+ trialy22(List,Trials,[],R).**/
167
+
168
+ trialy22([],_,R,R) :- !.
169
+ trialy22(List,Trials,RA,RB) :-
170
+ List=[Item|Items],
171
+ trialy21(Item,Trials,R1),
172
+ append(RA,[R1],RC),
173
+ trialy22(Items,Trials,RC,RB),!.
174
+
175
+ trialy21(Label,Trials,RA) :-
176
+ trialy3(Trials,[],R),
177
+ aggregate_all(count, member(true,R), Count),
178
+ RA=[Count,Label].
179
+
180
+ trialy3(0,R,R) :-!.
181
+ trialy3(Trials1,RA,RB) :-
182
+ trialy1(R1),
183
+ append(RA,[R1],RC),
184
+ Trials2 is Trials1-1,
185
+ trialy3(Trials2,RC,RB),!.
186
+
187
+ %% try other nouns
188
+ trialy1(R1) :-
189
+ %%control11(A1),
190
+ %%repeat,
191
+ trial0(A22), %% Control
192
+ sum(A22,0,S22),
193
+ mean(S22,A1),
194
+ %%repeat,
195
+ trial0(A21), %% Test 1
196
+ sum(A21,0,S02),
197
+ mean(S02,A2),
198
+ (A1>A2->R1=true;R1=fail).
199
+
200
+ trial0(S3) :- N is 10, trial1(N,[],S),trial01(S,S3).
201
+ trial01(S1,S3) :-
202
+ sort(S1,S),
203
+ %%midpoint(S,MP),
204
+ halves(S,H1,H2),
205
+ midpoint(H1,Q1),
206
+ midpoint(H2,Q3),
207
+ IQR is Q3-Q1,
208
+ sum(S,0,S02),
209
+ mean(S02,Mean),
210
+ furthestfrommean(S,Mean,V),
211
+ D1 is 1.5*IQR,
212
+ D2 is V-Mean,
213
+ (D2>D1->(delete(S,V,S2),trial01(S2,S3));S=S3).
214
+
215
+ %%trial1(0,[],_A) :- fail,!.
216
+ trial1(0,A,A) :- !.
217
+ trial1(N,A,B) :- mindreadtest(S), append(A,[S],A2),
218
+ N1 is N-1,trial1(N1,A2,B).
219
+
220
+ %%midpoint([],0) :- !.
221
+
222
+ midpoint(S,MP) :-
223
+ %% not(S=[]),
224
+ length(S,L),
225
+ A is mod(L,2),
226
+ (A is 0->
227
+ (M1 is L/2, M2 is M1+1,N1 is M1-1,N2 is M2-1,length(N11,N1),length(N21,N2),append(N11,[N12|_Rest1],S),append(N21,[N22|_Rest2],S),MP is (N12+N22)/2)
228
+ ;
229
+ (L2 is L+1, M1 is L2/2, N1 is M1-1,length(N11,N1),append(N11,[MP|_Rest],S))).
230
+
231
+ halves(S,H1,H2) :-
232
+ length(S,L),
233
+ A is mod(L,2),
234
+ (A is 0->
235
+ (M1 is L/2,length(H1,M1),append(H1,H2,S))
236
+ ;
237
+ (L2 is L-1,M1 is L2/2,length(H1,M1),append(H1,[_|H2],S))).
238
+
239
+ sum([],S,S):-!.
240
+ sum(S0,S1,S2) :-
241
+ S0=[S3|S4],
242
+ S5 is S1+S3,
243
+ sum(S4,S5,S2).
244
+
245
+ mean(Sum,Mean) :-
246
+ Mean is Sum/2.
247
+
248
+ furthestfrommean(S,Mean,V) :-
249
+ absdiffmean(S,Mean,[],D),
250
+ sort(D,D1),
251
+ reverse(D1,[[_,V]|_Rest]).
252
+
253
+ absdiffmean([],_M,D,D) :- !.
254
+ absdiffmean(S,M,D1,D2) :-
255
+ S=[S1|S2],
256
+ S3 is abs(S1-M),
257
+ append(D1,[[S3,S1]],D3),
258
+ absdiffmean(S2,M,D3,D2).
259
+
260
+ mindreadtest(Sec) :-
261
+ %% 250 br for characters to be br out with 10 br each from person to me - do when initial 250 br test done and doing 10 br test
262
+ %%comment(fiftyastest),
263
+ %%random(X),X1 is 10*X, X2 is floor(X1), (X2=<2 -> (
264
+ %%texttobr,write1(['true test']), %%); %% use breasonings breasoned out by computer for not by me, for job medicine for "me", at last time point
265
+ %%true), %% leave last time point blank
266
+ %%**texttobr2(640);true),%% make an A to detect reaction to gracious giving or blame of in following
267
+ get_time(TimeStamp1),
268
+ %%phrase_from_file(string(_String), 'file.txt'),
269
+ texttobr2(2), %% 100 As for answer (must be br before this on same day)
270
+ %% is gracious giving or blame
271
+ get_time(TimeStamp2),
272
+ %%comment(turnoffas),
273
+ Sec is TimeStamp2 - TimeStamp1.
274
+
275
+ concat_list2A(A1,B):-
276
+ A1=[A|List],
277
+ concat_list2A(A,List,B),!.
278
+
279
+ concat_list2A(A,[],A):-!.
280
+ concat_list2A(A,List,B) :-
281
+ List=[Item|Items],
282
+ string_concat(A,Item,C),
283
+ concat_list2A(C,Items,B).
Algorithm-Writer-with-Lists/algwriter-lists-random.pl ADDED
@@ -0,0 +1,295 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %% algwriter(A),write1(A).
2
+ %% write data,fns in exec format
3
+
4
+ :-include('../Text-to-Breasonings/text_to_breasonings.pl').
5
+
6
+ multiply(A,B,C) :-
7
+ C is A*B.
8
+
9
+ subtract(A,B,C) :-
10
+ C is A-B.
11
+
12
+ head(A,B) :- A=[B|_].
13
+
14
+ tail(A,B) :- A=[_|B].
15
+
16
+ wrap(A,B) :- B=[A].
17
+
18
+ algwriter(Na) :-
19
+ %%notrace,
20
+ phrase_from_file_s(string(BrDict0), "../Text-to-Breasonings/brdict1.txt"),
21
+ %%SepandPad="&#@~%`$?-+*^,()|.:;=_/[]<>{}\n\r\s\t\\\"!'0123456789",
22
+ splitfurther(BrDict0,BrDict01),
23
+ %%trace,
24
+ sort(BrDict01,BrDict02),
25
+ makerandomlist(BrDict02,5,[],R0),makerandomlist(R0,3,[],R1),makerandomlist(R0,3,[],R2),wrap(R1,Nb1),wrap(R2,Nb2),append(Nb1,Nb2,Nb3),trialy2A([3,4,5,6,7,8],NFns),randomfns(NFns,Nb3,Na),!.
26
+ makerandomlist(_,0,A,A).
27
+ makerandomlist(List,A,C1,C) :-
28
+ not((=(A,0))),trialy2A(List,N1),%%random(R),multiply(R,L,R1),ceiling(R1,N1),
29
+ wrap(N1,N2),append(C1,N2,Nb3),subtract(A,1,D),makerandomlist(List,D,Nb3,C).
30
+ randomfns(1,B,Nb3):-randomlist(B,Na1),randomlist(B,Na2),randomfn(Na1,Na2,Nb),wrap(Nb,Nb2),append(B,Nb2,Nb3).%%write1(".").
31
+ randomfns(A,B,C) :- not((=(A,1))),randomlist(B,Na1),randomlist(B,Na2),randomfn(Na1,Na2,Nb),wrap(Nb,Nb2),append(B,Nb2,Nb3),%%tail(B,T),
32
+ subtract(A,1,D),write(,),
33
+ randomfns(D,Nb3,C).
34
+ randomlist(B,Na) :- %%List1=[1,2,3,4,5,6,7,9,10],
35
+ length(B,L),length(C,L),append(C,_,B),trialy2A(C,Na).%%,random(R),length(B,Bl),multiply(R,Bl,N),ceiling(N,N1),getitemn(N1,B,Na).
36
+ getitemn(0,_A,[]).
37
+ getitemn(1,B,C) :- head(B,C).
38
+ getitemn(A,B,C) :- not((=(A,1))),tail(B,T),subtract(A,1,D),getitemn(D,T,C).
39
+ randomfn(A1,A2,B) :- %%repeat,
40
+ trialy2A([1,3,4,5,7],N1),%%random(R),multiply(R,8,N),ceiling(N,N1),
41
+ fna(N1,A1,A2,B).
42
+ fna(1,A1,_A2,B) :- reverse(A1,[],B),writeln1([[n,reverse],[A1,[],B]]),term_to_atom(A1,A11),term_to_atom(B,B1),concat_list(["I reversed ",A11,", giving ",B1,"."],C),writeln1(C).
43
+ %%fna(2,A1,_A2,B) :- sort0(A1,B),write1(sort0(A1,B)).
44
+ fna(3,A1,A2,B) :- append1(A1,A2,B),writeln1([[n,append1],[A1,A2,B]]),term_to_atom(A1,A11),term_to_atom(A2,A21),term_to_atom(B,B1),concat_list(["I appended ",A11," to ",A21,", giving ",B1,"."],C),writeln1(C).
45
+ fna(4,A1,A2,B) :- minus1(A1,A2,B),writeln1([[n,minus1],[A1,A2,B]]),term_to_atom(A1,A11),term_to_atom(A2,A21),term_to_atom(B,B1),concat_list(["I subtracted ",A21," from ",A11,", giving ",B1,"."],C),writeln1(C).
46
+ fna(5,A1,A2,B) :- intersection1(A1,A2,[],B),writeln1([[n,intersection1],[A1,A2,[],B]]),term_to_atom(A1,A11),term_to_atom(A2,A21),term_to_atom(B,B1),concat_list(["I found the intersection of ",A11," and ",A21,", ",B1,"."],C),writeln1(C).
47
+ %%fna(6,A1,A2,A2) :- mutually_exclusive(A1,A2),write1([[n,mutually_exclusive],[A1,A2]]).
48
+ fna(7,A1,A2,B) :- duplicates(A1,A2,[],B),writeln1([[n,duplicates],[A1,A2,[],B]]),term_to_atom(A1,A11),term_to_atom(A2,A21),term_to_atom(B,B1),concat_list(["I found the duplicates in ",A11," and ",A21,", ",B1,"."],C),writeln1(C).
49
+ %%fna(8,A1,A2,A1) :- substring(A1,A2),write1([[n,substring],[A1,A2]]).
50
+ reverse([],L,L).
51
+ reverse(L,M,N) :- head(L,H),tail(L,T),wrap(H,H1),append(H1,M,O),reverse(T,O,N).
52
+ sort0(L,N) :- sort1(L,[],N).
53
+ sort1([],L,L).
54
+ sort1(L,M1,N) :- not((=(L,[]))),head(L,H),tail(L,T),maximum(T,H,M2,[],R),wrap(M2,M3),append(M1,M3,M4),sort1(R,M4,N).
55
+ maximum([],L,L,R,R).
56
+ maximum(L,M1,N,R1,R2) :- not((=(L,[]))),head(L,H),tail(L,T),(>=(M1,H)->(=(M2,M1),wrap(H,H2),append(R1,H2,R3));(=(M2,H),wrap(M1,M12),append(R1,M12,R3))),maximum(T,M2,N,R3,R2).
57
+ map(_F,[],L,L).
58
+ map(F,L,M1,N) :- not((=(L,[]))),head(L,H),tail(L,T),functor(A,F,3),arg(1,A,M1),arg(2,A,H),arg(3,A,M2),A,map(F,T,M2,N).
59
+ findall(_F,[],L,L).
60
+ findall(F,L,M1,N) :- not((=(L,[]))),head(L,H),tail(L,T),functor(A,F,2),arg(1,A,H),arg(2,A,M2),(A->((wrap(M2,M3),append(M1,M3,M4)));(=(M1,M4))),findall(F,T,M4,N).
61
+ intersection1([],_A,L,L).
62
+ intersection1(L1,L2,L3a,L3) :- head(L1,I1),tail(L1,L4),intersection2(I1,L2,[],L5),append(L3a,L5,L6),intersection1(L4,L2,L6,L3).
63
+ intersection2(_A,[],L,L).
64
+ intersection2(I1,L1,L2,L3) :- head(L1,I1),tail(L1,L4),wrap(I1,I11),append(L2,I11,L5),intersection2(I1,L4,L5,L3).
65
+ intersection2(I1,L1,L2,L3) :- head(L1,I2),tail(L1,L4),not((=(I1,I2))),intersection2(I1,L4,L2,L3).
66
+ append1(B,C,A) :- append(B,C,A).
67
+ minus1(L,[],L).
68
+ minus1(L1,L2,L3) :- head(L2,I1),tail(L2,L5),delete2(L1,I1,[],L6),minus1(L6,L5,L3).
69
+ delete2([],_A,L,L).
70
+ delete2(L1,I1,L2,L3) :- head(L1,I1),tail(L1,L5),delete2(L5,I1,L2,L3).
71
+ delete2(L1,I1,L2,L3) :- head(L1,I2),tail(L1,L5),not((=(I1,I2))),wrap(I2,I21),append(L2,I21,L6),delete2(L5,I1,L6,L3).
72
+
73
+ mutually_exclusive([],_L):-!.
74
+ mutually_exclusive(L,M):-head(L,H),tail(L,T),membera3(M,H),mutually_exclusive(T,M),!.
75
+ membera3([],_L):-!.
76
+ membera3(L,M):-head(L,H),tail(L,T),not((=(M,H))),membera3(T,M),!.
77
+
78
+ duplicates([],_L,S,S).
79
+ duplicates(L,M,S1,S2):-head(L,H),tail(L,T),member(H,M),(deletea2(M,H,M1)->(true);(=(M,M1))),wrap(H,H1),append(S1,H1,S3),duplicates(T,M1,S3,S2),!.
80
+ duplicates(L,M,S1,S2):-head(L,H),tail(L,T),not((membera4(M,H))),duplicates(T,M,S1,S2).
81
+ deletea2([],_L,_M1):-fail.
82
+ deletea2(L,M,T):-head(L,H),tail(L,T),=(M,H).
83
+ deletea2(L,M,M1):-head(L,H),tail(L,T),not((=(M,H))),deletea2(T,M,M1).
84
+ membera4([],_L):-fail.
85
+ membera4(L,H):-head(L,H).
86
+ membera4(L,M):-head(L,H),tail(L,T),not(M=H),membera4(T,M).
87
+
88
+ substring([],[]).
89
+ substring([],B):-not((=(B,[]))),fail.
90
+ substring(A,B):-tail(A,At),(listhead(A,B)->(true);(substring(At,B))).
91
+ listhead(_L,[]).
92
+ listhead(A,B):-head(A,Ah),tail(A,At),head(B,Ah),tail(B,Bt),listhead(At,Bt).
93
+
94
+
95
+ %%%%%%%%%%%%
96
+
97
+ %%findbest(R,R) :-!.
98
+ findbest2(R,Item):-
99
+ sort(R,RA),
100
+ reverse(RA,RB),
101
+ RB=[[_,Item]|_Rest].
102
+
103
+
104
+ %%trialy2A([],R) :-
105
+ %% R=[[_,'A']].
106
+ %%trialy2A(List,R) :-
107
+ %% random_member(A,List),
108
+ %% R=[[_,A]].
109
+
110
+ trialy2A([],R) :-
111
+ R=[].
112
+ trialy2A(List,R) :-
113
+ random_member(R,List).
114
+ %%R=[[_,A]].
115
+
116
+ %%trialy2A(List,R) :-
117
+ %% notrace,trialy2B(List,R).%%,trace.
118
+ trialy2B(List,R) :-
119
+ length(List,Length),
120
+ ((Length=<9->
121
+ findr4(R4),
122
+ number_string(R4,R4A),
123
+ formr5([R4A],9,Length,R5),
124
+ findr(R5,List,R));
125
+ (Length=<99->
126
+ findr4(R41),
127
+ findr4(R42),
128
+ formr5([R41,R42],99,Length,R5),
129
+ findr(R5,List,R));
130
+ (Length=<999->
131
+ findr4(R41),
132
+ findr4(R42),
133
+ findr4(R43),
134
+ formr5([R41,R42,R43],999,Length,R5),
135
+ findr(R5,List,R));
136
+ (Length=<9999->
137
+ findr4(R41),
138
+ findr4(R42),
139
+ findr4(R43),
140
+ findr4(R44),
141
+ formr5([R41,R42,R43,R44],9999,Length,R5),
142
+ findr(R5,List,R));
143
+ (Length=<99999->
144
+ findr4(R41),
145
+ findr4(R42),
146
+ findr4(R43),
147
+ findr4(R44),
148
+ findr4(R45),
149
+ formr5([R41,R42,R43,R44,R45],99999,Length,R5),
150
+ findr(R5,List,R));
151
+ fail),
152
+ %%write1([r,R]),trace.
153
+ true.
154
+
155
+ findr4(R4) :-
156
+ List1=[0,1,2,3,4,5,6,7,8,9],
157
+ Trials is 30,
158
+ trialy22(List1,Trials,[],R1),
159
+ findbest2(R1,R4).
160
+ %%number_string(R3,R2),
161
+ formr5(RList,Upper,Length,R5) :-
162
+ %%findall(D,(member(C,RList),floor(C,D)),RList2),
163
+ concat_list2A(RList,R5A),
164
+ number_string(R5B,R5A),
165
+ R51 is floor((R5B/Upper)*Length),
166
+ (R5B=Upper->R5 is R51-1;R5=R51).
167
+ findr(R4,List,R) :-
168
+ %%floor(R4,R4A),
169
+ length(A,R4),
170
+ append(A,[R|_],List).
171
+
172
+ %%random_member(A,List),
173
+ %%R=[[_,A]].
174
+
175
+ /**
176
+ length(List,L),
177
+ Trials is L*3,
178
+ trialy22(List,Trials,[],R).**/
179
+
180
+ trialy22([],_,R,R) :- !.
181
+ trialy22(List,Trials,RA,RB) :-
182
+ List=[Item|Items],
183
+ trialy21(Item,Trials,R1),
184
+ append(RA,[R1],RC),
185
+ trialy22(Items,Trials,RC,RB),!.
186
+
187
+ trialy21(Label,Trials,RA) :-
188
+ trialy3(Trials,[],R),
189
+ aggregate_all(count, member(true,R), Count),
190
+ RA=[Count,Label].
191
+
192
+ trialy3(0,R,R) :-!.
193
+ trialy3(Trials1,RA,RB) :-
194
+ trialy1(R1),
195
+ append(RA,[R1],RC),
196
+ Trials2 is Trials1-1,
197
+ trialy3(Trials2,RC,RB),!.
198
+
199
+ %% try other nouns
200
+ trialy1(R1) :-
201
+ %%control11(A1),
202
+ %%repeat,
203
+ trial0(A22), %% Control
204
+ sum(A22,0,S22),
205
+ mean(S22,A1),
206
+ %%repeat,
207
+ trial0(A21), %% Test 1
208
+ sum(A21,0,S02),
209
+ mean(S02,A2),
210
+ (A1>A2->R1=true;R1=fail).
211
+
212
+ trial0(S3) :- N is 10, trial1(N,[],S),trial01(S,S3).
213
+ trial01(S1,S3) :-
214
+ sort(S1,S),
215
+ %%midpoint(S,MP),
216
+ halves(S,H1,H2),
217
+ midpoint(H1,Q1),
218
+ midpoint(H2,Q3),
219
+ IQR is Q3-Q1,
220
+ sum(S,0,S02),
221
+ mean(S02,Mean),
222
+ furthestfrommean(S,Mean,V),
223
+ D1 is 1.5*IQR,
224
+ D2 is V-Mean,
225
+ (D2>D1->(delete(S,V,S2),trial01(S2,S3));S=S3).
226
+
227
+ %%trial1(0,[],_A) :- fail,!.
228
+ trial1(0,A,A) :- !.
229
+ trial1(N,A,B) :- mindreadtest(S), append(A,[S],A2),
230
+ N1 is N-1,trial1(N1,A2,B).
231
+
232
+ %%midpoint([],0) :- !.
233
+
234
+ midpoint(S,MP) :-
235
+ %% not(S=[]),
236
+ length(S,L),
237
+ A is mod(L,2),
238
+ (A is 0->
239
+ (M1 is L/2, M2 is M1+1,N1 is M1-1,N2 is M2-1,length(N11,N1),length(N21,N2),append(N11,[N12|_Rest1],S),append(N21,[N22|_Rest2],S),MP is (N12+N22)/2)
240
+ ;
241
+ (L2 is L+1, M1 is L2/2, N1 is M1-1,length(N11,N1),append(N11,[MP|_Rest],S))).
242
+
243
+ halves(S,H1,H2) :-
244
+ length(S,L),
245
+ A is mod(L,2),
246
+ (A is 0->
247
+ (M1 is L/2,length(H1,M1),append(H1,H2,S))
248
+ ;
249
+ (L2 is L-1,M1 is L2/2,length(H1,M1),append(H1,[_|H2],S))).
250
+
251
+ sum([],S,S):-!.
252
+ sum(S0,S1,S2) :-
253
+ S0=[S3|S4],
254
+ S5 is S1+S3,
255
+ sum(S4,S5,S2).
256
+
257
+ mean(Sum,Mean) :-
258
+ Mean is Sum/2.
259
+
260
+ furthestfrommean(S,Mean,V) :-
261
+ absdiffmean(S,Mean,[],D),
262
+ sort(D,D1),
263
+ reverse(D1,[[_,V]|_Rest]).
264
+
265
+ absdiffmean([],_M,D,D) :- !.
266
+ absdiffmean(S,M,D1,D2) :-
267
+ S=[S1|S2],
268
+ S3 is abs(S1-M),
269
+ append(D1,[[S3,S1]],D3),
270
+ absdiffmean(S2,M,D3,D2).
271
+
272
+ mindreadtest(Sec) :-
273
+ %% 250 br for characters to be br out with 10 br each from person to me - do when initial 250 br test done and doing 10 br test
274
+ %%comment(fiftyastest),
275
+ %%random(X),X1 is 10*X, X2 is floor(X1), (X2=<2 -> (
276
+ %%texttobr,write1(['true test']), %%); %% use breasonings breasoned out by computer for not by me, for job medicine for "me", at last time point
277
+ %%true), %% leave last time point blank
278
+ %%**texttobr2(640);true),%% make an A to detect reaction to gracious giving or blame of in following
279
+ get_time(TimeStamp1),
280
+ %%phrase_from_file(string(_String), 'file.txt'),
281
+ texttobr2(2), %% 100 As for answer (must be br before this on same day)
282
+ %% is gracious giving or blame
283
+ get_time(TimeStamp2),
284
+ %%comment(turnoffas),
285
+ Sec is TimeStamp2 - TimeStamp1.
286
+
287
+ concat_list2A(A1,B):-
288
+ A1=[A|List],
289
+ concat_list2A(A,List,B),!.
290
+
291
+ concat_list2A(A,[],A):-!.
292
+ concat_list2A(A,List,B) :-
293
+ List=[Item|Items],
294
+ string_concat(A,Item,C),
295
+ concat_list2A(C,Items,B).
Algorithm-Writer-with-Lists/brdict3.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ [["a","a"],["person","subject"],["pear","pear"],["banana","banana"],["apple","apple"],["right","check"],["city","origin"],["paper","command"],["up","check"],["and","interpreter"],["box","minimise"],["twobox","compiler"],["variable","satisfy"],["software","generator"],["leash","all"],["square","end"],["xcorrection","xcorrection"],["one","true"],["dash","twodimensional"],["tick","secure"],["book","business"],["plus","useful"],["equals","compute"],["ball","sphere"],["ship","ship"],["down","current"],["zero","empty"],["brain","calculate"],["log","log"],["n","value"],["testtube","test"],["building","building"],["minus","false"],["dollarsymbol","money"],["happy","happy"],["lowera","word"],["filename","filename"],["gl","gl"],["txt","txt"],["notrace","notrace"],["mtrue","mtrue"],["m","m"],["grammar","grammar"],["logic","logic"],["to","to"],["alg","alg"],["sentences","sentences"],["medicine","medicine"],["by","by"],["lucian","lucian"],["note","music"],["pencilsharpener","object"],["hand","hand"]]
Algorithm-Writer-with-Lists/combophil.pl ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %% combophil.pl
2
+
3
+ %% Finds combinations of lines of philosophy
4
+
5
+ %% *** Deprecated by Repository Lucian-Academy/combophil_alg_log.pl
6
+
7
+ :-include('../listprologinterpreter/la_strings').
8
+
9
+ string(String) --> list(String).
10
+
11
+ list([]) --> [].
12
+ list([L|Ls]) --> [L], list(Ls).
13
+
14
+ %% e.g. combophil(2). to write on a combination of philosophies
15
+
16
+ combophil(NumberOfPhils) :-
17
+ phrase_from_file_s(string(Phil1), "../Text-to-Breasonings/file.txt"),
18
+ SepandPad="\n\r",
19
+ split_string(Phil1,SepandPad,SepandPad,Phil2),
20
+ delete(Phil2,"",Phil3),
21
+ sort(Phil3,Phil4),
22
+ length(Phil4,LengthPhil4),write("Number of philosophies in file.txt: "),
23
+ writeln(LengthPhil4),
24
+ length(PhilsLengthList,NumberOfPhils),
25
+ repeat,
26
+ findall(Phils1,(member(_,PhilsLengthList),random_member(Phils1,Phil4)),Phils2),
27
+ reverse(Phils2,Phils3),Phils3=[Phil6|Phils4],reverse(Phils4,Phils5),
28
+ findall(_,(member(Phil5,Phils5),writeln1(Phil5),nl,writeln1("because"),nl),_),
29
+ writeln1(Phil6),nl,
30
+ write("1-Continue or 2-End: "),
31
+ read_string(user_input, "\n", "\r", _End, Input),
32
+ (Input="2"->abort;fail).
33
+
Algorithm-Writer-with-Lists/combophil2qadb.pl ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %% combophil.pl x combophil2qadb.pl
2
+
3
+ % don;t change to mrtree because mr is working. Without seeing and thinking about options, it is meaningless.
4
+
5
+ %% Finds combinations of lines of philosophy
6
+
7
+ %% *** Deprecated by Repository Lucian-Academy/combophil_alg_log.pl
8
+
9
+ :-include('../listprologinterpreter/la_strings').
10
+
11
+ string(String) --> list(String).
12
+
13
+ list([]) --> [].
14
+ list([L|Ls]) --> [L], list(Ls).
15
+
16
+ %% e.g. combophil(2). to write on a combination of philosophies
17
+
18
+ combophil2qadb :- %(NumberOfPhils) :-
19
+
20
+ % get box, right, box words from brdict1
21
+ phrase_from_file_s(string(BrDict0), "../Text-to-Breasonings/brdict1.txt"),
22
+ %phrase_from_file_s(string(BrDict0), "../../brdict1.txt"),
23
+ %string_codes(String02b,BrDict0),
24
+ %atom_to_term(String02b,String02a,[]),
25
+ splitfurther(BrDict0,BrDict01),
26
+ sort(BrDict01,String02a),
27
+ findall(A2,(member(A,String02a),A=[A2,"box"]),A1),
28
+ findall(B2,(member(B,String02a),B=[B2,"right"]),B1),
29
+
30
+
31
+ phrase_from_file_s(string(Phil1), "../Text-to-Breasonings/file.txt"),
32
+ SepandPad="\n\r.",
33
+ split_string(Phil1,SepandPad,SepandPad,Phil2),
34
+ delete(Phil2,"",Phil3),
35
+ sort(Phil3,Phil4),
36
+ length(Phil4,LengthPhil4),write("Number of philosophies in file.txt: "),
37
+ writeln(LengthPhil4),
38
+ %length(PhilsLengthList,NumberOfPhils),
39
+
40
+ findall([C,Algorithm],(member(C,Phil4),
41
+ qa_db_finder(A1,B1,Algorithm)),D),
42
+
43
+ term_to_atom(D,K),
44
+ (open_s("file-o.txt",write,Stream1),
45
+ write(Stream1,K),
46
+ close(Stream1)),!.
47
+ /**
48
+ repeat,
49
+ findall(Phils1,(member(_,PhilsLengthList),random_member(Phils1,Phil4)),Phils2),
50
+ reverse(Phils2,Phils3),Phils3=[Phil6|Phils4],reverse(Phils4,Phils5),
51
+ findall(_,(member(Phil5,Phils5),writeln1(Phil5),nl,writeln1("because"),nl),_),
52
+ writeln1(Phil6),nl,
53
+ write("1-Continue or 2-End: "),
54
+ read_string(user_input, "\n", "\r", _End, Input),
55
+ (Input="2"->abort;fail).
56
+ **/
57
+
58
+
59
+ splitfurther(BrDict01,N) :-
60
+ phrase(file0(N),BrDict01).
61
+
62
+ file0(N) --> "[", file(N), "]", !.
63
+ file0([]) --> [].
64
+
65
+ file([L|Ls]) --> entry(L),",",
66
+ %%{writeln(L)}, %%***
67
+ file(Ls), !.
68
+ file([L]) --> entry(L),
69
+ %%{writeln(L)},
70
+ !.
71
+
72
+ entry([Word2,Word4]) -->
73
+ "[", word(Word), {string_codes(Word2,Word),string(Word2)},
74
+ ",",
75
+ word(Word3), {string_codes(Word4,Word3),string(Word4)},
76
+ "]".
77
+
78
+ word([X|Xs]) --> [X], {char_type(X,csymf)->true;(X=27->true;X=8217)}, word(Xs), !.
79
+ %%word([X]) --> [X], {char_type(X,csymf);(X=27;X=8217)}, !.
80
+ word([]) --> [].
81
+
82
+ qa_db_finder(A1,B1,Algorithm) :-
83
+ % name of algorithm
84
+ random_member(Verb,B1),
85
+
86
+ %writeln("What is the input variable?"),
87
+ random_member(I1,A1),
88
+ upper_first_letter(I1,I),
89
+ %read_string(user_input, "\n", "\r", _End, I),
90
+
91
+ %writeln("What variable is this last variable linked to?"),
92
+ random_member(N1,A1),
93
+ upper_first_letter(N1,N),
94
+ %read_string(user_input, "\n", "\r", _End2, N),
95
+
96
+ concat_list(["member(A1,",I,N,"),A1=[",I,",",N,"],"],Algorithm1),
97
+ Vars1=[I,N],
98
+
99
+ repeat1(A1,2,N,Algorithm1,Algorithm2,Vars1,Vars2),
100
+
101
+ string_concat(Algorithm3,",",Algorithm2),
102
+
103
+ %writeln("What is the final output variable?"),
104
+ random_member(O1,A1),
105
+ upper_first_letter(O1,O2),
106
+ append(Vars2,[O2],O3),
107
+ random_member(O,O3),
108
+ %read_string(user_input, "\n", "\r", _End3, O),
109
+ %%trace,
110
+
111
+ find_header_args1(Vars2,"",HA1),
112
+ %%string_concat(HA2,",",HA1),
113
+
114
+ concat_list(["a(",HA1,I,",",O,"):-"],Algorithm4),
115
+ concat_list([Algorithm4,Algorithm3,"."],Algorithm).
116
+
117
+ upper_first_letter(A,B) :-
118
+ string_concat(C,F,A),
119
+ string_length(C,1),
120
+ to_upper(C,D),
121
+ string_codes(E,[D]),
122
+ string_concat(E,F,B),!.
123
+
124
+ repeat1(A1,M1,N,Algorithm1,Algorithm2,Vars1,Vars2) :-
125
+ %writeln("Is this the final variable? (y/n)"),
126
+ random_member(Q,["y","n"]),
127
+ %read_string(user_input, "\n", "\r", _End, Q),
128
+
129
+ (Q="y"->(Algorithm2=Algorithm1,Vars2=Vars1);
130
+ %writeln("What variable is this last variable linked to?"),
131
+ %(random(X_A),X_B is ceiling(2*X_A),
132
+ %(X_B=1->
133
+ random_member(V1,A1),
134
+ upper_first_letter(V1,V2),
135
+ append(Vars1,[V2],V3),
136
+ random_member(V,V3),
137
+ %read_string(user_input, "\n", "\r", _End2, V),
138
+
139
+ concat_list(["member(A",M1,",",N,V,"),A",M1,"=[",N,",",V,"],"],Algorithm1a),
140
+ append(Vars1,[V],Vars3),
141
+ M2 is M1+1,
142
+ string_concat(Algorithm1,Algorithm1a,Algorithm1b),
143
+ repeat1(A1,M2,V,Algorithm1b,Algorithm2,Vars3,Vars2)
144
+
145
+ ).
146
+
147
+
148
+ find_header_args1([_],HA,HA) :- !.
149
+ find_header_args1(Vars,HA1,HA2) :-
150
+ Vars=[_|F],
151
+ Vars=[A,B|_],
152
+ concat_list([A,B,","],D),
153
+ string_concat(HA1,D,E),
154
+ find_header_args1(F,E,HA2).
155
+
156
+
157
+ concat_list(A1,B):-
158
+ A1=[A|List],
159
+ concat_list(A,List,B),!.
160
+
161
+ concat_list(A,[],A):-!.
162
+ concat_list(A,List,B) :-
163
+ List=[Item|Items],
164
+ string_concat(A,Item,C),
165
+ concat_list(C,Items,B).
Algorithm-Writer-with-Lists/combophil_grammar_logic_to_alg.pl ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+
3
+ grammar_logic_to_alg.pl
4
+
5
+
6
+ e.g. I liked God with you
7
+ [liked,God],[God,you] with extra data
8
+ - connect liked to you with member, return words on path
9
+
10
+ POS
11
+
12
+ goes backwards x forwards
13
+ - I v n by v n x ignore
14
+
15
+ n,v
16
+ - single type
17
+
18
+ adjective
19
+ - ignored x (ignore names in alg)
20
+ - like v,n
21
+
22
+ joining words - ignore, just pairs v, n
23
+ - on
24
+ - joins v to n
25
+ - separates vns
26
+
27
+ negative terms
28
+ - switch to positive or ignore
29
+
30
+ disappearing words
31
+ - my
32
+ - no list of these, because they are ignored
33
+
34
+ vv is v1, v2 x v1
35
+ nn is n1
36
+
37
+ *
38
+
39
+ later: can randomly generate details like given sentence
40
+
41
+ **/
42
+
43
+ :-include('../Text-to-Breasonings/text_to_breasonings.pl').
44
+
45
+ :- dynamic brdict/1.
46
+
47
+ combophil_grammar_logic_to_alg1 :-
48
+ phrase_from_file_s(string(Text1), "../Text-to-Breasonings/file.txt"),
49
+
50
+ phrase_from_file_s(string(BrDict0), "../Text-to-Breasonings/brdict1.txt"),
51
+ splitfurther(BrDict0,BrDict01),
52
+ sort(BrDict01,BrDict012),
53
+ retractall(brdict(_)),
54
+ assertz(brdict(BrDict012)),
55
+
56
+ %%SepandPad=".\n",
57
+ %%split_string(Text1,SepandPad,SepandPad,Text2a),
58
+ %%delete(Text2a,"",Text2),
59
+
60
+
61
+ %%findall(B2,(member(B1,Text2),
62
+ string_codes(Text11,Text1),
63
+ downcase_atom(%%B1
64
+ Text11,B11),atom_string(B11,B12),
65
+ SepandPad1=" .\n",
66
+ split_string(B12,SepandPad1,SepandPad1,A),
67
+ %%trace,
68
+ findall(D,(member(C3,A),
69
+ member([C3,"right"],BrDict012),member(C1,A),
70
+ not(member([C1,"right"],BrDict012)),
71
+ concat_list([C3," ",C1],C2),grammar_logic_to_alg(C2,D)),C),
72
+ %%writeln(C),trace,
73
+
74
+ %%grammar_logic_to_alg(D,B2)),C),
75
+ length(C,CLength),
76
+ writeln([CLength,sentences]),
77
+
78
+ C=[[[_,Sentence1,a_alg(List_a),_,bb_alg(List_bb)]|Cs1]|Cs2],
79
+ %%get_time(TS),stamp_date_time(TS,date(Year,Month,Day,Hour1,Minute1,Seconda,_A,_TZ,_False),local),
80
+ concat_list(["gla_del"],%%[Year,Month,Day,Hour1,Minute1,Seconda],
81
+ File1),
82
+ concat_list(["\"",File1,".txt\""],File2),
83
+
84
+ term_to_atom(List_a,List_a2),
85
+ string_atom(List_a3,List_a2),
86
+
87
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_a1),
88
+ writeln1([*,Sentence1,a_alg(List_a)]),
89
+ writeln(List_a1),
90
+
91
+ concat_list(["\n\nswipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,",[v,b]]],[[[n,function],[[v,a],[v,b]],\":-\",[[[n,tail],[[v,a],[v,b]]]]]],_),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_b1),
92
+ writeln1([*,Sentence1,b_alg(List_a)]),
93
+ writeln(List_b1),
94
+
95
+ term_to_atom(List_bb,List_bb2),
96
+ string_atom(List_bb3,List_bb2),
97
+
98
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_bb3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_bb1),
99
+ writeln1([*,Sentence1,bb_alg(List_bb)]),
100
+ writeln(List_bb1),
101
+
102
+ writeln1(Cs1),
103
+ writeln1(Cs2).
104
+
105
+ grammar_logic_to_alg(Sentence1,B) :- %% Not by multi-sentence algorithms, just by sentence
106
+ atom_string(Sentence0,Sentence1),
107
+ downcase_atom(Sentence0,Sentence01),
108
+ atom_string(Sentence01,Sentence02),
109
+ SepandPad="&#@~%`$?-+*^,()|.:;=_/[]<>{}\n\r\s\t\\\"!'0123456789",
110
+ Connectors=
111
+ ["the","a","i","on","with","of","an","for","to",
112
+ "was","were","and","in","my","from","out","by"],
113
+ split_string(Sentence02,SepandPad,SepandPad,Sentence2),
114
+ subtract(Sentence2,Connectors,Sentence3),
115
+ %%length(Sentence3,Length),
116
+
117
+ %% () add generated data
118
+ %%write_commands(Length,[],Commands), %% sentence alg
119
+
120
+ generate_sentences(Sentence3,[],Sentence_a,30), %% detail sentences
121
+ append(Sentence3,Sentence_a,Sentence4),
122
+ findall([*,Sentence1,a_alg(Sentence5),b_alg(Sentence5,a),bb_alg(Sentence6)],(member(Sentence4a,Sentence4),make_lists(Sentence4a,[],Sentence5),Sentence5=[_|Sentence6]),B),!.
123
+ %% detail algs
124
+
125
+ generate_sentences(_Sentence3,Sentence_a,Sentence_a,0) :- !.
126
+ generate_sentences(Sentence3,Sentence_a1,Sentence_a2,N1) :-
127
+ random_member(Item,Sentence3),
128
+ generate_sentence(Item,Sentence_a3),
129
+ append(Sentence_a1,[Sentence_a3],Sentence_a4),
130
+ N2 is N1-1,
131
+ generate_sentences(Sentence3,Sentence_a4,Sentence_a2,N2).
132
+
133
+ %%write_commands(0,Commands,Commands) :- !.
134
+ %%write_commands(Length1,Commands1,Commands2) :-
135
+ %% Length2 is Length1-1.
136
+ %% append(Commands1,[[[n,member2],[[v,Length1],[v,Length2]]]).
137
+ %%[[n,equals1],[[v,Length2],[[v,a***],[v,b]]]]].
138
+
139
+ %%[a,b]
140
+ %%[c,d]
141
+
142
+ generate_sentence(Item,Sentence) :-
143
+ random_member(Grammar1,[[n,v,n],[n,v,a,n],[v,n],[v,a,n]]),
144
+ brdict(BrDict012),
145
+ find_pos(Item,POS,BrDict012),
146
+ substitute1(Item,POS,Grammar1,[],Grammar2),
147
+ substitute2(Grammar2,BrDict012,[],Sentence).
148
+
149
+ find_pos(Item,POS2,BrDict012) :-
150
+ member([Item,POS1],BrDict012),
151
+ POS1="right",
152
+ POS2=v,!.
153
+ find_pos(Item,POS2,BrDict012) :-
154
+ member([Item,POS1],BrDict012),
155
+ POS1="plus",
156
+ POS2=a,!.
157
+ find_pos(_Item,POS2,_BrDict012) :-
158
+ POS2=n.
159
+
160
+ substitute1(_Item,_POS,[],Grammar,Grammar) :- !.
161
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
162
+ Grammar1=[Grammar4|Grammar5],
163
+ Grammar4=POS,
164
+ append_list([Grammar2,Item],Grammar6),
165
+ append(Grammar6,Grammar5,Grammar3),!.
166
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
167
+ Grammar1=[Grammar4|Grammar5],
168
+ not(Grammar4=POS),
169
+ append_list([Grammar2,Grammar4],Grammar6),
170
+ substitute1(Item,POS,Grammar5,Grammar6,Grammar3),!.
171
+
172
+ substitute2([],_BrDict012,Sentence,Sentence) :- !.
173
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
174
+ Grammar1=[Grammar2|Grammar3],
175
+ Grammar2=n,
176
+ findall(A,(member([A,"box"],BrDict012)),B),
177
+ random_member(Word,B),
178
+ append(Sentence1,[Word],Sentence3),
179
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
180
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
181
+ Grammar1=[Grammar2|Grammar3],
182
+ Grammar2=v,
183
+ findall(A,(member([A,"right"],BrDict012)),B),
184
+ random_member(Word,B),
185
+ append(Sentence1,[Word],Sentence3),
186
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
187
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
188
+ Grammar1=[Grammar2|Grammar3],
189
+ Grammar2=a,
190
+ findall(A,(member([A,"plus"],BrDict012)),B),
191
+ random_member(Word,B),
192
+ append(Sentence1,[Word],Sentence3),
193
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
194
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
195
+ Grammar1=[Grammar2|Grammar3],
196
+ append(Sentence1,[Grammar2],Sentence3),
197
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
198
+
199
+ make_lists(Sentence1,Sentence,Sentence) :-
200
+ Sentence1=[_Sentence2],
201
+ !.
202
+ make_lists(Sentence1,Sentence2,Sentence3) :-
203
+ Sentence1=[Sentence4|Sentence5],
204
+ Sentence5=[Sentence6|_Sentence7],
205
+ append(Sentence2,[[Sentence4,Sentence6]],Sentence8),
206
+ make_lists(Sentence5,Sentence8,Sentence3).
207
+
208
+
Algorithm-Writer-with-Lists/combophil_grammar_logic_to_alg_vps.pl ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+
3
+ grammar_logic_to_alg.pl
4
+
5
+
6
+ e.g. I liked God with you
7
+ [liked,God],[God,you] with extra data
8
+ - connect liked to you with member, return words on path
9
+
10
+ POS
11
+
12
+ goes backwards x forwards
13
+ - I v n by v n x ignore
14
+
15
+ n,v
16
+ - single type
17
+
18
+ adjective
19
+ - ignored x (ignore names in alg)
20
+ - like v,n
21
+
22
+ joining words - ignore, just pairs v, n
23
+ - on
24
+ - joins v to n
25
+ - separates vns
26
+
27
+ negative terms
28
+ - switch to positive or ignore
29
+
30
+ disappearing words
31
+ - my
32
+ - no list of these, because they are ignored
33
+
34
+ vv is v1, v2 x v1
35
+ nn is n1
36
+
37
+ *
38
+
39
+ later: can randomly generate details like given sentence
40
+
41
+ **/
42
+
43
+ :-include('../Text-to-Breasonings/text_to_breasonings.pl').
44
+
45
+ :- dynamic brdict/1.
46
+
47
+ truncate(List1,M,String0) :-
48
+ ((number(M),length(String0,M),
49
+ append(String0,_,List1))->true;
50
+ String0=List1),!.
51
+
52
+ combophil_grammar_logic_to_alg1(P,String000) :-%%(String000) :-
53
+ phrase_from_file_s(string(Text1), "../Lucian-Academy/luciansphilosophy.txt"),
54
+ combophil_grammar_logic_to_alg2(String000,Text1).
55
+
56
+ combophil_grammar_logic_to_alg1(File) :-%%(String000) :-
57
+ phrase_from_file_s(string(Text1), "../Text-to-Breasonings/file.txt"),
58
+ string_codes(Text11,Text1),
59
+ downcase_atom(%%B1
60
+ Text11,B11),atom_string(B11,B12),
61
+ SepandPad1=".\n",
62
+ split_string(B12,SepandPad1,SepandPad1,A0),
63
+ findall(_,(member(A00,A0),
64
+ combophil_grammar_logic_to_alg2(String000,A00),
65
+ %%String000="a b c",
66
+ time((N = u, M = u,texttobr2(N,u,String000,M),texttobr(N,u,String000,M))),
67
+ %% Give the meditators, etc. the As.
68
+ texttobr2(3)),_).
69
+
70
+ combophil_grammar_logic_to_alg2(String000,Text1) :-
71
+ phrase_from_file_s(string(BrDict0), "../Text-to-Breasonings/brdict1.txt"),
72
+ splitfurther(BrDict0,BrDict01),
73
+ sort(BrDict01,BrDict012),
74
+ retractall(brdict(_)),
75
+ assertz(brdict(BrDict012)),
76
+
77
+ %%SepandPad=".\n",
78
+ %%split_string(Text1,SepandPad,SepandPad,Text2a),
79
+ %%delete(Text2a,"",Text2),
80
+
81
+
82
+ %%findall(B2,(member(B1,Text2),
83
+ string_codes(Text11,Text1),
84
+ downcase_atom(%%B1
85
+ Text11,B11),atom_string(B11,B12),
86
+ SepandPad1=" .\n",
87
+ split_string(B12,SepandPad1,SepandPad1,A01),
88
+ append(["go"],A01,A0),
89
+ truncate(A0,20,A),
90
+
91
+ findall(D,(member(C3,A),
92
+ member([C3,"right"],BrDict012),member(C1,A),
93
+ not(member([C1,"right"],BrDict012)),
94
+ concat_list([C3," ",C1],C2),grammar_logic_to_alg(C2,D)),C),
95
+ %%writeln(C),trace,
96
+
97
+ %%grammar_logic_to_alg(D,B2)),C),
98
+ length(C,CLength),
99
+ writeln([CLength,sentences]),
100
+
101
+ C=[[[_,Sentence1,a_alg(List_a),_,bb_alg(List_bb)]|Cs1]|Cs2],
102
+ %%get_time(TS),stamp_date_time(TS,date(Year,Month,Day,Hour1,Minute1,Seconda,_A,_TZ,_False),local),
103
+ concat_list(["gla_del"],%%[Year,Month,Day,Hour1,Minute1,Seconda],
104
+ File1),
105
+ concat_list(["\"",File1,".txt\""],File2),
106
+
107
+ term_to_atom(List_a,List_a2),
108
+ string_atom(List_a3,List_a2),
109
+
110
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_a1),
111
+ writeln1([*,Sentence1,a_alg(List_a)]),
112
+ writeln(List_a1),
113
+
114
+ concat_list(["\n\nswipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,",[v,b]]],[[[n,function],[[v,a],[v,b]],\":-\",[[[n,tail],[[v,a],[v,b]]]]]],_),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_b1),
115
+ writeln1([*,Sentence1,b_alg(List_a)]),
116
+ writeln(List_b1),
117
+
118
+ term_to_atom(List_bb,List_bb2),
119
+ string_atom(List_bb3,List_bb2),
120
+
121
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_bb3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_bb1),
122
+ writeln1([*,Sentence1,bb_alg(List_bb)]),
123
+ writeln(List_bb1),
124
+
125
+ writeln1(Cs1),
126
+ writeln1(Cs2),
127
+
128
+ term_to_atom([[CLength,sentences],[*,Sentence1,a_alg(List_a)],List_a1,[*,Sentence1,b_alg(List_a)],List_b1,[*,Sentence1,bb_alg(List_bb)],List_bb1,Cs1,Cs2],String0001),
129
+ atom_string(String0001,String000)
130
+
131
+ %%a(P,String000):-
132
+
133
+
134
+ %%CLength=a,Sentence1=a,List_a=a,List_a1=a,List_b1=a,List_bb=a,List_bb1=a,Cs1=a,Cs2=a,
135
+
136
+ .
137
+
138
+ grammar_logic_to_alg(Sentence1,B) :- %% Not by multi-sentence algorithms, just by sentence
139
+ atom_string(Sentence0,Sentence1),
140
+ downcase_atom(Sentence0,Sentence01),
141
+ atom_string(Sentence01,Sentence02),
142
+ SepandPad="&#@~%`$?-+*^,()|.:;=_/[]<>{}\n\r\s\t\\\"!'0123456789",
143
+ Connectors=
144
+ ["the","a","i","on","with","of","an","for","to",
145
+ "was","were","and","in","my","from","out","by"],
146
+ split_string(Sentence02,SepandPad,SepandPad,Sentence2),
147
+ subtract(Sentence2,Connectors,Sentence3),
148
+ %%length(Sentence3,Length),
149
+
150
+ %% () add generated data
151
+ %%write_commands(Length,[],Commands), %% sentence alg
152
+
153
+ generate_sentences(Sentence3,[],Sentence_a,30), %% detail sentences
154
+ append(Sentence3,Sentence_a,Sentence4),
155
+ findall([*,Sentence1,a_alg(Sentence5),b_alg(Sentence5,a),bb_alg(Sentence6)],(member(Sentence4a,Sentence4),make_lists(Sentence4a,[],Sentence5),Sentence5=[_|Sentence6]),B),!.
156
+ %% detail algs
157
+
158
+ generate_sentences(_Sentence3,Sentence_a,Sentence_a,0) :- !.
159
+ generate_sentences(Sentence3,Sentence_a1,Sentence_a2,N1) :-
160
+ random_member(Item,Sentence3),
161
+ generate_sentence(Item,Sentence_a3),
162
+ append(Sentence_a1,[Sentence_a3],Sentence_a4),
163
+ N2 is N1-1,
164
+ generate_sentences(Sentence3,Sentence_a4,Sentence_a2,N2).
165
+
166
+ %%write_commands(0,Commands,Commands) :- !.
167
+ %%write_commands(Length1,Commands1,Commands2) :-
168
+ %% Length2 is Length1-1.
169
+ %% append(Commands1,[[[n,member2],[[v,Length1],[v,Length2]]]).
170
+ %%[[n,equals1],[[v,Length2],[[v,a***],[v,b]]]]].
171
+
172
+ %%[a,b]
173
+ %%[c,d]
174
+
175
+ generate_sentence(Item,Sentence) :-
176
+ random_member(Grammar1,[[n,v,n],[n,v,a,n],[v,n],[v,a,n]]),
177
+ brdict(BrDict012),
178
+ find_pos(Item,POS,BrDict012),
179
+ substitute1(Item,POS,Grammar1,[],Grammar2),
180
+ substitute2(Grammar2,BrDict012,[],Sentence).
181
+
182
+ find_pos(Item,POS2,BrDict012) :-
183
+ member([Item,POS1],BrDict012),
184
+ POS1="right",
185
+ POS2=v,!.
186
+ find_pos(Item,POS2,BrDict012) :-
187
+ member([Item,POS1],BrDict012),
188
+ POS1="plus",
189
+ POS2=a,!.
190
+ find_pos(_Item,POS2,_BrDict012) :-
191
+ POS2=n.
192
+
193
+ substitute1(_Item,_POS,[],Grammar,Grammar) :- !.
194
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
195
+ Grammar1=[Grammar4|Grammar5],
196
+ Grammar4=POS,
197
+ append_list([Grammar2,Item],Grammar6),
198
+ append(Grammar6,Grammar5,Grammar3),!.
199
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
200
+ Grammar1=[Grammar4|Grammar5],
201
+ not(Grammar4=POS),
202
+ append_list([Grammar2,Grammar4],Grammar6),
203
+ substitute1(Item,POS,Grammar5,Grammar6,Grammar3),!.
204
+
205
+ substitute2([],_BrDict012,Sentence,Sentence) :- !.
206
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
207
+ Grammar1=[Grammar2|Grammar3],
208
+ Grammar2=n,
209
+ findall(A,(member([A,"box"],BrDict012)),B),
210
+ random_member(Word,B),
211
+ append(Sentence1,[Word],Sentence3),
212
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
213
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
214
+ Grammar1=[Grammar2|Grammar3],
215
+ Grammar2=v,
216
+ findall(A,(member([A,"right"],BrDict012)),B),
217
+ random_member(Word,B),
218
+ append(Sentence1,[Word],Sentence3),
219
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
220
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
221
+ Grammar1=[Grammar2|Grammar3],
222
+ Grammar2=a,
223
+ findall(A,(member([A,"plus"],BrDict012)),B),
224
+ random_member(Word,B),
225
+ append(Sentence1,[Word],Sentence3),
226
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
227
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
228
+ Grammar1=[Grammar2|Grammar3],
229
+ append(Sentence1,[Grammar2],Sentence3),
230
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
231
+
232
+ make_lists(Sentence1,Sentence,Sentence) :-
233
+ Sentence1=[_Sentence2],
234
+ !.
235
+ make_lists(Sentence1,Sentence2,Sentence3) :-
236
+ Sentence1=[Sentence4|Sentence5],
237
+ Sentence5=[Sentence6|_Sentence7],
238
+ append(Sentence2,[[Sentence4,Sentence6]],Sentence8),
239
+ make_lists(Sentence5,Sentence8,Sentence3).
240
+
241
+
Algorithm-Writer-with-Lists/four_crosses.pl ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ four_crosses :-
2
+ prompt("What are the two items to cross?",_Two_items),
3
+ cross,cross,cross,
4
+ prompt("What is their cross?",_Cross11).
5
+
6
+ cross :-
7
+ prompt("What is their cross?",_Cross11),
8
+ prompt("What is at the same level as this?",_Cross12).
9
+
10
+ prompt(Prompt,String) :-
11
+ writeln(Prompt),
12
+ read_string(user_input, "\n", "\r", _End, String).
Algorithm-Writer-with-Lists/gl-api.pl ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :- use_module(library(http/thread_httpd)).
2
+ :- use_module(library(http/http_dispatch)).
3
+ :- use_module(library(http/http_error)).
4
+ :- use_module(library(http/html_write)).
5
+
6
+ % we need this module from the HTTP client library for http_read_data
7
+ :- use_module(library(http/http_client)).
8
+ :- http_handler('/', web_form, []).
9
+
10
+ % GitHub
11
+ :- include('grammar_logic_to_alg.pl').
12
+
13
+ server(Port) :-
14
+ http_server(http_dispatch, [port(Port)]).
15
+
16
+ /*
17
+ browse http://127.0.0.1:8000/
18
+ This demonstrates handling POST requests
19
+ */
20
+
21
+ web_form(_Request) :-
22
+ reply_html_page(
23
+ title('Grammar Logic'),
24
+ [
25
+ form([action='/landing', method='POST'], [
26
+ /**
27
+ p([], [
28
+ label([for=debug],'Debug (on/off):'),
29
+ input([name=debug, type=textarea])
30
+ ]),
31
+ **/
32
+ p([], [
33
+ label([for=query],'Grammar Logic Input:'),
34
+ input([name=query, type=textarea])
35
+ ]),
36
+
37
+ p([], input([name=submit, type=submit, value='Submit'], []))
38
+ ])]).
39
+
40
+ :- http_handler('/landing', landing_pad, []).
41
+
42
+ landing_pad(Request) :-
43
+
44
+ % working_directory(_, 'GitHub/Algorithm-Writer-with-Lists/'),
45
+ member(method(post), Request), !,
46
+ http_read_data(Request, Data, []),
47
+ format('Content-type: text/html~n~n', []),
48
+ format('<p>', []),
49
+ %%portray_clause(Data),
50
+
51
+ %%term_to_atom(Term,Data),
52
+
53
+ Data=[%%debug='off',%%Debug1,
54
+ query=Query1,submit=_],
55
+
56
+
57
+ grammar_logic_to_alg1(Query1,4000,Result),
58
+ %%format('</p><p>========~n', []),
59
+ %%portray_clause
60
+ portray_clause(Result),
61
+ %%writeln1(Data),
62
+
63
+ format('</p>').
Algorithm-Writer-with-Lists/grammar_logic_to_alg.pl ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+
3
+ grammar_logic_to_alg.pl
4
+
5
+
6
+ e.g. I liked God with you
7
+ [liked,God],[God,you] with extra data
8
+ - connect liked to you with member, return words on path
9
+
10
+ POS
11
+
12
+ goes backwards x forwards
13
+ - I v n by v n x ignore
14
+
15
+ n,v
16
+ - single type
17
+
18
+ adjective
19
+ - ignored x (ignore names in alg)
20
+ - like v,n
21
+
22
+ joining words - ignore, just pairs v, n
23
+ - on
24
+ - joins v to n
25
+ - separates vns
26
+
27
+ negative terms
28
+ - switch to positive or ignore
29
+
30
+ disappearing words
31
+ - my
32
+ - no list of these, because they are ignored
33
+
34
+ vv is v1, v2 x v1
35
+ nn is n1
36
+
37
+ *
38
+
39
+ later: can randomly generate details like given sentence
40
+
41
+ **/
42
+
43
+ :-include('../Text-to-Breasonings/text_to_breasonings.pl').
44
+ :-include('../Text-to-Breasonings/truncate.pl').
45
+ %:- include('../Philosophy/14 10 23.pl').
46
+
47
+ :- dynamic brdict/1.
48
+
49
+
50
+
51
+ mt_t2b2:-
52
+ %Thread=
53
+ %[
54
+ phrase_from_file_s(string(Essay_0), %"lacom5million.txt"
55
+ "../../private/lacom0.5million.txt"
56
+ ),
57
+
58
+ %Br is 5,
59
+ %Br is 5000000,
60
+ W is 0,
61
+
62
+ %grammar_logic_to_alg1(Essay_0,Br,GL_out1),
63
+ grammar_logic_to_alg113(Essay_0,GL_out1),
64
+
65
+ term_to_atom(GL_out1,GL_out2),
66
+ string_atom(GL_out,GL_out2),
67
+
68
+ texttobr2(u,u,GL_out,Br,false,false,false,false,false,false,W),
69
+ texttobr(u,u,GL_out,Br).%],
70
+
71
+ mt_t2b3:-mt_t2b2.
72
+
73
+ mt_t2b4:-mt_t2b2.
74
+
75
+ mt_t2b5:-mt_t2b2.
76
+
77
+ mt_t2b6:-mt_t2b2.
78
+
79
+ mt_t2b7:-mt_t2b2.
80
+
81
+ mt_t2b8:-mt_t2b2.
82
+
83
+ mt_t2b9:-mt_t2b2.
84
+ %trace,Thread.
85
+
86
+
87
+ mt_t2b :-
88
+
89
+ Goals=[mt_t2b2,mt_t2b3,mt_t2b4,mt_t2b5,mt_t2b6,mt_t2b7,mt_t2b8,mt_t2b9
90
+ ],
91
+
92
+ length(Goals,L),
93
+
94
+ %time(mt_t2b2).
95
+ time(concurrent(L,Goals,[])).
96
+
97
+ grammar_logic_to_alg1(String1,N,Result) :-
98
+
99
+ %term_to_atom(String,Essay_01),
100
+ %string_atom(Essay_02,Essay_01),
101
+
102
+ %working_directory(_, '../'),
103
+
104
+ %(open_s("../Text-to-Breasonings/file.txt",write,Stream1),
105
+ %write(Stream1,String),
106
+ %close(Stream1)),!,
107
+
108
+ truncate1(string,String1,N,String),
109
+
110
+ %working_directory(_, 'algwriter/'),
111
+ grammar_logic_to_alg114(String,Result).
112
+
113
+ grammar_logic_to_alg1 :-
114
+ grammar_logic_to_alg11([Sentence1,List_a,List_a1,List_b1,List_bb,List_bb1,Cs1,Cs2]),
115
+ writeln1([*,Sentence1,a_alg(List_a)]),
116
+ writeln(List_a1),
117
+ writeln1([*,Sentence1,b_alg(List_a)]),
118
+ writeln(List_b1),
119
+ writeln1([*,Sentence1,bb_alg(List_bb)]),
120
+ writeln(List_bb1),
121
+
122
+ writeln1(Cs1),
123
+ writeln1(Cs2).
124
+
125
+ grammar_logic_to_alg112(Result) :-
126
+ grammar_logic_to_alg11(Result1),term_to_atom(Result1,Result).
127
+
128
+ grammar_logic_to_alg114(Text,Result) :-
129
+ grammar_logic_to_alg113(Text,Result1),term_to_atom(Result1,Result).
130
+
131
+
132
+ grammar_logic_to_alg11([Sentence1,List_a,List_a1,List_b1,List_bb,List_bb1,Cs1,Cs2]) :-
133
+ phrase_from_file_s(string(Text1), "../Text-to-Breasonings/file.txt"),
134
+
135
+ grammar_logic_to_alg113(Text1,[Sentence1,List_a,List_a1,List_b1,List_bb,List_bb1,Cs1,Cs2]).
136
+
137
+
138
+ grammar_logic_to_alg113(Text1,[Sentence1,List_a,List_a1,List_b1,List_bb,List_bb1,Cs1,Cs2]) :-
139
+
140
+ phrase_from_file_s(string(BrDict0), "../Text-to-Breasonings/brdict1.txt"),
141
+ splitfurther(BrDict0,BrDict01),
142
+ sort(BrDict01,BrDict012),
143
+ retractall(brdict(_)),
144
+ assertz(brdict(BrDict012)),
145
+
146
+ SepandPad=".\n",
147
+ split_string(Text1,SepandPad,SepandPad,Text2a),
148
+ delete(Text2a,"",Text2),
149
+
150
+ findall(B2,(member(B1,Text2),grammar_logic_to_alg(B1,B2)),C),
151
+ length(C,CLength),
152
+ writeln([CLength,sentences]),
153
+
154
+ C=[[[_,Sentence1,a_alg(List_a),_,bb_alg(List_bb)]|Cs1]|Cs2],
155
+ %%get_time(TS),stamp_date_time(TS,date(Year,Month,Day,Hour1,Minute1,Seconda,_A,_TZ,_False),local),
156
+ concat_list(["gla_del"],%%[Year,Month,Day,Hour1,Minute1,Seconda],
157
+ File1),
158
+ concat_list(["\"",File1,".txt\""],File2),
159
+
160
+ term_to_atom(List_a,List_a2),
161
+ string_atom(List_a3,List_a2),
162
+
163
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_a1),
164
+
165
+ concat_list(["\n\nswipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,",[v,b]]],[[[n,function],[[v,a],[v,b]],\":-\",[[[n,tail],[[v,a],[v,b]]]]]],_),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_b1),
166
+
167
+ term_to_atom(List_bb,List_bb2),
168
+ string_atom(List_bb3,List_bb2),
169
+
170
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_bb3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_bb1).
171
+
172
+ grammar_logic_to_alg(Sentence1,B) :- %% Not by multi-sentence algorithms, just by sentence
173
+ atom_string(Sentence0,Sentence1),
174
+ downcase_atom(Sentence0,Sentence01),
175
+ atom_string(Sentence01,Sentence02),
176
+ SepandPad="&#@~%`$?-+*^,()|.:;=_/[]<>{}\n\r\s\t\\\"!'0123456789",
177
+
178
+ Connectors=
179
+ ["the","a","i","on","with","of","an","for","to",
180
+ "was","were","and","in","my","from","out","by"],
181
+ split_string(Sentence02,SepandPad,SepandPad,Sentence2),
182
+ subtract(Sentence2,Connectors,Sentence3),
183
+ %%length(Sentence3,Length),
184
+
185
+ %% () add generated data
186
+ %%write_commands(Length,[],Commands), %% sentence alg
187
+
188
+ generate_sentences(Sentence3,[],Sentence_a,30), %% detail sentences
189
+ append(Sentence3,Sentence_a,Sentence4),
190
+ findall([*,Sentence1,a_alg(Sentence5),b_alg(Sentence5,a),bb_alg(Sentence6)],(member(Sentence4a,Sentence4),make_lists(Sentence4a,[],Sentence5),Sentence5=[_|Sentence6]),B),!.
191
+ %% detail algs
192
+
193
+ generate_sentences(_Sentence3,Sentence_a,Sentence_a,0) :- !.
194
+ generate_sentences(Sentence3,Sentence_a1,Sentence_a2,N1) :-
195
+ random_member(Item,Sentence3),
196
+ generate_sentence(Item,Sentence_a3),
197
+ append(Sentence_a1,[Sentence_a3],Sentence_a4),
198
+ N2 is N1-1,
199
+ generate_sentences(Sentence3,Sentence_a4,Sentence_a2,N2).
200
+
201
+ %%write_commands(0,Commands,Commands) :- !.
202
+ %%write_commands(Length1,Commands1,Commands2) :-
203
+ %% Length2 is Length1-1.
204
+ %% append(Commands1,[[[n,member2],[[v,Length1],[v,Length2]]]).
205
+ %%[[n,equals1],[[v,Length2],[[v,a***],[v,b]]]]].
206
+
207
+ %%[a,b]
208
+ %%[c,d]
209
+
210
+ generate_sentence(Item,Sentence) :-
211
+ random_member(Grammar1,[[n,v,n],[n,v,a,n],[v,n],[v,a,n]]),
212
+ brdict(BrDict012),
213
+ find_pos(Item,POS,BrDict012),
214
+ substitute1(Item,POS,Grammar1,[],Grammar2),
215
+ substitute2(Grammar2,BrDict012,[],Sentence).
216
+
217
+ find_pos("right",v,_) :- !.
218
+ find_pos("plus",a,_) :- !.
219
+ find_pos(Item,POS2,BrDict012) :-
220
+ member([Item,POS1],BrDict012),
221
+ POS1="right",
222
+ POS2=v,!.
223
+ find_pos(Item,POS2,BrDict012) :-
224
+ member([Item,POS1],BrDict012),
225
+ POS1="plus",
226
+ POS2=a,!.
227
+ find_pos(_Item,POS2,_BrDict012) :-
228
+ POS2=n.
229
+
230
+ substitute1(_Item,_POS,[],Grammar,Grammar) :- !.
231
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
232
+ Grammar1=[Grammar4|Grammar5],
233
+ Grammar4=POS,
234
+ append_list([Grammar2,Item],Grammar6),
235
+ append(Grammar6,Grammar5,Grammar3),!.
236
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
237
+ Grammar1=[Grammar4|Grammar5],
238
+ not(Grammar4=POS),
239
+ append_list([Grammar2,Grammar4],Grammar6),
240
+ substitute1(Item,POS,Grammar5,Grammar6,Grammar3),!.
241
+
242
+ substitute2([],_BrDict012,Sentence,Sentence) :- !.
243
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
244
+ Grammar1=[Grammar2|Grammar3],
245
+ Grammar2=n,
246
+ findall(A,(member([A,"box"],BrDict012)),B),
247
+ random_member(Word,B),
248
+ append(Sentence1,[Word],Sentence3),
249
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
250
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
251
+ Grammar1=[Grammar2|Grammar3],
252
+ Grammar2=v,
253
+ findall(A,(member([A,"right"],BrDict012)),B),
254
+ random_member(Word,B),
255
+ append(Sentence1,[Word],Sentence3),
256
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
257
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
258
+ Grammar1=[Grammar2|Grammar3],
259
+ Grammar2=a,
260
+ findall(A,(member([A,"plus"],BrDict012)),B),
261
+ random_member(Word,B),
262
+ append(Sentence1,[Word],Sentence3),
263
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
264
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
265
+ Grammar1=[Grammar2|Grammar3],
266
+ append(Sentence1,[Grammar2],Sentence3),
267
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
268
+
269
+ make_lists(Sentence1,Sentence,Sentence) :-
270
+ Sentence1=[_Sentence2],
271
+ !.
272
+ make_lists(Sentence1,Sentence2,Sentence3) :-
273
+ Sentence1=[Sentence4|Sentence5],
274
+ Sentence5=[Sentence6|_Sentence7],
275
+ append(Sentence2,[[Sentence4,Sentence6]],Sentence8),
276
+ make_lists(Sentence5,Sentence8,Sentence3).
277
+
Algorithm-Writer-with-Lists/grammar_logic_to_alg2.pl ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+
3
+ ?- grammar_logic_to_alg2(24). ["Simplify algorithm","pia","pear"]
4
+ ["What is the word for","person"]
5
+ 1 harry
6
+ 2 june
7
+ 3 person
8
+ 4 pia
9
+
10
+ "Please choose from menu items 1-4, or enter a new word:"
11
+ |: subject
12
+ ["Simplify algorithm","harry","banana"]
13
+ ["Simplify algorithm","apple","june"]
14
+ [["subject","pia","pear","pear"],["subject","harry","banana","banana"],["subject","june","apple","apple"]]
15
+ true.
16
+
17
+ ?- grammar_logic_to_alg. ["Simplify algorithm","pia","pear"]
18
+ [["subject","pia","pear","pear"]]
19
+ true.
20
+
21
+ **/
22
+
23
+ %:-include('../listprologinterpreter/la_strings.pl').
24
+ :-include('../listprologinterpreter/la_strings.pl').
25
+ :-include('../listprologinterpreter/la_strings_string.pl').
26
+ :-include('../listprologinterpreter/la_maths.pl').
27
+
28
+ grammar_logic_to_alg2(First_n_words) :-
29
+
30
+ % get file
31
+ phrase_from_file_s(string(String1),"../Text-to-Breasonings/file.txt"),
32
+
33
+ % split into lines and sentences
34
+ %SepandPad2="\n\r",
35
+ SepandPad1="&#@~%`$?-+*^,()|.:;=_/[]<>{}\n\r\s\t\\\"!'0123456789",
36
+ string_codes(String1b,String1),
37
+ downcase_atom(String1b,String1c),
38
+ string_codes(String1c,String1a),
39
+ split_string(String1a,SepandPad1,SepandPad1,String21),
40
+ length(String21,L),
41
+ (L<First_n_words->First_n_words1=L;First_n_words1=First_n_words),
42
+ length(String2,First_n_words1),
43
+ append(String2,_,String21),
44
+
45
+
46
+ % phrase_from_file_s(string(BrDict0), "../../Text-to-Breasonings/brdict1.txt"),
47
+ phrase_from_file_s(string(BrDict0), "../Text-to-Breasonings/brdict1.txt"),
48
+ splitfurther(BrDict0,Term),
49
+
50
+ %trace,
51
+ phrase_from_file_s(string(BrDict3), "../Algorithm-Writer-with-Lists/brdict3.txt"),
52
+ string_codes(String02ba,BrDict3),
53
+ atom_to_term(String02ba,Term1,[]),
54
+
55
+ /**
56
+ phrase_from_file_s(string(BrDict4), "first-words.txt"),
57
+ string_codes(String02b,BrDict4),
58
+ atom_to_term(String02b,String02a,[]),
59
+ **/
60
+ %trace,
61
+ % Finds additions to brdict3.txt (box,values)
62
+ process_file(String2,Term,Term1,%String02a,%[],
63
+ Additions_to_brdict3,[],Pseudo_algorithms%,%[],
64
+ %First_words
65
+ ),
66
+
67
+ %***append(Term1,Additions_to_brdict3,Additions_to_brdict3_2),
68
+
69
+ term_to_atom(Additions_to_brdict3,String02a_b),
70
+ string_atom(String02a_c,String02a_b),
71
+
72
+ (open_s("../Algorithm-Writer-with-Lists/brdict3.txt",write,Stream1),
73
+ write(Stream1,String02a_c),
74
+ close(Stream1)),!,
75
+
76
+ %***append(String02a,First_words,First_words_2),
77
+
78
+ /**
79
+ term_to_atom(First_words,First_words_2_b),
80
+ string_atom(First_words_2_c,First_words_2_b),
81
+
82
+ (open_s("first-words.txt",write,Stream2),
83
+ write(Stream2,First_words_2_c),
84
+ close(Stream2)),!,
85
+ **/
86
+
87
+ writeln1(Pseudo_algorithms),!,
88
+
89
+ random_member(Summary_algorithm,Pseudo_algorithms),
90
+ writeln1("Summary algorithm:"),
91
+ writeln1(Summary_algorithm).
92
+
93
+ process_file(String2,Term,Term1,%_String02a,%Additions_to_brdict31,
94
+ Additions_to_brdict3,Pseudo_algorithms1,Pseudo_algorithms%,First_words1,
95
+ %First_words
96
+ ) :-
97
+ %trace,
98
+ String2=[Word1,Word2|Words],
99
+ writeln1(["Simplify algorithm",Word1,Word2]),
100
+ found(Word1,Term,Term1,Item1,%Term1,
101
+ Additions_to_brdict31_4),
102
+ found(Word2,Term,Additions_to_brdict31_4,Item2,%Additions_to_brdict31_4,
103
+ Additions_to_brdict31_5),
104
+ stronger_word([Item1,Word1],[Item2,Word2],Term,_Word3,%String02a,First_words3,
105
+ Item_a),
106
+ append(Pseudo_algorithms1,[Item_a],Pseudo_algorithms2),
107
+ %trace,
108
+ process_file(Words,Term,Additions_to_brdict31_5,%First_words3,%Additions_to_brdict31_5,
109
+ Additions_to_brdict3,Pseudo_algorithms2,Pseudo_algorithms%,First_words3,
110
+ %First_words
111
+ ).
112
+
113
+ process_file(_,_Term,Term1,%String02a,%Additions_to_brdict3,
114
+ Term1,Pseudo_algorithms,Pseudo_algorithms%,First_words,
115
+ %String02a
116
+ ) :- !.
117
+
118
+
119
+
120
+ % bd3 - person, subject and
121
+ found(Word1,Term,Additions_to_brdict1,Word2,%Additions_to_brdict1,
122
+ Additions_to_brdict2) :-
123
+ %(Word1="bot"->trace;true),
124
+ %trace,
125
+ %(Word1="june"->trace;true),
126
+
127
+ (member([Word1,Word21],Term)->true;(writeln(["Error:",Word1,"not in brdict1.txt."]),abort)),
128
+ (Word21=""->Word2=Word1;Word2=Word21),
129
+
130
+ append(Additions_to_brdict1,[[Word2,Word1]],Additions_to_brdict2)
131
+
132
+ /** (member([Word2,Item1],Additions_to_brdict1)->Additions_to_brdict1=Additions_to_brdict2;
133
+ (%trace,
134
+
135
+
136
+ findall(Word3,(member([Word3,Word2],Term)),Word22),
137
+ append([Word2],Word22,Word23),
138
+ %trace,
139
+ sort(Word23,Word241),
140
+ delete(Word241,"",Word24),
141
+
142
+ length(Word24,Menu_items_length),
143
+ (Menu_items_length=1->([Item1]=Word24,
144
+ append(Additions_to_brdict1,[[Word2,Item1]],Additions_to_brdict2));
145
+ (writeln1(["What is the word for",Word1]),
146
+ numbers(Menu_items_length,1,[],N),
147
+ findall([N1,"\t",Menu_item,"\n"],(member(N1,N),
148
+ get_item_n(Word24,N1,Menu_item)),Menu1),
149
+ maplist(append,[Menu1],[Menu3]),
150
+ concat_list(Menu3,Menu2),
151
+ writeln(Menu2),
152
+
153
+ repeat,%trace,
154
+ concat_list(["Please choose from menu items 1-",Menu_items_length,", or enter a new word (letters only, no spaces):"],Prompt),
155
+ writeln1(Prompt),
156
+ read_string(user_input,"\n","\r",_,Input),
157
+ ((%trace,
158
+ number_string(Input_num,Input),
159
+ Input_num>=1,Input_num=<Menu_items_length,
160
+ %trace,
161
+ get_item_n(Menu1,Input_num,[_,_,Item1,_]),
162
+ %trace,
163
+ append(Additions_to_brdict1,[[Word2,Item1]],Additions_to_brdict2))->true;
164
+ (string(Input)->(Input=Item1,append(Additions_to_brdict1,[[Word2,Item1]],Additions_to_brdict2))
165
+ )))))).
166
+ **/
167
+ .
168
+
169
+ stronger_word([Word1,Word1a],[Word2aa,Word2a],Term,Word3%,First_words1,First_words
170
+ ,Item) :-
171
+ %trace,
172
+ (member([Word1,Word21],Term)->true;(writeln(["Error:",Word1,"not in brdict1.txt."]),abort)),
173
+ (Word21=""->Word2=Word1;Word2=Word21),
174
+ %(member([Word2,Item1],Additions_to_brdict1)->Additions_to_brdict1=Additions_to_brdict2;
175
+ (%trace,
176
+
177
+
178
+ findall(Word3,(member([Word3,Word2],Term)),Word22),
179
+ append([Word2],Word22,Word23),
180
+ %trace,
181
+ sort(Word23,Word241),
182
+ delete(Word241,"",Word24),
183
+
184
+ length(Word24,Menu_items_length)),
185
+
186
+
187
+ (member([Word2aa,Word21a],Term)->true;(writeln(["Error:",Word2aa,"not in brdict1.txt."]),abort)),
188
+ (Word21a=""->Word2ab=Word2aa;Word2ab=Word21a),
189
+ %(member([Word2aa,Item1a],Additions_to_brdict1)->Additions_to_brdict1=Additions_to_brdict2;
190
+ (%trace,
191
+
192
+
193
+ findall(Word3,(member([Word3,Word2ab],Term)),Word22a),
194
+ append([Word2ab],Word22a,Word23a),
195
+ %trace,
196
+ sort(Word23a,Word241a),
197
+ delete(Word241a,"",Word24a),
198
+
199
+ length(Word24a,Menu_items_length_a)),
200
+
201
+ /**(member(Word1,String02a)->(First_words1=First_words,Item=[Word1,Word1a,Word2,Word2a]);
202
+ (member(Word2,String02a)->(First_words1=First_words,Item=[Word2,Word2a,Word1,Word1a]);
203
+ (%trace,
204
+ writeln(["Which of 1-",Word1,"or 2-",Word2,"is stronger?"]),
205
+ read_string(user_input,"\n","\r",_,Input),
206
+ **/
207
+ %trace,
208
+ (Menu_items_length>=Menu_items_length_a->(Word3=Word1,Item=[Word1,Word1a,Word2aa,Word2a]);
209
+ (Word3=Word2,Item=[Word2aa,Word2a,Word1,Word1a])).
210
+ %append(First_words1,[Word3],First_words)))).
211
+
212
+
213
+ % Append punctuation point to end if necessary.
214
+ % find last character in file
215
+ % string_concat(_,".",String1),
216
+
217
+ /**
218
+ split_string_onnonletter(String00,Chars,List1) :-
219
+ string_codes(String00,String1),
220
+ split_string_onnonletter(String1,[],Chars,List0),
221
+ string_codes(List0,List2),
222
+ split_string(List2,"@","@",List1),!.
223
+ split_string_onnonletter([],Input,_Chars,Input) :- !.
224
+ split_string_onnonletter(Input1,Input2,Chars,Input3) :-
225
+ Input1=[Input4|Input5],
226
+ %not(char_type(Input4,alpha)),
227
+ string_codes(Chars,Codes),
228
+ member(Code,Codes),
229
+ Input4=Code,
230
+ append(Input2,[Code],Input7),
231
+ append(Input7,`@`,Input6),
232
+ split_string_onnonletter(Input5,Input6,Chars,Input3), !.
233
+ split_string_onnonletter(Input1,Input2,Chars,Input3) :-
234
+ Input1=[Input4|Input5],
235
+ %char_type(Input4,alpha),
236
+ append(Input2,[Input4],Input6),
237
+ split_string_onnonletter(Input5,Input6,Chars,Input3), !.
238
+ **/
239
+
240
+ splitfurther(BrDict01,N) :-
241
+ phrase(file0(N),BrDict01).
242
+
243
+ file0(N) --> "[", file(N), "]", !.
244
+ file0([]) --> [].
245
+
246
+ %%file([]) --> [].
247
+ file([L|Ls]) --> entry(L),",",
248
+ %{writeln(L)}, %%***
249
+ file(Ls), !. %% file(Ls),{M=[Ls]})), !. %%, {writeln(["l",L])},",", file(Ls), {writeln(["ls",Ls])},!. %%, {append(L,Ls,M)}, !.
250
+ file([L]) --> entry(L),
251
+ %%{writeln(L)},
252
+ !. %%(entry(L),{M=L});{M=[],(writeln("Warning - Entry in incorrect format.")
253
+ %%,abort
254
+ %%)}, !.
255
+
256
+ entry([Word2,Word4]) -->
257
+ "[", word(Word), {string_codes(Word2,Word),string(Word2)},
258
+ ",",
259
+ word(Word3), {string_codes(Word4,Word3),string(Word4)},
260
+ "]".
261
+
262
+ word([X|Xs]) --> [X], {char_type(X,csymf)->true;(X=27->true;X=8217)}, word(Xs), !.
263
+ %%word([X]) --> [X], {char_type(X,csymf);(X=27;X=8217)}, !.
264
+ word([]) --> [].
265
+
Algorithm-Writer-with-Lists/grammar_logic_to_alg_mr.pl ADDED
@@ -0,0 +1,275 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+
3
+ grammar_logic_to_alg.pl
4
+
5
+
6
+ e.g. I liked God with you
7
+ [liked,God],[God,you] with extra data
8
+ - connect liked to you with member, return words on path
9
+
10
+ POS
11
+
12
+ goes backwards x forwards
13
+ - I v n by v n x ignore
14
+
15
+ n,v
16
+ - single type
17
+
18
+ adjective
19
+ - ignored x (ignore names in alg)
20
+ - like v,n
21
+
22
+ joining words - ignore, just pairs v, n
23
+ - on
24
+ - joins v to n
25
+ - separates vns
26
+
27
+ negative terms
28
+ - switch to positive or ignore
29
+
30
+ disappearing words
31
+ - my
32
+ - no list of these, because they are ignored
33
+
34
+ vv is v1, v2 x v1
35
+ nn is n1
36
+
37
+ *
38
+
39
+ later: can randomly generate details like given sentence
40
+
41
+ **/
42
+
43
+ :-include('../Text-to-Breasonings/text_to_breasonings.pl').
44
+ :-include('../Text-to-Breasonings/truncate.pl').
45
+ :-include('../Music-Composer/mindreadtestmusiccomposer-unusual-mr-tree.pl').
46
+
47
+
48
+ :- dynamic brdict/1.
49
+
50
+
51
+
52
+ mt_t2b2:-
53
+ %Thread=
54
+ %[
55
+ phrase_from_file_s(string(Essay_0), %"lacom5million.txt"
56
+ "../../private/lacom0.5million.txt"
57
+ ),
58
+
59
+ %Br is 5,
60
+ %Br is 5000000,
61
+ W is 0,
62
+
63
+ %grammar_logic_to_alg1(Essay_0,Br,GL_out1),
64
+ grammar_logic_to_alg113(Essay_0,GL_out1),
65
+
66
+ term_to_atom(GL_out1,GL_out2),
67
+ string_atom(GL_out,GL_out2),
68
+
69
+ texttobr2(u,u,GL_out,Br,false,false,false,false,false,false,W),
70
+ texttobr(u,u,GL_out,Br).%],
71
+
72
+ mt_t2b3:-mt_t2b2.
73
+
74
+ mt_t2b4:-mt_t2b2.
75
+
76
+ mt_t2b5:-mt_t2b2.
77
+
78
+ mt_t2b6:-mt_t2b2.
79
+
80
+ mt_t2b7:-mt_t2b2.
81
+
82
+ mt_t2b8:-mt_t2b2.
83
+
84
+ mt_t2b9:-mt_t2b2.
85
+ %trace,Thread.
86
+
87
+
88
+ mt_t2b :-
89
+
90
+ Goals=[mt_t2b2,mt_t2b3,mt_t2b4,mt_t2b5,mt_t2b6,mt_t2b7,mt_t2b8,mt_t2b9
91
+ ],
92
+
93
+ length(Goals,L),
94
+
95
+ %time(mt_t2b2).
96
+ time(concurrent(L,Goals,[])).
97
+
98
+ grammar_logic_to_alg1(String1,N,Result) :-
99
+
100
+ %term_to_atom(String,Essay_01),
101
+ %string_atom(Essay_02,Essay_01),
102
+
103
+ %working_directory(_, '../'),
104
+
105
+ %(open_s("../Text-to-Breasonings/file.txt",write,Stream1),
106
+ %write(Stream1,String),
107
+ %close(Stream1)),!,
108
+
109
+ truncate1(string,String1,N,String),
110
+
111
+ %working_directory(_, 'algwriter/'),
112
+ grammar_logic_to_alg114(String,Result).
113
+
114
+ grammar_logic_to_alg1 :-
115
+ grammar_logic_to_alg11([Sentence1,List_a,List_a1,List_b1,List_bb,List_bb1,Cs1,Cs2]),
116
+ writeln1([*,Sentence1,a_alg(List_a)]),
117
+ writeln(List_a1),
118
+ writeln1([*,Sentence1,b_alg(List_a)]),
119
+ writeln(List_b1),
120
+ writeln1([*,Sentence1,bb_alg(List_bb)]),
121
+ writeln(List_bb1),
122
+
123
+ writeln1(Cs1),
124
+ writeln1(Cs2).
125
+
126
+ grammar_logic_to_alg112(Result) :-
127
+ grammar_logic_to_alg11(Result1),term_to_atom(Result1,Result).
128
+
129
+ grammar_logic_to_alg114(Text,Result) :-
130
+ grammar_logic_to_alg113(Text,Result1),term_to_atom(Result1,Result).
131
+
132
+
133
+ grammar_logic_to_alg11([Sentence1,List_a,List_a1,List_b1,List_bb,List_bb1,Cs1,Cs2]) :-
134
+ phrase_from_file_s(string(Text1), "../Text-to-Breasonings/file.txt"),
135
+
136
+ grammar_logic_to_alg113(Text1,[Sentence1,List_a,List_a1,List_b1,List_bb,List_bb1,Cs1,Cs2]).
137
+
138
+
139
+ grammar_logic_to_alg113(Text1,[Sentence1,List_a,List_a1,List_b1,List_bb,List_bb1,Cs1,Cs2]) :-
140
+
141
+ phrase_from_file_s(string(BrDict0), "../Text-to-Breasonings/brdict1.txt"),
142
+ splitfurther(BrDict0,BrDict01),
143
+ sort(BrDict01,BrDict012),
144
+ retractall(brdict(_)),
145
+ assertz(brdict(BrDict012)),
146
+
147
+ SepandPad=".\n",
148
+ split_string(Text1,SepandPad,SepandPad,Text2a),
149
+ delete(Text2a,"",Text2),
150
+
151
+ findall(B2,(member(B1,Text2),grammar_logic_to_alg(B1,B2)),C),
152
+ length(C,CLength),
153
+ writeln([CLength,sentences]),
154
+
155
+ C=[[[_,Sentence1,a_alg(List_a),_,bb_alg(List_bb)]|Cs1]|Cs2],
156
+ %%get_time(TS),stamp_date_time(TS,date(Year,Month,Day,Hour1,Minute1,Seconda,_A,_TZ,_False),local),
157
+ concat_list(["gla_del"],%%[Year,Month,Day,Hour1,Minute1,Seconda],
158
+ File1),
159
+ concat_list(["\"",File1,".txt\""],File2),
160
+
161
+ term_to_atom(List_a,List_a2),
162
+ string_atom(List_a3,List_a2),
163
+
164
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_a1),
165
+
166
+ concat_list(["\n\nswipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,",[v,b]]],[[[n,function],[[v,a],[v,b]],\":-\",[[[n,tail],[[v,a],[v,b]]]]]],_),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_b1),
167
+
168
+ term_to_atom(List_bb,List_bb2),
169
+ string_atom(List_bb3,List_bb2),
170
+
171
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_bb3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_bb1).
172
+
173
+ grammar_logic_to_alg(Sentence1,B) :- %% Not by multi-sentence algorithms, just by sentence
174
+ atom_string(Sentence0,Sentence1),
175
+ downcase_atom(Sentence0,Sentence01),
176
+ atom_string(Sentence01,Sentence02),
177
+ SepandPad="&#@~%`$?-+*^,()|.:;=_/[]<>{}\n\r\s\t\\\"!'0123456789",
178
+ Connectors=
179
+ ["the","a","i","on","with","of","an","for","to",
180
+ "was","were","and","in","my","from","out","by"],
181
+ split_string(Sentence02,SepandPad,SepandPad,Sentence2),
182
+ subtract(Sentence2,Connectors,Sentence3),
183
+ %%length(Sentence3,Length),
184
+
185
+ %% () add generated data
186
+ %%write_commands(Length,[],Commands), %% sentence alg
187
+
188
+ generate_sentences(Sentence3,[],Sentence_a,1), %% detail sentences
189
+ append(Sentence3,Sentence_a,Sentence4),
190
+ findall([*,Sentence1,a_alg(Sentence5),b_alg(Sentence5,a),bb_alg(Sentence6)],(member(Sentence4a,Sentence4),make_lists(Sentence4a,[],Sentence5),Sentence5=[_|Sentence6]),B),!.
191
+ %% detail algs
192
+
193
+ generate_sentences(_Sentence3,Sentence_a,Sentence_a,0) :- !.
194
+ generate_sentences(Sentence3,Sentence_a1,Sentence_a2,N1) :-
195
+ random_member(Item,Sentence3),
196
+ generate_sentence(Item,Sentence_a3),
197
+ append(Sentence_a1,[Sentence_a3],Sentence_a4),
198
+ N2 is N1-1,
199
+ generate_sentences(Sentence3,Sentence_a4,Sentence_a2,N2).
200
+
201
+ %%write_commands(0,Commands,Commands) :- !.
202
+ %%write_commands(Length1,Commands1,Commands2) :-
203
+ %% Length2 is Length1-1.
204
+ %% append(Commands1,[[[n,member2],[[v,Length1],[v,Length2]]]).
205
+ %%[[n,equals1],[[v,Length2],[[v,a***],[v,b]]]]].
206
+
207
+ %%[a,b]
208
+ %%[c,d]
209
+
210
+ generate_sentence(Item,Sentence) :-
211
+ random_member(Grammar1,[[n,v,n],[n,v,a,n],[v,n],[v,a,n]]),
212
+ brdict(BrDict012),
213
+ find_pos(Item,POS,BrDict012),
214
+ substitute1(Item,POS,Grammar1,[],Grammar2),
215
+ substitute2(Grammar2,BrDict012,[],Sentence).
216
+
217
+ find_pos(Item,POS2,BrDict012) :-
218
+ member([Item,POS1],BrDict012),
219
+ POS1="right",
220
+ POS2=v,!.
221
+ find_pos(Item,POS2,BrDict012) :-
222
+ member([Item,POS1],BrDict012),
223
+ POS1="plus",
224
+ POS2=a,!.
225
+ find_pos(_Item,POS2,_BrDict012) :-
226
+ POS2=n.
227
+
228
+ substitute1(_Item,_POS,[],Grammar,Grammar) :- !.
229
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
230
+ Grammar1=[Grammar4|Grammar5],
231
+ Grammar4=POS,
232
+ append_list([Grammar2,Item],Grammar6),
233
+ append(Grammar6,Grammar5,Grammar3),!.
234
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
235
+ Grammar1=[Grammar4|Grammar5],
236
+ not(Grammar4=POS),
237
+ append_list([Grammar2,Grammar4],Grammar6),
238
+ substitute1(Item,POS,Grammar5,Grammar6,Grammar3),!.
239
+
240
+ substitute2([],_BrDict012,Sentence,Sentence) :- !.
241
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
242
+ Grammar1=[Grammar2|Grammar3],
243
+ Grammar2=n,
244
+ findall(A,(member([A,"box"],BrDict012)),B),
245
+ mind_read(Word,B),
246
+ append(Sentence1,[Word],Sentence3),
247
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
248
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
249
+ Grammar1=[Grammar2|Grammar3],
250
+ Grammar2=v,
251
+ findall(A,(member([A,"right"],BrDict012)),B),
252
+ mind_read(Word,B),
253
+ append(Sentence1,[Word],Sentence3),
254
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
255
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
256
+ Grammar1=[Grammar2|Grammar3],
257
+ Grammar2=a,
258
+ findall(A,(member([A,"plus"],BrDict012)),B),
259
+ mind_read(Word,B),
260
+ append(Sentence1,[Word],Sentence3),
261
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
262
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
263
+ Grammar1=[Grammar2|Grammar3],
264
+ append(Sentence1,[Grammar2],Sentence3),
265
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
266
+
267
+ make_lists(Sentence1,Sentence,Sentence) :-
268
+ Sentence1=[_Sentence2],
269
+ !.
270
+ make_lists(Sentence1,Sentence2,Sentence3) :-
271
+ Sentence1=[Sentence4|Sentence5],
272
+ Sentence5=[Sentence6|_Sentence7],
273
+ append(Sentence2,[[Sentence4,Sentence6]],Sentence8),
274
+ make_lists(Sentence5,Sentence8,Sentence3).
275
+
Algorithm-Writer-with-Lists/grammar_logic_to_alg_random.pl ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+
3
+ grammar_logic_to_alg_without_brdict.pl
4
+
5
+ **/
6
+
7
+ :-include('../Text-to-Breasonings/text_to_breasonings.pl').
8
+
9
+ :- dynamic brdict/1.
10
+ :- dynamic brdict_pos/1.
11
+
12
+ %% given N sentences to generate, takes a sentence and M sentences to find substitution words from
13
+
14
+ grammar_logic_to_alg1 :-
15
+ phrase_from_file_s(string(Text1), "../Text-to-Breasonings/luciansphilosophy.txt"),
16
+
17
+
18
+ phrase_from_file_s(string(BrDict0), "../Text-to-Breasonings/brdict1.txt"),
19
+ splitfurther(BrDict0,BrDict01),
20
+ sort(BrDict01,BrDict012),
21
+ retractall(brdict_pos(_)),
22
+ assertz(brdict_pos(BrDict012)),
23
+
24
+ SepandPad=".\n",
25
+ split_string(Text1,SepandPad,SepandPad,Text2a),
26
+ delete(Text2a,"",Text222),
27
+
28
+ %% random(N1),N2 is round(9*N1)+1,length(N2L,N2),
29
+ N2L=[_],
30
+ findall(N3,(member(_,N2L),random_member(N3,Text222)),N4),
31
+
32
+ %%writeln([n4,N4]),
33
+
34
+ %% append_list(N4,Text2),
35
+
36
+
37
+
38
+ findall(B2,( %%random(M1),M2 is round(9*M1)+1,length(M2L,M2),
39
+
40
+
41
+ %%SepandPad2=" .\n",
42
+ %%split_string(Text1,SepandPad2,SepandPad2,Text2aa),
43
+ %%delete(Text2aa,"",M3),
44
+
45
+ %%findall(M33,(member(_,M2L),random_member(M33,M3)),M4),
46
+ M4 = Text222,
47
+ %%writeln([m4,M4]),
48
+
49
+ %%append_list(Text2aaa,M5),
50
+ retractall(brdict(_)),
51
+ assertz(brdict(M4)),
52
+
53
+ member(B1,N4),
54
+
55
+ %%trace,
56
+
57
+ grammar_logic_to_alg(B1,B2)),C),
58
+ length(C,CLength),
59
+ writeln1([CLength,sentences]),
60
+
61
+ C=[[[_,Sentence1,a_alg(List_a),_,bb_alg(List_bb)]|Cs1]|Cs2],
62
+ %%get_time(TS),stamp_date_time(TS,date(Year,Month,Day,Hour1,Minute1,Seconda,_A,_TZ,_False),local),
63
+ concat_list(["gla_del"],%%[Year,Month,Day,Hour1,Minute1,Seconda],
64
+ File1),
65
+ concat_list(["\"",File1,".txt\""],File2),
66
+
67
+ term_to_atom(List_a,List_a2),
68
+ string_atom(List_a3,List_a2),
69
+
70
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_a1),
71
+ writeln1([*,Sentence1,a_alg(List_a)]),
72
+ writeln1(List_a1),
73
+
74
+ concat_list(["\n\nswipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_a3,",[v,b]]],[[[n,function],[[v,a],[v,b]],\":-\",[[[n,tail],[[v,a],[v,b]]]]]],_),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_b1),
75
+ writeln1([*,Sentence1,b_alg(List_a)]),
76
+ writeln1(List_b1),
77
+
78
+ term_to_atom(List_bb,List_bb2),
79
+ string_atom(List_bb3,List_bb2),
80
+
81
+ concat_list(["swipl -G100g -T20g -L2g\n['../listprolog'].\nleash(-all),visible(+all),protocol(",File2,"),trace,interpret(off,[[n,function],[",List_bb3,"]],[[[n,function],[[v,a]],\":-\",[[[n,length],[[v,a],0,1]]]],[[n,function],[[v,a]],\":-\",[[[n,head],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,e],[v,f]]]],[[n,reverse],[[v,a],[],[v,a1]]],[[n,head],[[v,a1],[v,d1]]],[[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],[[n,function2],[[v,a],[v,f],[v,f1]]]]],[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],\":-\",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]],[[n,function2],[[v,a],[v,b],[v,f]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]]]],[[n,function2],[[v,a],[v,b],[v,c]],\":-\",[[[n,member2],[[v,a],[v,d]]],[[n,equals1],[[v,d],[[v,b],[v,f]]]],[[n,function2],[[v,d],[v,f],[v,c]]]]],[[n,length],[[],[v,l],[v,l]]],[[n,length],[[v,l],[v,m1],[v,n]],\":-\",[[[n,not],[[[n,=],[[v,l],[]]]]],[[n,tail],[[v,l],[v,t]]],[[n,+],[[v,m1],1,[v,m2]]],[[n,length],[[v,t],[v,m2],[v,n]]]]]],[[]]),notrace,noprotocol.\nhalt.\nswipl -G100g -T20g -L2g\n['../Text-to-Breasonings/text_to_breasonings.pl'].\ntime((N is 3,\nM is 16000,\ntexttobr2(N,",File2,",u,M),texttobr(N,",File2,",u,M))).\n['../texttobr2qb'].\ntexttobr2(3).\nhalt."],List_bb1),
82
+ writeln1([*,Sentence1,bb_alg(List_bb)]),
83
+ writeln1(List_bb1),
84
+
85
+ writeln1(Cs1),
86
+ writeln1(Cs2).
87
+
88
+ grammar_logic_to_alg(Sentence1,B) :- %% Not by multi-sentence algorithms, just by sentence
89
+ atom_string(Sentence0,Sentence1),
90
+ downcase_atom(Sentence0,Sentence01),
91
+ atom_string(Sentence01,Sentence02),
92
+ SepandPad="&#@~%`$?-+*^,()|.:;=_/[]<>{}\n\r\s\t\\\"!'0123456789",
93
+ Connectors=
94
+ ["the","a","i","on","with","of","an","for","to",
95
+ "was","were","and","in","my","from","out","by"],
96
+ split_string(Sentence02,SepandPad,SepandPad,Sentence2),
97
+ subtract(Sentence2,Connectors,Sentence3),
98
+ %%length(Sentence3,Length),
99
+
100
+ %% () add generated data
101
+ %%write_commands(Length,[],Commands), %% sentence alg
102
+
103
+ generate_sentences(Sentence3,[],Sentence_a,30), %% detail sentences
104
+ append([Sentence3],Sentence_a,Sentence4),
105
+ findall([*,Sentence1,a_alg(Sentence5),b_alg(Sentence5,a),bb_alg(Sentence6)],(member(Sentence4a,Sentence4),make_lists(Sentence4a,[],Sentence5),Sentence5=[_|Sentence6]),B),!.
106
+ %% detail algs
107
+
108
+ generate_sentences(_Sentence3,Sentence_a,Sentence_a,0) :- !.
109
+ generate_sentences(Sentence3,Sentence_a1,Sentence_a2,N1) :-
110
+ random_member(Item,Sentence3),
111
+ %%trace,
112
+ generate_sentence(Item,Sentence_a3),
113
+ append(Sentence_a1,[Sentence_a3],Sentence_a4),
114
+ N2 is N1-1,
115
+ generate_sentences(Sentence3,Sentence_a4,Sentence_a2,N2).
116
+
117
+ %%write_commands(0,Commands,Commands) :- !.
118
+ %%write_commands(Length1,Commands1,Commands2) :-
119
+ %% Length2 is Length1-1.
120
+ %% append(Commands1,[[[n,member2],[[v,Length1],[v,Length2]]]).
121
+ %%[[n,equals1],[[v,Length2],[[v,a***],[v,b]]]]].
122
+
123
+ %%[a,b]
124
+ %%[c,d]
125
+
126
+ generate_sentence(Item,Sentence) :-
127
+ random_member(Grammar1,[[n,v,n,n,v,a,n,v,n,v,a,n],[n,v,n],[n,v,a,n],[v,n],[v,a,n]]),
128
+ brdict_pos(BrDict012),
129
+ find_pos(Item,POS,BrDict012),
130
+ substitute1(Item,POS,Grammar1,[],Grammar2),
131
+ substitute2(Grammar2,BrDict012,[],Sentence).
132
+
133
+ find_pos(Item,POS2,BrDict012) :-
134
+ POS1="right",
135
+ member([Item,POS1],BrDict012),
136
+ POS2=v,!.
137
+ find_pos(Item,POS2,BrDict012) :-
138
+ POS1="plus",
139
+ member([Item,POS1],BrDict012),
140
+ POS2=a,!.
141
+ find_pos(_Item,POS2,_BrDict012) :-
142
+ POS2=n.
143
+
144
+ substitute1(_Item,_POS,[],Grammar,Grammar) :- !.
145
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
146
+ Grammar1=[Grammar4|Grammar5],
147
+ Grammar4=POS,
148
+ append_list([Grammar2,Item],Grammar6),
149
+ append(Grammar6,Grammar5,Grammar3),!.
150
+ substitute1(Item,POS,Grammar1,Grammar2,Grammar3) :-
151
+ Grammar1=[Grammar4|Grammar5],
152
+ not(Grammar4=POS),
153
+ append_list([Grammar2,Grammar4],Grammar6),
154
+ substitute1(Item,POS,Grammar5,Grammar6,Grammar3),!.
155
+
156
+ substitute2([],_BrDict012,Sentence,Sentence) :- !.
157
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
158
+ Grammar1=[Grammar2|Grammar3],
159
+ Grammar2=n,
160
+ findall(A,(%%brdict(BD),member(A,BD),!,
161
+ member([A,"box"],BrDict012)),B),
162
+ random_member(Word,B),
163
+ append(Sentence1,[Word],Sentence3),
164
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
165
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
166
+ Grammar1=[Grammar2|Grammar3],
167
+ Grammar2=v,
168
+ findall(A,(%%brdict(BD),member(A,BD),!,
169
+ member([A,"right"],BrDict012)),B),
170
+ random_member(Word,B),
171
+ append(Sentence1,[Word],Sentence3),
172
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
173
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
174
+ Grammar1=[Grammar2|Grammar3],
175
+ Grammar2=a,
176
+ findall(A,(%%brdict(BD),member(A,BD),!,
177
+ member([A,"plus"],BrDict012)),B),
178
+ random_member(Word,B),
179
+ append(Sentence1,[Word],Sentence3),
180
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
181
+ substitute2(Grammar1,BrDict012,Sentence1,Sentence2) :-
182
+ Grammar1=[Grammar2|Grammar3],
183
+ append(Sentence1,[Grammar2],Sentence3),
184
+ substitute2(Grammar3,BrDict012,Sentence3,Sentence2).
185
+
186
+ make_lists(Sentence1,Sentence,Sentence) :-
187
+ Sentence1=[_Sentence2],
188
+ !.
189
+ make_lists(Sentence1,Sentence2,Sentence3) :-
190
+ Sentence1=[Sentence4|Sentence5],
191
+ Sentence5=[Sentence6|_Sentence7],
192
+ append(Sentence2,[[Sentence4,Sentence6]],Sentence8),
193
+ make_lists(Sentence5,Sentence8,Sentence3).
194
+
195
+ writeln1(Term) :-
196
+ term_to_atom(Term,Atom),
197
+ writeln(Atom),!.
Algorithm-Writer-with-Lists/grammar_logic_to_alg_sh.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ %% grammar_logic_to_alg.sh
2
+
3
+ swipl -G100g -T20g -L2g
4
+ [grammar_logic_to_alg].
5
+ leash(-all),visible(+all),protocol("./gla.txt").
6
+ grammar_logic_to_alg1.
7
+ noprotocol.
8
+ halt.
9
+
Algorithm-Writer-with-Lists/midnight_algorithm_input_list.pl ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :-include('../listprologinterpreter/la_strings').
2
+
3
+
4
+ midnight_algorithm_input_list :-
5
+ writeln("Algorithm name?"),
6
+ read_string(user_input, "\n", "\r", _End2, Alg_name),
7
+ writeln("List item 1?"),
8
+ read_string(user_input, "\n", "\r", _End21, List_item1),
9
+ writeln("List item 2?"),
10
+ read_string(user_input, "\n", "\r", _End22, List_item2),
11
+ writeln("List item 3?"),
12
+ read_string(user_input, "\n", "\r", _End23, List_item3),
13
+ writeln("List item 4?"),
14
+ read_string(user_input, "\n", "\r", _End24, List_item4),
15
+ Algorithm=
16
+
17
+ [[[n,Alg_name],[[[List_item1,List_item2],[List_item2,List_item3],[List_item3,List_item4]]]],
18
+ [[[n,Alg_name],[[[t,list],[[t,list2]]]]],
19
+ [[t,list2],[[t,string],[t,string]]],
20
+ [[n,reverse],[[[t,list],[[t,list2]]],[[t,list],[[t,list2]]],[[t,list],[[t,list2]]]]],
21
+ [[n,function2],[[[t,list],[[t,list2]]],[t,string],[t,string]]],
22
+ [[n,length],[[[t,list],[[t,list2]]],[t,number],[t,number]]]],
23
+
24
+ [[[n,Alg_name],[input]],
25
+ [[n,reverse],[input,input,output]],
26
+ [[n,function2],[input,input,input]],
27
+ [[n,length],[input,input,output]]],
28
+
29
+ [[[n,Alg_name],[[v,a]],":-",
30
+ [[[n,length],[[v,a],0,[v,b]]],
31
+ [[n,=],[[v,b],1]]]],
32
+
33
+ [[n,Alg_name],[[v,a]],":-",
34
+ [[[n,head],[[v,a],[v,d]]],
35
+ [[n,equals1],[[v,d],[[v,e],[v,f]]]],
36
+ [[n,reverse],[[v,a],[],[v,a1]]],
37
+ [[n,head],[[v,a1],[v,d1]]],
38
+ [[n,equals1],[[v,d1],[[v,e1],[v,f1]]]],
39
+ [[n,function2],[[v,a],[v,f],[v,f1]]]]],
40
+
41
+ [[n,reverse],[[],[v,l],[v,l]]],
42
+
43
+ [[n,reverse],[[v,l],[v,m],[v,n]],":-",
44
+ [[[n,head],[[v,l],[v,h]]],
45
+ [[n,tail],[[v,l],[v,t]]],
46
+ [[n,wrap],[[v,h],[v,h1]]],
47
+ [[n,append],[[v,h1],[v,m],[v,o]]],
48
+ [[n,reverse],[[v,t],[v,o],[v,n]]]]],
49
+
50
+ [[n,function2],[[v,a],[v,b],[v,f]],":-",
51
+ [[[n,member],[[v,d],[v,a]]],
52
+ [[n,equals1],[[v,d],[[v,b],[v,f]]]]]],
53
+
54
+ [[n,function2],[[v,a],[v,b],[v,c]],":-",
55
+ [[[n,member],[[v,d],[v,a]]],
56
+ [[n,equals1],[[v,d],[[v,b],[v,f]]]],
57
+ [[n,function2],[[v,d],[v,f],[v,c]]]]],
58
+
59
+ [[n,length],[[],[v,l],[v,l]]],
60
+
61
+ [[n,length],[[v,l],[v,m1],[v,n]],":-",
62
+ [[[n,not],[[[n,=],[[v,l],[]]]]],
63
+ [[n,tail],[[v,l],[v,t]]],
64
+ [[n,+],[[v,m1],1,[v,m2]]],
65
+ [[n,length],[[v,t],[v,m2],[v,n]]]]]],
66
+ [[]]],
67
+
68
+ writeln1([algorithm,Algorithm]).
Algorithm-Writer-with-Lists/mr_alg.pl ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ % [[[n,member],[[list],[_3386]]],[[n,member],[[list],[list]]],[[n,intersection],[[list],[list]]],[[n,member],[[list],[_5928]]],[[n,atom_string],[[atom],[string]]],[[n,string_length],[[string],[number]]]]
2
+
3
+ mr_alg(Commands3) :-
4
+ commands(Commands),
5
+ algorithm_length(Algorithm_length),
6
+ random_member(Command1,Commands),
7
+ Command1=[_Name1,[_Input1,Output1]],
8
+ mr_alg(Algorithm_length,Commands,Output1,[],Commands2),
9
+ append([Command1],Commands2,Commands3).
10
+
11
+ mr_alg(1,_,_,Commands,Commands) :- !.
12
+ mr_alg(Algorithm_length1,Commands,Output1,Commands1,Commands2) :-
13
+ findall(Command2,(member(Command2,Commands),
14
+ Command2=[_Name2,[Output1,_Output2]]),Commands3),
15
+ random_member(Command4,Commands3),
16
+ Command4=[_Name3,[_Input3,Output3]],
17
+ append(Commands1,[Command4],Commands5),
18
+ Algorithm_length2 is Algorithm_length1 - 1,
19
+ mr_alg(Algorithm_length2,Commands,Output3,Commands5,Commands2).
20
+
21
+ algorithm_length(5).
22
+
23
+ %& check if any of these have 3 args - x
24
+ commands([
25
+ [[n,+],[[number,number],[number]]],
26
+ [[n,-],[[number,number],[number]]],
27
+ [[n,*],[[number,number],[number]]],
28
+ [[n,/],[[number,number],[number]]],
29
+ [[n,/],[[number,number],[]]],
30
+ [[n,>],[[number,number],[]]],
31
+ [[n,>=],[[number,number],[]]],
32
+ [[n,<],[[number,number],[]]],
33
+ [[n,=<],[[number,number],[]]],
34
+ [[n,=],[[number,number],[]]],
35
+ [[n,=\=],[[number,number],[]]],
36
+ [[n,head],[[list],[_]]],
37
+ [[n,tail],[[list],[_]]],
38
+ [[n,member],[[list],[_]]], % swapped for this exercise
39
+ [[n,delete],[[list,_],[list]]],
40
+ [[n,append123],[[list,list],[list]]], % append([1],[2],A).
41
+ [[n,append132],[[list,list],[list]]], % append([1],A,[1,2]).
42
+ [[n,append231],[[list,list],[list]]], % append(A,[2],[1,2]).
43
+ % append(A,B,[1,2]). x
44
+ [[n,stringconcat123],[[string,string],[string]]], % string("1","2",A).
45
+ [[n,stringconcat132],[[string,string],[string]]], % string("1",A,"12").
46
+ [[n,stringconcat231],[[string,string],[string]]], % string(A,"2","12").
47
+ [[n,stringtonumber],[[string],[number]]],
48
+ [[n,random],[[number]]],
49
+ [[n,length],[[list],[number]]],
50
+ [[n,ceiling],[[number],[number]]],
51
+ [[n,sqrt],[[number],[number]]],
52
+ [[n,round],[[number],[number]]],
53
+ [[n,string_length],[[string],[number]]],
54
+ [[n,sort],[[list],[_]]],
55
+ [[n,intersection],[[list,list],[list]]],
56
+ [[n,atom_string],[[atom],[string]]]
57
+ ]).
Algorithm-Writer-with-Lists/random_dependencies.pl ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ random_dependencies(A) :-
2
+ random(N1),N2 is round(2*N1),
3
+ random_dependencies1(N2,A).
4
+ random_dependencies1(0,[]) :- !.
5
+ random_dependencies1(1,A) :-
6
+ random_member(B,[and,or,reverse,string_to_list,
7
+ split_into_sentences,is_positive_language,agrees,member,delete,minus,get_item_n,length,intersection,
8
+ substring,sum,sort,maximum,map,findall,duplicates,mutually_exclusive,list_head,add_number_to_list]),
9
+ random_dependencies(C),
10
+ A=[B,[C]].
11
+
12
+ random_dependencies1(2,A) :-
13
+ random_member(B,[and,or,reverse,string_to_list,
14
+ split_into_sentences,is_positive_language,agrees,member,delete,minus,get_item_n,length,intersection,
15
+ substring,sum,sort,maximum,map,findall,duplicates,mutually_exclusive,list_head,add_number_to_list]),
16
+ random_dependencies(C),random_dependencies(D),
17
+ A=[B,[C,D]].
Algorithm-Writer-with-Lists/ya_phil_to_alg.pl ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :-include('../Lucian-Academy/folders.pl').
2
+ :-include('mr_alg.pl').
3
+ :-include('../listprologinterpreter/la_strings.pl').
4
+ :-include('../listprologinterpreter/la_strings_string.pl').
5
+ :-include('../listprologinterpreter/la_files.pl').
6
+
7
+ % start with 1g memory
8
+
9
+ ya_phil_to_alg :-
10
+
11
+ folders(Courses1),
12
+ %random_member(Course,Courses1),
13
+ %get_texts(Dept,Texts) :-
14
+
15
+ findall(Texts1,(member(Dept,Courses1), concat_list(["../Lucian-Academy/",Dept,"/"],Dept1),
16
+ directory_files(Dept1,F),
17
+ delete_invisibles_etc(F,G),
18
+ findall(String02b,(member(Filex1,G),
19
+ string_concat(Dept1,Filex1,Filex),
20
+ phrase_from_file_s(string(String00a), Filex),
21
+ string_codes(String02b,String00a)),Texts1)
22
+ ),Texts2),
23
+
24
+ flatten(Texts2,Texts3),
25
+
26
+ term_to_atom(Texts3,Texts4),
27
+
28
+ split_string(Texts4,"\n\r","\n\r",Texts5),
29
+
30
+ findall([Text6,Alg],(member(Text6,Texts5),
31
+ mr_alg(Alg)),Algs1),
32
+
33
+ flatten(Algs1,Algs2),
34
+
35
+ term_to_atom(Algs2,Algs3),
36
+
37
+ %string_atom(Algs,Algs),
38
+
39
+ (open_s("yet_more_phil_algs.txt",write,Stream3),
40
+ write(Stream3,Algs3),
41
+ close(Stream3)),!.
42
+
43
+
44
+
45
+
Combination-Algorithm-Writer-Multiple-Predicates/.DS_Store ADDED
Binary file (6.15 kB). View file
 
Combination-Algorithm-Writer-Multiple-Predicates/LICENSE ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2019, Lucian Green
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Combination-Algorithm-Writer-Multiple-Predicates/LPCAWMP_docs.md ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Documentation of List Prolog Interpreter CAWMP Commands
2
+
3
+ * `[[n,[not,Operator]],[Variable1,Variable2]]` where `Operator=is` or `Operator="="` e.g. `[[n,[not,is]],[[v,a],1]]` returns true where `[v,a]` must be instantiated (not empty) and can equal 2.
4
+
5
+ * `[[n,[not,Operator]],[Variable1,Variable2]]` where `Operator=">"`, `Operator=">="`, `Operator="<"`, `Operator="=<"` or `Operator="=\="` e.g. `[[n,[not,>]],[3,2]]` returns false.
6
+
7
+ * `[[n,[not,member]],[Variable1,Variable2]]` e.g. `[[n,[not,member]],[["a","b","c"],"d"]]` returns true.
8
+
9
+ * `[[n,[not,head]],[Variable1,Variable2]]` e.g. `[[n,[not,head]],[["a","b","c"],"a"]]` returns false.
10
+
11
+ * `[[n,[not,tail]],[Variable1,Variable2]]` e.g. `[[n,[not,tail]],[["a","b","c"],["b","c"]]]` returns false.
12
+
13
+ * `[[n,[not,append]],[Variable1,Variable2,Variable3]]` e.g. `[[n,[not,append]],[["a"],["b","c"],["a","b","c"]]]` returns false.
14
+
15
+ * `[[n,[]],[Variable1]]` e.g. `[[n,[]],[[v,a]]]` returns `[v,a]=[]`.
16
+
17
+ * `[[n,""],[Variable1]]` e.g. `[[n,""],[[v,a]]]` returns `[v,a]=""`.
18
+
19
+ * `[[n,"_"],[Variable1]]` behaves similarly to _A in Prolog e.g. `[[n,"_"],[[v,a]]]` returns true.
20
+
Combination-Algorithm-Writer-Multiple-Predicates/README.md ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Combination Algorithm Writer with Multiple Predicates
2
+
3
+ * NB. This repository has been deprecated by <a href="https://github.com/luciangreen/Combination-Algorithm-Writer-Stable">Combination Algorithm Writer with Predicates Stable (CAWPS)</a>, which may return more commands per predicate, but this repository, CAWMP, has more features.
4
+
5
+ Combination Algorithm Writer with Multiple Predicates (CAWMP) is a SWI-Prolog algorithm that finds algorithms from given rules, algorithm parts (from algdict.pl) and writes algorithms with multiple predicates that satisfy the given input and output.
6
+
7
+ Please contact Lucian Green at <a href="mailto:luciangreen@lucianacademy.com">luciangreen@lucianacademy.com</a> with questions, comments and feedback about CAWMP. Please use <a href="https://github.com/luciangreen/Combination-Algorithm-Writer-Multiple-Predicates/blob/master/cawmp.bib">this citation</a> for research.
8
+
9
+ # Getting Started
10
+
11
+ Please read the following instructions on how to install the project on your computer for writing code.
12
+
13
+ # Prerequisites
14
+
15
+ Install List Prolog Interpreter Repository (https://github.com/luciangreen/listprologinterpreter) first.
16
+
17
+
18
+ # Installation from List Prolog Package Manager (LPPM)
19
+
20
+ * Optionally, you can install from LPPM by installing <a href="https://www.swi-prolog.org/build/">SWI-Prolog</a> for your machine, downloading the <a href="https://github.com/luciangreen/List-Prolog-Package-Manager">LPPM Repository</a>,
21
+ ```
22
+ mkdir GitHub
23
+ cd GitHub/
24
+ git clone https://github.com/luciangreen/List-Prolog-Package-Manager.git
25
+ cd List-Prolog-Package-Manager
26
+ swipl
27
+ ```
28
+ loading LPPM with `['lppm'].` then installing the package by running `lppm_install("luciangreen","Combination-Algorithm-Writer-Multiple-Predicates").`.
29
+
30
+ # Installing
31
+
32
+ * Download this repository.
33
+ * In SWI-Prolog, enter:
34
+ ```
35
+ ['cawplistprolog'].
36
+ ```
37
+ * Running
38
+
39
+ * Try CAWP regression tests (in <a href="https://github.com/luciangreen/Combination-Algorithm-Writer-Multiple-Predicates/blob/master/cawpverify.pl">cawpverify.pl</a>):
40
+ Example
41
+ `cawptest1(off,N,S).` where N is the test number 1-8.
42
+ Example Output
43
+ ```
44
+ [cawptest,passed,[[[n,add0],[[v,a],[v,b],[v,c]],":-",[[[n,+],[[v,a],[v,b],[v,d]]],[[n,=],[[v,d],[v,c]]]]]]]
45
+ ```
46
+ * Try CAWP regression tests (in <a href="https://github.com/luciangreen/Combination-Algorithm-Writer-Multiple-Predicates/blob/master/cawpverifya.pl">cawpverifya.pl</a>):
47
+
48
+ Example
49
+ `cawptest1a(off,7,S).`
50
+ Example Output
51
+ ```
52
+ [cawptest,7,passed]
53
+ S = passed.
54
+ ```
55
+
56
+ # Running Random Combination Algorithm Writer (RCAW)
57
+
58
+ * RCAW generates random algorithms.
59
+
60
+ * To load and install, follow the instructions above.
61
+
62
+ # Notes on CAWMP:
63
+
64
+ * Variables should be named a, b, c, and so on, without any gaps.
65
+ * In inputted data, i.e. specifications, the above applies.
66
+
67
+
68
+ CAWMP is called with the command: `caw00(Debug,Function,Rules,MaxLength,MaxPredicates,TotalVars,NumInputs,NumOutputs,Specifications,AlgDict,Program1,Program2).`
69
+ e.g.
70
+ ```
71
+ caw00(off,add0,[],2,1,3,[1,2],[0,1],
72
+ %% Specs
73
+ [[[[[[v,a],1],[[v,b],2]],[],true],
74
+ [[[[v,a],2],[[v,b],1]],[],true]]],
75
+ [ %% Algorithm dictionary
76
+ [[[n,1],1,1],[[v,a],[v,b]],":-",
77
+ [ [[n,+],[[v,a],1,[v,c]]],
78
+ [[n,=],[[v,c],[v,b]]]]],
79
+
80
+ [[[n,1],1,1],[[v,a],[v,b]],":-",
81
+ [ [[n,-],[[v,a],1,[v,c]]],
82
+ [[n,=],[[v,c],[v,b]]]]]
83
+ ],
84
+
85
+ [],Program2).
86
+ ```
87
+ It has the output:
88
+ ```
89
+ Program2=
90
+ [
91
+ [[n,1],[[v,a],[v,b]],":-",
92
+ [[[n,+],[[v,a],1,[v,c]]],
93
+ [[n,=],[[v,c],[v,b]]]]],
94
+ [[n,1],[[v,a],[v,b]],":-",
95
+ [[[n,-],[[v,a],1,[v,c]]],
96
+ [[n,=],[[v,c],[v,b]]]]],
97
+ [[n,add0],[[v,a],[v,b]],":-",
98
+ [[[n,1],[[v,b],[v,c]]],
99
+ [[n,=],[[v,a],[v,c]]]]]
100
+ ].
101
+ ```
102
+
103
+ * Debug is true for trace, false for no trace.
104
+ * Function is the inputted name of the algorithm.
105
+ * Rules are the commands to build the code from, e.g. `[[[n,+],2,1],[[n,[]],1,0]]`, which mean the `"+"` function has 2 inputs and one output and the `[]` function has one input and verifies an empty list. See the <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/LPI_docs.md">List Prolog Interpreter Documentation</a> for more commands.
106
+ * MaxLength is the maximum number of commands per predicate.
107
+ * MaxPredicates is the maximum number of predicates per algorithm.
108
+ * TotalVars is the number of different variables per predicate.
109
+ * Specifications have the form `A=[Input_variable_list, Output_variable_list, True (if this specification in true, or false if trying to eliminate possibilities)]`, where a variable list is `[B]` where `B` is e.g. `[[v,c],[1,2]]`. There is a list of specifications, e.g. `[A1,A2]` for a predicate (i.e. not the whole algorithm), and `[[A1,A2],[B1,B2]]` for the specifications for the algorithm, e.g.
110
+ ```
111
+ [[[[[[v,a],1],[[v,b],2]],[],true],
112
+ [[[[v,a],2],[[v,b],1]],[],true]]]
113
+ ```
114
+ * Program1 is the initial program (usually `[]`). Planned: partial multiple predicate algorithms may be given to the algorithm to complete.
115
+ * Program2 is the final outputted algorithm.
116
+ * CAWMP allows shorter and shorter lists of specifications to specify algorithms as its dictionary grows.
117
+
118
+ # Not tested updates
119
+
120
+ Neither Prolog nor List Prolog can return non-deterministic results after point of execution passes from a clause to a child clause, so the result of testing the repository is that multiple clauses cannot be generated in this case.
121
+
122
+ See the CAWPS Repository because it is all that is needed to build an algorithm bottom-up. Without generating base cases first, neither CAWMP nor CAWPS can work, and CAWPS can generate more commands per predicate than CAWMP.
123
+
124
+ # Documentation of List Prolog Interpreter CAWMP Commands
125
+
126
+ See <a href="https://github.com/luciangreen/Combination-Algorithm-Writer-Multiple-Predicates/blob/master/LPCAWMP_docs.md">Documentation of List Prolog Interpreter CAWMP Commands</a>.
127
+
128
+ # Authors
129
+
130
+ Lucian Green - Initial programmer - <a href="https://www.lucianacademy.com/">Lucian Academy</a>
131
+
132
+ # License
133
+
134
+ I licensed this project under the BSD3 License - see the <a href="LICENSE">LICENSE.md</a> file for details
135
+
Combination-Algorithm-Writer-Multiple-Predicates/caw5 copy 12.pl ADDED
@@ -0,0 +1,1252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**cawptest2(3,add,[[[n,[]],1,0],[[n,"_"],1,0]],4,1,4,[1,2,3],[0,1],
2
+ [
3
+ [[[[[v,a],[]],[[v,b],3],[[v,c],[4,5,6]]],
4
+ [[[v,d],[4,5,6]]],true]]
5
+ ],
6
+ [ %% Algorithm dictionary
7
+ ],
8
+ %% Result
9
+
10
+ [[[n,add],[[v,a],[v,b],[v,c],[v,d]],":-",[[[n,[]],[[v,a]]],[[n,"_"],[[v,a]]],[[n,"_"],[[v,b]]],[[n,=],[[v,c],[v,d]]]]]]
11
+
12
+ ).
13
+ **/
14
+
15
+ :- dynamic debug/1.
16
+ :- dynamic totalvars/1.
17
+ :- dynamic outputvars/1.
18
+ :- dynamic newrulenumber/1.
19
+ :- dynamic maxlength/1.
20
+ :- dynamic lastrule/1.
21
+ :- dynamic furthest_rule/1.
22
+ %%:- dynamic a/1.
23
+
24
+ /**
25
+
26
+ caw00(off,function3,[],5,7,[[[[a,1],[b,1]],[[c,2]],true],[[[a,1],[b,1]],[[c,2]],true],[[[a,1],[b,1]],[[c,1]],fail],[[[a,1],[b,1]],[[c,1]],fail]],[],Program),writeln(Program).
27
+
28
+ VarLists is in format list of [InputVarList,OutputVarList,Positivity], where these are specification lines that are either Positivity=true or fail
29
+
30
+ Notes:
31
+ - true specification line in first position of varlists, otherwise will try all except the specification line
32
+ - manually write a,a in algdict in a,b :- a,a,b because cawp would take too long finding all combinations including a,a
33
+ - give argument values in order 3 2 1 not 1 2 3 when adding, or will try 1+1+1 instead of 3 at first
34
+
35
+ **/
36
+
37
+ %%:- include('algdict.pl').
38
+ %%:- include('remove_duplicate_predicates.pl').
39
+
40
+ /**caw00a(Debug,PredicateName,Rules1,MaxLength,MaxPredicates,TotalVars,VarLists,Program1,Program2B) :-
41
+ (caw00(Debug,PredicateName,Rules1,MaxLength,MaxPredicates,TotalVars,VarLists,Program1,Program2B),%%writeln("If error, returned true");(furthest_rule([Number,Rules]),
42
+ writeln(["Error: No specification for predicate. Programs so far:",Rules,"\n\nPredicate number:",Number]).
43
+ **/
44
+
45
+ caw000(Debug,PredicateName,Rules,MaxLength,MaxPredicates,TotalVars,NumInputs,NumOutputs,VarLists,AlgDict,Program1,Program2A) :-
46
+ PredicateName=[PredicateName1],
47
+ MaxLength=[MaxLength1],
48
+ MaxPredicates=[MaxPredicates1],
49
+ TotalVars=[TotalVars1],
50
+ VarLists=[VarLists1],
51
+ %%AlgDict=[AlgDict1],
52
+ Program2A=[Program2A1],
53
+
54
+ caw00(Debug,PredicateName1,Rules,MaxLength1,MaxPredicates1,TotalVars1,NumInputs,NumOutputs,VarLists1,AlgDict,Program1,Program2A1),!.
55
+
56
+ caw000(Debug,PredicateName,Rules,MaxLength,MaxPredicates,TotalVars,NumInputs,NumOutputs,VarLists,AlgDict,Program1,Program2A) :-
57
+ PredicateName=[PredicateName1|PredicateName2],
58
+ MaxLength=[MaxLength1|MaxLength2],
59
+ MaxPredicates=[MaxPredicates1|MaxPredicates2],
60
+ TotalVars=[TotalVars1|TotalVars2],
61
+ VarLists=[VarLists1|VarLists2],
62
+ %%AlgDict=[AlgDict1|AlgDict2],
63
+ Program2A=[Program2A1|Program2A2],
64
+
65
+ caw00(Debug,PredicateName1,Rules,MaxLength1,MaxPredicates1,TotalVars1,NumInputs,NumOutputs,VarLists1,AlgDict,Program1,Program2A1),
66
+ %%trace,
67
+
68
+ findall(A,(member(B,Program2A1),findall(K,(member(F,NumInputs),member(G,NumOutputs),B=[[n,C],Q,D,E],K=[[[n,C],F,G],Q,D,E]),L),member(A,L)),J),
69
+ append(AlgDict,J,Program2AA4),
70
+ %%Program2AA3=[Program2AA4],
71
+ remvdup(Program2AA4,[],Program2A3),
72
+ %%trace,
73
+ caw000(Debug,PredicateName2,Rules,MaxLength2,MaxPredicates2,TotalVars2,NumInputs,NumOutputs,VarLists2,Program2A3,[],Program2A2).
74
+
75
+ caw00(Debug,PredicateName,Rules1,MaxLength,MaxPredicates,TotalVars,NumInputs,NumOutputs,VarLists,AlgDict,Program1,Program2B) :-
76
+ PredicatesA=AlgDict,
77
+ %% remove duplicate predicates
78
+ %%remvdup(PredicatesA0,[],PredicatesA),
79
+ split3(PredicatesA,[],Rules2),
80
+ split2(PredicatesA,[],Predicates),
81
+ %%writeln([Rules2,Predicates]),
82
+ append(Rules1,Rules2,Rules3),
83
+
84
+ %%retractall(a(_)),
85
+
86
+ retractall(debug(_)),
87
+ assertz(debug(Debug)),
88
+ retractall(totalvars(_)),
89
+ assertz(totalvars(TotalVars)),
90
+ retractall(maxlength(_)),
91
+ assertz(maxlength(MaxLength)),
92
+ retractall(lastrule(_)),
93
+ assertz(lastrule([])),
94
+ retractall(furthest_rule(_)),
95
+ assertz(furthest_rule([0,[]])),
96
+ %%retractall(newrulenumber(_)),
97
+ %%assertz(newrulenumber(0)),
98
+ retractall(numInputs(_)),
99
+ assertz(numInputs(NumInputs)),
100
+ retractall(numOutputs(_)),
101
+ assertz(numOutputs(NumOutputs)),
102
+
103
+ /**catch(call_with_time_limit(10,
104
+ **/caw01(VarLists,_,Predicates,PredicateName,Rules3,MaxLength,MaxPredicates,0,_,Program1,Program2B,_)
105
+
106
+ %%writeln1(Program2B)
107
+ /**),
108
+ time_limit_exceeded,
109
+ fail)
110
+ **/
111
+ .%%,not(length(Program2B,1)).
112
+
113
+ %%caw01([],_,_Predicates,_PredicateName,_Rules3,_MaxLength,_MaxPredicates,_New_rule_number1,_New_rule_number2,Program,Program) :- !. %% Recently added *****
114
+
115
+ caw01([],[],_Predicates,_PredicateName,_Rules3,_MaxLength,_MaxPredicates,_New_rule_number1,_New_rule_number2,_Program1,_Program2,_) :-
116
+ %%writeln(here1),
117
+ %%writeln1(["Error: No specification for predicate. Program so far:",Program1,"\n\nPredicate number:",New_rule_number]),abort,!.
118
+ fail,!.
119
+ caw01([[]],[[]],_Predicates,_PredicateName,_Rules3,_MaxLength,_MaxPredicates,_New_rule_number1,_New_rule_number2,Program,Program,_V3) :- %%writeln(here2),
120
+ fail.
121
+ %%caw01(_VarLists,_Predicates,_PredicateName,_Rules3,_MaxLength,MaxPredicates,New_rule_number,Program,Program) :- New_rule_number=<MaxPredicates,!.
122
+ caw01(VarLists,VarLists03,Predicates,PredicateName,Rules3,MaxLength,MaxPredicates,New_rule_number1,New_rule_number2,Program1,Program2B,_V) :-
123
+ %%writeln1(caw01(VarLists,_,Predicates,PredicateName,Rules3,MaxLength,MaxPredicates,New_rule_number1,New_rule_number2,Program1,Program2B)),
124
+ %%(Predicates=[[[n,add2],[[v,a],[v,b]],":-",[[[n,=],[[v,a],[]]],[[n,=],[[v,b],[]]]]],[[n,add3],[[v,a],[v,b]],":-",[[[n,tail],[[v,a],[v,b]]]]]]->trace;true),
125
+ VarLists=[VarLists0|VarLists02],
126
+ VarLists0=[VarLists1|VarLists2],
127
+
128
+ %%retractall(varlists(_)),
129
+ %%assertz(varlists(VarLists02)),
130
+
131
+ %%trace,
132
+ %%writeln1( findall(Program2A,caw0(Predicates,PredicateName,
133
+ %%Rules3,MaxLength,MaxPredicates,
134
+ %%VarLists1,VarLists02,New_rule_number1,New_rule_number2,Program1,Program2A),Program2)),
135
+ %%notrace,
136
+
137
+ findall([Program2A,VarLists041,New_rule_number2A],caw0(Predicates,PredicateName,
138
+ Rules3,MaxLength,MaxPredicates,
139
+ VarLists1,VarLists02,VarLists041,New_rule_number1,New_rule_number2A,Program1,Program2A,_V),Program2VarLists04),
140
+
141
+ findall(Program21,(member(MemberProgram2VarLists04,Program2VarLists04),MemberProgram2VarLists04=[Program21,_,_]),Program2),
142
+
143
+ findall(VarLists041,(member(MemberProgram2VarLists041,Program2VarLists04),MemberProgram2VarLists041=[_,VarLists041,_]),VarLists04),
144
+
145
+ findall(New_rule_number2A1,(member(MemberProgram2VarLists041New_rule_number2A1,Program2VarLists04),MemberProgram2VarLists041New_rule_number2A1=[_,_,New_rule_number2A1]),New_rule_number2A2),
146
+
147
+ %%trace,
148
+
149
+ %%writeln1( findall(Program2A,caw0(Predicates,PredicateName,
150
+ %%Rules3,MaxLength,MaxPredicates,
151
+ %%VarLists1,VarLists02,New_rule_number1,New_rule_number2,Program1,Program2A),Program2)),
152
+ %%notrace,
153
+ length(Program2,Program2L),
154
+ length(Program2LList,Program2L),
155
+ append(Program2LList,_,[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]),
156
+
157
+ member(Program2LListItem,Program2LList),
158
+ get_item_n(Program2,Program2LListItem,Program2B),
159
+ get_item_n(VarLists04,Program2LListItem,VarLists03),
160
+ get_item_n(New_rule_number2A2,Program2LListItem,New_rule_number2),
161
+ %%member(Program2B,Program2), %e->1
162
+ %%member(VarLists03,VarLists04), %e->1
163
+ %%writeln1(Program2B1),
164
+ %%writeln1(Program2B),
165
+ %%Program2B1=[Program2B],
166
+
167
+ %%trace,
168
+
169
+ %%writeln1(member(Program2B,Program2)),
170
+ %%notrace,
171
+
172
+
173
+ aggregate_all(count,(member(Item,VarLists2),
174
+ caw0(Predicates,PredicateName,Rules3,MaxLength,MaxPredicates,
175
+ Item,VarLists03,_VarLists031,New_rule_number1,_New_rule_number2A3,Program1,Program2B,_V2)),Count1),
176
+
177
+ %%trace,
178
+
179
+ %%writeln1( aggregate_all(count,(member(Item,VarLists2),
180
+ %%caw0(Predicates,PredicateName,Rules3,MaxLength,MaxPredicates,
181
+ %%Item,VarLists02,New_rule_number,Program1,Program2B)),Count)),
182
+ %%notrace,
183
+ %%writeln1(Program2),
184
+ %%trace,
185
+
186
+ %%writeln1(length(VarLists2,Count)),
187
+ %%notrace,
188
+
189
+ %%(findall(EVM1,(everyvarmentioned(Vars2,Program5),
190
+
191
+ length(VarLists2,Count2),
192
+ Count1>=Count2,
193
+
194
+ %%(Program2B=[[[n,1],[[v,a],[v,b],[v,c]],":-",[[[n,+],[[v,a],[v,b],[v,d]]],[[n,=],[[v,d],[v,c]]]]]]->true%%trace
195
+ %%;true),
196
+ !.%%!.%%!. %%Predicates->PredicatesA x
197
+
198
+ get_item_n(List1,N1,Item) :-
199
+ N2 is N1-1,
200
+ length(List3,N2),
201
+ append(List3,List4,List1),
202
+ List4=[Item|_].
203
+ caw0(Algorithms,PredicateName,Rules,MaxLength,MaxPredicates,VarLists,VarLists02,VarLists03,New_rule_number1,New_rule_number2,Program1,Program2,_V3) :-
204
+ VarLists=[InputVarList,OutputVarList,Positivity],
205
+ varnames(InputVarList,[],InputVars,[],InputValues),
206
+ varnames(OutputVarList,[],OutputVars,[],_OutputValues),
207
+ %%retractall(outputvars(_)),
208
+ %%assertz(outputvars(OutputVars)),
209
+ append(InputVars,OutputVars,Vars11),
210
+ %%Vars11=InputVars,
211
+ %%Vars12=InputVars,
212
+ append(InputValues,OutputVars,Vars2),
213
+ %%append(InputValues,OutputValues,Values),
214
+ Query=[[n,PredicateName],Vars2],
215
+ %%writeln( caw(Algorithms,Query,PredicateName,Rules,MaxLength,MaxPredicates,Vars11,InputVars,InputVars,_,OutputVarList,OutputVars,Positivity,VarLists02,New_rule_number1,New_rule_number2,Program1,Program2)),
216
+ caw(Algorithms,Query,PredicateName,Rules,MaxLength,MaxPredicates,Vars11,InputVars,InputVars,_,OutputVarList,OutputVars,Positivity,VarLists02,VarLists03,New_rule_number1,New_rule_number2,Program1,Program2,_V).
217
+ %%caw(_,_,_,_,_,N,_,_,_,_,_,_,_,_,_,_,N,P,P) :- fail,!. %% Turn off fail,! to have all solutions
218
+ /**caw(_Algorithms,_Query,_PredicateName,_Rules,_MaxLength,MaxPredicates,_Vars11,_InputVars,_InputVarsa,_InputVars2,_OutputVarList,_OutputVars,_Positivity,_VarLists02,New_rule_number1,New_rule_number2,_Program1,_Program11):-
219
+ %%writeln(caw_here(Algorithms,Query,PredicateName,Rules,MaxLength,MaxPredicates,Vars11,InputVars,InputVars,InputVars2,OutputVarList,OutputVars,Positivity,VarLists02,New_rule_number1,New_rule_number2,Program1,Program1)),
220
+ %%MaxPredicates1 is MaxPredicates-1,
221
+ writeln(["New_rule_number>MaxPredicates1",New_rule_number,">",MaxPredicates]),
222
+ New_rule_number>MaxPredicates,
223
+ fail,!.
224
+ **/
225
+ %%caw(_,_,_,_,_,_N,_,_,_,_,_,_,_,_,[],_,_N2?,P,P) :- !.
226
+ caw(_,_,_,_,0,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_F) :- fail, !. %% Turn off fail,! to have all solutions
227
+ caw(Algorithms1,Query,PredicateName,_Rules,_MaxLength,MaxPredicates,_VarList,InputVars1,InputVars2,_InputVarsa,VarLists,OutputVars,Positivity,VarLists02,VarLists02,New_rule_number1,New_rule_number1,Program1,Program2,_V) :-
228
+ %%writeln1(V),
229
+
230
+ %%MaxLength>0, ***
231
+
232
+ %%MaxPredicates1 is MaxPredicates,
233
+ New_rule_number1=<MaxPredicates,
234
+
235
+ %%trace,
236
+ addrules(InputVars2,OutputVars,OutputVars,[],_PenultimateVars,[],Program3),%%notrace,
237
+
238
+ %%writeln([addrules(InputVars2,OutputVars,OutputVars,[],PenultimateVars,[],Program3)]),
239
+ %%optimise(Program1,InputVars1,_InputVars3,PenultimateVars,Program4), %% IV2->3
240
+ %%writeln([optimise(Program1,InputVars1,InputVars3,PenultimateVars,Program4)]),
241
+ append(Program1,Program3,Program5),
242
+ not(Program5=[]),
243
+
244
+ append(InputVars1,OutputVars,Vars2),
245
+ Program22=[
246
+ [[n,PredicateName],Vars2,":-",
247
+ Program5
248
+ ]
249
+ ],
250
+ %%writeln1(interpret-short(Program22)),
251
+
252
+ %%everyvarmentioned(Vars2,Program5),
253
+ %%((PredicateName=1;PredicateName=2)->trace;true),
254
+ /**
255
+ not(Program22=[
256
+ [[n,add],_,_,
257
+ [[[n,+],_]|_]
258
+ ]
259
+ ]),
260
+ **/
261
+ %%(Program22=[[[n,1],[[v,a],[v,b],[v,c]],":-",[[[n,+],[[v,a],[v,b],[v,d]]],[[n,=],[[v,d],[v,c]]]]]],trace),
262
+
263
+ eliminate_unused_predicates(Program22,Algorithms1,Algorithms2),
264
+ %%writeln(eliminate_unused_predicates(Program22,Algorithms1,Algorithms2)),
265
+
266
+ %%Algorithms2=[[[n,_],_,_,Body]|_],length(Body,1),
267
+ %%(Program22=[[[n,function0],[[v,a],[v,b],[v,c]],":-",[[[n,function2],[[v,a],[v,b],[v,d]]],[[n,=],[[v,c],[v,d]]]]]]->writeln(eliminate_unused_predicates(Program22,Algorithms1,Algorithms2));true),
268
+
269
+ %%trace,
270
+ %%writeln(["1*",append(Algorithms2,Program22,Program2)]), %% ***** swapped a2,p22
271
+ %%[Program23]=Program22,
272
+ %%not(member(Program23,Algorithms2)),
273
+ append(Algorithms2,Program22,Program2), %% ***** swapped a2,p22
274
+ %%remvdup(Program2a,[],Program2),
275
+ %%Algorithms2=Program2,
276
+
277
+
278
+ %%not(Program2=[[[n,add0],[[v,a],[v,b],[v,c]],":-",[[[n,+],[[v,a],[v,b],[v,d]]],[[n,=],[[v,d],[v,c]]]]]]),
279
+ %%(Program2=[[[n,1],[[v,a],[v,b],[v,c]],":-",[[[n,+],[[v,a],[v,b],[v,d]]],[[n,=],[[v,d],[v,c]]]]]]->trace;true),
280
+
281
+ %%length(Program2,Program2L),not(Program2L=1),
282
+ %%not(New_rule_number=1),
283
+ %%=<MaxPredicates,
284
+ %%([off,[[n,add0],[[1,2,3],[v,b]]],[[[n,add2],[[v,a],[v,b]],":-",[[[n,=],[[v,a],[]]],[[n,=],[[v,b],[]]]]],[[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,add2],[[v,a],[v,d]]],[[n,=],[[v,d],[v,b]]]]]],[[[[v,b],[]]]]]=[Debug,Query,Program2,[VarLists]]->trace;true),
285
+
286
+ %%append_last_rule(Program2),
287
+
288
+ /**
289
+ length(Program2,Program2L),(Program2L>=2->(%%writeln(here2),trace
290
+ true);true),
291
+ **/
292
+ debug(Debug),
293
+
294
+ %%([off,[[n,add0],[[1,2,3],[v,b]]],[[[n,add2],[[v,a],[v,b]],":-",[[[n,=],[[v,a],[]]],[[n,=],[[v,b],[]]]]],[[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,add0],[[v,a],[v,d]]],[[n,=],[[v,d],[v,b]]]]]],[[[[v,b],[]]]]]=[Debug,Query,Program2,[VarLists]]->%%true
295
+ %%trace
296
+ %%;true),
297
+
298
+ %%([off,[[n,function3],[1,1,[v,c]]],[[[n,function1],[[v,a],[v,b],[v,c]],":-",[[[n,+],[[v,a],[v,b],[v,c]]]]],[[n,function3],[[v,a],[v,b],[v,c]],":-",[[[n,function1],[[v,a],[v,b],[v,d]]],[[n,=],[[v,d],[v,c]]]]]],[[[[v,c],2]]]]=[Debug,Query,Program2,[VarLists]]->true
299
+ %%trace
300
+ %%;true),
301
+
302
+ %%writeln1([V,"\n",Program2]),
303
+ %%writeln1(V),
304
+ %%writeln1([program2,Program2]),
305
+ %%writeln1([v,V]),
306
+
307
+ %% add 0s
308
+ %%(var(V)->true;(member([[n,add0],[[v,a],[v,b]],":-",[[[n,add3],[[v,a],[v,c]]],[[n,add0],[[v,c],[v,d]]],[[n,=],[[v,d],[v,b]]]]],V)->writeln1("v yes");true)),
309
+ %%**()(V=[[[n,add3],[[v,a],[v,b]],":-",[[[n,tail],[[v,a],[v,b]]]]],[[n,add0],[[v,a],[v,b]],":-",[[[n,add3],[[v,a],[v,c]]],[[n,add0],[[v,c],[v,d]]],[[n,=],[[v,d],[v,b]]]]]]->writeln1("v yes");true),
310
+
311
+ %%(var(Program2)->true;(member([[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,=],[[v,c],[v,b]]]]],Program2)->writeln1("p yes");true)),
312
+ %%**()(Program2=[[[n,add2],[[v,a],[v,b]],":-",[[[n,=],[[v,a],[]]],[[n,=],[[v,b],[]]]]],[[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,=],[[v,c],[v,b]]]]]]->writeln1("p yes");true),
313
+
314
+ %%vyes(Program2),
315
+
316
+ %%(((var(V)->true;(member([[n,add0],[[v,a],[v,b]],":-",[[[n,add3],[[v,a],[v,c]]],[[n,add0],[[v,c],[v,d]]],[[n,=],[[v,d],[v,b]]]]],V))),(var(Program2)->true;(member([[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,=],[[v,c],[v,b]]]]],Program2))))->trace;true),
317
+
318
+ %%**()((not(var(V)),V=[[[n,add3],[[v,a],[v,b]],":-",[[[n,tail],[[v,a],[v,b]]]]],[[n,add0],[[v,a],[v,b]],":-",[[[n,add3],[[v,a],[v,c]]],[[n,add0],[[v,c],[v,d]]],[[n,=],[[v,d],[v,b]]]]]],
319
+ %%Program2=[[[n,add2],[[v,a],[v,b]],":-",[[[n,=],[[v,a],[]]],[[n,=],[[v,b],[]]]]],[[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,=],[[v,c],[v,b]]]]]])->trace;true),
320
+ %%trace;
321
+ %%true),
322
+
323
+ %%writeln([program2,Program2]),
324
+ %%(length(Program5,4)->writeln([program2,Program2])),
325
+ %%(Program2=[[[n,add0],[[v,a],[v,b],[v,c],[v,d],[v,e]],":-",[[[n,+],[[v,a],[v,b],[v,f]]],[[n,+],[[v,c],[v,f],[v,g]]],[[n,+],[[v,d],[v,g],[v,h]]],[[n,=],[[v,h],[v,e]]]]]]->trace;true),
326
+ %%writeln(["Press c."]),(get_single_char(97)->true;true),
327
+
328
+ %%writeln1([interpret(Debug,Query,Program2,OutputVarList)]),
329
+
330
+ %writeln1(interpret(Debug,Query,Program2,VarLists)),
331
+
332
+
333
+ %%([Debug,Query,Program2,[VarLists]]=[off,[[n,add],[[],[1,2],[v,d]]],[[[n,add],[[v,a],[v,c],[v,d]],":-",[[[n,[]],[[v,a]]],[[n,=],[[v,c],[v,d]]]]]],[[[[v,d],[1,2]]]]]->trace;true),
334
+
335
+ %%([Debug,Query,Program2,[VarLists]]=[off,[[n,add0],[[1,2,3],[v,b]]],[[[n,add2],[[v,a],[v,b]],":-",[[[n,=],[[v,a],[]]],[[n,=],[[v,b],[]]]]],[[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,add2],[[v,a],[v,d]]],[[n,=],[[v,d],[v,b]]]]]],[[[[v,b],[]]]]]->trace;true),
336
+
337
+ %%(Program2=[[[n,add2],[[v,a],[v,b]],":-",[[[n,=],[[v,a],[]]],[[n,=],[[v,b],[]]]]],[[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,add2],[[v,a],[v,d]]],[[n,=],[[v,d],[v,b]]]]]]->trace;true),
338
+ %%interpret(Debug,Query,Program2,OutputVarList).
339
+ %%aggregate_all(count,(member(Item,VarLists),
340
+ %%varlists(VarLists02),
341
+ %%(
342
+
343
+ %%notrace,
344
+ try_interpret(Positivity,Debug,Query,Program2,VarLists),%%trace,%%writeln(here),
345
+ %%trace,
346
+ %%trace,
347
+ add_furthest_rule1(New_rule_number1,Program2),
348
+ %%(Program2=[[[n,add],[[v,a],[v,b],[v,c],[v,d]],":-",[[[n,=],[[v,c],[v,d]]]]]]->true%%trace
349
+ %%;true),
350
+ %%trace,
351
+ (no_singletons(Vars2,Program5)),
352
+
353
+ %%writeln1(Program2),
354
+ %%writeln1([cawptest,passed,Program2]),abort,
355
+ %%->true;(%%notrace,fail)),
356
+ %%writeln1(interpret(Debug,Query,Program2,[VarLists])),
357
+
358
+ %%(furthest_rule(A)->writeln(furthest_rule(A));true),%%notrace,
359
+ %%!.
360
+ !.%%!.
361
+ %%-> %% *** [VarLists] ?
362
+ /**(VarLists03=VarLists02,
363
+ %%retractall(varlists(_)),
364
+ %%assertz(varlists(VarLists03)),
365
+ Program2c=Program2);%%fail%%
366
+ (Program2c=[],VarLists03=VarLists02,%%append(VarLists,VarLists02,VarLists03),
367
+ retractall(varlists(_)),
368
+ assertz(varlists(VarLists03)),fail
369
+ )
370
+ ),!.%%),Count), **** [VarLists02] to VarLists02
371
+ %%length(OutputVarList,Count),!.
372
+ **/
373
+ /**
374
+
375
+ vyes(P):-%%((not(var(V)),
376
+ (member([[n,add0],[[v,a],[v,b]],":-",[[[n,add3],[[v,a],[v,c]]],[[n,add0],[[v,c],[v,d]]],[[n,=],[[v,d],[v,b]]]]],P),writeln("pyes*****")).%%,(var(Program2)->true;(member([[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,=],[[v,c],[v,b]]]]],Program2)))).
377
+ vyes(V):-%%((not(var(V)),
378
+ (member([[n,add0],[[v,a],[v,b]],":-",[[[n,add2],[[v,a],[v,c]]],[[n,=],[[v,c],[v,b]]]]],V)),writeln("vyes*****")%%,(var(Program2)->true;(member([],Program2)))).
379
+ .
380
+ vyes(V):-
381
+ true.
382
+ **/
383
+ caw(Algorithms,Query,PredicateName,Rules,MaxLength,MaxPredicates,VarList,InputVars1,InputVars2,InputVars3,VarLists,OutputVars,Positivity,VarLists02,VarLists03,New_rule_number1,New_rule_number2,Program1,Program4,_V) :-
384
+ %%writeln(here4),
385
+ %%trace,
386
+
387
+ %%writeln1(caw(Algorithms,Query,PredicateName,Rules,MaxLength,MaxPredicates,VarList,InputVars1,InputVars2,InputVars3,VarLists,OutputVars,Positivity,VarLists02,New_rule_number1,New_rule_number2,Program1,Program4)),
388
+ %%trace,
389
+ %%writeln([caw(Query,PredicateName,Rules,MaxLength,MaxPredicates,VarList,InputVars1,InputVars2,OutputVarList,OutputVars,Program1,Program4)]),?
390
+ MaxLength>=0, %%***
391
+ MaxLength2 is MaxLength - 1,
392
+ %%reverse(InputVars2,InputVars5),
393
+
394
+ %%writeln([new_rule_number,New_rule_number,maxPredicates,MaxPredicates]),
395
+ %%writeln(limit_reached(New_rule_number,MaxPredicates,Rules,PredicateName,InputVars1,OutputVars,Rules1)),
396
+ %%trace,
397
+ limit_reached(New_rule_number1,MaxPredicates,Rules,PredicateName,InputVars1,OutputVars,Rules1), %% *** Check these
398
+ %%writeln([rules1,Rules1]),
399
+ %%repeat,
400
+ %%writeln(limit_reached(New_rule_number,MaxPredicates,Rules,PredicateName,InputVars1,OutputVars,Rules1)),
401
+ %%get_char(_),
402
+ %%trace,
403
+
404
+
405
+ member([RuleName0,NumInputs0,NumOutputs0],Rules1),
406
+
407
+ %%[RuleName0,NumInputs0,NumOutputs0]=[other_new_branch,_,_],
408
+
409
+ %% **/
410
+ %%RuleName0=newrule123,
411
+ %%writeln([member([RuleName,NumInputs,NumOutputs],Rules)]),
412
+ %%writeln([rule(RuleName,NumInputs,NumOutputs,VarList,VarList2,Rule)]),
413
+
414
+
415
+ %%retractall(newrulenumber(_)),
416
+ %%assertz(newrulenumber(Newrulenumber1)),
417
+
418
+ %%[InputVars2,VarLists,Positivity]=[VarLists0311,VarLists0312,_VarLists0313], ***
419
+ %% **** InputVars1 or InputVars2?
420
+ %%length(VarLists0311,VarLists0311L),
421
+ %%length(VarLists0312,VarLists0312L),
422
+
423
+ %%***newbranchifcall(RuleName0,PredicateName,Itema),
424
+ numInputs(NumInputs1a),numOutputs(NumOutputs1a),
425
+ member(NumInputs1,NumInputs1a),%%[1,2,3]),%%,0,2,3]), %%*** 4
426
+ member(NumOutputs1,NumOutputs1a),%%[0,1]),%%,0,2,3]), %%*** 4
427
+
428
+ %%writeln([i,o,NumInputs1,NumOutputs1]),
429
+ %%*** caw x when creates a new pred, num extra clauses 0-2, doesn't add rule to pred, returns vl03 for (rules in) this pred
430
+ caw3(RuleName0,Algorithms,Algorithms2,NumInputs0,NumOutputs0,New_rule_number1,New_rule_number3,Rules,PredicateName,Program1,VarList,VarList2,InputVars2,InputVars4,OutputVars,Rule,NumInputs1,NumOutputs1,VarLists02,VarLists04,MaxPredicates,Rules2),
431
+
432
+
433
+ %%->true
434
+ %%;
435
+ %% InputVars5->InputVars2
436
+ %%writeln([rule(RuleName,NumInputs,NumOutputs,InputVars1,InputVars3,VarList,VarList2,Rule)]),
437
+ %%writeln(not(member(Rule,Program1))),
438
+ not(member(Rule,Program1)), %% Need to stop repeats of arity 1 calls
439
+ append(Program1,[Rule],Program3),
440
+ %%writeln([inputVars3,InputVars3]),
441
+ %%InputVars2=InputVars3,
442
+ %%writeln([program4,Program4]),
443
+
444
+ %%retractall(varlists(_)),
445
+ %%assertz(varlists(VarLists02)),
446
+ %%writeln1( caw(Algorithms2,Query,PredicateName,Rules2,MaxLength2,MaxPredicates,VarList2,InputVars1,InputVars4,InputVars3,VarLists,OutputVars,Positivity,VarLists02,New_rule_number3,New_rule_number2,Program3,Program4)),
447
+ caw(Algorithms2,Query,PredicateName,Rules2,MaxLength2,MaxPredicates,VarList2,InputVars1,InputVars4,InputVars3,VarLists,OutputVars,Positivity,VarLists04,VarLists03,New_rule_number3,New_rule_number2,Program3,Program4,_Algorithms2).
448
+
449
+ caw3(RuleName0,Algorithms,Algorithms2,NumInputs0,NumOutputs0,New_rule_number,New_rule_number1,Rules,PredicateName,Program1,VarList,VarList2,InputVars2,InputVars4,OutputVars,Rule,_NumInputs1,_NumOutputs1,VarLists02,VarLists02,_MaxPredicates,Rules2) :-
450
+
451
+ %% rules_existing
452
+ not(RuleName0=predicatename_new_branch),not(RuleName0=other_new_branch),not(RuleName0=[rules_new_branch,_]),
453
+ not(RuleName0=predicatename_existing),
454
+ %%trace,
455
+ find_rule_name(RuleName0,RuleName2),
456
+ Algorithms2=Algorithms,%%RuleName=RuleName0,
457
+ NumInputs=NumInputs0,NumOutputs=NumOutputs0,New_rule_number1 = New_rule_number,Rules2=Rules,
458
+
459
+ rulename_if_limit(RuleName2,PredicateName,RuleName),
460
+ rule(Program1,RuleName,NumInputs,NumOutputs,InputVars2,InputVars4,VarList,VarList2,OutputVars,Rule).
461
+
462
+
463
+ caw3(RuleName0,Algorithms,Algorithms2,NumInputs0,NumOutputs0,New_rule_number,New_rule_number1,Rules,PredicateName,Program1,VarList,VarList2,InputVars2,InputVars4,OutputVars,Rule,NumInputs1,NumOutputs1,VarLists02,VarLists03,MaxPredicates,Rules2) :-
464
+
465
+ %%Number_of_clauses2=1, %% Not tested nonrecursive multiclauses - use bottom up
466
+ member(Number_of_clauses1,[1]),%%,3,4]),
467
+ (Number_of_clauses1>=MaxPredicates->Number_of_clauses2=MaxPredicates;Number_of_clauses2=Number_of_clauses1),
468
+
469
+ create_multiple_nonrecursive_clauses(Number_of_clauses2,RuleName0,Algorithms,Algorithms2,NumInputs0,NumOutputs0,New_rule_number,New_rule_number1,Rules,PredicateName,Program1,VarList,VarList2,InputVars2,InputVars4,OutputVars,Rule,NumInputs1,NumOutputs1,VarLists02,VarLists03,MaxPredicates,Rules2).
470
+
471
+
472
+
473
+ create_multiple_nonrecursive_clauses(0,_RuleName0,Algorithms,Algorithms,_NumInputs0,_NumOutputs0,New_rule_number,New_rule_number,Rules,_PredicateName,_Program1,VarList,VarList,InputVars,InputVars,_OutputVars,_Rule, _NumInputs1,_NumOutputs1,VarLists02,VarLists02,_MaxPredicates,Rules) :-
474
+ !.
475
+
476
+ create_multiple_nonrecursive_clauses(Number_of_clauses1,RuleName0,Algorithms,Algorithms2,NumInputs0,NumOutputs0,New_rule_number1,New_rule_number3,Rules,PredicateName,Program1,VarList,VarList2,InputVars2,InputVars4,OutputVars,Rule,NumInputs1,NumOutputs1,VarLists02,VarLists03,MaxPredicates,Rules2) :-
477
+
478
+
479
+ caw4a(RuleName0,New_rule_number1,Rules,PredicateName,NumInputs0,NumOutputs0,NumInputs1,NumOutputs1,Rules3,New_rule_number4,RuleName),
480
+ %%writeln(before4b),
481
+ %%trace,
482
+ caw4b(VarLists02,VarLists04,Algorithms,New_rule_number5,MaxPredicates,New_rule_number4,Program1,RuleName,NumInputs1,NumOutputs1,InputVars2,InputVars5,VarList,VarList3,OutputVars,Rule,Algorithms3,Rules3),
483
+ %%trace,
484
+
485
+ Number_of_clauses2 is Number_of_clauses1-1,
486
+ create_multiple_nonrecursive_clauses(Number_of_clauses2,RuleName0,Algorithms3,Algorithms2,NumInputs0,NumOutputs0,New_rule_number5,New_rule_number3,Rules3,PredicateName,Program1,VarList3,VarList2,InputVars5,InputVars4,OutputVars,Rule,NumInputs1,NumOutputs1,VarLists04,VarLists03,MaxPredicates,Rules2).
487
+
488
+
489
+
490
+ caw4a(RuleName0,New_rule_number,Rules,_PredicateName,_NumInputs0,_NumOutputs0,NumInputs1,NumOutputs1,Rules2,New_rule_number1,RuleName) :-
491
+
492
+ RuleName0=other_new_branch, %% Command from Rules with new branch
493
+
494
+ %%trace,
495
+ New_rule_number1 is New_rule_number+1,
496
+ %%not(New_rule_number1=2),
497
+
498
+ RuleName=[n,New_rule_number1],
499
+
500
+ append(Rules,[[RuleName,NumInputs1,NumOutputs1]],Rules2)
501
+ .
502
+
503
+ caw4a(RuleName0,New_rule_number,Rules,PredicateName,NumInputs0,NumOutputs0,_NumInputs1,_NumOutputs1,Rules2,New_rule_number1,RuleName) :-
504
+ RuleName0=predicatename_new_branch, %% New branch with same name as current predicate
505
+ RuleName=[n,PredicateName],
506
+ New_rule_number1 is New_rule_number+1,
507
+ append(Rules,[[RuleName,NumInputs0,NumOutputs0]],Rules2)
508
+
509
+ .
510
+
511
+ caw4a(RuleName0,New_rule_number,Rules,_PredicateName,NumInputs0,NumOutputs0,_NumInputs1,_NumOutputs1,Rules2,New_rule_number1,RuleName) :-
512
+ RuleName0=[rules_new_branch,RuleName], %% New branch with same name as current predicate
513
+ %%trace,
514
+ %%RuleName=[n,RuleName1],
515
+ New_rule_number1 is New_rule_number+1,
516
+ append(Rules,[[RuleName,NumInputs0,NumOutputs0]],Rules2)
517
+ .
518
+
519
+ caw4a(RuleName0,New_rule_number,Rules,PredicateName,NumInputs0,NumOutputs0,_NumInputs1,_NumOutputs1,Rules2,New_rule_number1,RuleName) :-
520
+ RuleName0=predicatename_existing,
521
+ %%trace,
522
+ find_rule_name(RuleName0,RuleName2),
523
+ %%Algorithms2=Algorithms,
524
+ %%RuleName=RuleName0, *** not this
525
+ %%NumInputs1=NumInputs0,NumOutputs1=NumOutputs0,%% n i,o epsilon to 1
526
+ New_rule_number1 = New_rule_number,%%Rules2=Rules,
527
+
528
+ rulename_if_limit(RuleName2,PredicateName,RuleName),
529
+ append(Rules,[[RuleName,NumInputs0,NumOutputs0]],Rules2)
530
+ .
531
+ %%rule(Program1,RuleName,NumInputs,NumOutputs,InputVars2,InputVars4,VarList,VarList2,OutputVars,Rule).
532
+
533
+
534
+ caw4b(VarLists02,VarLists03,Algorithms,New_rule_number2,MaxPredicates,New_rule_number1,Program1,RuleName,NumInputs1,NumOutputs1,InputVars2,InputVars4,VarList,VarList2,OutputVars,Rule,Algorithms2,Rules2) :-
535
+ %%writeln1(append(Rules,[[RuleName,NumInputs0,NumOutputs0]],Rules2)),
536
+ %%MaxPredicates2 is MaxPredicates-1,
537
+
538
+ maxlength(MaxLength3),
539
+
540
+ %%writeln( caw01(VarLists02,Algorithms,New_rule_number1,New_rule_number2,Rules2,MaxLength3,MaxPredicates,New_rule_number1,[],Program2)),
541
+ %%writeln(here3),
542
+ %%trace,
543
+ %%writeln( RuleName=[_,RuleName1]),
544
+ RuleName=[_,RuleName1],
545
+ caw01(VarLists02,VarLists03,Algorithms,%%New_rule_number1
546
+ RuleName1
547
+ ,Rules2,MaxLength3,MaxPredicates,New_rule_number1,New_rule_number2,[],Program2,_Program12), %% *** VarLists02
548
+ %%writeln(here4),
549
+ %%to [VarLists02]
550
+ %%Program2=[[[[n,1],[[v,a],[v,b],[v,c]],":-",[[[n,+],[[v,a],[v,b],[v,d]]],[[n,=],[[v,d],[v,c]]]]]]],
551
+ %%writeln([caw01,Program2]),
552
+ %%trace,
553
+ rule(Program1,RuleName,NumInputs1,NumOutputs1,InputVars2,InputVars4,VarList,VarList2,OutputVars,Rule),
554
+ %%trace,
555
+ %%writeln(["2*",append(Program2,Algorithms,Algorithms2)]), %% *** swapped a,p2
556
+ %%[Program2a]=Program2,
557
+ %%not(member(Program2a,Algorithms)),
558
+ %append(Program2,Algorithms,Algorithms2) %% *** swapped a,p2
559
+ Program2=Algorithms2
560
+ .
561
+
562
+ find_rule_name(RuleName0,RuleName2) :-
563
+ RuleName0=[_,RuleName1],RuleName2=RuleName1.
564
+ find_rule_name(RuleName0,RuleName2) :-
565
+ not(RuleName0=[_,_RuleName1]),RuleName2=RuleName0.
566
+
567
+ try_interpret(Positivity,Debug,Query,Program2,VarLists) :-
568
+ Positivity=true,catch(call_with_time_limit(0.05,
569
+ international_interpret([lang,"en"],Debug,Query,Program2,[VarLists])),
570
+ time_limit_exceeded,
571
+ fail),!.
572
+
573
+ try_interpret(Positivity,Debug,Query,Program2,VarLists) :-
574
+ not(Positivity=true),catch(call_with_time_limit(0.05,
575
+ not(international_interpret([lang,"en"],Debug,Query,Program2,[VarLists]))),
576
+ time_limit_exceeded,
577
+ fail),!.
578
+
579
+ append_last_rule(Program2):-
580
+ lastrule(LastRule1),
581
+ append_last_rule1(Program2,LastRule1).
582
+
583
+ %%append_last_rule1(_Program2,LastRule1) :-
584
+ %% LastRule1=[],!.
585
+ append_last_rule1(Program2,LastRule1) :-
586
+ %% not(LastRule1=[]),
587
+ not(member(Program2,LastRule1)),
588
+ append(LastRule1,[Program2],LastRule2),
589
+ retractall(lastrule(_)),
590
+ assertz(lastrule(LastRule2)).
591
+
592
+ add_furthest_rule1(New_rule_number,Program1) :-
593
+ furthest_rule(Rule),Rule=[Number,Rules],
594
+ add_furthest_rule2(New_rule_number,Number,Rules,Program1).
595
+ /**add_furthest_rule2(New_rule_number,Number,_Rules,Program1) :-
596
+ New_rule_number>Number,
597
+ retractall(furthest_rule(_)),
598
+ assertz(furthest_rule([New_rule_number,[Program1]])),!.
599
+ **/
600
+ add_furthest_rule2(New_rule_number,_Number,Rules,Program1) :-
601
+ %%trace,
602
+ %%New_rule_number=Number,
603
+ retractall(furthest_rule(_)),
604
+ delete(Rules,Program1,Rules1),
605
+ append(Rules1,[Program1],Rules2),
606
+ assertz(furthest_rule([New_rule_number,Rules2])).
607
+ /**add_furthest_rule2(New_rule_number,Number,_Rules,_Program1) :-
608
+ New_rule_number<Number,!.
609
+ **/
610
+ rulename_if_limit(RuleName0,PredicateName,RuleName) :-
611
+ RuleName0=predicatename_existing,RuleName=[n,PredicateName],!.
612
+ rulename_if_limit(RuleName0,_PredicateName,RuleName) :-
613
+ not(RuleName0=predicatename_existing),RuleName=RuleName0.
614
+
615
+ /**
616
+ everyvarmentioned(Vars1,Program) :-
617
+ everyvarmentioned1(Vars1,Program).
618
+ %%underscore_occurs_once_per_var(Vars1,Program).
619
+
620
+ everyvarmentioned1([],_Program) :- !.
621
+ everyvarmentioned1(Vars1,Program) :-
622
+ Vars1=[Vars2|Vars3],
623
+
624
+ findall(Vars2,(member(C,Program),(
625
+ %%(C=[[n,"_"],[[v,b]]]->(true);true),
626
+ C=[_E,D],
627
+ member(Vars2,D)
628
+
629
+
630
+ %%Vars2=true
631
+ )),B),not(B=[]),
632
+ everyvarmentioned1(Vars3,Program).
633
+ **/
634
+
635
+ no_singletons(Vars1,Program):-
636
+ findall(DA,(member(C,Program),C=[_E,D],member(DA,D)),Vars2),
637
+ %%append_list(Vars2,Vars2A),
638
+ append(Vars1,Vars2,Vars3),
639
+ findall(Count1,(member(Item,Vars3),aggregate_all(count,(member(Item,Vars3)),Count1),
640
+ Count1=1),G),G=[].
641
+
642
+
643
+
644
+ /**
645
+ underscore_occurs_once_per_var([],_,_Program) :- !.
646
+ underscore_occurs_once_per_var(Vars1,Program) :-
647
+
648
+ findall(Var,(aggregate_all(count,(member(Var,Vars1),
649
+ member(C,Program),C=[[n,"_"],[Var]]),Count),Count=<1),List),
650
+ not(List=[]).
651
+ **/
652
+
653
+ limit_reached(New_rule_number,MaxPredicates,Rules0,PredicateName,InputVars1,OutputVars,Rules1) :-
654
+ New_rule_number<MaxPredicates,
655
+ length(InputVars1,InputVars1L),
656
+ length(OutputVars,OutputVarsL),
657
+ pred_already_in_list1(PredicateName,InputVars1L,OutputVarsL,Rules0,Rules),
658
+
659
+ append(Rules,[[predicatename_new_branch,InputVars1L,OutputVarsL], %% Uncommented for test 7, commented for test 7
660
+ [other_new_branch,_,_] %% commented for test 7
661
+ ],Rules1),!.
662
+ limit_reached(New_rule_number,MaxPredicates,Rules0,PredicateName,InputVars1,OutputVars,Rules1) :-
663
+ New_rule_number>=MaxPredicates,
664
+ length(InputVars1,InputVars1L),
665
+ length(OutputVars,OutputVarsL),
666
+ pred_already_in_list1(PredicateName,InputVars1L,OutputVarsL,Rules0,Rules1).
667
+
668
+ pred_already_in_list1(PredicateName,InputVars1L,OutputVarsL,Rules0,Rules) :-
669
+ pred_already_in_list2(PredicateName,InputVars1L,OutputVarsL,Rules0,Rules).
670
+
671
+ pred_already_in_list2(PredicateName,InputVars1L,OutputVarsL,Rules0,Rules) :-
672
+ member([[n,PredicateName],InputVars1L,OutputVarsL],Rules0),
673
+ apply_rules_existing_and_new_branch(Rules0,Rules),!.
674
+ pred_already_in_list2(PredicateName,InputVars1L,OutputVarsL,Rules0,Rules) :-
675
+ not(member([[n,PredicateName],InputVars1L,OutputVarsL],Rules0)),
676
+ apply_rules_existing_and_new_branch(Rules0,Rules01),
677
+ append(Rules01,[[predicatename_existing,InputVars1L,OutputVarsL] %% Uncommented for test 7
678
+ ],Rules).
679
+
680
+ apply_rules_existing_and_new_branch(Rules1,Rules2) :-
681
+ apply_rules_existing_and_new_branch2(Rules1,Rules3),
682
+ apply_rules_existing_and_new_branch3(Rules1,Rules4),
683
+ append(Rules3,Rules4,Rules2).
684
+
685
+ apply_rules_existing_and_new_branch2(Rules1,Rules2) :-
686
+ findall([A,C1,C2],(member(B,Rules1),B=[A1,C1,C2],member(D,[rules_existing
687
+ ]),A=[D,A1]),Rules2).
688
+
689
+ apply_rules_existing_and_new_branch3(Rules1,Rules2) :-
690
+ findall([A,C1,C2],(member(B,Rules1),B=[A1,C1,C2],member(D,[%%rules_new_branch
691
+ ]),A=[D,A1]),Rules2).
692
+
693
+ %%append(Rules,[[predicatename_existing,InputVars1L,OutputVarsL]%%%%,[other_existing,_,_]
694
+ %%],Rules1),!.
695
+
696
+ /**
697
+ newbranchifcall(RuleName0,PredicateName,Itema):-
698
+ RuleName0=[n,PredicateName],member(Itema,[useexisting,newbranch]).
699
+ newbranchifcall(RuleName0,PredicateName,Itema):-
700
+ not(RuleName0=[n,PredicateName]),Itema=useexisting.
701
+ **/
702
+
703
+ varnames([],Vars,Vars,Values,Values) :- !.
704
+ varnames(VarList,Vars1,Vars2,Values1,Values2) :-
705
+ VarList=[Var|Vars3],
706
+ Var=[VarName,Value],
707
+ append(Vars1,[VarName],Vars4),
708
+ append(Values1,[Value],Values3),
709
+ varnames(Vars3,Vars4,Vars2,Values3,Values2),!.
710
+
711
+ /**
712
+ %%addrules(_,_,[],PV,PV,Program,Program) :- !.
713
+ addrules(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program3) :-
714
+ writeln1(addrules(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program3)),
715
+ %%trace,
716
+ OutputVars2=[],%%[OutputVar|OutputVars3],
717
+ member(Var,VarList),
718
+ %%member(OutputVar,OutputVars1),%%()
719
+ append(VarList,OutputVars1,OutputVars4),
720
+ member(Var2,OutputVars4),
721
+ append(Program1,[[[n,=],[Var,Var2%%OutputVar
722
+ ]]],Program3),
723
+ append(PenultimateVars1,[Var],PenultimateVars2).
724
+
725
+ **/
726
+ /**
727
+ %%addrules(VarList,OutputVars1,OutputVars3,PenultimateVars3,PenultimateVars2,Program3,Program2).
728
+ addrules(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2) :-
729
+ %%trace,
730
+ OutputVars2=[OutputVar|OutputVars3],
731
+ member(Var,VarList),
732
+ member(OutputVar,OutputVars1),%%()
733
+ append(VarList,OutputVars1,OutputVars4),
734
+ member(Var2,OutputVars4),
735
+ append(Program1,[[[n,=],[Var,Var2%%OutputVar
736
+ ]]],Program3),
737
+ append(PenultimateVars1,[Var],PenultimateVars3),
738
+ addrules(VarList,OutputVars1,OutputVars3,PenultimateVars3,PenultimateVars2,Program3,Program2).
739
+ **/
740
+ addrules(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2) :-
741
+ find_addrules_outputvars(OutputVars1,OutputVars3),
742
+ addrules1(OutputVars3,VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2).%%;(
743
+
744
+ addrules1([],_VarList,_OutputVars1,_OutputVars2,PenultimateVars,PenultimateVars,Program,Program) :- !.
745
+ addrules1(OutputVars3,VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2) :-
746
+ OutputVars3=[_OutputVars31|OutputVars32],
747
+ addrules2(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars3,Program1,Program3),
748
+ addrules1(OutputVars32,VarList,OutputVars1,OutputVars2,PenultimateVars3,PenultimateVars2,Program3,Program2).
749
+
750
+ addrules2([],_,_,PV,PV,Program,Program) :- !.
751
+ addrules2([[v,_]],_,_,PV,PV,Program,Program) :- !.
752
+ addrules2(VarList,OutputVars1,_OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program3) :- %%*** Underscored OutputVars2
753
+ %%writeln1(addrules(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program3)),
754
+ %%trace,
755
+ %%OutputVars2=[],%%[OutputVar|OutputVars3],
756
+ member(Var,VarList),
757
+ %%member(OutputVar,OutputVars1),%%()
758
+ append(VarList,OutputVars1,OutputVars4),
759
+ member(Var2,OutputVars4),
760
+ not(Var=Var2),
761
+ append(Program1,[[[n,=],[Var,Var2%%OutputVar
762
+ ]]],Program3),
763
+ append(PenultimateVars1,[Var],PenultimateVars2).
764
+
765
+ find_addrules_outputvars(OutputVars1,OutputVars3) :-
766
+ OutputVars1=[],OutputVars3=[_],!.
767
+ find_addrules_outputvars(OutputVars1,OutputVars3) :-
768
+ not(OutputVars1=[]),OutputVars3=OutputVars1.
769
+ %% optimise([[append,[a,a,d]],[append,[a,a,e]],[append,[a,a,f]],[append,[a,b,g]]],[g],P).
770
+ /****
771
+ optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program2) :-
772
+ findrulesflowingtopv1(Program1,InputVars1,InputVars2,PenultimateVars,[],Rules,true),
773
+ %%findrulesflowingtopv1a(Program1,_Program32,InputVars1,InputVars2,PenultimateVars,[],_Rules1),
774
+ intersection(Program1,Rules,Program3),
775
+ unique1(Program3,[],Program2).
776
+ findrulesflowingtopv1(_,_,_,[],Rules,Rules,false).
777
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
778
+ (Var=[v,_]),%%***;length(Var,1)),
779
+ findrulesflowingtopv20(Program0,Program0,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1).
780
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Vars1,Rules1,Rules2,IV1Flag1) :-
781
+ Vars1=[Var|Vars2],
782
+ findrulesflowingtopv20(Program0,Program0,InputVars1,InputVars2,Var,Rules1,Rules3,IV1Flag2),
783
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Vars2,Rules3,Rules2,IV1Flag3),
784
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
785
+
786
+ %%findrulesflowingtopv2([],Program,Program,_,_,Rules,Rules).
787
+ findrulesflowingtopv20(_,[],_InputVars1,_InputVars2,_Var,Rules,Rules,false).
788
+ findrulesflowingtopv20(Program0,Rules4,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
789
+ Rules4=[Rule|Rules],
790
+ (findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules3,IV1Flag2)->true;(Rules3=Rules1,IV1Flag2=false)),
791
+ %%delete(Program0,Rule,Program1),
792
+ findrulesflowingtopv20(Program0,Rules,InputVars1,InputVars2,Var,Rules3,Rules2,IV1Flag3),%%p1->0
793
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
794
+ %%findrulesflowingtopv2(_,[],[],_,_,_,Rules,Rules).
795
+ findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
796
+ Rule=[[n,_PredicateName],Vars],
797
+ restlast(Vars,[],Rest,Var),
798
+ %%delete(Program1,[PredicateName,Vars],Program2),
799
+ %%Program2=Program1,
800
+ %%(not(intersection(Rulesx,Rules1))-> x
801
+ %% append, append, unique1
802
+ %%append(Rules1,[Rule],Rules3);Rules3=Rules1),
803
+
804
+ %%member(Var2,Rest),
805
+ %%member(Var2,InputVars1),
806
+
807
+ length(Rest,Length1), Length1>=1,
808
+ subtract(Rest,InputVars1,IV3s),
809
+ length(IV3s,Length3),
810
+ subtract(Rest,IV3s,IV1s),
811
+ length(IV1s,Length2), Length2>=1,
812
+ subtract(IV3s,InputVars2,[]),
813
+
814
+ IV1Flag2=true,
815
+
816
+ %%delete(Program0,Rule,Program1),
817
+
818
+ %%(delete(Program0,Rule,Program3),
819
+ %%iv3s1(IV3s,Program3,IV3s,[]),
820
+ (Length3>=1->
821
+ (findrulesflowingtopv1(Program0,InputVars1,InputVars2,IV3s,[],Rules5,IV1Flag3),not(Rules5=[]));
822
+ (Rules5=[],IV1Flag3=false)),
823
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag4),
824
+ %%->true; Rules5=[],IV1Flag1=IV1Flag4),
825
+
826
+ ((findrulesflowingtopv1(Program0,InputVars1,InputVars2,IV1s,[],Rules6,IV1Flag5), %%iv1s->rest, etc
827
+ iv1flagdisjunction(IV1Flag4,IV1Flag5,IV1Flag1))->true;(Rules6=[],IV1Flag1=IV1Flag4)),
828
+
829
+ append([Rule],Rules1,Rules9),
830
+ append(Rules9,Rules5,Rules7),
831
+ append(Rules7,Rules6,Rules8),
832
+ unique1(Rules8,[],Rules2).
833
+
834
+
835
+ **
836
+ findrulesflowingtopv2(_Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
837
+ Rule=[_PredicateName,Vars],
838
+ restlast(Vars,[],Rest,Var),
839
+ %%delete(Program1,[PredicateName,Vars],Program2),
840
+ %%Program2=Program1,
841
+ (not(member(Rule,Rules1))->
842
+ append(Rules1,[Rule],Rules2);Rules2=Rules1),
843
+ subset(Rest,InputVars2),
844
+
845
+ intersection(Rest,InputVars1,Intersection),
846
+ length(Intersection,0),
847
+
848
+ %% not((member(Var2,Rest),
849
+ %% member(Var2,InputVars1))),
850
+
851
+ IV1Flag1=false.
852
+ **
853
+ **
854
+ findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
855
+ Rule=[_PredicateName,Vars],
856
+ restlast(Vars,[],Rest,Var),
857
+ %%delete(Program1,[PredicateName,Vars],Program3),
858
+ %%Program3=Program1,
859
+ %%append(Rules1,[Rule],Rules3),
860
+ subset(Rest,InputVars2),
861
+
862
+ intersection(Rest,InputVars1,Intersection),
863
+ length(Intersection,0),
864
+
865
+ %% not((member(Var2,Rest),
866
+ %% member(Var2,InputVars1))),
867
+
868
+ %% delete(Program0,Rule,Program1),
869
+
870
+ IV1Flag2=false,
871
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Rest,[],Rules4,IV1Flag3),
872
+ %%not(Rules4=[]),
873
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1),
874
+
875
+ append(Rules1,[Rule],Rules7),
876
+ append(Rules7,Rules4,Rules8),
877
+ unique1(Rules8,[],Rules2).
878
+ **
879
+ **
880
+ %%->true;(Program2=Program1,Rules2=Rules1)).
881
+ findrulesflowingtopv2(Rule,Program0,Program1,_Program2,InputVars1,InputVars,Var,Rules1,Rules2,IV1Flag1) :-
882
+ Rule=[PredicateName,Vars],
883
+ restlast(Vars,[],Rest,Var),
884
+ %%delete(Program1,[PredicateName,Vars],Program4),
885
+ %%Program4=Program1,
886
+ append(Rules1,[[PredicateName,Vars]],Rules3),
887
+ findrulesflowingtopv1(Program0,Program1,_Program2,InputVars1,InputVars,Rest,Rules3,Rules2,IV1Flag3),
888
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
889
+
890
+ %%findrulesflowingtopv2(Program5,Program2,Rest,Rules3,Rules2).
891
+
892
+ **
893
+ iv1flagdisjunction(A,B,true) :-
894
+ (A=true); (B=true).
895
+ iv1flagdisjunction(_,_,false).
896
+ **
897
+ iv3s0([],_,IV3s1,IV3s2).
898
+ iv3s0(IV3s,Program0,IV3s1,IV3s2).
899
+ IV3s=[IV3|IV3s3],
900
+ iv3s1(IV3,Program0,IV3s1,IV3s4),
901
+ iv3s0(IV3s3,Program0,IV3s4,IV3s2).
902
+ iv3s1(_,[],IV3s,IV3s).
903
+ iv3s1(IV3,Program0,IV3s1,IV3s2) :-
904
+ Program0=[Rule|Rules],
905
+ iv3s2(IV3,Rule,IV3s1,IV3s3),
906
+ iv3s1(IV3,Rules,IV3s3,IV3s2).
907
+ iv3s2(IV3,Rule,IV3s,IV3s1,IV3s2).
908
+ Rule=[_PredicateName,Vars],
909
+ restlast(Vars,[],_Rest,IV3),
910
+ delete(IV3s1,IV3,IV3s2).
911
+
912
+
913
+ findrulesflowingtopv1a(_,_,_,_,[],Rules,Rules).
914
+ findrulesflowingtopv1a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2) :-
915
+ atom(Var),
916
+ findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2).
917
+ findrulesflowingtopv1a(Program1,Program2,InputVars1,InputVars2,Vars1,Rules1,Rules2) :-
918
+ Vars1=[Var|Vars2],
919
+ findrulesflowingtopv2(Program1,Program3,InputVars1,InputVars2,Var,Rules1,Rules3),
920
+ findrulesflowingtopv1a(Program3,Program2,InputVars1,InputVars2,Vars2,Rules3,Rules2).
921
+ %%findrulesflowingtopv2([],Program,Program,_,_,Rules,Rules).
922
+ findrulesflowingtopv2a([],[],_,_,_,Rules,Rules).
923
+ findrulesflowingtopv2a(Program1,Program2,_InputVars1,InputVars2,Var,Rules1,Rules2) :-
924
+ member([PredicateName,Vars],Program1),
925
+ restlast(Vars,[],Rest,Var),
926
+ (
927
+ %%delete(Program1,[PredicateName,Vars],Program2),
928
+ Program2=Program1,
929
+ append(Rules1,[[PredicateName,Vars]],Rules2),
930
+ subset(Rest,InputVars2)).
931
+ findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2) :-
932
+ member([PredicateName,Vars],Program1),
933
+ restlast(Vars,[],Rest,Var),
934
+ (
935
+ %%delete(Program1,[PredicateName,Vars],Program3),
936
+ Program3=Program1,
937
+ append(Rules1,[[PredicateName,Vars]],Rules3),
938
+ subset(Rest,InputVars2)),
939
+ findrulesflowingtopv1a(Program3,Program2,InputVars1,InputVars2,Rest,Rules3,Rules2).
940
+
941
+ %%->true;(Program2=Program1,Rules2=Rules1)).
942
+ findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars,Var,Rules1,Rules2) :-
943
+ member([PredicateName,Vars],Program1),
944
+ restlast(Vars,[],Rest,Var),
945
+ %%delete(Program1,[PredicateName,Vars],Program4),
946
+ Program4=Program1,
947
+ append(Rules1,[[PredicateName,Vars]],Rules3),
948
+ findrulesflowingtopv1a(Program4,Program2,InputVars1,InputVars,Rest,Rules3,Rules2).
949
+ %%findrulesflowingtopv2(Program5,Program2,Rest,Rules3,Rules2).
950
+ **
951
+ **/
952
+ restlast([],_,_,_) :- fail, !.
953
+ restlast([Last],Rest,Rest,Last) :-
954
+ Last=[v,_],!.
955
+ restlast(Last,Rest,Rest,Last) :-
956
+ length(Last,1),!.
957
+ restlast(Vars1,Rest1,Rest2,Last) :-
958
+ Vars1=[Var|Vars2],
959
+ append(Rest1,[Var],Rest3),
960
+ restlast(Vars2,Rest3,Rest2,Last),!.
961
+
962
+
963
+ rule(Program1,RuleName,InModes,OutModes,InputVars1,InputVars2,VarList,VarList2,OutputVars,Rule) :-
964
+
965
+ /*
966
+ findall(Rule_vars1,member([_Rule_name,Rule_vars1],Program1),Rule_vars2),foldr(append,Rule_vars2,Rule_vars3),
967
+
968
+ % count vars
969
+ sort(Rule_vars3,K),
970
+ findall(G,(member(G,K),findall(G,member(G,Rule_vars3),H),length(H,J),J>2),L),
971
+ % remove vars occuring more than twice
972
+ %(not(InputVars10=[])->trace;true),
973
+ (var(InputVars10)->InputVars10=InputVars1;subtract(InputVars10,L,InputVars1)),
974
+ (var(VarList0)->VarList0=VarList;subtract(VarList0,L,VarList)),
975
+ %notrace,
976
+ */
977
+ %trace,
978
+ %writeln1(rule(Program1,RuleName,InModes,OutModes,InputVars1,InputVars2,VarList,VarList2,OutputVars,Rule)),
979
+ %notrace,
980
+
981
+ rule1(RuleName,InModes,OutModes,InputVars1,InputVars2a,VarList,VarList2a,OutputVars,Rule1),
982
+ ((1 is InModes+OutModes,member(Rule1,Program1))->(InputVars1=[_|InputVars3],rule(Program1,RuleName,InModes,OutModes,InputVars3,InputVars2,VarList,VarList2,OutputVars,Rule2),Rule=Rule2);(%%InputVars1=InputVars3, *** Commented out
983
+ Rule=Rule1,InputVars2=InputVars2a,VarList2a=VarList2))
984
+ %%writeln1(Rule),(Rule=[[n,-],[[v,c],[v,c],[v,d]]]->true;true)
985
+ .%%->writeln(rule(RuleName,InModes,OutModes,InputVars1,InputVars2,VarList,VarList2,OutputVars,Rule));true).%%(writeln(stop),abort)).
986
+ %%.
987
+ rule1(RuleName,InModes,OutModes,InputVars1,InputVars2,VarList,VarList2,OutputVars,Rule) :-
988
+ %%writeln(rule(RuleName,InModes,OutModes,InputVars1,InputVars2,VarList,VarList2,OutputVars,Rule)),
989
+ get_members(InModes,InputVars1,[],Vars1),
990
+ rulea(OutModes,RuleName,Vars1,VarList,VarList2,
991
+ Rule,OutputVars,Vars2),
992
+ (num_modes(OutModes,Last,InputVars1),
993
+ (equals_or(Vars1,Last)->true;equals_or(Vars2,Last))),
994
+ append(InputVars1,Vars2,InputVars2)
995
+ %%retractall(lastrule(_)),
996
+ %%assertz(lastrule(Rule)),
997
+ .
998
+
999
+ num_modes(OutModes,Last,_InputVars1) :-
1000
+ OutModes=0,Last=[],!.
1001
+ num_modes(OutModes,Last,InputVars1) :-
1002
+ not(OutModes=0),restlast(InputVars1,[],_,Last),!.
1003
+
1004
+ member_functions(Rule,Algorithms,Item) :-
1005
+ member(Item,Algorithms),
1006
+ Item=[Rule|_Rest].
1007
+
1008
+ %%get_members(0,_,Vars,Vars):-true.
1009
+ get_members(_,[],Vars,Vars).
1010
+ get_members(InModes1,InputVars,Vars1,Vars2) :-
1011
+ (not(InModes1=0)->(
1012
+ %%findnsols(InModes1,A,(member(A,InputVars)),Vars2).
1013
+ %%length(Vars2,InModes1),append(Vars2,_,InputVars).
1014
+ member(Var,InputVars),
1015
+ %%InputVars=[Var|InputVars2],
1016
+ %%delete(InputVars,Var,InputVars2),
1017
+ append(Vars1,[Var],Vars3),
1018
+ InModes2 is InModes1-1,
1019
+ get_members(InModes2,InputVars,Vars3,Vars2));
1020
+ Vars1=Vars2). %%InModes2->1
1021
+
1022
+ equals_or([],_) :- true,!.%%fail.***
1023
+ equals_or(List,Item) :-
1024
+ List=[Item|_Rest],!.
1025
+ equals_or(List,Item1) :-
1026
+ List=[Item2|Rest],
1027
+ not(Item1=Item2),
1028
+ equals_or(Rest,Item1),!.
1029
+
1030
+ rulea(OutModes,RuleName,Vars1,VarList,VarList3,Rule,OutputVars,Vars2) :-
1031
+ get_vars(OutModes,VarList,VarList3,OutputVars,[],Vars2),
1032
+ append(Vars1,Vars2,Vars3),
1033
+ Rule=[RuleName,Vars3],!.
1034
+
1035
+ get_vars(0,VarList,VarList,_,Vars,Vars) :- !.
1036
+ get_vars(OutModes1,VarList1,VarList2,OutputVars,Vars1,Vars2) :-
1037
+ var(VarList1,Var,VarList3,OutputVars),
1038
+ append(Vars1,[Var],Vars3),
1039
+ OutModes2 is OutModes1 - 1,
1040
+ get_vars(OutModes2,VarList3,VarList2,OutputVars,Vars3,Vars2),!.
1041
+
1042
+ /**
1043
+
1044
+ rule(RuleName,1,1,InputVars1,InputVars2,VarList,VarList2,Rule) :-
1045
+ member(Var,InputVars1),
1046
+ rule2(RuleName,Var,VarList,VarList2,Rule,Var1),
1047
+ append(InputVars1,[Var1],InputVars2).
1048
+ rule2(RuleName,Var,VarList,VarList2,Rule,Var1) :-
1049
+ var(VarList,Var1,VarList2),
1050
+ Rule=[RuleName,[Var,Var1]],!.
1051
+
1052
+ rule(RuleName,1,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-
1053
+ member(Var,InputVars1),
1054
+ rule3(RuleName,Var,VarList,VarList2,Rule,Vars),
1055
+ append(InputVars1,Vars,InputVars2).
1056
+ rule3(RuleName,Var,VarList,VarList3,Rule,[Var1,Var2]) :-
1057
+ var(VarList,Var1,VarList2),
1058
+ var(VarList2,Var2,VarList3),
1059
+ Rule=[RuleName,[Var,Var1,Var2]],!.
1060
+
1061
+ rule(RuleName,2,1,InputVars1,InputVars2,VarList,VarList2,Rule) :-
1062
+ member(Var,InputVars1),
1063
+ member(Vara,InputVars1),
1064
+ rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1),
1065
+ append(InputVars1,[Var1],InputVars2).
1066
+ rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1) :-
1067
+ var(VarList,Var1,VarList2),
1068
+ Rule=[RuleName,[Var,Vara,Var1]],!.
1069
+
1070
+ rule(RuleName,2,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-
1071
+ member(Var,InputVars),
1072
+ member(Vara,InputVars),
1073
+ rule5(RuleName,Var,Vara,VarList,VarList2,Rule,Vars),
1074
+ append(InputVars1,Vars,InputVars2).
1075
+ rule5(RuleName,Var,Vara,VarList,VarList3,Rule,[Var1,Var2]) :-
1076
+ var(VarList,Var1,VarList2),
1077
+ var(VarList2,Var2,VarList3),
1078
+ Rule=[RuleName,[Var,Vara,Var1,Var2]],!.
1079
+
1080
+ **/
1081
+
1082
+ %%var(Item,Var,Vars,Vars) :-
1083
+ %% member([Item,Var],Vars).
1084
+ var(Vars1,Var1,Vars2,OutputVars) :-
1085
+ length(Vars1,Vars1Length1),
1086
+ Vars1Length2 is Vars1Length1-1,
1087
+ length(Vars3,Vars1Length2),
1088
+ append(Vars3,[Var2A],Vars1),
1089
+ Var2A=[v,Var2],
1090
+ char_code(Var2,Var2Code1),
1091
+ Var2Code2 is Var2Code1 + 1,
1092
+ var2(Var2Code2,Var1A,OutputVars),
1093
+ Var1=[v,Var1A],
1094
+ append(Vars1,[Var1],Vars2),!.
1095
+
1096
+ var2(Code,Var1,OutputVars) :-
1097
+ %%outputvars(OutputVars),
1098
+ totalvars(TotalVars),
1099
+ Code2 is 96+TotalVars,
1100
+ Code =< Code2, %% 122
1101
+ char_code(Var1,Code),
1102
+ not(member(Var1,OutputVars)),!.
1103
+ var2(Var2Code,Code3,OutputVars) :-
1104
+ Var2Code2 is Var2Code + 1,
1105
+ totalvars(TotalVars),
1106
+ Code2 is 96+TotalVars,
1107
+ Var2Code2 =< Code2,
1108
+ var2(Var2Code2,Code3,OutputVars),!.
1109
+ /**
1110
+ algorithmstopredicates1([],Predicates1,Predicates1) :-!.
1111
+ algorithmstopredicates1(Algorithms1,Predicates1,Predicates2) :-
1112
+ Algorithms1=[Algorithm1|Algorithms2],
1113
+ Algorithm1=[_TestNumber,_Queries,Algorithm3],
1114
+ algorithmstopredicates2(Algorithm3,[],Algorithm4),
1115
+ append_list(Predicates1,Algorithm4,Predicates4),
1116
+ algorithmstopredicates1(Algorithms2,Predicates4,Predicates2).
1117
+ algorithmstopredicates2([],Predicates1,Predicates1) :- !.
1118
+ algorithmstopredicates2(Algorithms1,Predicates1,Predicates2) :-
1119
+ Algorithms1=[Algorithm1|Algorithms2],
1120
+ Algorithm1=[Name,In,Out|Rest],
1121
+ append(Predicates1,[[Name,In,Out|Rest]],Predicates4),
1122
+ algorithmstopredicates2(Algorithms2,Predicates4,
1123
+ Predicates2).
1124
+ **/
1125
+ split3([],List,List) :- !.
1126
+ split3(Predicates1,List1,List2) :-
1127
+ Predicates1=[Item1|List4],
1128
+ Item1= [[[n,Name],In,Out]|_Rest],
1129
+ append(List1,[[[n,Name],In,Out]],List6),
1130
+ split3(List4,List6,List2),!.
1131
+
1132
+ split2([],List,List) :- !.
1133
+ split2(Predicates1,List1,List2) :-
1134
+ Predicates1=[Item1|List4],
1135
+ Item1= [[[n,Name],_In,_Out]|Rest],
1136
+ append(List1,[[[n,Name]|Rest]],List6),
1137
+ split2(List4,List6,List2),!.
1138
+
1139
+ /**
1140
+ split2([],List,List) :- !.
1141
+ split2(Predicates1,List1,List2) :-
1142
+ Predicates1=[Item1|List4],
1143
+ Item1=[[n,[Name,[[test,Test1],[numin,Numin],
1144
+ [numout,Numout]]]]|Rest],
1145
+ member([[n,[Name,[[test,_Test3],[numin,Numin],
1146
+ [numout,Numout]]]]|Rest],List4),
1147
+ delete(List4,[[n,[Name,[[test,_Test4],[numin,Numin],
1148
+ [numout,Numout]]]]|Rest],
1149
+ List7),
1150
+ append(List1,[[[n,[Name,[[test,Test1],[numin,Numin],
1151
+ [numout,Numout]]]]|Rest]],List6),
1152
+ split2(List7,List6,List2),!.
1153
+ split2(Predicates1,List1,List2) :-
1154
+ Predicates1=[Item1|List4],
1155
+ Item1=[[n,[Name,[[test,Test1],[numin,Numin],
1156
+ [numout,Numout]]]]|Rest],
1157
+ append(List1,[[[n,[Name,[[test,Test1],[numin,Numin],
1158
+ [numout,Numout]]]]|Rest]],List6),
1159
+ split2(List4,List6,List2),!.
1160
+
1161
+ append_list(A,[],A):-!.
1162
+ append_list(A,List,B) :-
1163
+ List=[Item|Items],
1164
+ append(A,[Item],C),
1165
+ append_list(C,Items,B).
1166
+
1167
+ **/
1168
+
1169
+ eliminate_unused_predicates(Program1a,Algorithms1a,Algorithms2) :-
1170
+ %% System calls and mode arities
1171
+ %%System_calls=[[is,1,1],[+,2,1],[=,2,1],[wrap,1,1],
1172
+ %%[unwrap,1,1],[head,1,1],[tail,1,1],[member,1,1],
1173
+ %%[delete,2,1],[append,2,1]], %% Ignore whether system calls are in Program and Algorithm - the interpreter will have detected whether system and user predicates clash earlier
1174
+
1175
+ Program1a=[[[n, PredicateName], Arguments, ":-", _Body]],
1176
+ length(Arguments,ArgumentsLength),
1177
+ Start=[[[n,PredicateName],ArgumentsLength]],
1178
+ convert_to_grammar_part1(Program1a,[],_Program1b,Program1),
1179
+ %% Find calls in Program
1180
+ %%writeln([program1,Program1]),
1181
+ find_calls1(Start,Program1,[],Program2),
1182
+ %%writeln([program2,Program2]),
1183
+ %% Find calls in Algorithm
1184
+ convert_to_grammar_part1(Algorithms1a,[],_Algorithms1b,Algorithms1),
1185
+ %%writeln([algorithms1,Algorithms1]),
1186
+ find_calls1(Program2,Algorithms1,[],Algorithms3),
1187
+ %%writeln([algorithms3,Algorithms3]),
1188
+ append(Program2,Algorithms3,Rules),
1189
+ %% Eliminate user predicates mentioned in Program and Algorithms in Algorithms
1190
+ eliminate_unused_predicates1(Rules,Algorithms1,[],
1191
+ Algorithms2).
1192
+
1193
+ find_calls1(_,[],Program,Program) :- !.
1194
+ find_calls1(Program0,Program1,Program2,Program3) :-
1195
+ Program1=[[_Program4a,Program4]|Program5],
1196
+ %% The first predicate in Program4 only is needed to find the calls x
1197
+ (findall(Program7a,(((member([[n,PredicateName],Arguments,":-",Program6],Program4)->true;((member([[n,PredicateName],Arguments],Program4),Program6=[])->true;Program4=[[n,PredicateName],Arguments,":-",Program6])),
1198
+ length(Arguments,ArgumentsLength),
1199
+ Item=[[n,PredicateName],ArgumentsLength],
1200
+ (member(Item,Program0)->Program6=Program6a;Program6a=[])%%->true;
1201
+ %%Item=Program0
1202
+ ),
1203
+ (find_calls2(Program6a,[],Program7a))),[Program7])),
1204
+ %%append(Program2,Program7,Program8),
1205
+ %%append(Program0,Program7,Program01));
1206
+ %%(Program8=Program2,Program01=Program0)),
1207
+ append(Program2,Program7,Program8),
1208
+ append(Program0,Program7,Program01),
1209
+ find_calls1(Program01,Program5,Program8,Program3).
1210
+
1211
+ find_calls2([],Program,Program) :- !.
1212
+ /**
1213
+ find_calls2(Program1,Program2,Program3) :-
1214
+ Program1=[Line|Program41],
1215
+ Line=[[n,code]|Program42],
1216
+ find_calls2(Program41,Program2,Program5),
1217
+ append(Program5,Program42,Program6),
1218
+ find_calls2(Program6,[],Program3).
1219
+ **/
1220
+
1221
+ find_calls2(Program1,Program2,Program3) :-
1222
+ Program1=[Line|Program4],
1223
+ (Line=[[n,PredicateName],Arguments]->
1224
+ length(Arguments,ArgumentsLength);
1225
+ (Line=[[n,PredicateName]],ArgumentsLength=0)), %% correct syntax is [true] not true
1226
+ Item=[[[n,PredicateName],ArgumentsLength]],
1227
+ append(Program2,Item,Program5),
1228
+ find_calls2(Program4,Program5,Program3).
1229
+
1230
+ eliminate_unused_predicates1(_Rules,[],Algorithms,Algorithms) :- !.
1231
+ eliminate_unused_predicates1(Rules,Algorithms1,Algorithms2,Algorithms3) :-
1232
+ Algorithms1=[[Algorithms4a,Algorithms4]|Algorithms5],
1233
+ %%(Algorithms4a=[]->
1234
+ %%eliminate_unused_predicates1(Rules,Algorithms5,Algorithms2,
1235
+ %%Algorithms3),%%;
1236
+ ((findall(Algorithms6a,(((member(Algorithms4a1,Algorithms4),Algorithms4a1=[[n,_]|_])->true;Algorithms4a1=Algorithms4), ((Algorithms4a1=[[n,PredicateName],Arguments,":-",_Program6],
1237
+ %%Algorithms4a1=[[n,PredicateName],Arguments])->
1238
+ length(Arguments,ArgumentsLength))->true;
1239
+ (Algorithms4a1=[[n,PredicateName],Arguments2],
1240
+ length(Arguments2,ArgumentsLength)->true;
1241
+ (Algorithms4a1=[[n,PredicateName]],ArgumentsLength=0))),
1242
+ Item=[[n,PredicateName],ArgumentsLength],
1243
+ (member(Item,Rules)->
1244
+ (Algorithms4a=[]->Algorithms2=Algorithms6a;
1245
+ append(Algorithms2,
1246
+ [Algorithms4a],Algorithms6a));
1247
+ Algorithms6a=Algorithms2)),Algorithms6b)),
1248
+ Algorithms6b=[Algorithms6c|_],
1249
+ (var(Algorithms6c)->Algorithms6=[];Algorithms6=Algorithms6c),
1250
+ %%length(Algorithms4,Count)),
1251
+ eliminate_unused_predicates1(Rules,Algorithms5,Algorithms6,
1252
+ Algorithms3)).
Combination-Algorithm-Writer-Multiple-Predicates/cawmp.bib ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ @misc {cawmp,
2
+ author = {Lucian Green},
3
+ title = {Combination Algorithm Writer with Multiple Predicates System},
4
+ year = {2019},
5
+ howpublished = "https://github.com/luciangreen/Combination-Algorithm-Writer-Multiple-Predicates",
6
+ URL = "https://github.com/luciangreen/Combination-Algorithm-Writer-Multiple-Predicates"
7
+ }
Combination-Algorithm-Writer-Multiple-Predicates/cawplistprolog.pl ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :- include('../listprologinterpreter/grammar.pl').
2
+ :- include('lpi_caw_commands.pl').
3
+ :- include('../listprologinterpreter/listprologinterpreter1listrecursion4.pl').
4
+ :- include('../listprologinterpreter/listprologinterpreter3preds5.pl').
5
+ :- include('../listprologinterpreter/lpiverify4.pl').
6
+ :- include('../listprologinterpreter/lpiverify4_open.pl').
7
+ :- include('../listprologinterpreter/lpiverify4_types.pl').
8
+ :- include('../listprologinterpreter/lpiverify4_open_types.pl').
9
+ :- include('caw5 copy 12.pl').
10
+ :- include('cawpverify.pl').
11
+ %%:- include('rcawp.pl').
12
+ %%:- include('rcaw.pl').
13
+ %%:- include('../Text-to-Breasonings/texttobr2.pl').
14
+ :- include('../listprologinterpreter/la_strings.pl').
15
+ :- include('../listprologinterpreter/la_string_codes.pl').
16
+ :- include('../listprologinterpreter/la_maths.pl').
17
+ :- include('remove_duplicate_predicates.pl').
18
+ :- include('cawpverifya.pl').
19
+
20
+
21
+ %%:- include('caw5 copy 12.pl').
22
+ %%:- include('cawpverify.pl').
23
+ %%:- include('rcawp.pl').
24
+ %%:- include('rcaw.pl').
25
+ %%:- include('texttobr2.pl').
26
+ :- include('../listprologinterpreter/la_strings.pl').
27
+ %:- include('../Languages/lang_db_generator.pl'). % leave off, run separately through Languages
28
+ %:- include('../listprologinterpreter/lpiverify4-fr.pl').
29
+ :- include('../listprologinterpreter/operators.pl').
30
+ :- include('../listprologinterpreter/lpiverify4_test_lang_all.pl').
31
+ :- include('../listprologinterpreter/lpiverify4_test_bt_lang_all.pl').
32
+ :- include('../Languages/make_docs.pl').
33
+ :- include('../SSI/find_pred_sm.pl').
34
+ :- include('../listprologinterpreter/e4_fa_get_vals.pl').
35
+ %:- include('../listprologinterpreter/equals4_first_args.pl').
36
+ :- include('../listprologinterpreter/expression_not_var.pl').
37
+ :- include('../listprologinterpreter/collect_arguments.pl').
38
+ :- include('../listprologinterpreter/reserved_words2.pl').
39
+ :- include('../listprologinterpreter/expand_types.pl').
40
+ :- include('../listprologinterpreter/replace_in_term.pl').
41
+ :- include('../listprologinterpreter/preds_converters_and_matrices.pl').
42
+ %:- include('numbers_of_items_correspond.pl').
43
+ :- include('../listprologinterpreter/match_get_put_vals.pl').
Combination-Algorithm-Writer-Multiple-Predicates/cawpverify.pl ADDED
@@ -0,0 +1,429 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ %% cawptest(Debug[on/off],Total,Score).
3
+
4
+ cawptest(Debug,NTotal,Score) :- cawptest(Debug,0,NTotal,0,Score),!.
5
+ cawptest(_Debug,NTotal,NTotal,Score,Score) :- NTotal=9, !.
6
+ cawptest(Debug,NTotal1,NTotal2,Score1,Score2) :-
7
+ NTotal3 is NTotal1+1,
8
+ cawptest2(NTotal3,Function,Rules,MaxLength,MaxPredicates,TotalVars,Numinputs,Numoutputs,Specifications,AlgDict,Program1),
9
+
10
+ %%writeln([cawptest2(NTotal3,Specifications,Program1)]),
11
+ (((%%writeln(caw00(Debug,function0,[],5,TotalVars,Specifications,[],Program1)),
12
+ caw00(Debug,Function,Rules,MaxLength,MaxPredicates,TotalVars,Numinputs, Numoutputs,Specifications,AlgDict,[],Program1)
13
+
14
+ %%sort(Program1,ProgramA),
15
+ %%sort(Program2,ProgramA)
16
+ %%writeln1(Program1),writeln1(Program2)
17
+ %%Program1=Program2
18
+ ))->(Score3 is Score1+1,writeln([cawptest,NTotal3,passed]));(Score3=Score1,writeln([cawptest,NTotal3,failed]))),
19
+ writeln(""),
20
+ cawptest(Debug,NTotal3,NTotal2,Score3,Score2),!.
21
+
22
+ %% Test individual cases, Debug=trace=on or off, N=case number, Passed=output=result
23
+
24
+ cawptest1(Debug,N,Passed) :-
25
+ cawptest2(N,Function,Rules,MaxLength,MaxPredicates,TotalVars,Numinputs, Numoutputs,Specifications,AlgDict,Program1),
26
+ %%writeln1([cawptest2(N,Specifications,Program1)]),
27
+ %%writeln1(caw00(Debug,Function,Rules,MaxLength,MaxPredicates,TotalVars,Numinputs, Numoutputs,Specifications,AlgDict,[],Program2)),
28
+
29
+ (((caw00(Debug,Function,Rules,MaxLength,MaxPredicates,TotalVars,Numinputs, Numoutputs,Specifications,AlgDict,[],Program1)
30
+ %%sort(Program1,ProgramA),
31
+ %%sort(Program2,ProgramA)
32
+
33
+ %writeln(Program1),
34
+ %writeln(Program2),
35
+ %Program1=Program2
36
+ ))->(Passed=passed,writeln([cawptest,N,passed]));(Passed=failed,writeln([cawptest,N,failed]))),!.
37
+
38
+
39
+ /**
40
+ cawptest2(1,add0,[[[n,+],2,1%% Modes=2 inputs, 1 output
41
+ ]],2,2,%% MaxPredicates is not the number of predicates in the result, it is the number of non-dictionary predicates in the result.
42
+ 4,
43
+ [2],[1],%% Numinputs, Numoutputs tested for
44
+ [
45
+ [[[[[v,a],1],[[v,b],1]],[[[v,c],2]],true]],
46
+ [[[[[v,a],1],[[v,b],1]],[[[v,c],2]],true]]
47
+ ]
48
+ ,
49
+ [], %% Algorithm dictionary
50
+ [ %% Result
51
+ [[n,1],[[v,a],[v,b],[v,c]],":-",
52
+ [ [[n,+],[[v,a],[v,b],[v,d]]],
53
+ [[n,=],[[v,d],[v,c]]]]],
54
+ [[n,add0],[[v,a],[v,b],[v,c]],":-",
55
+ [ [[n,1],[[v,a],[v,b],[v,d]]],
56
+ [[n,=],[[v,d],[v,c]]]]]
57
+ ]).
58
+ **/
59
+ cawptest2(1,add0,[[[n,+],2,1%% Modes=2 inputs, 1 output
60
+ ]],2,2,%% MaxPredicates is not the number of predicates in the result, it is the number of non-dictionary predicates in the result.
61
+ 4,
62
+ [2],[1],%% Numinputs, Numoutputs tested for
63
+ [
64
+ [[[[[v,a],1],[[v,b],1]],[[[v,c],2]],true]],
65
+ [[[[[v,a],2],[[v,b],1]],[[[v,c],3]],true]]
66
+ ]
67
+ ,
68
+ [], %% Algorithm dictionary
69
+ [ %% Result
70
+ [[n,1],[[v,a],[v,b],[v,c]],":-",
71
+ [ [[n,+],[[v,a],[v,b],[v,d]]],
72
+ [[n,=],[[v,d],[v,c]]]]],
73
+ [[n,add0],[[v,a],[v,b],[v,c]],":-",
74
+ [ [[n,1],[[v,a],[v,b],[v,d]]],
75
+ [[n,=],[[v,d],[v,c]]]]]
76
+ ]).
77
+
78
+ %% With a=input and b=input, returns [[[n,1],[[v,a],[v,b]],:-,[[[n,+],[[v,a],1,[v,c]]],[[n,=],[[v,c],[v,b]]]]],[[n,1],[[v,a],[v,b]],:-,[[[n,-],[[v,a],1,[v,c]]],[[n,=],[[v,c],[v,b]]]]],[[n,add0],[[v,a],[v,b]],:-,[[[n,1],[[v,b],[v,c]]],[[n,=],[[v,a],[v,a]]]]]] which is incorrect, and with a=input and b=output nondeterministic clauses fail
79
+ %% Non-determinism is not supported in List Prolog. List Prolog should use if-then instead of non-deterministic clauses.
80
+ %% Use if-then with calls to predicates as consequents.
81
+ cawptest2(2,add0,[],2,1,3,[1,2],[0,1],
82
+ [[[[[[v,a],1],[[v,b],2]],[],true],[[[[v,a],2],[[v,b],1]],[],true]]],
83
+
84
+ [ %% Algorithm dictionary
85
+ [[[n,1],1,1],[[v,a],[v,b]],":-",
86
+ [ [[n,+],[[v,a],1,[v,c]]],
87
+ [[n,=],[[v,c],[v,b]]]]],
88
+
89
+ [[[n,1],1,1],[[v,a],[v,b]],":-",
90
+ [ [[n,-],[[v,a],1,[v,c]]],
91
+ [[n,=],[[v,c],[v,b]]]]]
92
+ ],
93
+
94
+ %% Result
95
+
96
+ [[[n,1],[[v,a],[v,b]],":-",
97
+ [[[n,+],[[v,a],1,[v,c]]],
98
+ [[n,=],[[v,c],[v,b]]]]],
99
+ [[n,1],[[v,a],[v,b]],":-",
100
+ [[[n,-],[[v,a],1,[v,c]]],
101
+ [[n,=],[[v,c],[v,b]]]]],
102
+ [[n,add0],[[v,a],[v,b]],":-",
103
+ [[[n,1],[[v,a],[v,c]]],
104
+ [[n,=],[[v,b],[v,c]]]]]]
105
+
106
+ ).
107
+
108
+ %%[[[n,a2],[[v,a],[v,b]],:-,[[[n,+],[[v,a],1,[v,c]]],[[n,=],[[v,c],[v,b]]]]],[[n,add0],[[v,a],[v,b]],:-,[[[n,a2],[[v,a],[v,c]]]]]]
109
+
110
+
111
+ %% () Can underscore vars in spec, ignore in choose var, everyvarcovered
112
+
113
+ %% Was test 9 on bu16
114
+
115
+ cawptest2(3,add,[[[n,[]],1,0],[[n,"_"],1,0]],1,1,1,[1],[0],
116
+ [
117
+ /**[[[[[v,a],[1,2,3]],[[v,b],3],[[v,c],[]]],
118
+ [[[v,d],[4,5,6]],[[v,e],[2,3]]],true],**/
119
+ [[[[[v,a],3]],
120
+ [],true]]
121
+
122
+ /**
123
+ [[[[[[v,a],[1,2,3]],[[v,b],3],[[v,c],[]]],
124
+ [[[v,d],[4,5,6]],[[v,e],[2,3]]],true],
125
+ [[[[v,a],[]],[[v,b],3],[[v,c],[4,5,6]]],
126
+ [[[v,d],[4,5,6]],[[v,e],5]],true]]]],
127
+ **/
128
+ ],
129
+ [ %% Algorithm dictionary
130
+ ],
131
+ %% Result
132
+
133
+ [[[n,add],[[v,a]],":-",[[[n,"_"],[[v,a]]]]]]
134
+
135
+ /**
136
+ [[n,1],[[v,a],[v,b],[v,c],[v,d]],":-", %% Test by self
137
+ [[[n,[]],[[v,c]]]]],
138
+ [[n,add],[[v,a],[v,b],[v,c],[v,d]],":-",
139
+ [[n,1],[[v,a],[v,b],[v,c],[v,d]]]]**/
140
+ ).
141
+
142
+
143
+
144
+ cawptest2(4,function3,[[[n,+],2,1]],3,1,5,[2],[1],
145
+ [[[[[[v,a],1],[[v,b],1]],[[[v,c],3]],true],
146
+ [[[[v,a],1],[[v,b],2]],[[[v,c],5]],true]]],
147
+ [ %% Algorithm dictionary
148
+ ],
149
+ [ %% Result
150
+ [[n,function3],[[v,a],[v,b],[v,c]],":-",
151
+ [[[n,+],[[v,a],[v,b],[v,d]]],
152
+ [[n,+],[[v,b],[v,d],[v,e]]],
153
+ [[n,=],[[v,e],[v,c]]]]]]).
154
+
155
+
156
+ cawptest2(5,add0,[[[n,+],2,1]],1,1,4,% 3 x 5 %% 2,2,3
157
+ [2],[1],
158
+ [
159
+ [[[[[v,a],1],[[v,b],1]],[[[v,c],2]],true]]
160
+ %% [[[[[v,a],1]],[[[v,b],2]],true]],
161
+ %% [[[[[v,a],1],[[v,b],1]],[[v,c],2],true]]
162
+ ]
163
+ ,
164
+ [ %% Algorithm dictionary
165
+ ],
166
+ [ %% Result
167
+ [[n,add0],[[v,a],[v,b],[v,c]],":-",
168
+ [ [[n,+],[[v,a],[v,b],[v,d]]],
169
+ [[n,=],[[v,d],[v,c]]]]]
170
+
171
+ ]).
172
+
173
+ %%%%*****
174
+
175
+
176
+ cawptest2(6,function3,[],2,1,4,[2],[1],
177
+ [[[[[[v,a],1],[[v,b],1]],[[[v,c],2]],true],
178
+ [[[[v,a],1],[[v,b],2]],[[[v,c],3]],true],
179
+ [[[[v,a],1],[[v,b],1]],[[[v,c],1]],fail],
180
+ [[[[v,a],1],[[v,b],1]],[[[v,c],3]],fail]]],
181
+ [ %% Algorithm dictionary
182
+
183
+ [[[n,function1],2,1],[[v,a],[v,b],[v,c]],":-",
184
+ [
185
+ [[n,+],[[v,a],[v,b],[v,c]]]
186
+ ]
187
+ ]
188
+
189
+ ],
190
+ [ %% Result
191
+ [[n,function1],[[v,a],[v,b],[v,c]],":-",
192
+ [[[n,+],[[v,a],[v,b],[v,c]]]]],
193
+ [[n,function3],[[v,a],[v,b],[v,c]],":-",
194
+ [[[n,function1],[[v,a],[v,b],[v,d]]],
195
+ [[n,=],[[v,d],[v,c]]]]]]).
196
+
197
+ /**
198
+ %%**
199
+ cawptest2(7,add0,[],3,3,%% it could be 5
200
+ 4,
201
+ [1],[1],
202
+ [
203
+ [[[[[v,a],[1,2]]],[[[v,b],[]]],true]],
204
+ [[[[[v,a],[1,2]]],[[[v,b],[]]],true]],
205
+ [[[[[v,a],[1,2]]],[[[v,b],[2]]],true]],
206
+ [[[[[v,a],[2]]],[[[v,b],[]]],true]],
207
+ [[[[[v,a],[]]],[[[v,b],[]]],true]]
208
+ ],
209
+ [ %% Algorithm dictionary
210
+ [[[n,add2],1,1],[[v,a],[v,b]],":-",
211
+ [[[n,=],[[v,a],[]]],
212
+ [[n,=],[[v,b],[]]]]],
213
+ [[[n,add3],1,1],[[v,a],[v,b]],":-",
214
+ [[[n,tail],[[v,a],[v,b]]]]]
215
+ ],
216
+ [ %% Result
217
+ [[n,add2],[[v,a],[v,b]],":-",
218
+ [[[n,=],[[v,a],[]]],
219
+ [[n,=],[[v,b],[]]]]],
220
+ [[n,add3],[[v,a],[v,b]],":-",
221
+ [[[n,tail],[[v,a],[v,b]]]]],
222
+ [[n,add0],[[v,a],[v,b]],":-",
223
+ [[[n,1],[[v,a],[v,c]]],
224
+ [[n,=],[[v,c],[v,b]]]]],
225
+ [[n,1],[[v,a],[v,b]],":-",
226
+ [[[n,add2],[[v,a],[v,c]]],
227
+ [[n,=],[[v,c],[v,b]]]]],
228
+ [[n,1],[[v,a],[v,b]],":-",
229
+ [[[n,add3],[[v,a],[v,c]]],
230
+ [[n,1],[[v,c],[v,d]]],
231
+ [[n,=],[[v,d],[v,b]]]]]]).
232
+
233
+ cawptest2(7.1,add0,[],2,1,%% it could be 5
234
+ 3,
235
+ [1],[1],
236
+ [
237
+ %%[[[[[v,a],[1,2]]],[[[v,b],[]]],true]],
238
+ %%[[[[[v,a],[1,2]]],[[[v,b],[]]],true]],
239
+ %%[[[[[v,a],[1,2]]],[[[v,b],[2]]],true]],
240
+ %%[[[[[v,a],[2]]],[[[v,b],[]]],true]],
241
+ [[[[[v,a],[]]],[[[v,b],[]]],true]]
242
+ ],
243
+ [ %% Algorithm dictionary
244
+ [[[n,add2],1,1],[[v,a],[v,b]],":-",
245
+ [[[n,=],[[v,a],[]]],
246
+ [[n,=],[[v,b],[]]]]]
247
+ %%[[[n,add3],1,1],[[v,a],[v,b]],":-",
248
+ %% [[[n,tail],[[v,a],[v,b]]]]]
249
+ ],
250
+ [ %% Result
251
+ [[n,add2],[[v,a],[v,b]],":-",
252
+ [[[n,=],[[v,a],[]]],
253
+ [[n,=],[[v,b],[]]]]],
254
+ [[n,add0],[[v,a],[v,b]],":-",
255
+ [[[n,add2],[[v,a],[v,c]]],
256
+ [[n,=],[[v,c],[v,b]]]]]]
257
+ ).
258
+
259
+ cawptest2(7.2,add0,[],3,2,%% it could be 5
260
+ 3,
261
+ [1],[1],
262
+ [
263
+ [[[[[v,a],[1,2]]],[[[v,b],[]]],true]],
264
+ [[[[[v,a],[1,2]]],[[[v,b],[2]]],true]],
265
+ [[[[[v,a],[2]]],[[[v,b],[]]],true]],
266
+ [[[[[v,a],[]]],[[[v,b],[]]],true]]
267
+ ],
268
+ [ %% Algorithm dictionary
269
+ [[[n,add2],1,1],[[v,a],[v,b]],":-",
270
+ [[[n,=],[[v,a],[]]],
271
+ [[n,=],[[v,b],[]]]]],
272
+ [[[n,add3],1,1],[[v,a],[v,b]],":-",
273
+ [[[n,tail],[[v,a],[v,b]]]]]
274
+ ],
275
+ [ %% Result
276
+ [[n,add2],[[v,a],[v,b]],":-",
277
+ [[[n,=],[[v,a],[]]],
278
+ [[n,=],[[v,b],[]]]]],
279
+ [[n,add3],[[v,a],[v,b]],":-",
280
+ [[[n,tail],[[v,a],[v,b]]]]],
281
+ [[n,add0],[[v,a],[v,b]],":-",
282
+ [[[n,add2],[[v,a],[v,c]]],
283
+ [[n,=],[[v,c],[v,b]]]]],
284
+ [[n,add0],[[v,a],[v,b]],":-",
285
+ [[[n,add3],[[v,a],[v,c]]],
286
+ [[n,add0],[[v,c],[v,d]]],
287
+ [[n,=],[[v,d],[v,b]]]]]]).
288
+ **/
289
+
290
+ cawptest2(7,add0,[[[n,+],2,1%% Modes=2 inputs, 1 output
291
+ ],[[n,-],2,1]],4,1,%% MaxPredicates is not the number of predicates in the result, it is the number of non-dictionary predicates in the result.
292
+ 6,
293
+ [2],[1],%% Numinputs, Numoutputs tested for
294
+ [
295
+ [[[[[v,a],1],[[v,b],1]],[[[v,c],0],[[v,d],2]],true],
296
+ [[[[v,a],1],[[v,b],2]],[[[v,c],1],[[v,d],3]],true]]
297
+ ]
298
+ ,
299
+ [], %% Algorithm dictionary
300
+ /**[ %% Result
301
+ [[n,add0],[[v,a],[v,b],[v,c],[v,d]],":-",
302
+ [ [[n,+],[[v,a],[v,b],[v,e]]],
303
+ [[n,+],[[v,a],[v,b],[v,f]]],
304
+ [[n,=],[[v,c],[v,e]]],
305
+ [[n,=],[[v,f],[v,d]]]]]**/
306
+
307
+ /**
308
+ [[[n,add0],[[v,a],[v,b],[v,c],[v,d]],":-",
309
+ [[[n,+],[[v,a],[v,a],[v,e]]],
310
+ [[n,+],[[v,a],[v,b],[v,f]]],
311
+ [[n,=],[[v,e],[v,c]]],
312
+ [[n,=],[[v,e],[v,d]]]]]
313
+ **/
314
+
315
+ /**
316
+ [[[n,add0],[[v,a],[v,b],[v,c],[v,d]],":-",
317
+ [[[n,+],[[v,a],[v,b],[v,e]]],
318
+ [[n,-],[[v,b],[v,a],[v,f]]],
319
+ [[n,=],[[v,e],[v,d]]],
320
+ [[n,=],[[v,f],[v,c]]]]]]
321
+ **/
322
+
323
+ [[[n,add0],[[v,a],[v,b],[v,c],[v,d]],":-",
324
+ [[[n,+],[[v,a],[v,b],[v,e]]],
325
+ [[n,-],[[v,b],[v,a],[v,f]]],
326
+ [[n,=],[[v,e],[v,d]]],
327
+ [[n,=],[[v,f],[v,c]]]]]]
328
+
329
+ ).
330
+
331
+ /**
332
+ Not tested nonrecursive multiclauses
333
+ cawptest2(8,add0,[[[n,+],2,1],[[n,-],2,1]],2,3,4,[2],[1],
334
+ [[[[[[v,a],1],[[v,b],1]],[[[v,b],2]],true],[[[[v,a],2],[[v,b],1]],[[[v,b],1]],true]],
335
+ [[[[[v,a],1],[[v,b],1]],[[[v,b],2]],true]],
336
+ [[[[[v,a],2],[[v,b],1]],[[[v,b],1]],true]]],
337
+
338
+ [ %% Algorithm dictionary
339
+ ],
340
+
341
+ %% Result
342
+
343
+ [[[n,1],[[v,a],[v,b],[v,c]],":-",
344
+ [[[n,+],[[v,a],[v,b],[v,d]]],
345
+ [[n,=],[[v,c],[v,d]]]]],
346
+ [[n,1],[[v,a],[v,b],[v,c]],":-",
347
+ [[[n,-],[[v,a],[v,b],[v,d]]],
348
+ [[n,=],[[v,d],[v,c]]]]],
349
+ [[n,add0],[[v,a],[v,b],[v,c]],":-",
350
+ [[[n,1],[[v,a],[v,b],[v,d]]],
351
+ [[n,=],[[v,c],[v,d]]]]]]
352
+ ).
353
+ **/
354
+
355
+ /**
356
+
357
+ cawptest2(8,add0,[[[n,+],2,1%% Modes=2 inputs, 1 output
358
+ ]],4,1,%% MaxPredicates is not the number of predicates in the result, it is the number of non-dictionary predicates in the result.
359
+ 8,
360
+ [2],[1],%% Numinputs, Numoutputs tested for
361
+ [
362
+ [[[[[v,a],1],[[v,b],1],[[v,c],2],[[v,d],1]],[[[v,e],5]],true],
363
+ [[[[v,a],2],[[v,b],2],[[v,c],2],[[v,d],1]],[[[v,e],7]],true]]
364
+ ]
365
+ ,
366
+ [],
367
+ %% add options to remove extra choice points
368
+ %% test on lpiv
369
+ %% caw00(off,add0,[[[n,+],2,1]],4,1,8,[2,4],[1],[[[[[[v,a],1],[[v,b],1],[[v,c],2],[[v,d],1]],[[[v,e],5]],true],[[[[v,a],2],[[v,b],2],[[v,c],2],[[v,d],1]],[[[v,e],7]],true]]],[],[],P).
370
+ %% x:
371
+
372
+ [[[n,add0],[[v,a],[v,b],[v,c],[v,d],[v,e]],":-",[[[n,+],[[v,a],[v,b],[v,f]]],[[n,+],[[v,c],[v,f],[v,g]]],[[n,+],[[v,d],[v,g],[v,h]]],[[n,=],[[v,h],[v,e]]]]]]
373
+
374
+ ).
375
+
376
+ **/
377
+
378
+ % ["Computational English","COMPUTATIONAL ENGLISH by Lucian Green Conglish Reflection 2 of 4.txt",0,algorithms,"14. *I prepared to serve the vegetable burger. I did this by cooking the vegetable patty. First, I made the patty from semolina, soy and carrot. Second, I minced it up. Third, I cooked it. In this way, I prepared to serve the vegetable burger by cooking the vegetable patty."]
379
+
380
+
381
+ cawptest2(8,append1,[[[n,append],2,1%% Modes=2 inputs, 1 output
382
+ ]],3,1,%% MaxPredicates is not the number of predicates in the result, it is the number of non-dictionary predicates in the result.
383
+ 7,
384
+ [3],[1],%% Numinputs, Numoutputs tested for
385
+ [
386
+ [[[[[v,a],["top"]],[[v,b],["middle"]],[[v,c],["bottom"]]],
387
+ [[[v,d],["top","middle","bottom"]]],true]]
388
+ ]
389
+ ,
390
+ [],
391
+ [[[n,append1],[[v,a],[v,b],[v,c],[v,d]],":-" ,[[[n,append],[[v,a],[v,b],[v,e]]],[[n,append],[[v,e],[v,c],[v,f]]],[[n,=],[[v,f],[v,d]]]]]]
392
+
393
+ ).
394
+
395
+ % ["Fundamentals of Pedagogy and Pedagogy Indicators","FUNDAMENTALS OF PEDAGOGY by Lucian Green Two Uses 23 of 30.txt",0,algorithms,"225. ALEXIS: *The subject should write logically connected breasonings."]
396
+
397
+ /**
398
+
399
+ cawptest2(9,mp*,[[[n,append],2,1%% Modes=2 inputs, 1 output
400
+ ]],3,1,%% MaxPredicates is not the number of predicates in the result, it is the number of non-dictionary predicates in the result.
401
+ 7,
402
+ [3],[1],%% Numinputs, Numoutputs tested for
403
+ [
404
+ [[[[[v,a],["p1"]],[[v,b],["p1","p2"]]],
405
+ [[[v,c],["p2"]]],true]]
406
+ ]
407
+ ,
408
+ [],
409
+ [[[n,mp],[[v,a],[v,b],[v,c],[v,d]],":-" ,[[[n,append],[[v,a],[v,b],[v,e]]],[[n,append],[[v,e],[v,c],[v,f]]],[[n,=],[[v,f],[v,d]]]]]]
410
+
411
+ ).
412
+
413
+ **/
414
+
415
+ /*
416
+ cawptest2(9,append1,[[[n,append],2,1%% Modes=2 inputs, 1 output
417
+ ]],3,1,%% MaxPredicates is not the number of predicates in the result, it is the number of non-dictionary predicates in the result.
418
+ 7,
419
+ [3],[1],%% Numinputs, Numoutputs tested for
420
+ [
421
+ [[[[[v,a],["top"]],[[v,b],["middle"]],[[v,c],["bottom"]]],
422
+ [[[v,d],["top","middle","bottom"]]],true]]
423
+ ]
424
+ ,
425
+ [],
426
+ [[[n,append1],[[v,a],[v,b],[v,c],[v,d]],":-" ,[[[n,append],[[v,a],[v,b],[v,e]]],[[n,append],[[v,e],[v,c],[v,f]]],[[n,=],[[v,f],[v,d]]]]]]
427
+
428
+ ).
429
+ */
Combination-Algorithm-Writer-Multiple-Predicates/cawpverifya.pl ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %% Test individual cases, Debug=trace=on or off, N=case number, Passed=output=result
2
+
3
+ cawptest1a(Debug,N,Passed) :-
4
+ cawptest2a(N,Function,Rules,MaxLength,MaxPredicates,TotalVars,Numinputs, Numoutputs,Specifications,AlgDict,Program1),
5
+ %%writeln1([cawptest2(N,Specifications,Program1)]),
6
+ %%writeln1(caw00(Debug,Function,Rules,MaxLength,MaxPredicates,TotalVars,Numinputs, Numoutputs,Specifications,AlgDict,[],Program2)),
7
+
8
+ (((caw000(Debug,Function,Rules,MaxLength,MaxPredicates,TotalVars,Numinputs, Numoutputs,Specifications,AlgDict,[],Program1)
9
+ %%sort(Program1,ProgramA),
10
+ %%sort(Program2,ProgramA)
11
+
12
+ %%writeln(Program1),
13
+ %%writeln(Program2),
14
+ %%Program1=Program2
15
+ ))->(Passed=passed,writeln([cawptest,N,passed]));(Passed=failed,writeln([cawptest,N,failed]))),!.
16
+
17
+ cawptest2a(7,[add0,add0],[],[2,3],[1,1],%% it could be 5
18
+ [3,4], %% 4 not 3 because of bug
19
+ [1],[1],
20
+ [
21
+ [[[[[[v,a],[]]],[[[v,b],[]]],true]]],
22
+
23
+ [[[[[[v,a],[1,2,3]]],[[[v,b],[]]],true]]]
24
+ %%[[[[[v,a],[2]]],[[[v,b],[]]],true]], %% Needs to progress bottom up by writing base case first
25
+ %%[[[[[v,a],[1,2]]],[[[v,b],[]]],true]], %% add0 second clause has this not spec below
26
+ %%[[[[[v,a],[1,2]]],[[[v,b],[2]]],true]],
27
+ %%[[[[[v,a],[2]]],[[[v,b],[]]],true]]
28
+ %%[[[[[v,a],[1,2]]],[[[v,b],[]]],true]],
29
+ ],
30
+ [ %% Algorithm dictionary
31
+ [[[n,add2],1,1],[[v,a],[v,b]],":-",
32
+ [[[n,=],[[v,a],[]]],
33
+ [[n,=],[[v,b],[]]]]],
34
+ [[[n,add3],1,1],[[v,a],[v,b]],":-",
35
+ [[[n,tail],[[v,a],[v,b]]]]]
36
+
37
+ %% Result
38
+ /**[[[n,add1],1,1],[[v,a],[v,b]],":-",
39
+ [[[n,add2],[[v,a],[v,c]]],
40
+ [[n,=],[[v,c],[v,b]]]]]
41
+ **/
42
+ ],
43
+ [ %% Result
44
+ %%[[n,add3],[[v,a],[v,b]],":-", %% swapped a3,a2
45
+ %%[[[n,tail],[[v,a],[v,b]]]]],
46
+
47
+ /**[[n,add2],[[v,a],[v,b]],":-",
48
+ [[[n,=],[[v,a],[]]],
49
+ [[n,=],[[v,b],[]]]]],
50
+ [[n,add0],[[v,a],[v,b]],":-",
51
+ [[[n,add2],[[v,a],[v,c]]],
52
+ [[n,=],[[v,c],[v,b]]]]]],
53
+ **/
54
+ %% Resulting base case
55
+
56
+ [[[n,add2],[[v,a],[v,b]],":-",
57
+ [[[n,=],[[v,a],[]]],
58
+ [[n,=],[[v,b],[]]]]],
59
+ [[n,add0],[[v,a],[v,b]],":-",
60
+ [[[n,add2],[[v,a],[v,c]]],
61
+ [[n,=],[[v,c],[v,b]]]]]],
62
+
63
+ [ %% Resulting recursive algorithm
64
+
65
+ [[n,add2],[[v,a],[v,b]],":-",
66
+ [[[n,=],[[v,a],[]]],
67
+ [[n,=],[[v,b],[]]]]],
68
+ [[n,add3],[[v,a],[v,b]],":-",
69
+ [[[n,tail],[[v,a],[v,b]]]]],
70
+ [[n,add0],[[v,a],[v,b]],":-",
71
+ [[[n,add2],[[v,a],[v,c]]],
72
+ [[n,=],[[v,c],[v,b]]]]],
73
+ [[n,add0],[[v,a],[v,b]],":-",
74
+ [[[n,add3],[[v,a],[v,c]]],
75
+ [[n,add0],[[v,c],[v,d]]],
76
+ [[n,=],[[v,d],[v,b]]]]]]]
77
+
78
+ ).
79
+
80
+
81
+ %%[[[n,add],[[v,a],[v,c],[v,d]],":-",[[[n,[]],[[v,a]]],[[n,=],[[v,c],[v,d]]]]]]
82
+
83
+ %% Add cover all vars before returning
84
+ %%**/
85
+
86
+
87
+ /** Doesn't work
88
+ %% before now, io=21
89
+ cawptest2(7,add0,[],2,3,5,% 3 x 5
90
+ [1,2],[0,1],
91
+ [[[[[[v,a],1],[[v,b],2]],[],true],[[[[v,a],2],[[v,b],1]],[],true]],
92
+ %%[[[[[v,a],1],[[v,b],2]],[],true],[[[[v,a],2],[[v,b],1]],[],true]],
93
+ [[[[[v,a],1]],[[[v,b],2]],true]],
94
+ [[[[[v,a],2]],[[[v,b],1]],true]]
95
+ ],
96
+
97
+ [ %% Algorithm dictionary
98
+
99
+ [[[n,a2],1,1],[[v,a],[v,b]],":-",
100
+ [ [[n,+],[[v,a],1,[v,c]]],
101
+ [[n,=],[[v,c],[v,b]]]]],
102
+
103
+ [[[n,a3],1,1],[[v,a],[v,b]],":-",
104
+ [ [[n,-],[[v,a],1,[v,c]]],
105
+ [[n,=],[[v,c],[v,b]]]]]
106
+ ],
107
+
108
+ [ %% Result
109
+ [[n,a2],[[v,a],[v,b]],":-",
110
+ [ [[n,+],[[v,a],1,[v,c]]],
111
+ [[n,=],[[v,c],[v,b]]]]],
112
+
113
+ [[n,a3],[[v,a],[v,b]],":-",
114
+ [ [[n,-],[[v,a],1,[v,c]]],
115
+ [[n,=],[[v,c],[v,b]]]]],
116
+
117
+ [[n,add0],[[v,a],[v,b]],":-",
118
+ [ [[n,1],[[v,a],[v,b]]]]],
119
+
120
+ [[n,1],[[v,a],[v,b]],":-",
121
+ [ [[n,a2],[[v,a],[v,c]]],
122
+ [[n,=],[[v,c],[v,b]]]]],
123
+
124
+ [[n,1],[[v,a],[v,b]],":-",
125
+ [ [[n,a3],[[v,a],[v,c]]],
126
+ [[n,=],[[v,c],[v,b]]]]]
127
+ ]).
128
+
129
+ **/
Combination-Algorithm-Writer-Multiple-Predicates/convert_lp_to_caw0000.pl ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %
2
+
3
+
4
+ /**
5
+
6
+ convert_lp_to_caw0000(
7
+ [
8
+ [[n,reverse],[[],[v,l],[v,l]]],
9
+ [[n,reverse],[[v,l],[v,m],[v,n]],":-",
10
+ [ [[n,head],[[v,l],[v,h]]],
11
+ [[n,tail],[[v,l],[v,t]]],
12
+ [[n,wrap],[[v,h],[v,h1]]],
13
+ [[n,append],[[v,h1],[v,m],[v,o]]],
14
+ [[n,reverse],[[v,t],[v,o],[v,n]]]
15
+ ]
16
+ ]
17
+ ],
18
+
19
+ [
20
+ % gets used i,o (with header with vars that are only either
21
+ % in the header or used in another reverse_c4*
22
+
23
+ % skips writeln, other predetermined commands
24
+ % assumes wrap, unwrap used
25
+ % doesn't use read_string (takes var input instead)
26
+
27
+ [[n,reverse],[[],[v,l],[v,l]]],
28
+
29
+ [[n,reverse_c41],[[v,l],[v,t],[v,h1]],":-",
30
+ [ [[n,head],[[v,l],[v,h]]],
31
+ [[n,tail],[[v,l],[v,t]]],
32
+ [[n,wrap],[[v,h],[v,h1]]]
33
+ ]],
34
+
35
+ % with vars it needs from another r_c4* or the header
36
+
37
+ [[n,reverse_c42],[[v,h1],[v,m],[v,t],[v,n]],":-",
38
+ [ [[n,append],[[v,h1],[v,m],[v,o]]],
39
+ [[n,reverse],[[v,t],[v,o],[v,n]]]
40
+ ]]
41
+ ],
42
+
43
+ [
44
+ % finds algs (configuration of calls to certain
45
+ % commands with header
46
+ % puts together pred
47
+
48
+ [[n,reverse],[[],[v,l],[v,l]]],
49
+
50
+ [[n,reverse],[[v,l],[v,m],[v,n]],":-",
51
+ [ [n,reverse_c41],[[v,l],[v,t],[v,h1]],
52
+ [n,reverse_c42],[[v,h1],[v,m],[v,t],[v,n]]
53
+ ]]
54
+ ],
55
+
56
+ % finds specs for r_c4*'s by using lpi with a
57
+ % flag switched on that returns the specs
58
+ % - specs are i,o vars for lines, where c4 alg
59
+ % finds algs meeting spec for set of lines
60
+
61
+ % * LPI numbers pred, pred lines, has widgets that
62
+ % indicate these and the var states
63
+ % - with assertz x another var
64
+
65
+ **/
66
+
67
+ convert_lp_to_caw0000 :-
68
+
Combination-Algorithm-Writer-Multiple-Predicates/lpi_caw_commands.pl ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ interpretstatement1(ssi,_F0,_Functions,[[n,[not,Operator]],[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
2
+ isop(Operator),
3
+ interpretpart(not_is,Variable1,Variable2,Vars1,Vars2),!.
4
+
5
+ interpretstatement1(ssi,_F0,_Functions,[[n,[not,Operator]],[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
6
+ comparisonoperator(Operator),
7
+ %%writeln1(4),
8
+ interpretpart(not_iscomparison,Operator,Variable1,Variable2,Vars1,Vars2).
9
+
10
+ interpretstatement1(ssi,_F0,_Functions,[[n,[not,member]],[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
11
+ %%writeln1(8),
12
+ interpretpart(not_member,Variable1,Variable2,Vars1,Vars2).
13
+
14
+ interpretstatement1(ssi,_F0,_Functions,[[n,[not,head]],[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
15
+ %%writeln1(6),
16
+ interpretpart(not_head,Variable1,Variable2,Vars1,Vars2).
17
+
18
+ interpretstatement1(ssi,_F0,_Functions,[[n,[not,tail]],[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
19
+ %%writeln1(61),
20
+ interpretpart(not_tail,Variable1,Variable2,Vars1,Vars2).
21
+
22
+ interpretstatement1(ssi,_F0,_Functions,[[n,[not,append]],[Variable1,Variable2,Variable3]],Vars1,Vars2,true,nocut) :-
23
+ %%writeln1(9),
24
+ interpretpart(not_append,Variable1,Variable2,Variable3,Vars1,Vars2).
25
+
26
+ interpretstatement1(ssi,_F0,_Functions,[[n,[not,stringconcat]],[Variable1,Variable2,Variable3]],Vars1,Vars2,true,nocut) :-
27
+ interpretpart(not_stringconcat,Variable1,Variable2,Variable3,Vars1,Vars2).
28
+
29
+ interpretstatement1(ssi,_F0,_Functions,[[n,[]],[Variable1]],Vars1,Vars2,true,nocut) :-
30
+ %%writeln1(8),
31
+ interpretpart([],Variable1,Vars1,Vars2).
32
+
33
+ interpretstatement1(ssi,_F0,_Functions,[[n,""],[Variable1]],Vars1,Vars2,true,nocut) :-
34
+ %%writeln1(8),
35
+ interpretpart("",Variable1,Vars1,Vars2).
36
+
37
+ interpretstatement1(ssi,_F0,_Functions,[[n,"_"],[Variable1]],Vars1,Vars2,true,nocut) :-
38
+ %%writeln1(8),
39
+ interpretpart("_",Variable1,Vars1,Vars2).
40
+
41
+
42
+ interpretpart(not_is,Variable1,Variable2,Vars1,Vars1) :-
43
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
44
+ not(isempty(Value1)),
45
+ not(isempty(Value2)),
46
+ debug_call(Skip,[[n,[not,=]],[Value1,Value2]]),
47
+ ((not(Value1 = Value2)
48
+ )->
49
+ debug_exit(Skip,[[n,[not,=]],[Value1,Value2]])
50
+ ; debug_fail(Skip,[[n,[not,=]],[Value1,Value2]])),!.
51
+
52
+ interpretpart(not_iscomparison,Operator,Variable1,Variable2,Vars1,Vars1) :- getvalues(Variable1,Variable2,Value1,Value2,Vars1),
53
+ debug_call(Skip,[[n,[not,Operator]],[Value1,Value2]]),
54
+ ((isval(Value1),
55
+ isval(Value2),
56
+ Expression=..[Operator,Value1,Value2],
57
+ not(Expression))->
58
+ debug_exit(Skip,[[n,[not,Operator]],[Value1,Value2]])
59
+ ; debug_fail(Skip,[[n,[not,Operator]],[Value1,Value2]])),!.
60
+
61
+ interpretpart(not_member,Variable1,Variable2,Vars1,Vars1) :-
62
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
63
+ debug_call(Skip,[[n,[not,member]],[Value1,Value2]]),
64
+ (((not(Value2=empty)->%%member(Value2,Value1),
65
+ (not(member(Value2,Value1))
66
+ %%putvalue(Variable2,Value3,Vars1,Vars2)%%,Vars2=Vars1
67
+ )))->
68
+ debug_exit(Skip,[[n,[not,member]],[Value1,Value2]])
69
+ ; debug_fail(Skip,[[n,[not,member]],[Value1,Value2]])),!.
70
+
71
+ interpretpart(not_head,Variable1,Variable2,Vars1,Vars1) :-
72
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
73
+ debug_call(Skip,[[n,[not,head]],[Value1,Value2]]),
74
+ ((not(Value1=[Value2|_Rest])
75
+ %%val1emptyorvalsequal(Value2,Value1A),
76
+ %%putvalue(Variable2,Value1A,Vars1,Vars2)
77
+ )->
78
+ debug_exit(Skip,[[n,[not,head]],[Value1,Value2]])
79
+ ; debug_fail(Skip,[[n,[not,head]],[Value1,Value2]])),!.
80
+
81
+ interpretpart(not_tail,Variable1,Variable2,Vars1,Vars1) :-
82
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
83
+ debug_call(Skip,[[n,[not,tail]],[Value1,Value2]]),
84
+ ((not(Value1=[_Head|Value2])
85
+ %%removebrackets(Value1A,Value1B),
86
+ %%val1emptyorvalsequal(Value2,Value1A),
87
+ %%putvalue(Variable2,Value1A,Vars1,Vars2)
88
+ )->
89
+ debug_exit(Skip,[[n,[not,tail]],[Value1,Value2]])
90
+ ; debug_fail(Skip,[[n,[not,tail]],[Value1,Value2]])),!.
91
+
92
+ interpretpart(not_append,Variable1,Variable2,Variable3,Vars1,Vars1) :-
93
+
94
+ getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),
95
+ debug_call(Skip,[[n,[not,append]],[Value1,Value2,Value3]]),
96
+ ((islist(Value1),islist(Value2),
97
+ not(append1(Value1,Value2,Value3))
98
+ %%val1emptyorvalsequal(Value3,Value3A),
99
+ %% putvalue(Variable3,Value3A,Vars1,Vars2)
100
+ )->
101
+ debug_exit(Skip,[[n,[not,append]],[Value1,Value2,Value3]])
102
+ ; debug_fail(Skip,[[n,[not,append]],[Value1,Value2,Value3]])),!.
103
+
104
+ interpretpart(not_stringconcat,Variable1,Variable2,Variable3,Vars1,Vars1) :-
105
+
106
+ getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),
107
+ debug_call(Skip,[[n,[not_stringconcat]],[Value1,Value2,Value3]]),
108
+ ((string(Value1),string(Value2),string(Value3),
109
+ stringconcat(Value1,Value2,Value3)
110
+ %%val1emptyorvalsequal(Value3,Value3A),
111
+ %%putvalue(Variable3,Value3A,Vars1,Vars2)
112
+ )->
113
+ debug_exit(Skip,[[n,[not_stringconcat]],[Value1,Value2,Value3]])
114
+ ; debug_fail(Skip,[[n,[not_stringconcat]],[Value1,Value2,Value3]])),!.
115
+
116
+ interpretpart([],Variable1,Vars1,Vars1) :-
117
+ getvalue(Variable1,Value1,Vars1),
118
+ %%getvalue(Value1,Value1A,Vars1),
119
+ %%isvalstr(Value1),
120
+ %%isvalstr(Value1A),
121
+ not(isempty(Value1)),
122
+ %%isempty(Value2),
123
+ %%val1emptyorvalsequal(Value2,Value1),
124
+ %%isval(Value2),
125
+ debug_call(Skip,[[n,[]],[Value1]]),
126
+ ( Value1=[]->
127
+ debug_exit(Skip,[[n,[]],[Value1]])
128
+ ; debug_fail(Skip,[[n,[]],[Value1]])),!.
129
+
130
+ interpretpart([],Variable1,Vars1,Vars2) :-
131
+ getvalue(Variable1,Value1,Vars1),
132
+ %%getvalue(Value1,Value1A,Vars1),
133
+ %%isvalstr(Value1),
134
+ %%isvalstr(Value1A),
135
+ isempty(Value1),
136
+ %%val1emptyorvalsequal(Value1,[]),
137
+ %%isval(Value2),
138
+ debug_call(Skip,[[n,[]],[variable]]),
139
+ ( putvalue(Variable1,[],Vars1,Vars2)->
140
+ debug_exit(Skip,[[n,[]],[[]]])
141
+ ; debug_fail(Skip,[[n,[]],[variable]])),!.
142
+
143
+ interpretpart("",Variable1,Vars1,Vars1) :-
144
+ getvalue(Variable1,Value1,Vars1),
145
+ %%getvalue(Value1,Value1A,Vars1),
146
+ %%isvalstr(Value1),
147
+ %%isvalstr(Value1A),
148
+ not(isempty(Value1)),
149
+ %%isempty(Value2),
150
+ %%val1emptyorvalsequal(Value2,Value1),
151
+ %%isval(Value2),
152
+ debug_call(Skip,[[n,""],[Value1]]),
153
+ ( Value1=""->
154
+ debug_exit(Skip,[[n,""],[Value1]])
155
+ ; debug_fail(Skip,[[n,""],[Value1]])),!.
156
+
157
+ interpretpart("",Variable1,Vars1,Vars2) :-
158
+ getvalue(Variable1,Value1,Vars1),
159
+ %%getvalue(Value1,Value1A,Vars1),
160
+ %%isvalstr(Value1),
161
+ %%isvalstr(Value1A),
162
+ isempty(Value1),
163
+ %%val1emptyorvalsequal(Value1,""),
164
+ %%isval(Value2),
165
+ debug_call(Skip,[[n,""],[variable]]),
166
+ ( putvalue(Variable1,"",Vars1,Vars2)->
167
+ debug_exit(Skip,[[n,""],[""]])
168
+ ; debug_fail(Skip,[[n,""],[variable]])),!.
169
+
170
+
171
+ interpretpart("_",_,V,V) :-
172
+ debug_call(Skip,[[n,"_"],[variable]]),
173
+ debug_exit(Skip,[[n,"_"],[variable]]),!.
Combination-Algorithm-Writer-Multiple-Predicates/rcaw.pl ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :- dynamic debug/1.
2
+
3
+ caw00(Debug,PredicateName,Rules,MaxLength,TotalVars,_InputVarList,_OutputVarList,Program1,_Program2,Ps1) :-
4
+ repeat,
5
+ %%MaxLength2 is MaxLength + 1,
6
+ %%TotalVars = MaxLength,
7
+ randvars(MaxLength,MaxLength,[],RandVars),
8
+ populatevars(RandVars,MaxLength,[],PV),
9
+ Code is MaxLength + 1 + 97,
10
+ char_code(Char,Code),
11
+ OutputVarList=[[[v,Char],1]],
12
+ retractall(debug(_)),
13
+ assertz(debug(Debug)),
14
+ retractall(totalvars(_)),
15
+ assertz(totalvars(TotalVars)),
16
+ caw0(PredicateName,Rules,MaxLength,PV,OutputVarList,Program1,_Program3,Ps),
17
+ sort(Ps,Ps1),not(Ps1=[]),!.
18
+
19
+ randvars(0,_,V,V) :- !.
20
+ randvars(N,L,V1,V2) :-
21
+ random(N1), N2A is round(97+(N1*L)), char_code(V3,N2A), V31=[v,V3], ((member(V31,V1))->randvars(N,L,V1,V2);
22
+ (append(V1,[V31],V4),
23
+ NA is N-1, randvars(NA,L,V4,V2))),!.
24
+ randvars2(0,_L,V,V) :- !.
25
+ randvars2(N,L,V1,V2) :-
26
+ random(N1), N2A is round(97+(N1*L)), char_code(V3,N2A), atom_string(V3,V4), %%V41=[v,V4],
27
+ ((member(V4,V1))->randvars2(N,L,V1,V2);
28
+ (append(V1,[V4],V5),
29
+ NA is N-1, randvars2(NA,L,V5,V2))),!.
30
+ populatevars([],_,PV,PV) :- !.
31
+ populatevars([RV1|RVs],MaxLength2,PV1,PV2) :-
32
+ randvars2(MaxLength2,MaxLength2,[],RV2),
33
+ append(PV1,[[RV1,RV2]],PV3),
34
+ populatevars(RVs,MaxLength2,PV3,PV2),!.
35
+
36
+ caw0(PredicateName,Rules,MaxLength,InputVarList,OutputVarList,Program1,Program2,Ps2) :-
37
+ varnames(InputVarList,[],InputVars,[],InputValues),
38
+ varnames(OutputVarList,[],OutputVars,[],_OutputValues),
39
+ retractall(outputvars(_)),
40
+ assertz(outputvars(OutputVars)),
41
+ append(InputVars,OutputVars,Vars11),
42
+ %%Vars11=InputVars,
43
+ %%Vars12=InputVars,
44
+ append(InputValues,OutputVars,Vars2),
45
+ %%append(InputValues,OutputValues,Values),
46
+ Query=[PredicateName,Vars2],
47
+ caw1(Query,PredicateName,Rules,MaxLength,Vars11,InputVars,InputVars,_,OutputVarList,OutputVars,Program1,Program2,[],Ps2), !.
48
+
49
+ caw1(_,_,_,0,_,_,_,_,_,_,_,_,Ps,Ps) :- !.
50
+ caw1(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program2,Programs2,Ps1) :-
51
+ MaxLength2 is MaxLength - 1,
52
+ addrules0(InputVars2,OutputVars,OutputVars,[],Program3),
53
+ %%writeln([addrules(InputVars2,OutputVars,OutputVars,[],PenultimateVars,[],Program3)]),
54
+ %%optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program4), %% IV2->3
55
+ %%writeln([optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program4)]),
56
+ append(Program1,Program3,Program5),
57
+ append(InputVars1,OutputVars,Vars2),
58
+ Program2=[
59
+ [PredicateName,Vars2,":-",
60
+ Program5
61
+ ]
62
+ ],debug(Debug),
63
+
64
+ %%writeln([interpret(Debug,Query,Program2,OutputVarList2)]),
65
+
66
+ interpret(Debug,Query,Program2,OutputVarList2),
67
+ %%writeln([interpret(Debug,Query,Program2,OutputVarList2)]),
68
+ append(Programs2,[[Query,Program2,OutputVarList2]],Programs3),
69
+ caw1a(Query,PredicateName,Rules,MaxLength2,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,[],_Program2,Programs3,Ps1),!.
70
+
71
+ %%caw1(_Query,_PredicateName,_Rules,_MaxLength,_VarList,_InputVars1,_InputVars2,_InputVars3,_OutputVarList,_OutputVars,_Program1,_Program4,Ps,Ps) :- writeln(here1),!.
72
+ caw1(_Query,_PredicateName,_Rules,_MaxLength2,_VarList,_InputVars1,_InputVars2,_InputVars3,_OutputVarList,_OutputVars,[],_Program2,Programs3,Programs3) :- !.
73
+ %%writeln([here1, caw1(Query,PredicateName,Rules,MaxLength2,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,[],_Program21,Programs3,Programs3)]),!.
74
+
75
+ caw1a(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program4,Ps1,Ps2) :-
76
+
77
+ %%writeln([caw(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,OutputVarList,OutputVars,Program1,Program4)]),
78
+ %%MaxLength2 is MaxLength - 1,
79
+ %%writeln(["ml",MaxLength2]),
80
+ reverse(InputVars2,InputVars5),
81
+ random_member([RuleName,NumInputs,NumOutputs],Rules),
82
+ %%writeln([member([RuleName,NumInputs,NumOutputs],Rules)]),
83
+ %%writeln([rule(RuleName,NumInputs,NumOutputs,VarList,VarList2,Rule)]),
84
+ rule(RuleName,NumInputs,NumOutputs,InputVars5,InputVars4,VarList,VarList2,Rule),
85
+ %%writeln([inputVars1,InputVars1]),
86
+ %% writeln([rule(RuleName,NumInputs,NumOutputs,InputVars5,InputVars4,VarList,VarList2,Rule)]),
87
+ append(Program1,[Rule],Program3),
88
+ %%writeln([inputVars3,InputVars3]),
89
+ %%InputVars2=InputVars3,
90
+ %%writeln([program4,Program4]),
91
+
92
+ %%caw1a(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program4,Ps,Ps) :-
93
+ %%writeln([here,caw1a(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program4,Ps,Ps)])
94
+
95
+
96
+ caw(Query,PredicateName,Rules,MaxLength,VarList2,InputVars1,InputVars4,InputVars3,OutputVarList,OutputVars,Program3,Program4,Ps1,Ps2),!.
97
+
98
+
99
+ caw(_,_,_,0,_,_,_,_,_,_,_,_,Ps,Ps) :- !.
100
+ caw(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program2,Programs1,Programs2) :-
101
+ MaxLength2 is MaxLength - 1,
102
+ addrules(InputVars2,OutputVars,OutputVars,[],_PenultimateVars,[],Program3),
103
+ %%writeln([addrules(InputVars2,OutputVars,OutputVars,[],PenultimateVars,[],Program3)]),
104
+ %%optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program4), %% IV2->3
105
+ %%writeln([optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program4)]),
106
+ append(Program1,Program3,Program5),
107
+ append(InputVars1,OutputVars,Vars2),
108
+ Program2=[
109
+ [PredicateName,Vars2,":-",
110
+ Program5
111
+ ]
112
+ ],debug(Debug),
113
+ %%***
114
+ %% () choose iv1 as args during caw, () eliminate e problem, could move forward in optimiser but don't need it v
115
+ %% 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
116
+ %% chunks will solve having at least 1 rule that connects to last output
117
+ %% can optimise number of inputs
118
+
119
+ %% test member, = in caw
120
+
121
+ %%writeln([interpret(Debug,Query,Program2,OutputVarList2)]),
122
+
123
+ interpret(Debug,Query,Program2,OutputVarList2),
124
+ %%writeln([interpret(Debug,Query,Program2,OutputVarList2)]),
125
+ append(Programs1,[[Query,Program2,OutputVarList2]],Programs3),
126
+ cawa(Query,PredicateName,Rules,MaxLength2,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,[],_Program2,Programs3,Programs2),!.
127
+ %%caw(_,_,_,_,_,_,_,_,_,_,_,_,Ps,Ps) :- !.
128
+
129
+ caw(_Query,_PredicateName,_Rules,_MaxLength2,_VarList,_InputVars1,_InputVars2,_InputVars3,_OutputVarList,_OutputVars,[],_Program2,Programs3,Programs3) :-
130
+ %%writeln([here2, caw(Query,PredicateName,Rules,MaxLength2,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,[],_Program21,Programs3,Programs3)]),
131
+ !.
132
+
133
+
134
+ cawa(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program4,Ps1,Ps2) :-
135
+ %%writeln([caw(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,OutputVarList,OutputVars,Program1,Program4)]),
136
+ %%MaxLength2 is MaxLength - 1,
137
+ %%writeln(["ml",MaxLength2]),
138
+ random_member([RuleName,NumInputs,NumOutputs],Rules),
139
+ %%writeln([member([RuleName,NumInputs,NumOutputs],Rules)]),
140
+ %%writeln([rule(RuleName,NumInputs,NumOutputs,VarList,VarList2,Rule)]),
141
+ rule(RuleName,NumInputs,NumOutputs,InputVars2,InputVars4,VarList,VarList2,Rule),
142
+ %%writeln([inputVars1,InputVars1]),
143
+ %% writeln([rule(RuleName,NumInputs,NumOutputs,InputVars2,InputVars4,VarList,VarList2,Rule)]),
144
+ append(Program1,[Rule],Program3),
145
+ %%writeln([inputVars3,InputVars3]),
146
+ %%InputVars2=InputVars3,
147
+ %%writeln([program4,Program4]),
148
+ caw(Query,PredicateName,Rules,MaxLength,VarList2,InputVars1,InputVars4,InputVars3,OutputVarList,OutputVars,Program3,Program4,Ps1,Ps2), !.
149
+
150
+ varnames([],Vars,Vars,Values,Values) :- !.
151
+ varnames(VarList,Vars1,Vars2,Values1,Values2) :-
152
+ VarList=[Var|Vars3],
153
+ Var=[VarName,Value],
154
+ append(Vars1,[VarName],Vars4),
155
+ append(Values1,[Value],Values3),
156
+ varnames(Vars3,Vars4,Vars2,Values3,Values2),!.
157
+
158
+ addrules0(_,_,[],Program,Program) :- !.
159
+ addrules0(VarList,OutputVars1,OutputVars2,Program1,Program2) :-
160
+ OutputVars2=[OutputVar|OutputVars3],
161
+ random_member(Var,VarList),
162
+ random_member(OutputVar,OutputVars1),
163
+ append(Program1,[[[n,=],[OutputVar,Var]]],Program3),
164
+ addrules0(VarList,OutputVars1,OutputVars3,Program3,Program2),!.
165
+
166
+ addrules(_,_,[],PV,PV,Program,Program) :- !.
167
+ addrules(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2) :-
168
+ restlast(VarList,[],_,Var),
169
+ %%OutputVars2=[OutputVar|OutputVars3],
170
+ random_member(OutputVar,OutputVars2),
171
+ delete(OutputVars1,OutputVar,OutputVars3),
172
+ %% member(Var,VarList),
173
+ random_member(OutputVar,OutputVars1),
174
+ append(Program1,[[[n,=],[OutputVar,Var]]],Program3),
175
+ append(PenultimateVars1,[Var],PenultimateVars3),
176
+ addrules2(VarList,OutputVars1,OutputVars3,PenultimateVars3,PenultimateVars2,Program3,Program2),!.
177
+
178
+ addrules2(_,_,[],PV,PV,Program,Program) :- !.
179
+ addrules2(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2) :-
180
+ %% restlast(VarList,[],_,Var),
181
+ OutputVars2=[OutputVar|OutputVars3],
182
+ random_member(Var,VarList),
183
+ not(member(Var,PenultimateVars1)),
184
+ random_member(OutputVar,OutputVars1),
185
+ append(Program1,[[[n,=],[OutputVar,Var]]],Program3),
186
+ append(PenultimateVars1,[Var],PenultimateVars3),
187
+ addrules2(VarList,OutputVars1,OutputVars3,PenultimateVars3,PenultimateVars2,Program3,Program2),!.
188
+
189
+ iv1flagdisjunction(A,B,true) :-
190
+ ((A=true)->true; (B=true)),!.
191
+ iv1flagdisjunction(_,_,false) :- !.
192
+
193
+
194
+ restlast([],_,_,_) :- fail, !.
195
+ restlast([Last],Rest,Rest,Last) :-
196
+ Last=[v,_],!.
197
+ restlast(Last,Rest,Rest,Last) :-
198
+ length(Last,1),!.
199
+ restlast(Vars1,Rest1,Rest2,Last) :-
200
+ Vars1=[Var|Vars2],
201
+ append(Rest1,[Var],Rest3),
202
+ restlast(Vars2,Rest3,Rest2,Last),!.
203
+
204
+
205
+
206
+ rule(RuleName,1,1,InputVars1,InputVars2,VarList,VarList2,Rule) :-
207
+ random_member(Var,InputVars1),
208
+ rule2(RuleName,Var,VarList,VarList2,Rule,Var1),
209
+ restlast(InputVars1,[],_,Last), %% Last should be outputs - 2nd last rule?
210
+ (Var=Last->true;Last=Var1),
211
+ append(InputVars1,[Var1],InputVars2),!.
212
+ rule(RuleName,1,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-
213
+ random_member(Var,InputVars1),
214
+ rule3(RuleName,Var,VarList,VarList2,Rule,Var1,Var2),
215
+ restlast(InputVars1,[],_,Last),
216
+ (Var=Last->true;
217
+ (Last=Var1->true;Last=Var2)),
218
+ append(InputVars1,[Var1,Var2],InputVars2),!.
219
+ rule(RuleName,2,0,InputVars,InputVars,VarList,VarList,Rule) :-
220
+ %%writeln([rule(RuleName,2,1,InputVars1,InputVars2,VarList,VarList2,Rule)]),
221
+ random_member(Var,InputVars),
222
+ random_member(Vara,InputVars),
223
+ rule6(RuleName,Var,Vara,_VarList,_VarList2,Rule),
224
+ restlast(InputVars,[],_,Last),
225
+ %%writeln([last,Last]),
226
+ (Var=Last->true;Vara=Last),!.
227
+ rule(RuleName,2,1,InputVars1,InputVars2,VarList,VarList2,Rule) :-
228
+ %%writeln([rule(RuleName,2,1,InputVars1,InputVars2,VarList,VarList2,Rule)]),
229
+ random_member(Var,InputVars1),
230
+ random_member(Vara,InputVars1),
231
+ rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1),
232
+ restlast(InputVars1,[],_,Last),
233
+ %%writeln([last,Last]),
234
+ ((Var=Last->true;Vara=Last)->true;
235
+ (Last=Var1)),
236
+ %%writeln([var,Var,vara,Vara]),
237
+ append(InputVars1,[Var1],InputVars2),!.
238
+ rule(RuleName,2,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-
239
+ random_member(Var,InputVars),
240
+ random_member(Vara,InputVars),
241
+ rule5(RuleName,Var,Vara,VarList,VarList2,Rule,Var1,Var2),
242
+ restlast(InputVars1,[],_,Last),
243
+ ((Var=Last->true;Vara=Last)->true;
244
+ (Last=Var1->true;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
245
+ append(InputVars1,[Var1,Var2],InputVars2),!.
246
+
247
+
248
+ rule2(RuleName,Var,VarList,VarList2,Rule,Var1) :-
249
+ var(VarList,Var1,VarList2),
250
+ Rule=[RuleName,[Var,Var1]],!.%%,
251
+ %%member(Var,!.
252
+
253
+ rule3(RuleName,Var,VarList,VarList3,Rule,Var1,Var2) :-
254
+ var(VarList,Var1,VarList2),
255
+ var(VarList2,Var2,VarList3),
256
+ Rule=[RuleName,[Var,Var1,Var2]],!.
257
+
258
+ rule6(RuleName,Var,Vara,_VarList,_VarList2,Rule) :-
259
+ Rule=[RuleName,[Var,Vara]],!.
260
+
261
+ rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1) :-
262
+ var(VarList,Var1,VarList2),
263
+ Rule=[RuleName,[Var,Vara,Var1]],!.
264
+
265
+ %%ae be with predicate support also
266
+ rule5(RuleName,Var,Vara,VarList,VarList3,Rule,Var1,Var2) :-
267
+ var(VarList,Var1,VarList2),
268
+ var(VarList2,Var2,VarList3),
269
+ Rule=[RuleName,[Var,Vara,Var1,Var2]],!.
270
+
271
+ %%var(Item,Var,Vars,Vars) :-
272
+ %% member([Item,Var],Vars).
273
+ var(Vars1,Var1,Vars2) :-
274
+ length(Vars1,Vars1Length1),
275
+ Vars1Length2 is Vars1Length1-1,
276
+ length(Vars3,Vars1Length2),
277
+ append(Vars3,[Var2],Vars1),
278
+ Var2=[v,Var21],
279
+ char_code(Var21,Var2Code1),
280
+ Var2Code2 is Var2Code1 + 1,
281
+ var2(Var2Code2,Var1),
282
+ append(Vars1,[Var1],Vars2),!.
283
+
284
+ var2(Code,Var1) :-
285
+ outputvars(OutputVars),
286
+ totalvars(TotalVars),
287
+ Code2 is 96+TotalVars,
288
+ Code =< Code2, %% 122
289
+ char_code(Var11,Code),
290
+ Var1=[v,Var11],
291
+ not(member(Var1,OutputVars)),!.
292
+ var2(Var2Code,Code3) :-
293
+ Var2Code2 is Var2Code + 1,
294
+ totalvars(TotalVars),
295
+ Code2 is 96+TotalVars,
296
+ Var2Code2 =< Code2,
297
+ var2(Var2Code2,Code31),
298
+ Code3=[v,Code31],!.
Combination-Algorithm-Writer-Multiple-Predicates/remove_duplicate_predicates.pl ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %% remove_duplicate_predicates
2
+
3
+ %%remove_duplicate_predicates(Predicates1
4
+ %% Predicates1=[Predicate2|Predicates3],
5
+
6
+ %% remvdup([a,a,a,b,b,c],[],A)
7
+ %% A = [a, b, c].
8
+
9
+ remvdup([],A,A):-!.
10
+ remvdup(A,B,C):-
11
+ A=[A1|As],
12
+ delete(As,A1,B2),
13
+ append(B,[A1],D),
14
+ remvdup(B2,D,C).
Combination-Algorithm-Writer-Stable/LICENSE ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2019, Lucian Green
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Combination-Algorithm-Writer-Stable/LPI CAW commands workaround.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ LPI CAW commands workaround for single argument commands not working, but no singletons working
2
+
3
+ _A - enter value in header (one output, so is it, do outputs one at a time)
4
+
5
+ [] and "" - enter [] or "" somewhere in header and use normal "="
6
+
7
+ not - can still use except for [], "", etc., see previous line
8
+
9
+ With no singletons working, CAWP is more stable than CAWMP
Combination-Algorithm-Writer-Stable/README.md ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Combination-Algorithm-Writer-Stable
2
+ Combination Algorithm Writer with Predicates Stable (CAWPS)
3
+
4
+ Please note: This works with the example below, which the CAWMP repository doesn't.
5
+
6
+ Combination Algorithm Writer is a SWI-Prolog algorithm that finds combinations of given commands that satisfy the given input and output.
7
+
8
+ It verifies for no singletons.
9
+
10
+ # Prerequisites
11
+
12
+ * Please download and install SWI-Prolog for your machine at `https://www.swi-prolog.org/build/`.
13
+
14
+ # 1. Install manually
15
+
16
+ Download <a href="http://github.com/luciangreen/Combination-Algorithm-Writer-Stable/">this repository</a>.
17
+
18
+ # 2. Or Install from List Prolog Package Manager (LPPM)
19
+
20
+ * Download the <a href="https://github.com/luciangreen/List-Prolog-Package-Manager">LPPM Repository</a>:
21
+
22
+ ```
23
+ mkdir GitHub
24
+ cd GitHub/
25
+ git clone https://github.com/luciangreen/List-Prolog-Package-Manager.git
26
+ cd List-Prolog-Package-Manager
27
+ swipl
28
+ ['lppm'].
29
+ lppm_install("luciangreen","Combination-Algorithm-Writer-Stable").
30
+ halt
31
+ ```
32
+
33
+ # Running
34
+
35
+ * In Shell:
36
+ `cd Combination-Algorithm-Writer-Stable`
37
+ `swipl`
38
+
39
+ * Load all files in the form:
40
+ ```
41
+ ['lpi_caw_commands.pl'].
42
+ ['listprologinterpreter1listrecursion4copy52'].
43
+ ['listprologinterpreter3preds5copy52'].
44
+ ['caw5copy11'].
45
+ ```
46
+ Running
47
+
48
+ ```
49
+ caw00(off,f,[[+,2,1]],4,8,[[a,1],[b,1],[c,2],[d,1]],[[e,5]],[[[f1,4,1],[a,b,c,d,e],(:-),[[+,[a,b,f]],[+,[c,f,g]],[+,[d,g,h]],[=,[e,h]]]]],[],P),writeln(P).
50
+
51
+ [[f1,[a,b,c,d,e],:-,[[+,[a,b,f]],[+,[c,f,g]],[+,[d,g,h]],[=,[e,h]]]],[f,[a,b,c,d,e],:-,[[+,[a,b,f]],[+,[c,d,g]],[+,[f,g,h]],[=,[e,h]]]]]
52
+ .
53
+ .
54
+ .
55
+ ```
56
+
57
+ ```
58
+ caw00(off,reverse,[[head,1,1],[tail,1,1],[wrap,1,1],[append,2,1],[reverse,2,1]],6,8,[[a,[1,2,3]],[b,[]]],[[c,[3,2,1]]],[[[reverse,2,1],[[],a,a]]],[],P),writeln(P).
59
+
60
+ not finished
61
+ ```
62
+
63
+ Note: Use (:-) instead of :-.
64
+
65
+ # CAWPS API
66
+
67
+ * To run CAWPS on a Prolog server:
68
+ * Move `cawps-api.pl` to the root (`/username/` or `~` on a server) of your machine.
69
+ * Re-enter the paths to your Prolog files in it.
70
+ * Enter `[cawps-api.pl]` in SWI-Prolog and `server(8000).`.
71
+ * On a local host access the algorithm at `http://127.0.0.1:8000` and replace 127.0.0.1 with your server address on a server.
Combination-Algorithm-Writer-Stable/caw5copy11.pl ADDED
@@ -0,0 +1,386 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :- dynamic debug/1.
2
+ :- dynamic totalvars/1.
3
+ :- dynamic outputvars/1.
4
+
5
+ :- dynamic test/1.
6
+
7
+ :-include('../listprologinterpreter/la_strings.pl').
8
+
9
+ test1(A) :- test(B),A is B+1,retractall(test(_)),assertz(test(A)).
10
+
11
+ caw00(Debug,PredicateName,Rules1,MaxLength,TotalVars,InputVarList,OutputVarList,Predicates1,Program1,Program2) :-
12
+ split3(Predicates1,[],Rules2),
13
+ split2(Predicates1,[],Predicates),
14
+ %%writeln([Rules2,Predicates]),
15
+ append(Rules1,Rules2,Rules3),
16
+
17
+ retractall(debug(_)),
18
+ assertz(debug(Debug)),
19
+ retractall(totalvars(_)),
20
+ assertz(totalvars(TotalVars)),
21
+
22
+ retractall(test(_)),
23
+ assertz(test(0)),
24
+ caw0(Predicates,PredicateName,Rules3,MaxLength,InputVarList,OutputVarList,Program1,Program2).
25
+
26
+ caw0(Predicates,PredicateName,Rules,MaxLength,InputVarList,OutputVarList,Program1,Program2) :-
27
+ varnames(InputVarList,[],InputVars,[],InputValues),
28
+ varnames(OutputVarList,[],OutputVars,[],_OutputValues),
29
+ retractall(outputvars(_)),
30
+ assertz(outputvars(OutputVars)),
31
+ append(InputVars,OutputVars,Vars11),
32
+ %%Vars11=InputVars,
33
+ %%Vars12=InputVars,
34
+ append(InputValues,OutputVars,Vars2),
35
+ %%append(InputValues,OutputValues,Values),
36
+ Query=[PredicateName,Vars2],
37
+ caw(Predicates,Query,PredicateName,Rules,MaxLength,Vars11,InputVars,InputVars,_,OutputVarList,OutputVars,Program1,Program2).
38
+ caw(_,_,_,_,0,_,_,_,_,_,_,_) :- fail, !.
39
+ caw(Predicates,Query,PredicateName,_,_,_VarList,InputVars1,InputVars2,_,OutputVarList,OutputVars,Program1,Program2) :-
40
+ addrules(InputVars2,OutputVars,OutputVars,[],PenultimateVars,[],Program3),
41
+ %%writeln([addrules(InputVars2,OutputVars,OutputVars,[],PenultimateVars,[],Program3)]),
42
+ %%optimise(Program1,InputVars1,InputVars3,PenultimateVars,Program4), %% IV2->3
43
+ %%writeln([optimise(Program1,InputVars1,InputVars3,PenultimateVars,Program4)]),
44
+ append(Program1,Program3,Program5),
45
+ append(InputVars1,OutputVars,Vars2),
46
+ Program22=[
47
+ [PredicateName,Vars2,(:-),
48
+ Program5
49
+ ]
50
+ ],
51
+
52
+ append(Predicates,Program22,Program2),
53
+
54
+ (debug(on)->Debug=on;Debug=off),
55
+ %writeln([interpret(Debug,Query,Program2,OutputVarList)]),
56
+ %%writeln(""),
57
+ %trace,
58
+
59
+ catch(call_with_time_limit(0.05,
60
+ interpret(Debug,Query,Program2,OutputVarList)),
61
+ time_limit_exceeded,
62
+ fail),
63
+ %test1(A),writeln(A),
64
+ no_singletons(Vars2,Program5),!.
65
+ caw(Predicates,Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program4) :-
66
+ %%writeln([caw(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,OutputVarList,OutputVars,Program1,Program4)]),
67
+ MaxLength2 is MaxLength - 1,
68
+ %%writeln(["ml",MaxLength2]),
69
+ member([RuleName,NumInputs,NumOutputs],Rules),
70
+ %%writeln([member([RuleName,NumInputs,NumOutputs],Rules)]),
71
+ %%writeln([rule(RuleName,NumInputs,NumOutputs,VarList,VarList2,Rule)]),
72
+ rule(Program1,RuleName,NumInputs,NumOutputs,InputVars2,InputVars4,VarList,VarList2,Rule),
73
+ %%writeln([rule(RuleName,NumInputs,NumOutputs,InputVars1,InputVars3,VarList,VarList2,Rule)]),
74
+ append(Program1,[Rule],Program3),
75
+ %%writeln([inputVars3,InputVars3]),
76
+ %%InputVars2=InputVars3,
77
+ %%writeln([program4,Program4]),
78
+ caw(Predicates,Query,PredicateName,Rules,MaxLength2,VarList2,InputVars1,InputVars4,InputVars3,OutputVarList,OutputVars,Program3,Program4).
79
+
80
+ varnames([],Vars,Vars,Values,Values) :- !.
81
+ varnames(VarList,Vars1,Vars2,Values1,Values2) :-
82
+ VarList=[Var|Vars3],
83
+ Var=[VarName,Value],
84
+ append(Vars1,[VarName],Vars4),
85
+ append(Values1,[Value],Values3),
86
+ varnames(Vars3,Vars4,Vars2,Values3,Values2),!.
87
+
88
+ addrules(_,_,[],PV,PV,Program,Program) :- !.
89
+ addrules(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2) :-
90
+ OutputVars2=[OutputVar|OutputVars3],
91
+ member(Var,VarList),
92
+ member(OutputVar,OutputVars1),
93
+ append(Program1,[[=,[OutputVar,Var]]],Program3),
94
+ append(PenultimateVars1,[Var],PenultimateVars3),
95
+ addrules(VarList,OutputVars1,OutputVars3,PenultimateVars3,PenultimateVars2,Program3,Program2).
96
+
97
+ %% optimise([[append,[a,a,d]],[append,[a,a,e]],[append,[a,a,f]],[append,[a,b,g]]],[g],P).
98
+
99
+ optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program2) :-
100
+ findrulesflowingtopv1(Program1,InputVars1,InputVars2,PenultimateVars,[],Rules,true),
101
+ %%findrulesflowingtopv1a(Program1,_Program32,InputVars1,InputVars2,PenultimateVars,[],_Rules1),
102
+ intersection(Program1,Rules,Program3),
103
+ unique1(Program3,[],Program2).
104
+ findrulesflowingtopv1(_,_,_,[],Rules,Rules,false).
105
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
106
+ (atom(Var);length(Var,1)),
107
+ findrulesflowingtopv20(Program0,Program0,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1).
108
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Vars1,Rules1,Rules2,IV1Flag1) :-
109
+ Vars1=[Var|Vars2],
110
+ findrulesflowingtopv20(Program0,Program0,InputVars1,InputVars2,Var,Rules1,Rules3,IV1Flag2),
111
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Vars2,Rules3,Rules2,IV1Flag3),
112
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
113
+
114
+ %%findrulesflowingtopv2([],Program,Program,_,_,Rules,Rules).
115
+ findrulesflowingtopv20(_,[],_InputVars1,_InputVars2,_Var,Rules,Rules,false).
116
+ findrulesflowingtopv20(Program0,Rules4,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
117
+ Rules4=[Rule|Rules],
118
+ (findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules3,IV1Flag2)->true;(Rules3=Rules1,IV1Flag2=false)),
119
+ %%delete(Program0,Rule,Program1),
120
+ findrulesflowingtopv20(Program0,Rules,InputVars1,InputVars2,Var,Rules3,Rules2,IV1Flag3),%%p1->0
121
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
122
+ %%findrulesflowingtopv2(_,[],[],_,_,_,Rules,Rules).
123
+ findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
124
+ Rule=[_PredicateName,Vars],
125
+ restlast(Vars,[],Rest,Var),
126
+ %%delete(Program1,[PredicateName,Vars],Program2),
127
+ %%Program2=Program1,
128
+ %%(not(intersection(Rulesx,Rules1))-> x
129
+ %% append, append, unique1
130
+ %%append(Rules1,[Rule],Rules3);Rules3=Rules1),
131
+
132
+ %%member(Var2,Rest),
133
+ %%member(Var2,InputVars1),
134
+
135
+ length(Rest,Length1), Length1>=1,
136
+ subtract(Rest,InputVars1,IV3s),
137
+ length(IV3s,Length3),
138
+ subtract(Rest,IV3s,IV1s),
139
+ length(IV1s,Length2), Length2>=1,
140
+ subtract(IV3s,InputVars2,[]),
141
+
142
+ IV1Flag2=true,
143
+
144
+ %%delete(Program0,Rule,Program1),
145
+
146
+ %%(delete(Program0,Rule,Program3),
147
+ %%iv3s1(IV3s,Program3,IV3s,[]),
148
+ (Length3>=1->
149
+ (findrulesflowingtopv1(Program0,InputVars1,InputVars2,IV3s,[],Rules5,IV1Flag3),not(Rules5=[]));
150
+ (Rules5=[],IV1Flag3=false)),
151
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag4),
152
+ %%->true; Rules5=[],IV1Flag1=IV1Flag4),
153
+
154
+ ((findrulesflowingtopv1(Program0,InputVars1,InputVars2,IV1s,[],Rules6,IV1Flag5), %%iv1s->rest, etc
155
+ iv1flagdisjunction(IV1Flag4,IV1Flag5,IV1Flag1))->true;(Rules6=[],IV1Flag1=IV1Flag4)),
156
+
157
+ append([Rule],Rules1,Rules9),
158
+ append(Rules9,Rules5,Rules7),
159
+ append(Rules7,Rules6,Rules8),
160
+ unique1(Rules8,[],Rules2).
161
+
162
+ /**
163
+ findrulesflowingtopv2(_Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
164
+ Rule=[_PredicateName,Vars],
165
+ restlast(Vars,[],Rest,Var),
166
+ %%delete(Program1,[PredicateName,Vars],Program2),
167
+ %%Program2=Program1,
168
+ (not(member(Rule,Rules1))->
169
+ append(Rules1,[Rule],Rules2);Rules2=Rules1),
170
+ subset(Rest,InputVars2),
171
+ intersection(Rest,InputVars1,Intersection),
172
+ length(Intersection,0),
173
+ %% not((member(Var2,Rest),
174
+ %% member(Var2,InputVars1))),
175
+ IV1Flag1=false.
176
+ **/
177
+ /**
178
+ findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
179
+ Rule=[_PredicateName,Vars],
180
+ restlast(Vars,[],Rest,Var),
181
+ %%delete(Program1,[PredicateName,Vars],Program3),
182
+ %%Program3=Program1,
183
+ %%append(Rules1,[Rule],Rules3),
184
+ subset(Rest,InputVars2),
185
+
186
+ intersection(Rest,InputVars1,Intersection),
187
+ length(Intersection,0),
188
+ %% not((member(Var2,Rest),
189
+ %% member(Var2,InputVars1))),
190
+ %% delete(Program0,Rule,Program1),
191
+ IV1Flag2=false,
192
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Rest,[],Rules4,IV1Flag3),
193
+ %%not(Rules4=[]),
194
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1),
195
+ append(Rules1,[Rule],Rules7),
196
+ append(Rules7,Rules4,Rules8),
197
+ unique1(Rules8,[],Rules2).
198
+ **/
199
+ /**
200
+ %%->true;(Program2=Program1,Rules2=Rules1)).
201
+ findrulesflowingtopv2(Rule,Program0,Program1,_Program2,InputVars1,InputVars,Var,Rules1,Rules2,IV1Flag1) :-
202
+ Rule=[PredicateName,Vars],
203
+ restlast(Vars,[],Rest,Var),
204
+ %%delete(Program1,[PredicateName,Vars],Program4),
205
+ %%Program4=Program1,
206
+ append(Rules1,[[PredicateName,Vars]],Rules3),
207
+ findrulesflowingtopv1(Program0,Program1,_Program2,InputVars1,InputVars,Rest,Rules3,Rules2,IV1Flag3),
208
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
209
+ %%findrulesflowingtopv2(Program5,Program2,Rest,Rules3,Rules2).
210
+ **/
211
+ iv1flagdisjunction(A,B,true) :-
212
+ (A=true); (B=true).
213
+ iv1flagdisjunction(_,_,false).
214
+ /**
215
+ iv3s0([],_,IV3s1,IV3s2).
216
+ iv3s0(IV3s,Program0,IV3s1,IV3s2).
217
+ IV3s=[IV3|IV3s3],
218
+ iv3s1(IV3,Program0,IV3s1,IV3s4),
219
+ iv3s0(IV3s3,Program0,IV3s4,IV3s2).
220
+ iv3s1(_,[],IV3s,IV3s).
221
+ iv3s1(IV3,Program0,IV3s1,IV3s2) :-
222
+ Program0=[Rule|Rules],
223
+ iv3s2(IV3,Rule,IV3s1,IV3s3),
224
+ iv3s1(IV3,Rules,IV3s3,IV3s2).
225
+ iv3s2(IV3,Rule,IV3s,IV3s1,IV3s2).
226
+ Rule=[_PredicateName,Vars],
227
+ restlast(Vars,[],_Rest,IV3),
228
+ delete(IV3s1,IV3,IV3s2).
229
+ findrulesflowingtopv1a(_,_,_,_,[],Rules,Rules).
230
+ findrulesflowingtopv1a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2) :-
231
+ atom(Var),
232
+ findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2).
233
+ findrulesflowingtopv1a(Program1,Program2,InputVars1,InputVars2,Vars1,Rules1,Rules2) :-
234
+ Vars1=[Var|Vars2],
235
+ findrulesflowingtopv2(Program1,Program3,InputVars1,InputVars2,Var,Rules1,Rules3),
236
+ findrulesflowingtopv1a(Program3,Program2,InputVars1,InputVars2,Vars2,Rules3,Rules2).
237
+ %%findrulesflowingtopv2([],Program,Program,_,_,Rules,Rules).
238
+ findrulesflowingtopv2a([],[],_,_,_,Rules,Rules).
239
+ findrulesflowingtopv2a(Program1,Program2,_InputVars1,InputVars2,Var,Rules1,Rules2) :-
240
+ member([PredicateName,Vars],Program1),
241
+ restlast(Vars,[],Rest,Var),
242
+ (
243
+ %%delete(Program1,[PredicateName,Vars],Program2),
244
+ Program2=Program1,
245
+ append(Rules1,[[PredicateName,Vars]],Rules2),
246
+ subset(Rest,InputVars2)).
247
+ findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2) :-
248
+ member([PredicateName,Vars],Program1),
249
+ restlast(Vars,[],Rest,Var),
250
+ (
251
+ %%delete(Program1,[PredicateName,Vars],Program3),
252
+ Program3=Program1,
253
+ append(Rules1,[[PredicateName,Vars]],Rules3),
254
+ subset(Rest,InputVars2)),
255
+ findrulesflowingtopv1a(Program3,Program2,InputVars1,InputVars2,Rest,Rules3,Rules2).
256
+ %%->true;(Program2=Program1,Rules2=Rules1)).
257
+ findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars,Var,Rules1,Rules2) :-
258
+ member([PredicateName,Vars],Program1),
259
+ restlast(Vars,[],Rest,Var),
260
+ %%delete(Program1,[PredicateName,Vars],Program4),
261
+ Program4=Program1,
262
+ append(Rules1,[[PredicateName,Vars]],Rules3),
263
+ findrulesflowingtopv1a(Program4,Program2,InputVars1,InputVars,Rest,Rules3,Rules2).
264
+ %%findrulesflowingtopv2(Program5,Program2,Rest,Rules3,Rules2).
265
+ **/
266
+
267
+ restlast([],_,_,_) :- fail, !.
268
+ restlast([Last],Rest,Rest,Last) :-
269
+ atom(Last),!.
270
+ restlast(Last,Rest,Rest,Last) :-
271
+ length(Last,1),!.
272
+ restlast(Vars1,Rest1,Rest2,Last) :-
273
+ Vars1=[Var|Vars2],
274
+ append(Rest1,[Var],Rest3),
275
+ restlast(Vars2,Rest3,Rest2,Last),!.
276
+
277
+
278
+ rule(Predicates,RuleName,NumInputs,NumOutputs,InputVars20,InputVars4,VarList0,VarList2,Rule) :-
279
+
280
+ %InputVars20=InputVars2,%
281
+ %VarList0=VarList,
282
+ %writeln(Predicates),
283
+ /*
284
+ %writeln([Predicates,Predicates]),
285
+ %(not(Predicates=[])->trace;true),
286
+ findall(Rule_vars1,member([_Rule_name,Rule_vars1],Predicates),Rule_vars2),foldr(append,Rule_vars2,Rule_vars3),
287
+
288
+ % count vars
289
+ sort(Rule_vars3,K),
290
+ findall(G,(member(G,K),findall(G,member(G,Rule_vars3),H),length(H,J),J>2),L),
291
+ % remove vars occuring more than twice
292
+
293
+ (var(InputVars20)->InputVars20=InputVars2;(%trace,
294
+ subtract(InputVars20,L,InputVars2)%,notrace
295
+ )),
296
+
297
+ (var(VarList0)->VarList0=VarList;subtract(VarList0,L,VarList)),
298
+ */
299
+
300
+ rule1(RuleName,NumInputs,NumOutputs,InputVars20,InputVars4,VarList0,VarList2,Rule).
301
+
302
+
303
+ rule1(RuleName,1,1,InputVars1,InputVars2,VarList,VarList2,Rule) :-
304
+ member(Var,InputVars1),
305
+ rule2(RuleName,Var,VarList,VarList2,Rule,Var1),
306
+ append(InputVars1,[Var1],InputVars2).
307
+ rule2(RuleName,Var,VarList,VarList2,Rule,Var1) :-
308
+ var(VarList,Var1,VarList2),
309
+ Rule=[RuleName,[Var,Var1]],!.
310
+
311
+ rule1(RuleName,1,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-
312
+ member(Var,InputVars1),
313
+ rule3(RuleName,Var,VarList,VarList2,Rule,Vars),
314
+ append(InputVars1,Vars,InputVars2).
315
+ rule3(RuleName,Var,VarList,VarList3,Rule,[Var1,Var2]) :-
316
+ var(VarList,Var1,VarList2),
317
+ var(VarList2,Var2,VarList3),
318
+ Rule=[RuleName,[Var,Var1,Var2]],!.
319
+
320
+ rule1(RuleName,2,1,InputVars1,InputVars2,VarList,VarList2,Rule) :-
321
+ member(Var,InputVars1),
322
+ member(Vara,InputVars1),
323
+ rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1),
324
+ append(InputVars1,[Var1],InputVars2).
325
+ rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1) :-
326
+ var(VarList,Var1,VarList2),
327
+ Rule=[RuleName,[Var,Vara,Var1]],!.
328
+
329
+ rule1(RuleName,2,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-
330
+ member(Var,InputVars),
331
+ member(Vara,InputVars),
332
+ rule5(RuleName,Var,Vara,VarList,VarList2,Rule,Vars),
333
+ append(InputVars1,Vars,InputVars2).
334
+ rule5(RuleName,Var,Vara,VarList,VarList3,Rule,[Var1,Var2]) :-
335
+ var(VarList,Var1,VarList2),
336
+ var(VarList2,Var2,VarList3),
337
+ Rule=[RuleName,[Var,Vara,Var1,Var2]],!.
338
+
339
+ %%var(Item,Var,Vars,Vars) :-
340
+ %% member([Item,Var],Vars).
341
+ var(Vars1,Var1,Vars2) :-
342
+ length(Vars1,Vars1Length1),
343
+ Vars1Length2 is Vars1Length1-1,
344
+ length(Vars3,Vars1Length2),
345
+ append(Vars3,[Var2],Vars1),
346
+ char_code(Var2,Var2Code1),
347
+ Var2Code2 is Var2Code1 + 1,
348
+ var2(Var2Code2,Var1),
349
+ append(Vars1,[Var1],Vars2),!.
350
+
351
+ var2(Code,Var1) :-
352
+ outputvars(OutputVars),
353
+ totalvars(TotalVars),
354
+ Code2 is 96+TotalVars,
355
+ Code =< Code2, %% 122
356
+ char_code(Var1,Code),
357
+ not(member(Var1,OutputVars)),!.
358
+ var2(Var2Code,Code3) :-
359
+ Var2Code2 is Var2Code + 1,
360
+ totalvars(TotalVars),
361
+ Code2 is 96+TotalVars,
362
+ Var2Code2 =< Code2,
363
+ var2(Var2Code2,Code3),!.
364
+
365
+
366
+ no_singletons(Vars1,Program):-
367
+ findall(DA,(member(C,Program),C=[_E,D],member(DA,D)),Vars2),
368
+ %%append_list(Vars2,Vars2A),
369
+ append(Vars1,Vars2,Vars3),
370
+ findall(Count1,(member(Item,Vars3),aggregate_all(count,(member(Item,Vars3)),Count1),
371
+ Count1=1),G),G=[].
372
+
373
+ split3([],List,List) :- !.
374
+ split3(Predicates1,List1,List2) :-
375
+ Predicates1=[Item1|List4],
376
+ Item1= [[Name,In,Out]|_Rest],
377
+ append(List1,[[Name,In,Out]],List6),
378
+ split3(List4,List6,List2),!.
379
+
380
+ split2([],List,List) :- !.
381
+ split2(Predicates1,List1,List2) :-
382
+ Predicates1=[Item1|List4],
383
+ Item1= [[Name,_In,_Out]|Rest],
384
+ append(List1,[[Name|Rest]],List6),
385
+ split2(List4,List6,List2),!.
386
+
Combination-Algorithm-Writer-Stable/cawplistprolog.pl ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ :-include('lpi_caw_commands.pl').
2
+ :-include('listprologinterpreter1listrecursion4copy52').
3
+ :-include('listprologinterpreter3preds5copy52').
4
+ :-include('caw5copy11').
5
+ :-include('cawpverify.pl').
Combination-Algorithm-Writer-Stable/cawps-api.pl ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :- use_module(library(http/thread_httpd)).
2
+ :- use_module(library(http/http_dispatch)).
3
+ :- use_module(library(http/http_error)).
4
+ :- use_module(library(http/html_write)).
5
+
6
+ % we need this module from the HTTP client library for http_read_data
7
+ :- use_module(library(http/http_client)).
8
+ :- http_handler('/', web_form, []).
9
+
10
+ :- include('files/Combination-Algorithm-Writer-Stable/listprologinterpreter1listrecursion4copy52.pl').
11
+ :- include('files/Combination-Algorithm-Writer-Stable/listprologinterpreter3preds5copy52.pl').
12
+ :- include('files/Combination-Algorithm-Writer-Stable/caw5copy11.pl').
13
+ :- include('files/Combination-Algorithm-Writer-Stable/lpi_caw_commands.pl').
14
+
15
+ server(Port) :-
16
+ http_server(http_dispatch, [port(Port)]).
17
+
18
+ /*
19
+ browse http://127.0.0.1:8000/
20
+ This demonstrates handling POST requests
21
+ */
22
+
23
+ web_form(_Request) :-
24
+ reply_html_page(include('Combination Algorithm Writer Stable'),
25
+
26
+ [
27
+ form([action='/landing', method='POST'], [
28
+ /**
29
+ p([], [
30
+ label([for=debug],'Debug (on/off):'),
31
+ input([name=debug, type=textarea])
32
+ ]),
33
+ **/
34
+ p([], [
35
+ label([for=predicatename],'Predicate Name (e.g. f):'),
36
+ input([name=predicatename, type=textarea])
37
+ ]),
38
+
39
+ p([], [
40
+ label([for=rules],'Rules to choose from (e.g. [[+,2,1]]):'),
41
+ input([name=rules, type=textarea])
42
+ ]),
43
+
44
+ p([], [
45
+ label([for=maxlength],'Max Length of Algorithm Body (e.g. 4):'),
46
+ input([name=maxlength, type=textarea])
47
+ ]),
48
+
49
+ p([], [
50
+ label([for=totalvars],'Total Vars in Predicate (e.g. 8):'),
51
+ input([name=totalvars, type=textarea])
52
+ ]),
53
+ p([], [
54
+ label([for=inputvarlist],'InputVarList (e.g. [[a,1],[b,1],[c,2],[d,1]]):'),
55
+ input([name=inputvarlist, type=textarea])
56
+ ]),
57
+
58
+ p([], [
59
+ label([for=outputvarlist],'OutputVarList (e.g. [[e,5]]):'),
60
+ input([name=outputvarlist, type=textarea])
61
+ ]),
62
+
63
+ p([], [
64
+ label([for=algorithmlibrary],'Algorithm Library (e.g. [[[f1,4,1],[a,b,c,d,e],(:-),[[+,[a,b,f]],[+,[c,f,g]],[+,[d,g,h]],[=,[e,h]]]]], where 4 is the number of inputs and 1 is the number of outputs):'),
65
+ input([name=algorithmlibrary, type=textarea])
66
+ ]),
67
+
68
+ p([], input([name=submit, type=submit, value='Submit'], []))
69
+ ])]).
70
+
71
+ :- http_handler('/landing', landing_pad, []).
72
+
73
+ landing_pad(Request) :-
74
+ member(method(post), Request), !,
75
+ http_read_data(Request, Data, []),
76
+ format('Content-type: text/html~n~n', []),
77
+ format('<p>', []),
78
+ %%portray_clause(Data),
79
+
80
+ %%term_to_atom(Term,Data),
81
+
82
+ %% Predicatename,Rules,Maxlength,Totalvars,Inputvarlist,Outputvarlist,Algorithmlibrary
83
+ %%portray_clause(Data),
84
+
85
+
86
+ Data=[%%debug='off',%%Debug1,
87
+ predicatename=Predicatename,rules=Rules,maxlength=Maxlength,totalvars=Totalvars,inputvarlist=Inputvarlist,outputvarlist=Outputvarlist,algorithmlibrary=Algorithmlibrary,submit=_],
88
+
89
+ term_to_atom(Debug1,'off'),
90
+ term_to_atom(Predicatename1,Predicatename),
91
+ term_to_atom(Rules1,Rules),
92
+ term_to_atom(Maxlength1,Maxlength),
93
+ term_to_atom(Totalvars1,Totalvars),
94
+ term_to_atom(Inputvarlist1,Inputvarlist),
95
+ term_to_atom(Outputvarlist1,Outputvarlist),
96
+ term_to_atom(Algorithmlibrary1,Algorithmlibrary),
97
+
98
+ caw00(Debug1,Predicatename1,Rules1,Maxlength1,Totalvars1,Inputvarlist1,Outputvarlist1,Algorithmlibrary1,[],Result),
99
+ %%format('</p><p>========~n', []),
100
+ portray_clause(Result),
101
+ %%writeln1(Data),
102
+
103
+ format('</p>').
Combination-Algorithm-Writer-Stable/cawpverify.pl ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %% cawptest(Debug[on/off],Total,Score).
2
+
3
+ cawptest(Debug,NTotal,Score) :- cawptest(Debug,0,NTotal,0,Score),!.
4
+ cawptest(_Debug,NTotal,NTotal,Score,Score) :- NTotal=1, !.
5
+ cawptest(Debug,NTotal1,NTotal2,Score1,Score2) :-
6
+ NTotal3 is NTotal1+1,
7
+ cawptest2(NTotal3,Function,Rules,MaxLength,TotalVars,Specifications,AlgDict,Program1),
8
+
9
+ %%writeln([cawptest2(NTotal3,Specifications,Program1)]),
10
+ (((%%writeln(caw00(Debug,function0,[],5,TotalVars,Specifications,[],Program1)),
11
+ caw00(Debug,Function,Rules,MaxLength,TotalVars,Specifications,AlgDict,[],Program1)
12
+
13
+ %%sort(Program1,ProgramA),
14
+ %%sort(Program2,ProgramA)
15
+ %%writeln1(Program1),writeln1(Program2)
16
+ %%Program1=Program2
17
+ ))->(Score3 is Score1+1,writeln([cawptest,NTotal3,passed]));(Score3=Score1,writeln([cawptest,NTotal3,failed]))),
18
+ writeln(""),
19
+ cawptest(Debug,NTotal3,NTotal2,Score3,Score2),!.
20
+
21
+ %% Test individual cases, Debug=trace=on or off, N=case number, Passed=output=result
22
+
23
+ cawptest1(Debug,N,Passed) :-
24
+ cawptest2(N,Function,Rules,MaxLength,TotalVars,InputVarList,OutputVarList,AlgDict,Program1),
25
+ %%writeln1([cawptest2(N,Specifications,Program1)]),
26
+ %%writeln1(caw00(Debug,Function,Rules,MaxLength,MaxPredicates,TotalVars,Numinputs, Numoutputs,Specifications,AlgDict,[],Program2)),
27
+
28
+ (((caw00(Debug,Function,Rules,MaxLength,TotalVars,InputVarList,OutputVarList,AlgDict,[],Program2),
29
+ %%sort(Program1,ProgramA),
30
+ %%sort(Program2,ProgramA)
31
+ trace,
32
+ writeln(Program1),
33
+ writeln(Program2),Program1=Program2
34
+ ))->(Passed=passed,writeln([cawptest,N,passed]));(Passed=failed,writeln([cawptest,N,failed]))),!.
35
+
36
+ % ["Mind Reading","mr for time travel 3.txt",0,algorithms,"34. I mind read the correct universe containing all my collections of areas of study, then time travelled to it."]
37
+
38
+ % The area of study was +++.
39
+
40
+ cawptest2(1,f,[[+,2,1%% Modes=2 inputs, 1 output
41
+ ]],4,
42
+ 8,
43
+ [[a,1],[b,1],[c,2],[d,1]],[[e,5]]
44
+ ,
45
+ [
46
+ %[[f1,4,1],[a,b,c,d,e],(:-),[[+,[a,b,f]],[+,[c,f,g]],[+,[d,g,h]],[=,[e,h]]]]
47
+ ], %% Algorithm dictionary
48
+ %% Result
49
+ %[f1,[a,b,c,d,e],:-,[[+,[a,b,f]],[+,[c,f,g]],[+,[d,g,h]],[=,[e,h]]]],
50
+ [[f,[a,b,c,d,e],:-,[[+,[a,b,f]],[+,[c,d,g]],[+,[f,g,h]],[=,[e,h]]]]]
51
+ ).
Combination-Algorithm-Writer-Stable/listprologinterpreter1listrecursion4copy52.pl ADDED
@@ -0,0 +1,584 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**:- include('listprologinterpreter3preds5.pl').
2
+ :- include('lpiverify4.pl').
3
+ :- include('caw5 copy 11.pl').
4
+ **/
5
+ :- dynamic debug/1.
6
+
7
+ /** List Prolog Interpreter **/
8
+
9
+ interpret(Debug,Query,Functions,Result) :-
10
+ %%writeln([i1]),
11
+ interpret1(Debug,Query,Functions,Functions,Result),
12
+ !.
13
+ interpret1(Debug,Query,Functions1,Functions2,Result) :-
14
+ %%writeln([i11]),
15
+ retractall(debug(_)),
16
+ assertz(debug(Debug)),
17
+ retractall(cut(_)),
18
+ assertz(cut(off)),
19
+ member1(Query,Functions1,Functions2,Result).
20
+ %%member1([_,R],_,[],R).
21
+ %%member1(_,_,[],[]).
22
+ member1(_,_,[],_) :- fail.
23
+ member1(Query,Functions,Functions2,Vars8) :-
24
+ %%writeln([m1]),
25
+ cut(off)->(
26
+ (Query=[Function,Arguments1],
27
+ (Functions2=[[Function,Arguments2,(:-),Body]|_Functions3]),
28
+ length(Arguments1,Length),
29
+ length(Arguments2,Length),
30
+ checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!, %%->ca2
31
+ %%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),
32
+ (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),
33
+ interpretbody(Functions,Functions2,Vars1,Vars2,Body,true),
34
+ updatevars(FirstArgs,Vars2,[],Result),
35
+ %%reverse(Result,[],Vars7),
36
+ ((not(Result=[])->
37
+ %%Result=[Var71|Vars72],
38
+ unique1(Result,[],Vars8),
39
+ %%writeln(["FirstArgs",FirstArgs,"Vars",Vars2,"Result",Result,"Vars7",Vars7,"Vars72",Vars72,"Var71",Var71,"Vars8",Vars8]),
40
+ %%writeln(["Vars8",Vars8]),
41
+ findresult3(Arguments1,Vars8,[],Result2)
42
+ %%writeln([findresult3,"Arguments1",Arguments1,"Vars8",Vars8,"Result2",Result2])
43
+ );(
44
+ %%writeln(here1),
45
+ Vars8=[],Result2=[])),
46
+ %%writeln(["Arguments1",Arguments1,"Vars2",Vars2,"Result",Result]),
47
+ (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true))
48
+ ->true;
49
+ (%%Query=[Function,_Arguments1],
50
+ %%Functions2=[[Function,_Arguments2,(:-),_Body]|Functions3], %% make like previous trunk?
51
+ member11(Query,Functions,Functions2,Vars8))
52
+ );(turncut(off)%%,Result=[]
53
+ ).
54
+ member11(Query,Functions,Functions2,Result) :-
55
+ %%writeln([m11]),
56
+ %%writeln(["Query",Query,"Functions",Functions,"Functions2",Functions2,"Result",Result]),
57
+ cut(off)->(
58
+ (Query=[Function],
59
+ (Functions2=[[Function,(:-),Body]|_Functions3]),
60
+ (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true),
61
+ Result=[],
62
+ interpretbody(Functions,Functions2,[],_Vars2,Body,true),!,
63
+ (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true)
64
+ )->true;
65
+ (%%Query=[Function],
66
+ %%Functions2=[[Function]|Functions3],
67
+ member12(Query,Functions,Functions2,Result))
68
+ );(turncut(off)).
69
+ member12(Query,Functions,Functions2,Vars8) :-
70
+ %%writeln([m12]),
71
+ cut(off)->(
72
+ (Query=[Function,Arguments1],
73
+ (Functions2=[[Function,Arguments2]|_Functions3]),
74
+ length(Arguments1,Length),
75
+ length(Arguments2,Length),
76
+ checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!, %% ->ca2
77
+ %%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),
78
+ updatevars(FirstArgs,Vars1,[],Result),
79
+ %%reverse(Result,[],Vars7),
80
+ ((not(Result=[])->
81
+ %%Result=[Var71|Vars72],
82
+ unique1(Result,[],Vars8),
83
+ findresult3(Arguments1,Vars8,[],Result2)
84
+ );(
85
+ %%writeln(here2),
86
+ Vars8=[],Result2=[])),
87
+ (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),
88
+ (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true)
89
+ )->true;
90
+ (%%Query=[Function,_Arguments1],
91
+ %%Functions2=[[Function,_Arguments2]|Functions3],
92
+ member13(Query,Functions,Functions2,Vars8))
93
+ );(turncut(off)).
94
+ member13(Query,Functions,Functions2,Result) :-
95
+ %%writeln([m13]),
96
+ cut(off)->(
97
+ (Query=[Function],!,
98
+ (Functions2=[[Function]|_Functions3]),
99
+ (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true),
100
+ Result=[],
101
+ %%interpretbody(Functions,[],_Vars2,Body,true),
102
+ (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true)
103
+ )->true;
104
+ (%%Query=[Function],
105
+ Functions2=[_Function|Functions3],
106
+ member1(Query,Functions,Functions3,Result))
107
+ );(turncut(off)).
108
+ interpret2(Query,Functions1,Functions2,Result) :-
109
+ %%writeln(i2),
110
+ %%writeln(["%%interpret2 Query",Query,"Functions1",Functions1,"Functions2",Functions2]),
111
+ member2(Query,Functions1,Functions2,Result).
112
+ %%member2([_,R],_,[],R).
113
+ %%member2(_,_,[],[]).
114
+ member2(_,_,[],_) :- fail.
115
+ member2(Query,Functions,Functions2,Vars8) :-
116
+ %%writeln([m2]),
117
+ cut(off)->(
118
+ (Query=[Function,Arguments1],
119
+ (Functions2=[[Function,Arguments2,(:-),Body]|_Functions3]),
120
+ length(Arguments1,Length),
121
+ length(Arguments2,Length),
122
+ checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!,
123
+ %%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),
124
+ (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),
125
+ interpretbody(Functions,Functions2,Vars1,Vars2,Body,true), %%**arg2 change
126
+ %%writeln(["Functions",Functions,"Functions2",Functions2,"Vars1",Vars1,"Vars2",Vars2,"Body",Body]),
127
+ updatevars(FirstArgs,Vars2,[],Result),
128
+ %%reverse(Result,[],Vars7),
129
+ ((not(Result=[])->
130
+ %%Result=[Var71|Vars72],
131
+ unique1(Result,[],Vars8),
132
+ findresult3(Arguments1,Vars8,[],Result2)
133
+ %%writeln(["Vars2",Vars2,"Result",Result]),
134
+ );(
135
+ writeln(here3),
136
+ Vars8=[],Result2=[])),
137
+ (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true)
138
+ )->true;
139
+ (%%Query=[Function,_Arguments1],
140
+ %%Functions2=[[Function,_Arguments2,(:-),_Body]|Functions3],
141
+ member21(Query,Functions,Functions2,Vars8))
142
+ );(turncut(off)).
143
+ member21(Query,Functions,Functions2,Result) :-
144
+ %%writeln([m21]),
145
+ cut(off)->(
146
+ (Query=[Function],
147
+ (Functions2=[[Function,(:-),Body]|_Functions3]),
148
+ Vars1=[],
149
+ (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true),
150
+ interpretbody(Functions,Functions2,Vars1,_Vars2,Body,true),!, %%**arg2 change
151
+ (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true)
152
+ )->true;
153
+ (%%Query=[Function],
154
+ %%Functions2=[[Function]|Functions3],
155
+ member22(Query,Functions,Functions2,Result))
156
+ );(turncut(off)).
157
+ member22(Query,Functions,Functions2,Vars8) :-
158
+ %%writeln([m22]),
159
+ cut(off)->(
160
+ (Query=[Function,Arguments1],
161
+ (Functions2=[[Function,Arguments2]|_Functions3]),
162
+ length(Arguments1,Length),
163
+ length(Arguments2,Length),
164
+ checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!,
165
+ %%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),
166
+ updatevars(FirstArgs,Vars1,[],Result),
167
+ %%reverse(Result,[],Vars7),
168
+ ((not(Result=[])->
169
+ %%Result=[Var71|Vars72],
170
+ unique1(Result,[],Vars8),
171
+ findresult3(Arguments1,Vars8,[],Result2)
172
+ );(
173
+ %%writeln(here4),
174
+ Vars8=[],Result2=[])),
175
+ (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),
176
+ (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true)
177
+ )->true;
178
+ (%%Query=[Function,_Arguments1],
179
+ %%Functions2=[[Function,_Arguments2]|Functions3],
180
+ member23(Query,Functions,Functions2,Vars8))
181
+ );(turncut(off)).
182
+ member23(Query,Functions,Functions2,Vars8) :-
183
+ %%writeln([m23]),
184
+ cut(off)->(
185
+ (Query=[Function],!,
186
+ (Functions2=[[Function]|_Functions3]),
187
+ (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true),
188
+ Vars8=[],
189
+ (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true)
190
+ )->true;
191
+ (%%Query=[Function],
192
+ Functions2=[_Function|Functions3],
193
+ member2(Query,Functions,Functions3,Vars8))
194
+ );(turncut(off)).
195
+ checkarguments([],[],Vars,Vars,FirstArgs,FirstArgs).
196
+ checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :- %%
197
+ %%writeln(1),
198
+ Arguments1=[Value|Arguments3], %% Value may be a number, string, list or tree
199
+ expressionnotatom(Value),
200
+ Arguments2=[Variable2|Arguments4],
201
+ isvar(Variable2),
202
+ putvalue(Variable2,Value,Vars1,Vars3),
203
+ checkarguments(Arguments3,Arguments4,Vars3,Vars2,FirstArgs1,FirstArgs2).
204
+ checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :- %%A
205
+ %%writeln(2),
206
+ Arguments1=[Variable|Arguments3], %% Value may be a number, string, list or tree
207
+ isvar(Variable),
208
+ Arguments2=[Value|Arguments4],
209
+ expressionnotatom(Value),
210
+ putvalue(Variable,Value,Vars1,Vars3),
211
+ append(FirstArgs1,[[Variable,_]],FirstArgs3),
212
+ checkarguments(Arguments3,Arguments4,Vars3,Vars2,FirstArgs3,FirstArgs2).
213
+ checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :-
214
+ %%writeln(3),
215
+ Arguments1=[Variable1|Arguments3],
216
+ isvar(Variable1),
217
+ Arguments2=[Variable2|Arguments4],
218
+ isvar(Variable2),
219
+ (getvalue(Variable2,Value,Vars1)->true;Value=empty),
220
+ putvalue(Variable2,Value,Vars1,Vars3),
221
+ append(FirstArgs1,[[Variable1,Variable2]],FirstArgs3),
222
+ checkarguments(Arguments3,Arguments4,Vars3,Vars2,FirstArgs3,FirstArgs2).
223
+ checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :-
224
+ %%writeln(4),
225
+ Arguments1=[Value1|Arguments3],
226
+ expressionnotatom(Value1),
227
+ Arguments2=[Value1|Arguments4],
228
+ expressionnotatom(Value1),
229
+ checkarguments(Arguments3,Arguments4,Vars1,Vars2,FirstArgs1,FirstArgs2).
230
+
231
+ interpretbody(_Functions1,_Functions2,Vars,Vars,[],true) :- !.
232
+
233
+ interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-
234
+ Body=[Statements1|Statements2],
235
+ interpretbody(Functions0,Functions,Vars1,Vars3,Statements1,Result2),
236
+ interpretbody(Functions0,Functions,Vars3,Vars2,Statements2,Result3),
237
+ %%((Result3=cut)->!;true),
238
+ logicalconjunction(Result1,Result2,Result3),!.
239
+
240
+ interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-
241
+ Body=[[not,[Statements]]|Statements2],
242
+ interpretbody(Functions0,Functions,Vars1,Vars3,Statements,Result2),
243
+ %%((Result2=cut)->!;true),
244
+ interpretbody(Functions0,Functions,Vars3,Vars2,Statements2,Result3),
245
+ %%((Result3=cut)->!;true),
246
+ logicalnot(Result2,Result4),
247
+ (logicalconjunction(Result1,Result4,Result3)->true;(Result1=false)),!.
248
+ interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-
249
+ Body=[[Statements1],or,[Statements2]],
250
+ (interpretbody(Functions0,Functions,Vars1,Vars2,Statements1,Result1);
251
+ %%,((Value1=cut)->!;true));
252
+ interpretbody(Functions0,Functions,Vars1,Vars2,Statements2,Result1)),!.
253
+ %%,((Value=cut)->!;true)).
254
+ %%(logicaldisjunction(Result1,Value1,Value2)->true;(Result1=false)).
255
+
256
+
257
+ interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-
258
+ Body=[Statement|Statements],
259
+ %%writeln(["Functions0",Functions0,"Functions",Functions,"Statement",Statement,"Vars1",Vars1,"Vars3",Vars3,"Result2",Result2,"Cut",Cut]),
260
+ interpretstatement1(Functions0,Functions,Statement,Vars1,Vars3,Result2,Cut),
261
+ %%writeln(["here1"]),
262
+ ((not(Cut=cut))->(Functions2=Functions);(turncut(on))), %% cut to interpret1/2 (assertz)
263
+ %%writeln(["here3"]),
264
+ interpretbody(Functions0,Functions2,Vars3,Vars2,Statements,Result3),
265
+ %%((Result3=cut)->!;true),
266
+ %%writeln(["here4"]),
267
+ logicalconjunction(Result1,Result2,Result3),!.
268
+ %%writeln([Result1,Result2,Result3]).
269
+ turncut(State1) :-
270
+ cut(State2),
271
+ retract(cut(State2)),
272
+ assertz(cut(State1)).
273
+ logicaldisjunction(true,Result2,Result3) :-
274
+ true(Result2);true(Result3).
275
+ logicalconjunction(true,Result2,Result3) :-
276
+ true(Result2),true(Result3).
277
+ logicalnot(Result1,Result2) :-
278
+ true(Result1),false(Result2).
279
+ logicalnot(Result1,Result2) :-
280
+ false(Result1),true(Result2).
281
+ true(true).
282
+ false(false).
283
+
284
+ %%interpretstatement1(_F0,[],_,Vars,Vars,true,nocut) :- !
285
+ %%writeln("AND HERE!")
286
+ %% .
287
+
288
+ interpretstatement1(_F0,_Functions,[cut,[]],Vars,Vars,true,cut) :- !.
289
+
290
+ interpretstatement1(_F0,_Functions,[atom,[Variable]],Vars,Vars,true,nocut) :-
291
+ getvalue(Variable,Value,Vars),
292
+ (debug(on)->(writeln([call,[atom,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true),
293
+ atom(Value),
294
+ (debug(on)->(writeln([exit,[atom,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true).
295
+
296
+ interpretstatement1(_F0,_Functions,[string,[Variable]],Vars,Vars,true,nocut) :-
297
+ getvalue(Variable,Value,Vars),
298
+ (debug(on)->(writeln([call,[string,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true),
299
+ string(Value),
300
+ (debug(on)->(writeln([exit,[string,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true).
301
+
302
+ interpretstatement1(_F0,_Functions,[number,[Variable]],Vars,Vars,true,nocut) :-
303
+ getvalue(Variable,Value,Vars),
304
+ (debug(on)->(writeln([call,[number,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true),
305
+ number(Value),
306
+ (debug(on)->(writeln([exit,[number,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true).
307
+
308
+ interpretstatement1(_F0,_Functions,[variable,[Variable]],Vars,Vars,true,nocut) :-
309
+ var(Variable),
310
+ (debug(on)->(writeln([call,[variable,[Variable]],"Press c."]),(not(get_single_char(97))->true;abort));true),
311
+ (debug(on)->(writeln([exit,[variable,[Variable]],"Press c."]),(not(get_single_char(97))->true;abort));true).
312
+
313
+ interpretstatement1(_F0,_Functions,[Operator,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
314
+ isop(Operator),
315
+ interpretpart(is,Variable1,Variable2,Vars1,Vars2).
316
+
317
+ interpretstatement1(_F0,_Functions,[Operator,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
318
+ %%writeln(31),
319
+ isop(Operator),
320
+ interpretpart(is,Variable2,Variable1,Vars1,Vars2).
321
+
322
+ interpretstatement1(_F0,_Functions,[+,[Variable2,Variable3,Variable1]],Vars1,Vars2,true,nocut) :-
323
+ %%writeln(4),
324
+ interpretpart(isplus,Variable1,Variable2,Variable3,Vars1,Vars2).
325
+
326
+ %%interpretstatement1(_F0,_Functions,[Variable2+Variable3,is,Variable1],Vars1,Vars2,true,nocut) :-
327
+ %%writeln(41),
328
+ %%interpretpart(isplus,Variable1,Variable2,Variable3,Vars1,Vars2).
329
+
330
+ interpretstatement1(_F0,_Functions,[=,[Variable1,[Variable2,Variable3]]],Vars1,Vars2,true,nocut) :-
331
+ %%writeln(5),
332
+ interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2).
333
+
334
+ %%interpretstatement1(_F0,_Functions,[[Variable2,Variable3]=Variable1],Vars1,Vars2,true,nocut) :-
335
+ %%writeln(51),
336
+ %% interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2).
337
+
338
+ interpretstatement1(_F0,_Functions,[=,[Variable1,[Variable2]]],Vars1,Vars2,true,nocut) :-
339
+ %%writeln(52),
340
+ interpretpart(bracket1,Variable1,Variable2,Vars1,Vars2).
341
+
342
+ interpretstatement1(_F0,_Functions,[=,[[Variable1],[Variable2]]],Vars1,Vars2,true,nocut) :-
343
+ %%writeln(53),
344
+ interpretpart(bracket2,Variable1,Variable2,Vars1,Vars2).
345
+
346
+ interpretstatement1(_F0,_Functions,[head,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
347
+ %%writeln(6),
348
+ interpretpart(head,Variable1,Variable2,Vars1,Vars2).
349
+
350
+ interpretstatement1(_F0,_Functions,[tail,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
351
+ %%writeln(61),
352
+ interpretpart(tail,Variable1,Variable2,Vars1,Vars2).
353
+
354
+
355
+ interpretstatement1(_F0,_Functions,[member,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
356
+ %%writeln(8),
357
+ interpretpart(member,Variable1,Variable2,Vars1,Vars2).
358
+
359
+ interpretstatement1(_F0,_Functions,[delete,[Variable1,Variable2,Variable3]],Vars1,Vars2,true,nocut) :-
360
+ %%writeln(),
361
+ interpretpart(delete,Variable1,Variable2,Variable3,Vars1,Vars2).
362
+ %%** all in form f,[1,1,etc], including + with 0,1
363
+
364
+
365
+ interpretstatement1(_F0,_Functions,[wrap,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
366
+ %%writeln1(52), wrap
367
+ %%writeln([[n,wrap],[Variable1,Variable2]]),
368
+ interpretpart(bracket1,Variable1,Variable2,Vars1,Vars2).
369
+
370
+
371
+
372
+ interpretstatement1(_F0,_Functions,[append,[Variable1,Variable2,Variable3]],Vars1,Vars2,true,nocut) :-
373
+ %%writeln(9),
374
+ interpretpart(append,Variable1,Variable2,Variable3,Vars1,Vars2).
375
+
376
+ interpretstatement1(Functions0,_Functions,Query1,Vars1,Vars8,true,nocut) :-
377
+ %%writeln("h1/10"),
378
+ Query1=[Function,Arguments],
379
+ %%writeln(["Arguments",Arguments,"Vars1",Vars1]),
380
+ substitutevarsA1(Arguments,Vars1,[],Vars3,[],FirstArgs), %%% var to value, after updatevars: more vars to values, and select argument vars from latest vars
381
+ %%writeln([substitutevarsA1,arguments,Arguments,vars1,Vars1,vars3,Vars3,firstargs,FirstArgs]),
382
+ Query2=[Function,Vars3], %% Bodyvars2?
383
+ %% debug(on)->writeln([call,[Function,[Vars3]]]),
384
+ %%writeln(["Query2",Query2,"Functions0",Functions0]),
385
+ interpret2(Query2,Functions0,Functions0,Result1),
386
+ updatevars2(FirstArgs,Result1,[],Vars5),
387
+ updatevars3(Vars1,Vars5,Vars6),
388
+ reverse(Vars6,[],Vars7),
389
+ ((not(Vars7=[])->
390
+ %%Vars7=[Var71|Vars72],
391
+ unique1(Vars7,[],Vars8)
392
+ );(
393
+ writeln(here1),
394
+ Vars8=[])).
395
+ %%**** reverse and take first instance of each variable.
396
+ %%findresult3(Arguments,Vars6,[],Result2)
397
+ %%writeln(["FirstArgs",FirstArgs,"Result1",Result1,"Vars5",Vars5,"Vars4",Vars4]),
398
+ %%writeln(["Vars1:",Vars1,"Vars4:",Vars4]),
399
+ %% debug(on)->writeln([exit,[Function,[Result2]]]).
400
+ interpretstatement1(Functions0,_Functions,Query,Vars,Vars,true) :-
401
+ Query=[_Function],
402
+ debug(on)->writeln([call,[Function]]),
403
+ interpret2(Query,Functions0,Functions0,_Result1),
404
+ debug(on)->writeln([exit,[Function]]).
405
+
406
+
407
+ interpretstatement1(_Functions0, _Functions,_Query,_Vars1,_Vars2,false) :-
408
+ writeln([false]).
409
+
410
+ interpretstatement2(Value,_Vars,Value) :-
411
+ (number(Value);atom(Value)).
412
+ interpretstatement2(Variable,Vars1,Value) :-
413
+ getvalue(Variable,Value,Vars1).
414
+ interpretstatement3(A + B,Vars,Value1) :-
415
+ interpretstatement2(A,Vars,Value2),
416
+ interpretstatement2(B,Vars,Value3),
417
+ Value1 = Value2 + Value3.
418
+ interpretstatement3(Value,_Vars,Value) :-
419
+ (number(Value);atom(Value)).
420
+ interpretstatement3(Variable,Vars,Value) :-
421
+ getvalue(Variable,Value,Vars).
422
+ getvalue(Variable,Value,Vars) :-
423
+ ((not(isvar(Variable)),isvalstrorundef(Value),Variable=Value);
424
+ (isvar(Variable),isvalstrorundef(Value),getvar(Variable,Value,Vars))).
425
+ putvalue(Variable,Value,Vars1,Vars2) :-
426
+ ((not(isvar(Variable)),isvalstrorundef(Value),Variable=Value);
427
+ (isvar(Variable),isvalstrorundef(Value),updatevar(Variable,Value,Vars1,Vars2))),!.
428
+ getvar(Variable,Value,Vars) :-
429
+ member([Variable,Value],Vars),
430
+ not(Value=empty).
431
+ getvar(undef,undef,_Vars) :-
432
+ !.
433
+ getvar(Variable,empty,Vars) :-
434
+ not(member([Variable,_Value],Vars));
435
+ member([Variable,empty],Vars).
436
+ updatevar(undef,_Value,Vars,Vars) :-
437
+ !.
438
+ updatevar(Variable,Value,Vars1,Vars2) :-
439
+ ((((member([Variable,empty],Vars1),
440
+ delete(Vars1,[Variable,empty],Vars3),
441
+ append(Vars3,[[Variable,Value]],Vars2));
442
+ ((not(member([Variable,Value1],Vars1)),
443
+ ((Value1=empty)->true;(Value1=Value)))),
444
+ append(Vars1,[[Variable,Value]],Vars2));
445
+ (member([Variable,Value],Vars1),Vars2=Vars1));
446
+ (undef(Variable),
447
+ append(Vars1,[[Variable,Value]],Vars2))).
448
+ updatevars(_FirstArgs,[],Vars,Vars).
449
+ updatevars(FirstArgs,Vars1,Vars2,Vars3) :-
450
+ Vars1=[[Variable1,Value]|Vars4],
451
+ ((member([Variable2,Variable1],FirstArgs), %% removed brackets around firstargs here and 2 line below
452
+ append(Vars2,[[Variable2,Value]],Vars5));
453
+ (member([Variable1,_Variable2],FirstArgs),
454
+ append(Vars2,[[Variable1,Value]],Vars5))),
455
+ updatevars(FirstArgs,Vars4,Vars5,Vars3),
456
+ !.
457
+ updatevars(FirstArgs,Vars1,Vars2,Vars3) :-
458
+ Vars1=[_Vars4|Vars5],
459
+ updatevars(FirstArgs,Vars5,Vars2,Vars3).
460
+ updatevars2(_FirstArgs,[],Vars,Vars).
461
+ updatevars2(FirstArgs,Vars1,Vars2,Vars3) :-
462
+ Vars1=[[Variable,Value]|Vars4],
463
+ (member(Variable,FirstArgs), %% removed brackets around firstargs here and 2 line below, ** vars1 into arg in (10), check cond
464
+ append(Vars2,[[Variable,Value]],Vars5)),
465
+ updatevars2(FirstArgs,Vars4,Vars5,Vars3).
466
+ updatevars3(Vars1,[],Vars1).
467
+ updatevars3(Vars1,Vars2,Vars4) :-
468
+ Vars2=[[Variable,Value]|Vars5],
469
+ delete(Vars1,[Variable,empty],Vars6),
470
+ append(Vars6,[[Variable,Value]],Vars7),
471
+ updatevars3(Vars7,Vars5,Vars4),
472
+ !.
473
+ updatevars3(Vars1,Vars2,Vars4) :-
474
+ Vars2=[[Variable,Value]|Vars5],
475
+ append(Vars1,[[Variable,Value]],Vars6),
476
+ updatevars3(Vars6,Vars5,Vars4).
477
+ reverse([],List,List).
478
+ reverse(List1,List2,List3) :-
479
+ List1=[Head|Tail],
480
+ append([Head],List2,List4),
481
+ reverse(Tail,List4,List3).
482
+ unique1([],Items,Items).
483
+ unique1([Item|Items1],Items2,Items3) :-
484
+ delete(Items1,Item,Items4),
485
+ append(Items2,[Item],Items5),
486
+ unique1(Items4,Items5,Items3).
487
+
488
+ isvar(Variable) :-
489
+ atom(Variable).
490
+ isval(Value) :-
491
+ number(Value).
492
+ isvalstr(N) :-
493
+ isval(N);string(N).
494
+ /**isvalstrempty(N) :-
495
+ isval(N);(string(N);N=empty).**/
496
+ isvalstrempty(N) :-
497
+ isval(N),!.
498
+ isvalstrempty(N) :-
499
+ string(N),!.
500
+ isvalstrempty(empty) :- !.
501
+ /**isvalstrempty(N) :-
502
+ atom(N),fail,!.
503
+ **/
504
+ isvalstrorundef(N) :-
505
+ var(N);(not(var(N)),(isval(N);expression(N))).
506
+ undef(N) :-
507
+ var(N).
508
+ /**
509
+ expression(N) :-
510
+ isval(N);(string(N);atom(N)),!.
511
+ expression([]).
512
+ expression(empty).
513
+ expression([N]) :-
514
+ expression(N).
515
+ expression([N|Ns]):-
516
+ expression(N),expression(Ns).
517
+ **/
518
+
519
+ expression(empty) :-
520
+ !.
521
+ expression(N) :-
522
+ isval(N),!.
523
+ expression(N) :-
524
+ string(N),!.
525
+ expression(N) :-
526
+ atom(N),!.
527
+ expression([]) :-
528
+ !.
529
+ expression(N) :-
530
+ %% not(atom(N)),
531
+ length(N,L),L>=2,
532
+ expression2(N).
533
+ expression2([]).
534
+ expression2([N|Ns]) :-
535
+ expression3(N),
536
+ expression2(Ns).
537
+ expression3(N) :-
538
+ isval(N),!.
539
+ expression3(N) :-
540
+ string(N),!.
541
+ expression3(N) :-
542
+ atom(N),!.
543
+
544
+ expressionnotatom(N) :-
545
+ isvalstrempty(N),!.
546
+ expressionnotatom(N) :-
547
+ not(atom(N)),
548
+ length(N,L),L>=2,
549
+ expressionnotatom2(N).
550
+ expressionnotatom2([]).
551
+ expressionnotatom2([N|Ns]) :-
552
+ isvalstrempty(N),
553
+ expressionnotatom2(Ns).
554
+
555
+ substitutevarsA1(Arguments,Vars1,Vars2,Vars3,FirstArgs1,FirstArgs2) :-
556
+ substitutevarsA2(Arguments,Vars1,Vars2,Vars3,FirstArgs1,FirstArgs2).
557
+ substitutevarsA2([],_Vars1,Vars2,Vars2,FirstArgs,FirstArgs).
558
+ substitutevarsA2(Arguments,Vars1,Vars2,Vars3,FirstArgs1,FirstArgs2) :-
559
+ Arguments=[Variable|Variables],
560
+ ((getvalue(Variable,Value,Vars1),
561
+ Value=empty)->
562
+ ((append(Vars2,[Variable],Vars4)),
563
+ (isvar(Variable)->append(FirstArgs1,[Variable],
564
+ FirstArgs3);FirstArgs3=FirstArgs1));
565
+ (getvalue(Variable,Value,Vars1),
566
+ append(Vars2,[Value],Vars4)),
567
+ FirstArgs3=FirstArgs1),
568
+ substitutevarsA2(Variables,Vars1,Vars4,Vars3,FirstArgs3,FirstArgs2).
569
+
570
+ findresult3([],_Result,Result2,Result2).
571
+ findresult3(Arguments1,Result1,Result2,Result3) :-
572
+ Arguments1=[Value|Arguments2],
573
+ expressionnotatom(Value),
574
+ append(Result2,[Value],Result4),
575
+ findresult3(Arguments2,Result1,Result4,Result3).
576
+ findresult3(Arguments1,Result1,Result2,Result3) :-
577
+ Arguments1=[Variable|Arguments2],
578
+ isvar(Variable),
579
+ member([Variable,Value],Result1),
580
+ append(Result2,[Value],Result4),
581
+ findresult3(Arguments2,Result1,Result4,Result3).
582
+
583
+
584
+
Combination-Algorithm-Writer-Stable/listprologinterpreter3preds5copy52.pl ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ interpretpart(is,Variable1,Value1,Vars1,Vars2) :-
2
+ getvalue(Value1,Value1A,Vars1),
3
+ %%isvalstr(Value1),
4
+ %%isvalstr(Value1A),
5
+ expression(Value1A),
6
+ %%val1emptyorvalsequal(Value1A,Value1),
7
+ %%isval(Value2),
8
+ putvalue(Variable1,Value1A,Vars1,Vars2),
9
+ (debug(on)->(writeln([call,[variable,is,Value1A],"Press c."]),(not(get_single_char(97))->true;abort));true),
10
+ (debug(on)->(writeln([exit,[Variable1,is,Value1A],"Press c."]),(not(get_single_char(97))->true;abort));true).
11
+ interpretpart(isplus,Variable1,Variable2,Variable3,Vars1,Vars2) :-
12
+ getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),
13
+ Value1A is Value2 + Value3,
14
+ val1emptyorvalsequal(Value1,Value1A),
15
+ putvalue(Variable1,Value1A,Vars1,Vars2),
16
+ (debug(on)->(writeln([call,[variable,is,Value2+Value3],"Press c."]),(not(get_single_char(97))->true;abort));true),
17
+ (debug(on)->(writeln([exit,[Value1A,is,Value2+Value3],"Press c."]),(not(get_single_char(97))->true;abort));true).
18
+ interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2) :-
19
+ getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),
20
+ Value1 = [Value2A, Value3A],
21
+ val1emptyorvalsequal(Value2,Value2A),
22
+ val1emptyorvalsequal(Value3,Value3A),
23
+ putvalue(Variable2,Value2A,Vars1,Vars3),
24
+ putvalue(Variable3,Value3A,Vars3,Vars2),
25
+ (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).
26
+ interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2) :-
27
+ getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),
28
+ Value1A = [Value2, Value3],
29
+ val1emptyorvalsequal(Value1,Value1A),
30
+ putvalue(Variable1,Value1A,Vars1,Vars2),
31
+ (debug(on)->(writeln([call,[variable,=,[Value2,Value3]],"Press c."]),(not(get_single_char(97))->true;abort));true),
32
+ (debug(on)->(writeln([exit,[[Value2,Value3],=,[Value2,Value3]],"Press c."]),(not(get_single_char(97))->true;abort));true).
33
+ interpretpart(bracket1,Variable1,Variable2,Vars1,Vars2) :-
34
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
35
+ Value1A = [Value2],
36
+ val1emptyorvalsequal(Value1,Value1A),
37
+ %%val1emptyorvalsequal(Value1A,Value2),
38
+ putvalue(Variable1,Value1A,Vars1,Vars2),
39
+ (debug(on)->(writeln([call,[variable,=,[Value2]],"Press c."]),(not(get_single_char(97))->true;abort));true),
40
+ (debug(on)->(writeln([exit,[Variable2,=,[Value2]],"Press c."]),(not(get_single_char(97))->true;abort));true).
41
+ interpretpart(bracket2,Variable1,Variable2,Vars1,Vars2) :-
42
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
43
+ Value1A = Value2,
44
+ val1emptyorvalsequal(Value1,Value1A),
45
+ %%val1emptyorvalsequal(Value2A,Value1),
46
+ putvalue(Variable1,Value1A,Vars1,Vars2),
47
+ (debug(on)->(writeln([call,[[variable],=,[Value2]],"Press c."]),(not(get_single_char(97))->true;abort));true),
48
+ (debug(on)->(writeln([exit,[[Value2],=,[Value2]],"Press c."]),(not(get_single_char(97))->true;abort));true).
49
+ interpretpart(head,Variable1,Variable2,Vars1,Vars2) :-
50
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
51
+ Value1=[Value1A|_Rest],
52
+ val1emptyorvalsequal(Value2,Value1A),
53
+ putvalue(Variable2,Value1A,Vars1,Vars2),
54
+ (debug(on)->(writeln([call,[head,Value1,variable],"Press c."]),(not(get_single_char(97))->true;abort));true),
55
+ (debug(on)->(writeln([exit,[head,Value1,Value1A],"Press c."]),(not(get_single_char(97))->true;abort));true), !.
56
+ interpretpart(tail,Variable1,Variable2,Vars1,Vars2) :-
57
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
58
+ Value1=[_Head|Value1A],
59
+ %%removebrackets(Value1A,Value1B),
60
+ val1emptyorvalsequal(Value2,Value1A),
61
+ putvalue(Variable2,Value1A,Vars1,Vars2),
62
+ (debug(on)->(writeln([call,[tail,Value1,variable],"Press c."]),(not(get_single_char(97))->true;abort));true),
63
+ (debug(on)->(writeln([exit,[tail,Value1,Value1A],"Press c."]),(not(get_single_char(97))->true;abort));true).
64
+ interpretpart(member,Variable1,Variable2,Vars1,Vars2) :-
65
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
66
+ (not(Value1=empty)->(member(Value1,Value2),Vars2=Vars1,
67
+ (debug(on)->(writeln([call,[member,Value1,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true),
68
+ (debug(on)->(writeln([exit,[member,Value1,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true));
69
+ (member(Value3,Value2),
70
+ putvalue(Variable1,Value3,Vars1,Vars2),
71
+ (debug(on)->(writeln([call,[member,variable1,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true),
72
+ (debug(on)->(writeln([exit,[member,Value3,Value2],"Press c."]),(not(get_single_char(97))->true;abort));true))).
73
+
74
+ interpretpart(delete,Variable1,Variable2,Variable3,Vars1,Vars2) :-
75
+ getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),
76
+ delete(Value1,Value2,Value3A),
77
+ val1emptyorvalsequal(Value3,Value3A),
78
+ putvalue(Variable3,Value3A,Vars1,Vars2),
79
+ (debug(on)->(writeln([call,[delete,Value1,Value2,variable3],"Press c."]),(not(get_single_char(97))->true;abort));true),
80
+ (debug(on)->(writeln([exit,[delete,Value1,Value2,Value3A],"Press c."]),(not(get_single_char(97))->true;abort));true).
81
+
82
+
83
+ interpretpart(append,Variable1,Variable2,Variable3,Vars1,Vars2) :-
84
+ getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars1),
85
+ append1(Value1,Value2,Value3A),
86
+ val1emptyorvalsequal(Value3,Value3A),
87
+ putvalue(Variable3,Value3A,Vars1,Vars2),
88
+ (debug(on)->(writeln([call,[append,Value1,Value2,variable3],"Press c."]),(not(get_single_char(97))->true;abort));true),
89
+ (debug(on)->(writeln([exit,[append,Value1,Value2,Value3A],"Press c."]),(not(get_single_char(97))->true;abort));true).
90
+ getvalues(Variable1,Variable2,Value1,Value2,Vars) :-
91
+ getvalue(Variable1,Value1,Vars),
92
+ getvalue(Variable2,Value2,Vars).
93
+ getvalues(Variable1,Variable2,Variable3,Value1,Value2,Value3,Vars) :-
94
+ getvalue(Variable1,Value1,Vars),
95
+ getvalue(Variable2,Value2,Vars),
96
+ getvalue(Variable3,Value3,Vars).
97
+ val1emptyorvalsequal(empty,Value) :-
98
+ not(Value=empty).
99
+ val1emptyorvalsequal(Value,Value) :-
100
+ not(Value=empty).
101
+ isop(is).
102
+ isop(=).
103
+ append1([],Item,Item) :-
104
+ !.
105
+ append1(Item1,Item2,Item3) :-
106
+ ((isvalstr(Item1),Item1A=[Item1]);(not(isvalstr(Item1)),Item1A=Item1)),
107
+ ((isvalstr(Item2),Item2A=[Item2]);(not(isvalstr(Item2)),Item2A=Item2)),
108
+ %%((isvalstr(Item3),Item3A=[Item3]);(not(isvalstr(Item3)),Item3A=Item3)),
109
+ append(Item1A,Item2A,Item3).
110
+ /**delete1(Item1,Item2,Item3) :-
111
+ ((isvalstr(Item1),Item1A=[Item1]);(not(isvalstr(Item1)),Item1A=Item1)),
112
+ ((isvalstr(Item2),Item2A=[Item2]);(not(isvalstr(Item2)),Item2A=Item2)),
113
+ %%((isvalstr(Item3),Item3A=[Item3]);(not(isvalstr(Item3)),Item3A=Item3)),
114
+ delete(Item1A,Item2A,Item3).
115
+ **/
116
+
117
+ removebrackets([[Value]],Value) :-!.
118
+ removebrackets(Value,Value).
Combination-Algorithm-Writer-Stable/lpi_caw_commands.pl ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ interpretstatement1(_F0,_Functions,[[not,Operator],[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
2
+ isop(Operator),
3
+ interpretpart(not_is,Variable1,Variable2,Vars1,Vars2),!.
4
+
5
+ interpretstatement1(_F0,_Functions,[[not,Operator],[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
6
+ comparisonoperator(Operator),
7
+ %%writeln1(4),
8
+ interpretpart(not_iscomparison,Operator,Variable1,Variable2,Vars1,Vars2).
9
+
10
+ interpretpart(not_is,Variable1,Variable2,Vars1,Vars1) :-
11
+ getvalues(Variable1,Variable2,Value1,Value2,Vars1),
12
+ not(isempty(Value1)),
13
+ not(isempty(Value2)),
14
+ %%writeln([call,[[not,=],[Value1,Value2]]]),
15
+ ((not(Value1 = Value2)
16
+ )->
17
+ true%%writeln([exit,[[not,=],[Value1,Value2]]])
18
+ ; fail)%%writeln([fail,[[not,=],[Value1,Value2]]]))
19
+ ,!.
20
+
21
+ interpretpart(not_iscomparison,Operator,Variable1,Variable2,Vars1,Vars1) :- getvalues(Variable1,Variable2,Value1,Value2,Vars1),
22
+ %%writeln([call,[[not,=],[Value1,Value2]]]),
23
+ ((isval(Value1),
24
+ isval(Value2),
25
+ Expression=..[Operator,Value1,Value2],
26
+ not(Expression))->
27
+ true%%writeln([exit,[[not,=],[Value1,Value2]]])
28
+ ; fail)%%writeln([fail,[[not,=],[Value1,Value2]]]))
29
+ ,!.
30
+
31
+ isempty(N) :-
32
+ N=empty.
33
+
34
+ comparisonoperator(>).
35
+ comparisonoperator(>=).
36
+ comparisonoperator(<).
37
+ comparisonoperator(=<).
38
+ %%comparisonoperator(=).
39
+ comparisonoperator(=\=).
Combination-Algorithm-Writer-with-Predicates/LICENSE ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2019, Lucian Green
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Combination-Algorithm-Writer-with-Predicates/LPI CAW commands workaround.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ LPI CAW commands workaround for single argument commands not working, but no singletons working
2
+
3
+ _A - enter value in header (one output, so is it, do outputs one at a time)
4
+
5
+ [] and "" - enter [] or "" somewhere in header and use normal "="
6
+
7
+ not - can still use except for [], "", etc., see previous line
8
+
9
+ With no singletons working, CAWP is more stable than CAWMP
Combination-Algorithm-Writer-with-Predicates/README.md ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Combination-Algorithm-Writer-with-Predicates-Stable
2
+ Combination Algorithm Writer with Predicates Stable (CAWPS)
3
+
4
+ This was copied from CAWPS on 22 3 20, replacing the content of CAWP.
5
+
6
+ Please note: This works with the example below, which CAWMP doesn't.
7
+
8
+ Combination Algorithm Writer is a SWI-Prolog algorithm that finds combinations of given commands that satisfy the given input and output.
9
+
10
+ It verifies for no singletons.
11
+
12
+ # Prerequisites
13
+
14
+ * Please download and install SWI-Prolog for your machine at `https://www.swi-prolog.org/build/`.
15
+
16
+ # 1. Install manually
17
+
18
+ Download <a href="http://github.com/luciangreen/Combination-Algorithm-Writer-with-Predicates/">this repository</a>.
19
+
20
+ # 2. Or Install from List Prolog Package Manager (LPPM)
21
+
22
+ * Download the <a href="https://github.com/luciangreen/List-Prolog-Package-Manager">LPPM Repository</a>:
23
+
24
+ ```
25
+ mkdir GitHub
26
+ cd GitHub/
27
+ git clone https://github.com/luciangreen/List-Prolog-Package-Manager.git
28
+ cd List-Prolog-Package-Manager
29
+ swipl
30
+ ['lppm'].
31
+ lppm_install("luciangreen","Combination-Algorithm-Writer-with-Predicates").
32
+ halt
33
+ ```
34
+
35
+ # Running
36
+
37
+ * In Shell:
38
+ `cd Combination-Algorithm-Writer-with-Predicates`
39
+ `swipl`
40
+
41
+ * Load all files in the form:
42
+ ```
43
+ ['lpi_caw_commands.pl'].
44
+ ['listprologinterpreter1listrecursion4copy52'].
45
+ ['listprologinterpreter3preds5copy52'].
46
+ ['caw5copy11'].
47
+ ```
48
+ Running
49
+
50
+ ```
51
+ caw00(off,f,[[+,2,1]],4,8,[[a,1],[b,1],[c,2],[d,1]],[[e,5]],[[[f1,4,1],[a,b,c,d,e],(:-),[[+,[a,b,f]],[+,[c,f,g]],[+,[d,g,h]],[=,[e,h]]]]],[],P),writeln(P).
52
+
53
+ [[f1,[a,b,c,d,e],:-,[[+,[a,b,f]],[+,[c,f,g]],[+,[d,g,h]],[=,[e,h]]]],[f,[a,b,c,d,e],:-,[[+,[a,b,f]],[+,[c,d,g]],[+,[f,g,h]],[=,[e,h]]]]]
54
+ .
55
+ .
56
+ .
57
+ ```
58
+
59
+ ```
60
+ caw00(off,reverse,[[head,1,1],[tail,1,1],[wrap,1,1],[append,2,1],[reverse,2,1]],6,8,[[a,[1,2,3]],[b,[]]],[[c,[3,2,1]]],[[[reverse,2,1],[[],a,a]]],[],P),writeln(P).
61
+
62
+ not finished
63
+ ```
64
+
65
+ Note: Use (:-) instead of :-.
66
+
67
+ # CAWPS API
68
+
69
+ * To run CAWPS on a Prolog server:
70
+ * Move `cawps-api.pl` to the root (`/username/` or `~` on a server) of your machine.
71
+ * Re-enter the paths to your Prolog files in it.
72
+ * Enter `[cawps-api.pl]` in SWI-Prolog and `server(8000).`.
73
+ * On a local host access the algorithm at `http://127.0.0.1:8000` and replace 127.0.0.1 with your server address on a server.
Combination-Algorithm-Writer-with-Predicates/caw5copy11.pl ADDED
@@ -0,0 +1,351 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :- dynamic debug/1.
2
+ :- dynamic totalvars/1.
3
+ :- dynamic outputvars/1.
4
+
5
+ caw00(Debug,PredicateName,Rules1,MaxLength,TotalVars,InputVarList,OutputVarList,Predicates1,Program1,Program2) :-
6
+ split3(Predicates1,[],Rules2),
7
+ split2(Predicates1,[],Predicates),
8
+ %%writeln([Rules2,Predicates]),
9
+ append(Rules1,Rules2,Rules3),
10
+
11
+ retractall(debug(_)),
12
+ assertz(debug(Debug)),
13
+ retractall(totalvars(_)),
14
+ assertz(totalvars(TotalVars)),
15
+ caw0(Predicates,PredicateName,Rules3,MaxLength,InputVarList,OutputVarList,Program1,Program2).
16
+
17
+ caw0(Predicates,PredicateName,Rules,MaxLength,InputVarList,OutputVarList,Program1,Program2) :-
18
+ varnames(InputVarList,[],InputVars,[],InputValues),
19
+ varnames(OutputVarList,[],OutputVars,[],_OutputValues),
20
+ retractall(outputvars(_)),
21
+ assertz(outputvars(OutputVars)),
22
+ append(InputVars,OutputVars,Vars11),
23
+ %%Vars11=InputVars,
24
+ %%Vars12=InputVars,
25
+ append(InputValues,OutputVars,Vars2),
26
+ %%append(InputValues,OutputValues,Values),
27
+ Query=[PredicateName,Vars2],
28
+ caw(Predicates,Query,PredicateName,Rules,MaxLength,Vars11,InputVars,InputVars,_,OutputVarList,OutputVars,Program1,Program2).
29
+ caw(_,_,_,_,0,_,_,_,_,_,_,_) :- fail, !.
30
+ caw(Predicates,Query,PredicateName,_,_,_VarList,InputVars1,InputVars2,_,OutputVarList,OutputVars,Program1,Program2) :-
31
+ addrules(InputVars2,OutputVars,OutputVars,[],PenultimateVars,[],Program3),
32
+ %%writeln([addrules(InputVars2,OutputVars,OutputVars,[],PenultimateVars,[],Program3)]),
33
+ %%optimise(Program1,InputVars1,InputVars3,PenultimateVars,Program4), %% IV2->3
34
+ %%writeln([optimise(Program1,InputVars1,InputVars3,PenultimateVars,Program4)]),
35
+ append(Program1,Program3,Program5),
36
+ append(InputVars1,OutputVars,Vars2),
37
+ Program22=[
38
+ [PredicateName,Vars2,(:-),
39
+ Program5
40
+ ]
41
+ ],
42
+
43
+ append(Predicates,Program22,Program2),
44
+
45
+ (debug(on)->Debug=on;Debug=off),
46
+ %%writeln([interpret(Debug,Query,Program2,OutputVarList)]),
47
+ %%writeln(""),
48
+ catch(call_with_time_limit(0.05,
49
+ interpret(Debug,Query,Program2,OutputVarList)),
50
+ time_limit_exceeded,
51
+ fail),
52
+ no_singletons(Vars2,Program5),!.
53
+ caw(Predicates,Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,InputVars3,OutputVarList,OutputVars,Program1,Program4) :-
54
+ %%writeln([caw(Query,PredicateName,Rules,MaxLength,VarList,InputVars1,InputVars2,OutputVarList,OutputVars,Program1,Program4)]),
55
+ MaxLength2 is MaxLength - 1,
56
+ %%writeln(["ml",MaxLength2]),
57
+ member([RuleName,NumInputs,NumOutputs],Rules),
58
+ %%writeln([member([RuleName,NumInputs,NumOutputs],Rules)]),
59
+ %%writeln([rule(RuleName,NumInputs,NumOutputs,VarList,VarList2,Rule)]),
60
+ rule(RuleName,NumInputs,NumOutputs,InputVars2,InputVars4,VarList,VarList2,Rule),
61
+ %%writeln([rule(RuleName,NumInputs,NumOutputs,InputVars1,InputVars3,VarList,VarList2,Rule)]),
62
+ append(Program1,[Rule],Program3),
63
+ %%writeln([inputVars3,InputVars3]),
64
+ %%InputVars2=InputVars3,
65
+ %%writeln([program4,Program4]),
66
+ caw(Predicates,Query,PredicateName,Rules,MaxLength2,VarList2,InputVars1,InputVars4,InputVars3,OutputVarList,OutputVars,Program3,Program4).
67
+
68
+ varnames([],Vars,Vars,Values,Values) :- !.
69
+ varnames(VarList,Vars1,Vars2,Values1,Values2) :-
70
+ VarList=[Var|Vars3],
71
+ Var=[VarName,Value],
72
+ append(Vars1,[VarName],Vars4),
73
+ append(Values1,[Value],Values3),
74
+ varnames(Vars3,Vars4,Vars2,Values3,Values2),!.
75
+
76
+ addrules(_,_,[],PV,PV,Program,Program) :- !.
77
+ addrules(VarList,OutputVars1,OutputVars2,PenultimateVars1,PenultimateVars2,Program1,Program2) :-
78
+ OutputVars2=[OutputVar|OutputVars3],
79
+ member(Var,VarList),
80
+ member(OutputVar,OutputVars1),
81
+ append(Program1,[[=,[OutputVar,Var]]],Program3),
82
+ append(PenultimateVars1,[Var],PenultimateVars3),
83
+ addrules(VarList,OutputVars1,OutputVars3,PenultimateVars3,PenultimateVars2,Program3,Program2).
84
+
85
+ %% optimise([[append,[a,a,d]],[append,[a,a,e]],[append,[a,a,f]],[append,[a,b,g]]],[g],P).
86
+
87
+ optimise(Program1,InputVars1,InputVars2,PenultimateVars,Program2) :-
88
+ findrulesflowingtopv1(Program1,InputVars1,InputVars2,PenultimateVars,[],Rules,true),
89
+ %%findrulesflowingtopv1a(Program1,_Program32,InputVars1,InputVars2,PenultimateVars,[],_Rules1),
90
+ intersection(Program1,Rules,Program3),
91
+ unique1(Program3,[],Program2).
92
+ findrulesflowingtopv1(_,_,_,[],Rules,Rules,false).
93
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
94
+ (atom(Var);length(Var,1)),
95
+ findrulesflowingtopv20(Program0,Program0,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1).
96
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Vars1,Rules1,Rules2,IV1Flag1) :-
97
+ Vars1=[Var|Vars2],
98
+ findrulesflowingtopv20(Program0,Program0,InputVars1,InputVars2,Var,Rules1,Rules3,IV1Flag2),
99
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Vars2,Rules3,Rules2,IV1Flag3),
100
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
101
+
102
+ %%findrulesflowingtopv2([],Program,Program,_,_,Rules,Rules).
103
+ findrulesflowingtopv20(_,[],_InputVars1,_InputVars2,_Var,Rules,Rules,false).
104
+ findrulesflowingtopv20(Program0,Rules4,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
105
+ Rules4=[Rule|Rules],
106
+ (findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules3,IV1Flag2)->true;(Rules3=Rules1,IV1Flag2=false)),
107
+ %%delete(Program0,Rule,Program1),
108
+ findrulesflowingtopv20(Program0,Rules,InputVars1,InputVars2,Var,Rules3,Rules2,IV1Flag3),%%p1->0
109
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
110
+ %%findrulesflowingtopv2(_,[],[],_,_,_,Rules,Rules).
111
+ findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
112
+ Rule=[_PredicateName,Vars],
113
+ restlast(Vars,[],Rest,Var),
114
+ %%delete(Program1,[PredicateName,Vars],Program2),
115
+ %%Program2=Program1,
116
+ %%(not(intersection(Rulesx,Rules1))-> x
117
+ %% append, append, unique1
118
+ %%append(Rules1,[Rule],Rules3);Rules3=Rules1),
119
+
120
+ %%member(Var2,Rest),
121
+ %%member(Var2,InputVars1),
122
+
123
+ length(Rest,Length1), Length1>=1,
124
+ subtract(Rest,InputVars1,IV3s),
125
+ length(IV3s,Length3),
126
+ subtract(Rest,IV3s,IV1s),
127
+ length(IV1s,Length2), Length2>=1,
128
+ subtract(IV3s,InputVars2,[]),
129
+
130
+ IV1Flag2=true,
131
+
132
+ %%delete(Program0,Rule,Program1),
133
+
134
+ %%(delete(Program0,Rule,Program3),
135
+ %%iv3s1(IV3s,Program3,IV3s,[]),
136
+ (Length3>=1->
137
+ (findrulesflowingtopv1(Program0,InputVars1,InputVars2,IV3s,[],Rules5,IV1Flag3),not(Rules5=[]));
138
+ (Rules5=[],IV1Flag3=false)),
139
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag4),
140
+ %%->true; Rules5=[],IV1Flag1=IV1Flag4),
141
+
142
+ ((findrulesflowingtopv1(Program0,InputVars1,InputVars2,IV1s,[],Rules6,IV1Flag5), %%iv1s->rest, etc
143
+ iv1flagdisjunction(IV1Flag4,IV1Flag5,IV1Flag1))->true;(Rules6=[],IV1Flag1=IV1Flag4)),
144
+
145
+ append([Rule],Rules1,Rules9),
146
+ append(Rules9,Rules5,Rules7),
147
+ append(Rules7,Rules6,Rules8),
148
+ unique1(Rules8,[],Rules2).
149
+
150
+ /**
151
+ findrulesflowingtopv2(_Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
152
+ Rule=[_PredicateName,Vars],
153
+ restlast(Vars,[],Rest,Var),
154
+ %%delete(Program1,[PredicateName,Vars],Program2),
155
+ %%Program2=Program1,
156
+ (not(member(Rule,Rules1))->
157
+ append(Rules1,[Rule],Rules2);Rules2=Rules1),
158
+ subset(Rest,InputVars2),
159
+ intersection(Rest,InputVars1,Intersection),
160
+ length(Intersection,0),
161
+ %% not((member(Var2,Rest),
162
+ %% member(Var2,InputVars1))),
163
+ IV1Flag1=false.
164
+ **/
165
+ /**
166
+ findrulesflowingtopv2(Program0,Rule,InputVars1,InputVars2,Var,Rules1,Rules2,IV1Flag1) :-
167
+ Rule=[_PredicateName,Vars],
168
+ restlast(Vars,[],Rest,Var),
169
+ %%delete(Program1,[PredicateName,Vars],Program3),
170
+ %%Program3=Program1,
171
+ %%append(Rules1,[Rule],Rules3),
172
+ subset(Rest,InputVars2),
173
+
174
+ intersection(Rest,InputVars1,Intersection),
175
+ length(Intersection,0),
176
+ %% not((member(Var2,Rest),
177
+ %% member(Var2,InputVars1))),
178
+ %% delete(Program0,Rule,Program1),
179
+ IV1Flag2=false,
180
+ findrulesflowingtopv1(Program0,InputVars1,InputVars2,Rest,[],Rules4,IV1Flag3),
181
+ %%not(Rules4=[]),
182
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1),
183
+ append(Rules1,[Rule],Rules7),
184
+ append(Rules7,Rules4,Rules8),
185
+ unique1(Rules8,[],Rules2).
186
+ **/
187
+ /**
188
+ %%->true;(Program2=Program1,Rules2=Rules1)).
189
+ findrulesflowingtopv2(Rule,Program0,Program1,_Program2,InputVars1,InputVars,Var,Rules1,Rules2,IV1Flag1) :-
190
+ Rule=[PredicateName,Vars],
191
+ restlast(Vars,[],Rest,Var),
192
+ %%delete(Program1,[PredicateName,Vars],Program4),
193
+ %%Program4=Program1,
194
+ append(Rules1,[[PredicateName,Vars]],Rules3),
195
+ findrulesflowingtopv1(Program0,Program1,_Program2,InputVars1,InputVars,Rest,Rules3,Rules2,IV1Flag3),
196
+ iv1flagdisjunction(IV1Flag2,IV1Flag3,IV1Flag1).
197
+ %%findrulesflowingtopv2(Program5,Program2,Rest,Rules3,Rules2).
198
+ **/
199
+ iv1flagdisjunction(A,B,true) :-
200
+ (A=true); (B=true).
201
+ iv1flagdisjunction(_,_,false).
202
+ /**
203
+ iv3s0([],_,IV3s1,IV3s2).
204
+ iv3s0(IV3s,Program0,IV3s1,IV3s2).
205
+ IV3s=[IV3|IV3s3],
206
+ iv3s1(IV3,Program0,IV3s1,IV3s4),
207
+ iv3s0(IV3s3,Program0,IV3s4,IV3s2).
208
+ iv3s1(_,[],IV3s,IV3s).
209
+ iv3s1(IV3,Program0,IV3s1,IV3s2) :-
210
+ Program0=[Rule|Rules],
211
+ iv3s2(IV3,Rule,IV3s1,IV3s3),
212
+ iv3s1(IV3,Rules,IV3s3,IV3s2).
213
+ iv3s2(IV3,Rule,IV3s,IV3s1,IV3s2).
214
+ Rule=[_PredicateName,Vars],
215
+ restlast(Vars,[],_Rest,IV3),
216
+ delete(IV3s1,IV3,IV3s2).
217
+ findrulesflowingtopv1a(_,_,_,_,[],Rules,Rules).
218
+ findrulesflowingtopv1a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2) :-
219
+ atom(Var),
220
+ findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2).
221
+ findrulesflowingtopv1a(Program1,Program2,InputVars1,InputVars2,Vars1,Rules1,Rules2) :-
222
+ Vars1=[Var|Vars2],
223
+ findrulesflowingtopv2(Program1,Program3,InputVars1,InputVars2,Var,Rules1,Rules3),
224
+ findrulesflowingtopv1a(Program3,Program2,InputVars1,InputVars2,Vars2,Rules3,Rules2).
225
+ %%findrulesflowingtopv2([],Program,Program,_,_,Rules,Rules).
226
+ findrulesflowingtopv2a([],[],_,_,_,Rules,Rules).
227
+ findrulesflowingtopv2a(Program1,Program2,_InputVars1,InputVars2,Var,Rules1,Rules2) :-
228
+ member([PredicateName,Vars],Program1),
229
+ restlast(Vars,[],Rest,Var),
230
+ (
231
+ %%delete(Program1,[PredicateName,Vars],Program2),
232
+ Program2=Program1,
233
+ append(Rules1,[[PredicateName,Vars]],Rules2),
234
+ subset(Rest,InputVars2)).
235
+ findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars2,Var,Rules1,Rules2) :-
236
+ member([PredicateName,Vars],Program1),
237
+ restlast(Vars,[],Rest,Var),
238
+ (
239
+ %%delete(Program1,[PredicateName,Vars],Program3),
240
+ Program3=Program1,
241
+ append(Rules1,[[PredicateName,Vars]],Rules3),
242
+ subset(Rest,InputVars2)),
243
+ findrulesflowingtopv1a(Program3,Program2,InputVars1,InputVars2,Rest,Rules3,Rules2).
244
+ %%->true;(Program2=Program1,Rules2=Rules1)).
245
+ findrulesflowingtopv2a(Program1,Program2,InputVars1,InputVars,Var,Rules1,Rules2) :-
246
+ member([PredicateName,Vars],Program1),
247
+ restlast(Vars,[],Rest,Var),
248
+ %%delete(Program1,[PredicateName,Vars],Program4),
249
+ Program4=Program1,
250
+ append(Rules1,[[PredicateName,Vars]],Rules3),
251
+ findrulesflowingtopv1a(Program4,Program2,InputVars1,InputVars,Rest,Rules3,Rules2).
252
+ %%findrulesflowingtopv2(Program5,Program2,Rest,Rules3,Rules2).
253
+ **/
254
+
255
+ restlast([],_,_,_) :- fail, !.
256
+ restlast([Last],Rest,Rest,Last) :-
257
+ atom(Last),!.
258
+ restlast(Last,Rest,Rest,Last) :-
259
+ length(Last,1),!.
260
+ restlast(Vars1,Rest1,Rest2,Last) :-
261
+ Vars1=[Var|Vars2],
262
+ append(Rest1,[Var],Rest3),
263
+ restlast(Vars2,Rest3,Rest2,Last),!.
264
+
265
+
266
+
267
+
268
+ rule(RuleName,1,1,InputVars1,InputVars2,VarList,VarList2,Rule) :-
269
+ member(Var,InputVars1),
270
+ rule2(RuleName,Var,VarList,VarList2,Rule,Var1),
271
+ append(InputVars1,[Var1],InputVars2).
272
+ rule2(RuleName,Var,VarList,VarList2,Rule,Var1) :-
273
+ var(VarList,Var1,VarList2),
274
+ Rule=[RuleName,[Var,Var1]],!.
275
+
276
+ rule(RuleName,1,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-
277
+ member(Var,InputVars1),
278
+ rule3(RuleName,Var,VarList,VarList2,Rule,Vars),
279
+ append(InputVars1,Vars,InputVars2).
280
+ rule3(RuleName,Var,VarList,VarList3,Rule,[Var1,Var2]) :-
281
+ var(VarList,Var1,VarList2),
282
+ var(VarList2,Var2,VarList3),
283
+ Rule=[RuleName,[Var,Var1,Var2]],!.
284
+
285
+ rule(RuleName,2,1,InputVars1,InputVars2,VarList,VarList2,Rule) :-
286
+ member(Var,InputVars1),
287
+ member(Vara,InputVars1),
288
+ rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1),
289
+ append(InputVars1,[Var1],InputVars2).
290
+ rule4(RuleName,Var,Vara,VarList,VarList2,Rule,Var1) :-
291
+ var(VarList,Var1,VarList2),
292
+ Rule=[RuleName,[Var,Vara,Var1]],!.
293
+
294
+ rule(RuleName,2,2,InputVars1,InputVars2,VarList,VarList2,Rule) :-
295
+ member(Var,InputVars),
296
+ member(Vara,InputVars),
297
+ rule5(RuleName,Var,Vara,VarList,VarList2,Rule,Vars),
298
+ append(InputVars1,Vars,InputVars2).
299
+ rule5(RuleName,Var,Vara,VarList,VarList3,Rule,[Var1,Var2]) :-
300
+ var(VarList,Var1,VarList2),
301
+ var(VarList2,Var2,VarList3),
302
+ Rule=[RuleName,[Var,Vara,Var1,Var2]],!.
303
+
304
+ %%var(Item,Var,Vars,Vars) :-
305
+ %% member([Item,Var],Vars).
306
+ var(Vars1,Var1,Vars2) :-
307
+ length(Vars1,Vars1Length1),
308
+ Vars1Length2 is Vars1Length1-1,
309
+ length(Vars3,Vars1Length2),
310
+ append(Vars3,[Var2],Vars1),
311
+ char_code(Var2,Var2Code1),
312
+ Var2Code2 is Var2Code1 + 1,
313
+ var2(Var2Code2,Var1),
314
+ append(Vars1,[Var1],Vars2),!.
315
+
316
+ var2(Code,Var1) :-
317
+ outputvars(OutputVars),
318
+ totalvars(TotalVars),
319
+ Code2 is 96+TotalVars,
320
+ Code =< Code2, %% 122
321
+ char_code(Var1,Code),
322
+ not(member(Var1,OutputVars)),!.
323
+ var2(Var2Code,Code3) :-
324
+ Var2Code2 is Var2Code + 1,
325
+ totalvars(TotalVars),
326
+ Code2 is 96+TotalVars,
327
+ Var2Code2 =< Code2,
328
+ var2(Var2Code2,Code3),!.
329
+
330
+
331
+ no_singletons(Vars1,Program):-
332
+ findall(DA,(member(C,Program),C=[_E,D],member(DA,D)),Vars2),
333
+ %%append_list(Vars2,Vars2A),
334
+ append(Vars1,Vars2,Vars3),
335
+ findall(Count1,(member(Item,Vars3),aggregate_all(count,(member(Item,Vars3)),Count1),
336
+ Count1=1),G),G=[].
337
+
338
+ split3([],List,List) :- !.
339
+ split3(Predicates1,List1,List2) :-
340
+ Predicates1=[Item1|List4],
341
+ Item1= [[Name,In,Out]|_Rest],
342
+ append(List1,[[Name,In,Out]],List6),
343
+ split3(List4,List6,List2),!.
344
+
345
+ split2([],List,List) :- !.
346
+ split2(Predicates1,List1,List2) :-
347
+ Predicates1=[Item1|List4],
348
+ Item1= [[Name,_In,_Out]|Rest],
349
+ append(List1,[[Name|Rest]],List6),
350
+ split2(List4,List6,List2),!.
351
+
Combination-Algorithm-Writer-with-Predicates/cawps-api.pl ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :- use_module(library(http/thread_httpd)).
2
+ :- use_module(library(http/http_dispatch)).
3
+ :- use_module(library(http/http_error)).
4
+ :- use_module(library(http/html_write)).
5
+
6
+ % we need this module from the HTTP client library for http_read_data
7
+ :- use_module(library(http/http_client)).
8
+ :- http_handler('/', web_form, []).
9
+
10
+ :- include('files/Combination-Algorithm-Writer-Stable/listprologinterpreter1listrecursion4copy52.pl').
11
+ :- include('files/Combination-Algorithm-Writer-Stable/listprologinterpreter3preds5copy52.pl').
12
+ :- include('files/Combination-Algorithm-Writer-Stable/caw5copy11.pl').
13
+ :- include('files/Combination-Algorithm-Writer-Stable/lpi_caw_commands.pl').
14
+
15
+ server(Port) :-
16
+ http_server(http_dispatch, [port(Port)]).
17
+
18
+ /*
19
+ browse http://127.0.0.1:8000/
20
+ This demonstrates handling POST requests
21
+ */
22
+
23
+ web_form(_Request) :-
24
+ reply_html_page(include('Combination Algorithm Writer Stable'),
25
+
26
+ [
27
+ form([action='/landing', method='POST'], [
28
+ /**
29
+ p([], [
30
+ label([for=debug],'Debug (on/off):'),
31
+ input([name=debug, type=textarea])
32
+ ]),
33
+ **/
34
+ p([], [
35
+ label([for=predicatename],'Predicate Name (e.g. f):'),
36
+ input([name=predicatename, type=textarea])
37
+ ]),
38
+
39
+ p([], [
40
+ label([for=rules],'Rules to choose from (e.g. [[+,2,1]]):'),
41
+ input([name=rules, type=textarea])
42
+ ]),
43
+
44
+ p([], [
45
+ label([for=maxlength],'Max Length of Algorithm Body (e.g. 4):'),
46
+ input([name=maxlength, type=textarea])
47
+ ]),
48
+
49
+ p([], [
50
+ label([for=totalvars],'Total Vars in Predicate (e.g. 8):'),
51
+ input([name=totalvars, type=textarea])
52
+ ]),
53
+ p([], [
54
+ label([for=inputvarlist],'InputVarList (e.g. [[a,1],[b,1],[c,2],[d,1]]):'),
55
+ input([name=inputvarlist, type=textarea])
56
+ ]),
57
+
58
+ p([], [
59
+ label([for=outputvarlist],'OutputVarList (e.g. [[e,5]]):'),
60
+ input([name=outputvarlist, type=textarea])
61
+ ]),
62
+
63
+ p([], [
64
+ label([for=algorithmlibrary],'Algorithm Library (e.g. [[[f1,4,1],[a,b,c,d,e],(:-),[[+,[a,b,f]],[+,[c,f,g]],[+,[d,g,h]],[=,[e,h]]]]], where 4 is the number of inputs and 1 is the number of outputs):'),
65
+ input([name=algorithmlibrary, type=textarea])
66
+ ]),
67
+
68
+ p([], input([name=submit, type=submit, value='Submit'], []))
69
+ ])]).
70
+
71
+ :- http_handler('/landing', landing_pad, []).
72
+
73
+ landing_pad(Request) :-
74
+ member(method(post), Request), !,
75
+ http_read_data(Request, Data, []),
76
+ format('Content-type: text/html~n~n', []),
77
+ format('<p>', []),
78
+ %%portray_clause(Data),
79
+
80
+ %%term_to_atom(Term,Data),
81
+
82
+ %% Predicatename,Rules,Maxlength,Totalvars,Inputvarlist,Outputvarlist,Algorithmlibrary
83
+ %%portray_clause(Data),
84
+
85
+
86
+ Data=[%%debug='off',%%Debug1,
87
+ predicatename=Predicatename,rules=Rules,maxlength=Maxlength,totalvars=Totalvars,inputvarlist=Inputvarlist,outputvarlist=Outputvarlist,algorithmlibrary=Algorithmlibrary,submit=_],
88
+
89
+ term_to_atom(Debug1,'off'),
90
+ term_to_atom(Predicatename1,Predicatename),
91
+ term_to_atom(Rules1,Rules),
92
+ term_to_atom(Maxlength1,Maxlength),
93
+ term_to_atom(Totalvars1,Totalvars),
94
+ term_to_atom(Inputvarlist1,Inputvarlist),
95
+ term_to_atom(Outputvarlist1,Outputvarlist),
96
+ term_to_atom(Algorithmlibrary1,Algorithmlibrary),
97
+
98
+ caw00(Debug1,Predicatename1,Rules1,Maxlength1,Totalvars1,Inputvarlist1,Outputvarlist1,Algorithmlibrary1,[],Result),
99
+ %%format('</p><p>========~n', []),
100
+ portray_clause(Result),
101
+ %%writeln1(Data),
102
+
103
+ format('</p>').
Combination-Algorithm-Writer-with-Predicates/listprologinterpreter1listrecursion4copy52.pl ADDED
@@ -0,0 +1,576 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**:- include('listprologinterpreter3preds5.pl').
2
+ :- include('lpiverify4.pl').
3
+ :- include('caw5 copy 11.pl').
4
+ **/
5
+ :- dynamic debug/1.
6
+
7
+ /** List Prolog Interpreter **/
8
+
9
+ interpret(Debug,Query,Functions,Result) :-
10
+ %%writeln([i1]),
11
+ interpret1(Debug,Query,Functions,Functions,Result),
12
+ !.
13
+ interpret1(Debug,Query,Functions1,Functions2,Result) :-
14
+ %%writeln([i11]),
15
+ retractall(debug(_)),
16
+ assertz(debug(Debug)),
17
+ retractall(cut(_)),
18
+ assertz(cut(off)),
19
+ member1(Query,Functions1,Functions2,Result).
20
+ %%member1([_,R],_,[],R).
21
+ %%member1(_,_,[],[]).
22
+ member1(_,_,[],_) :- fail.
23
+ member1(Query,Functions,Functions2,Vars8) :-
24
+ %%writeln([m1]),
25
+ cut(off)->(
26
+ (Query=[Function,Arguments1],
27
+ (Functions2=[[Function,Arguments2,(:-),Body]|_Functions3]),
28
+ length(Arguments1,Length),
29
+ length(Arguments2,Length),
30
+ checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!, %%->ca2
31
+ %%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),
32
+ (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),
33
+ interpretbody(Functions,Functions2,Vars1,Vars2,Body,true),
34
+ updatevars(FirstArgs,Vars2,[],Result),
35
+ %%reverse(Result,[],Vars7),
36
+ ((not(Result=[])->
37
+ %%Result=[Var71|Vars72],
38
+ unique1(Result,[],Vars8),
39
+ %%writeln(["FirstArgs",FirstArgs,"Vars",Vars2,"Result",Result,"Vars7",Vars7,"Vars72",Vars72,"Var71",Var71,"Vars8",Vars8]),
40
+ %%writeln(["Vars8",Vars8]),
41
+ findresult3(Arguments1,Vars8,[],Result2)
42
+ %%writeln([findresult3,"Arguments1",Arguments1,"Vars8",Vars8,"Result2",Result2])
43
+ );(
44
+ %%writeln(here1),
45
+ Vars8=[],Result2=[])),
46
+ %%writeln(["Arguments1",Arguments1,"Vars2",Vars2,"Result",Result]),
47
+ (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true))
48
+ ->true;
49
+ (%%Query=[Function,_Arguments1],
50
+ %%Functions2=[[Function,_Arguments2,(:-),_Body]|Functions3], %% make like previous trunk?
51
+ member11(Query,Functions,Functions2,Vars8))
52
+ );(turncut(off)%%,Result=[]
53
+ ).
54
+ member11(Query,Functions,Functions2,Result) :-
55
+ %%writeln([m11]),
56
+ %%writeln(["Query",Query,"Functions",Functions,"Functions2",Functions2,"Result",Result]),
57
+ cut(off)->(
58
+ (Query=[Function],
59
+ (Functions2=[[Function,(:-),Body]|_Functions3]),
60
+ (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true),
61
+ Result=[],
62
+ interpretbody(Functions,Functions2,[],_Vars2,Body,true),!,
63
+ (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true)
64
+ )->true;
65
+ (%%Query=[Function],
66
+ %%Functions2=[[Function]|Functions3],
67
+ member12(Query,Functions,Functions2,Result))
68
+ );(turncut(off)).
69
+ member12(Query,Functions,Functions2,Vars8) :-
70
+ %%writeln([m12]),
71
+ cut(off)->(
72
+ (Query=[Function,Arguments1],
73
+ (Functions2=[[Function,Arguments2]|_Functions3]),
74
+ length(Arguments1,Length),
75
+ length(Arguments2,Length),
76
+ checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!, %% ->ca2
77
+ %%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),
78
+ updatevars(FirstArgs,Vars1,[],Result),
79
+ %%reverse(Result,[],Vars7),
80
+ ((not(Result=[])->
81
+ %%Result=[Var71|Vars72],
82
+ unique1(Result,[],Vars8),
83
+ findresult3(Arguments1,Vars8,[],Result2)
84
+ );(
85
+ %%writeln(here2),
86
+ Vars8=[],Result2=[])),
87
+ (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),
88
+ (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true)
89
+ )->true;
90
+ (%%Query=[Function,_Arguments1],
91
+ %%Functions2=[[Function,_Arguments2]|Functions3],
92
+ member13(Query,Functions,Functions2,Vars8))
93
+ );(turncut(off)).
94
+ member13(Query,Functions,Functions2,Result) :-
95
+ %%writeln([m13]),
96
+ cut(off)->(
97
+ (Query=[Function],!,
98
+ (Functions2=[[Function]|_Functions3]),
99
+ (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true),
100
+ Result=[],
101
+ %%interpretbody(Functions,[],_Vars2,Body,true),
102
+ (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true)
103
+ )->true;
104
+ (%%Query=[Function],
105
+ Functions2=[_Function|Functions3],
106
+ member1(Query,Functions,Functions3,Result))
107
+ );(turncut(off)).
108
+ interpret2(Query,Functions1,Functions2,Result) :-
109
+ %%writeln(i2),
110
+ %%writeln(["%%interpret2 Query",Query,"Functions1",Functions1,"Functions2",Functions2]),
111
+ member2(Query,Functions1,Functions2,Result).
112
+ %%member2([_,R],_,[],R).
113
+ %%member2(_,_,[],[]).
114
+ member2(_,_,[],_) :- fail.
115
+ member2(Query,Functions,Functions2,Vars8) :-
116
+ %%writeln([m2]),
117
+ cut(off)->(
118
+ (Query=[Function,Arguments1],
119
+ (Functions2=[[Function,Arguments2,(:-),Body]|_Functions3]),
120
+ length(Arguments1,Length),
121
+ length(Arguments2,Length),
122
+ checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!,
123
+ %%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),
124
+ (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),
125
+ interpretbody(Functions,Functions2,Vars1,Vars2,Body,true), %%**arg2 change
126
+ %%writeln(["Functions",Functions,"Functions2",Functions2,"Vars1",Vars1,"Vars2",Vars2,"Body",Body]),
127
+ updatevars(FirstArgs,Vars2,[],Result),
128
+ %%reverse(Result,[],Vars7),
129
+ ((not(Result=[])->
130
+ %%Result=[Var71|Vars72],
131
+ unique1(Result,[],Vars8),
132
+ findresult3(Arguments1,Vars8,[],Result2)
133
+ %%writeln(["Vars2",Vars2,"Result",Result]),
134
+ );(
135
+ writeln(here3),
136
+ Vars8=[],Result2=[])),
137
+ (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true)
138
+ )->true;
139
+ (%%Query=[Function,_Arguments1],
140
+ %%Functions2=[[Function,_Arguments2,(:-),_Body]|Functions3],
141
+ member21(Query,Functions,Functions2,Vars8))
142
+ );(turncut(off)).
143
+ member21(Query,Functions,Functions2,Result) :-
144
+ %%writeln([m21]),
145
+ cut(off)->(
146
+ (Query=[Function],
147
+ (Functions2=[[Function,(:-),Body]|_Functions3]),
148
+ Vars1=[],
149
+ (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true),
150
+ interpretbody(Functions,Functions2,Vars1,_Vars2,Body,true),!, %%**arg2 change
151
+ (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true)
152
+ )->true;
153
+ (%%Query=[Function],
154
+ %%Functions2=[[Function]|Functions3],
155
+ member22(Query,Functions,Functions2,Result))
156
+ );(turncut(off)).
157
+ member22(Query,Functions,Functions2,Vars8) :-
158
+ %%writeln([m22]),
159
+ cut(off)->(
160
+ (Query=[Function,Arguments1],
161
+ (Functions2=[[Function,Arguments2]|_Functions3]),
162
+ length(Arguments1,Length),
163
+ length(Arguments2,Length),
164
+ checkarguments(Arguments1,Arguments2,[],Vars1,[],FirstArgs),!,
165
+ %%writeln([checkarguments,"Arguments1",Arguments1,"Arguments2",Arguments2,"Vars1",Vars1,"FirstArgs",FirstArgs]),
166
+ updatevars(FirstArgs,Vars1,[],Result),
167
+ %%reverse(Result,[],Vars7),
168
+ ((not(Result=[])->
169
+ %%Result=[Var71|Vars72],
170
+ unique1(Result,[],Vars8),
171
+ findresult3(Arguments1,Vars8,[],Result2)
172
+ );(
173
+ %%writeln(here4),
174
+ Vars8=[],Result2=[])),
175
+ (debug(on)->(writeln([call,[Function,Arguments1],"Press c."]),(not(get_single_char(97))->true;abort));true),
176
+ (debug(on)->(writeln([exit,[Function,Result2],"Press c."]),(not(get_single_char(97))->true;abort));true)
177
+ )->true;
178
+ (%%Query=[Function,_Arguments1],
179
+ %%Functions2=[[Function,_Arguments2]|Functions3],
180
+ member23(Query,Functions,Functions2,Vars8))
181
+ );(turncut(off)).
182
+ member23(Query,Functions,Functions2,Vars8) :-
183
+ %%writeln([m23]),
184
+ cut(off)->(
185
+ (Query=[Function],!,
186
+ (Functions2=[[Function]|_Functions3]),
187
+ (debug(on)->(writeln([call,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true),
188
+ Vars8=[],
189
+ (debug(on)->(writeln([exit,[Function],"Press c."]),(not(get_single_char(97))->true;abort));true)
190
+ )->true;
191
+ (%%Query=[Function],
192
+ Functions2=[_Function|Functions3],
193
+ member2(Query,Functions,Functions3,Vars8))
194
+ );(turncut(off)).
195
+ checkarguments([],[],Vars,Vars,FirstArgs,FirstArgs).
196
+ checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :- %%
197
+ %%writeln(1),
198
+ Arguments1=[Value|Arguments3], %% Value may be a number, string, list or tree
199
+ expressionnotatom(Value),
200
+ Arguments2=[Variable2|Arguments4],
201
+ isvar(Variable2),
202
+ putvalue(Variable2,Value,Vars1,Vars3),
203
+ checkarguments(Arguments3,Arguments4,Vars3,Vars2,FirstArgs1,FirstArgs2).
204
+ checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :- %%A
205
+ %%writeln(2),
206
+ Arguments1=[Variable|Arguments3], %% Value may be a number, string, list or tree
207
+ isvar(Variable),
208
+ Arguments2=[Value|Arguments4],
209
+ expressionnotatom(Value),
210
+ putvalue(Variable,Value,Vars1,Vars3),
211
+ append(FirstArgs1,[[Variable,_]],FirstArgs3),
212
+ checkarguments(Arguments3,Arguments4,Vars3,Vars2,FirstArgs3,FirstArgs2).
213
+ checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :-
214
+ %%writeln(3),
215
+ Arguments1=[Variable1|Arguments3],
216
+ isvar(Variable1),
217
+ Arguments2=[Variable2|Arguments4],
218
+ isvar(Variable2),
219
+ (getvalue(Variable2,Value,Vars1)->true;Value=empty),
220
+ putvalue(Variable2,Value,Vars1,Vars3),
221
+ append(FirstArgs1,[[Variable1,Variable2]],FirstArgs3),
222
+ checkarguments(Arguments3,Arguments4,Vars3,Vars2,FirstArgs3,FirstArgs2).
223
+ checkarguments(Arguments1,Arguments2,Vars1,Vars2,FirstArgs1,FirstArgs2) :-
224
+ %%writeln(4),
225
+ Arguments1=[Value1|Arguments3],
226
+ expressionnotatom(Value1),
227
+ Arguments2=[Value1|Arguments4],
228
+ expressionnotatom(Value1),
229
+ checkarguments(Arguments3,Arguments4,Vars1,Vars2,FirstArgs1,FirstArgs2).
230
+
231
+ interpretbody(_Functions1,_Functions2,Vars,Vars,[],true) :- !.
232
+
233
+ interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-
234
+ Body=[Statements1|Statements2],
235
+ interpretbody(Functions0,Functions,Vars1,Vars3,Statements1,Result2),
236
+ interpretbody(Functions0,Functions,Vars3,Vars2,Statements2,Result3),
237
+ %%((Result3=cut)->!;true),
238
+ logicalconjunction(Result1,Result2,Result3),!.
239
+
240
+ interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-
241
+ Body=[[not,[Statements]]|Statements2],
242
+ interpretbody(Functions0,Functions,Vars1,Vars3,Statements,Result2),
243
+ %%((Result2=cut)->!;true),
244
+ interpretbody(Functions0,Functions,Vars3,Vars2,Statements2,Result3),
245
+ %%((Result3=cut)->!;true),
246
+ logicalnot(Result2,Result4),
247
+ (logicalconjunction(Result1,Result4,Result3)->true;(Result1=false)),!.
248
+ interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-
249
+ Body=[[Statements1],or,[Statements2]],
250
+ (interpretbody(Functions0,Functions,Vars1,Vars2,Statements1,Result1);
251
+ %%,((Value1=cut)->!;true));
252
+ interpretbody(Functions0,Functions,Vars1,Vars2,Statements2,Result1)),!.
253
+ %%,((Value=cut)->!;true)).
254
+ %%(logicaldisjunction(Result1,Value1,Value2)->true;(Result1=false)).
255
+
256
+
257
+ interpretbody(Functions0,Functions,Vars1,Vars2,Body,Result1) :-
258
+ Body=[Statement|Statements],
259
+ %%writeln(["Functions0",Functions0,"Functions",Functions,"Statement",Statement,"Vars1",Vars1,"Vars3",Vars3,"Result2",Result2,"Cut",Cut]),
260
+ interpretstatement1(Functions0,Functions,Statement,Vars1,Vars3,Result2,Cut),
261
+ %%writeln(["here1"]),
262
+ ((not(Cut=cut))->(Functions2=Functions);(turncut(on))), %% cut to interpret1/2 (assertz)
263
+ %%writeln(["here3"]),
264
+ interpretbody(Functions0,Functions2,Vars3,Vars2,Statements,Result3),
265
+ %%((Result3=cut)->!;true),
266
+ %%writeln(["here4"]),
267
+ logicalconjunction(Result1,Result2,Result3),!.
268
+ %%writeln([Result1,Result2,Result3]).
269
+ turncut(State1) :-
270
+ cut(State2),
271
+ retract(cut(State2)),
272
+ assertz(cut(State1)).
273
+ logicaldisjunction(true,Result2,Result3) :-
274
+ true(Result2);true(Result3).
275
+ logicalconjunction(true,Result2,Result3) :-
276
+ true(Result2),true(Result3).
277
+ logicalnot(Result1,Result2) :-
278
+ true(Result1),false(Result2).
279
+ logicalnot(Result1,Result2) :-
280
+ false(Result1),true(Result2).
281
+ true(true).
282
+ false(false).
283
+
284
+ %%interpretstatement1(_F0,[],_,Vars,Vars,true,nocut) :- !
285
+ %%writeln("AND HERE!")
286
+ %% .
287
+
288
+ interpretstatement1(_F0,_Functions,[cut,[]],Vars,Vars,true,cut) :- !.
289
+
290
+ interpretstatement1(_F0,_Functions,[atom,[Variable]],Vars,Vars,true,nocut) :-
291
+ getvalue(Variable,Value,Vars),
292
+ (debug(on)->(writeln([call,[atom,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true),
293
+ atom(Value),
294
+ (debug(on)->(writeln([exit,[atom,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true).
295
+
296
+ interpretstatement1(_F0,_Functions,[string,[Variable]],Vars,Vars,true,nocut) :-
297
+ getvalue(Variable,Value,Vars),
298
+ (debug(on)->(writeln([call,[string,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true),
299
+ string(Value),
300
+ (debug(on)->(writeln([exit,[string,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true).
301
+
302
+ interpretstatement1(_F0,_Functions,[number,[Variable]],Vars,Vars,true,nocut) :-
303
+ getvalue(Variable,Value,Vars),
304
+ (debug(on)->(writeln([call,[number,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true),
305
+ number(Value),
306
+ (debug(on)->(writeln([exit,[number,[Value]],"Press c."]),(not(get_single_char(97))->true;abort));true).
307
+
308
+ interpretstatement1(_F0,_Functions,[variable,[Variable]],Vars,Vars,true,nocut) :-
309
+ var(Variable),
310
+ (debug(on)->(writeln([call,[variable,[Variable]],"Press c."]),(not(get_single_char(97))->true;abort));true),
311
+ (debug(on)->(writeln([exit,[variable,[Variable]],"Press c."]),(not(get_single_char(97))->true;abort));true).
312
+
313
+ interpretstatement1(_F0,_Functions,[Operator,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
314
+ isop(Operator),
315
+ interpretpart(is,Variable1,Variable2,Vars1,Vars2).
316
+
317
+ interpretstatement1(_F0,_Functions,[Operator,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
318
+ %%writeln(31),
319
+ isop(Operator),
320
+ interpretpart(is,Variable2,Variable1,Vars1,Vars2).
321
+
322
+ interpretstatement1(_F0,_Functions,[+,[Variable2,Variable3,Variable1]],Vars1,Vars2,true,nocut) :-
323
+ %%writeln(4),
324
+ interpretpart(isplus,Variable1,Variable2,Variable3,Vars1,Vars2).
325
+
326
+ %%interpretstatement1(_F0,_Functions,[Variable2+Variable3,is,Variable1],Vars1,Vars2,true,nocut) :-
327
+ %%writeln(41),
328
+ %%interpretpart(isplus,Variable1,Variable2,Variable3,Vars1,Vars2).
329
+
330
+ interpretstatement1(_F0,_Functions,[=,[Variable1,[Variable2,Variable3]]],Vars1,Vars2,true,nocut) :-
331
+ %%writeln(5),
332
+ interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2).
333
+
334
+ %%interpretstatement1(_F0,_Functions,[[Variable2,Variable3]=Variable1],Vars1,Vars2,true,nocut) :-
335
+ %%writeln(51),
336
+ %% interpretpart(match,Variable1,Variable2,Variable3,Vars1,Vars2).
337
+
338
+ interpretstatement1(_F0,_Functions,[=,[Variable1,[Variable2]]],Vars1,Vars2,true,nocut) :-
339
+ %%writeln(52),
340
+ interpretpart(bracket1,Variable1,Variable2,Vars1,Vars2).
341
+
342
+ interpretstatement1(_F0,_Functions,[=,[[Variable1],[Variable2]]],Vars1,Vars2,true,nocut) :-
343
+ %%writeln(53),
344
+ interpretpart(bracket2,Variable1,Variable2,Vars1,Vars2).
345
+
346
+ interpretstatement1(_F0,_Functions,[head,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
347
+ %%writeln(6),
348
+ interpretpart(head,Variable1,Variable2,Vars1,Vars2).
349
+
350
+ interpretstatement1(_F0,_Functions,[tail,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
351
+ %%writeln(61),
352
+ interpretpart(tail,Variable1,Variable2,Vars1,Vars2).
353
+
354
+
355
+ interpretstatement1(_F0,_Functions,[member,[Variable1,Variable2]],Vars1,Vars2,true,nocut) :-
356
+ %%writeln(8),
357
+ interpretpart(member,Variable1,Variable2,Vars1,Vars2).
358
+
359
+ interpretstatement1(_F0,_Functions,[delete,[Variable1,Variable2,Variable3]],Vars1,Vars2,true,nocut) :-
360
+ %%writeln(),
361
+ interpretpart(delete,Variable1,Variable2,Variable3,Vars1,Vars2).
362
+ %%** all in form f,[1,1,etc], including + with 0,1
363
+
364
+ interpretstatement1(_F0,_Functions,[append,[Variable1,Variable2,Variable3]],Vars1,Vars2,true,nocut) :-
365
+ %%writeln(9),
366
+ interpretpart(append,Variable1,Variable2,Variable3,Vars1,Vars2).
367
+
368
+ interpretstatement1(Functions0,_Functions,Query1,Vars1,Vars8,true,nocut) :-
369
+ %%writeln("h1/10"),
370
+ Query1=[Function,Arguments],
371
+ %%writeln(["Arguments",Arguments,"Vars1",Vars1]),
372
+ substitutevarsA1(Arguments,Vars1,[],Vars3,[],FirstArgs), %%% var to value, after updatevars: more vars to values, and select argument vars from latest vars
373
+ %%writeln([substitutevarsA1,arguments,Arguments,vars1,Vars1,vars3,Vars3,firstargs,FirstArgs]),
374
+ Query2=[Function,Vars3], %% Bodyvars2?
375
+ %% debug(on)->writeln([call,[Function,[Vars3]]]),
376
+ %%writeln(["Query2",Query2,"Functions0",Functions0]),
377
+ interpret2(Query2,Functions0,Functions0,Result1),
378
+ updatevars2(FirstArgs,Result1,[],Vars5),
379
+ updatevars3(Vars1,Vars5,Vars6),
380
+ reverse(Vars6,[],Vars7),
381
+ ((not(Vars7=[])->
382
+ %%Vars7=[Var71|Vars72],
383
+ unique1(Vars7,[],Vars8)
384
+ );(
385
+ writeln(here1),
386
+ Vars8=[])).
387
+ %%**** reverse and take first instance of each variable.
388
+ %%findresult3(Arguments,Vars6,[],Result2)
389
+ %%writeln(["FirstArgs",FirstArgs,"Result1",Result1,"Vars5",Vars5,"Vars4",Vars4]),
390
+ %%writeln(["Vars1:",Vars1,"Vars4:",Vars4]),
391
+ %% debug(on)->writeln([exit,[Function,[Result2]]]).
392
+ interpretstatement1(Functions0,_Functions,Query,Vars,Vars,true) :-
393
+ Query=[_Function],
394
+ debug(on)->writeln([call,[Function]]),
395
+ interpret2(Query,Functions0,Functions0,_Result1),
396
+ debug(on)->writeln([exit,[Function]]).
397
+
398
+
399
+ interpretstatement1(_Functions0, _Functions,_Query,_Vars1,_Vars2,false) :-
400
+ writeln([false]).
401
+
402
+ interpretstatement2(Value,_Vars,Value) :-
403
+ (number(Value);atom(Value)).
404
+ interpretstatement2(Variable,Vars1,Value) :-
405
+ getvalue(Variable,Value,Vars1).
406
+ interpretstatement3(A + B,Vars,Value1) :-
407
+ interpretstatement2(A,Vars,Value2),
408
+ interpretstatement2(B,Vars,Value3),
409
+ Value1 = Value2 + Value3.
410
+ interpretstatement3(Value,_Vars,Value) :-
411
+ (number(Value);atom(Value)).
412
+ interpretstatement3(Variable,Vars,Value) :-
413
+ getvalue(Variable,Value,Vars).
414
+ getvalue(Variable,Value,Vars) :-
415
+ ((not(isvar(Variable)),isvalstrorundef(Value),Variable=Value);
416
+ (isvar(Variable),isvalstrorundef(Value),getvar(Variable,Value,Vars))).
417
+ putvalue(Variable,Value,Vars1,Vars2) :-
418
+ ((not(isvar(Variable)),isvalstrorundef(Value),Variable=Value);
419
+ (isvar(Variable),isvalstrorundef(Value),updatevar(Variable,Value,Vars1,Vars2))),!.
420
+ getvar(Variable,Value,Vars) :-
421
+ member([Variable,Value],Vars),
422
+ not(Value=empty).
423
+ getvar(undef,undef,_Vars) :-
424
+ !.
425
+ getvar(Variable,empty,Vars) :-
426
+ not(member([Variable,_Value],Vars));
427
+ member([Variable,empty],Vars).
428
+ updatevar(undef,_Value,Vars,Vars) :-
429
+ !.
430
+ updatevar(Variable,Value,Vars1,Vars2) :-
431
+ ((((member([Variable,empty],Vars1),
432
+ delete(Vars1,[Variable,empty],Vars3),
433
+ append(Vars3,[[Variable,Value]],Vars2));
434
+ ((not(member([Variable,Value1],Vars1)),
435
+ ((Value1=empty)->true;(Value1=Value)))),
436
+ append(Vars1,[[Variable,Value]],Vars2));
437
+ (member([Variable,Value],Vars1),Vars2=Vars1));
438
+ (undef(Variable),
439
+ append(Vars1,[[Variable,Value]],Vars2))).
440
+ updatevars(_FirstArgs,[],Vars,Vars).
441
+ updatevars(FirstArgs,Vars1,Vars2,Vars3) :-
442
+ Vars1=[[Variable1,Value]|Vars4],
443
+ ((member([Variable2,Variable1],FirstArgs), %% removed brackets around firstargs here and 2 line below
444
+ append(Vars2,[[Variable2,Value]],Vars5));
445
+ (member([Variable1,_Variable2],FirstArgs),
446
+ append(Vars2,[[Variable1,Value]],Vars5))),
447
+ updatevars(FirstArgs,Vars4,Vars5,Vars3),
448
+ !.
449
+ updatevars(FirstArgs,Vars1,Vars2,Vars3) :-
450
+ Vars1=[_Vars4|Vars5],
451
+ updatevars(FirstArgs,Vars5,Vars2,Vars3).
452
+ updatevars2(_FirstArgs,[],Vars,Vars).
453
+ updatevars2(FirstArgs,Vars1,Vars2,Vars3) :-
454
+ Vars1=[[Variable,Value]|Vars4],
455
+ (member(Variable,FirstArgs), %% removed brackets around firstargs here and 2 line below, ** vars1 into arg in (10), check cond
456
+ append(Vars2,[[Variable,Value]],Vars5)),
457
+ updatevars2(FirstArgs,Vars4,Vars5,Vars3).
458
+ updatevars3(Vars1,[],Vars1).
459
+ updatevars3(Vars1,Vars2,Vars4) :-
460
+ Vars2=[[Variable,Value]|Vars5],
461
+ delete(Vars1,[Variable,empty],Vars6),
462
+ append(Vars6,[[Variable,Value]],Vars7),
463
+ updatevars3(Vars7,Vars5,Vars4),
464
+ !.
465
+ updatevars3(Vars1,Vars2,Vars4) :-
466
+ Vars2=[[Variable,Value]|Vars5],
467
+ append(Vars1,[[Variable,Value]],Vars6),
468
+ updatevars3(Vars6,Vars5,Vars4).
469
+ reverse([],List,List).
470
+ reverse(List1,List2,List3) :-
471
+ List1=[Head|Tail],
472
+ append([Head],List2,List4),
473
+ reverse(Tail,List4,List3).
474
+ unique1([],Items,Items).
475
+ unique1([Item|Items1],Items2,Items3) :-
476
+ delete(Items1,Item,Items4),
477
+ append(Items2,[Item],Items5),
478
+ unique1(Items4,Items5,Items3).
479
+
480
+ isvar(Variable) :-
481
+ atom(Variable).
482
+ isval(Value) :-
483
+ number(Value).
484
+ isvalstr(N) :-
485
+ isval(N);string(N).
486
+ /**isvalstrempty(N) :-
487
+ isval(N);(string(N);N=empty).**/
488
+ isvalstrempty(N) :-
489
+ isval(N),!.
490
+ isvalstrempty(N) :-
491
+ string(N),!.
492
+ isvalstrempty(empty) :- !.
493
+ /**isvalstrempty(N) :-
494
+ atom(N),fail,!.
495
+ **/
496
+ isvalstrorundef(N) :-
497
+ var(N);(not(var(N)),(isval(N);expression(N))).
498
+ undef(N) :-
499
+ var(N).
500
+ /**
501
+ expression(N) :-
502
+ isval(N);(string(N);atom(N)),!.
503
+ expression([]).
504
+ expression(empty).
505
+ expression([N]) :-
506
+ expression(N).
507
+ expression([N|Ns]):-
508
+ expression(N),expression(Ns).
509
+ **/
510
+
511
+ expression(empty) :-
512
+ !.
513
+ expression(N) :-
514
+ isval(N),!.
515
+ expression(N) :-
516
+ string(N),!.
517
+ expression(N) :-
518
+ atom(N),!.
519
+ expression([]) :-
520
+ !.
521
+ expression(N) :-
522
+ %% not(atom(N)),
523
+ length(N,L),L>=2,
524
+ expression2(N).
525
+ expression2([]).
526
+ expression2([N|Ns]) :-
527
+ expression3(N),
528
+ expression2(Ns).
529
+ expression3(N) :-
530
+ isval(N),!.
531
+ expression3(N) :-
532
+ string(N),!.
533
+ expression3(N) :-
534
+ atom(N),!.
535
+
536
+ expressionnotatom(N) :-
537
+ isvalstrempty(N),!.
538
+ expressionnotatom(N) :-
539
+ not(atom(N)),
540
+ length(N,L),L>=2,
541
+ expressionnotatom2(N).
542
+ expressionnotatom2([]).
543
+ expressionnotatom2([N|Ns]) :-
544
+ isvalstrempty(N),
545
+ expressionnotatom2(Ns).
546
+
547
+ substitutevarsA1(Arguments,Vars1,Vars2,Vars3,FirstArgs1,FirstArgs2) :-
548
+ substitutevarsA2(Arguments,Vars1,Vars2,Vars3,FirstArgs1,FirstArgs2).
549
+ substitutevarsA2([],_Vars1,Vars2,Vars2,FirstArgs,FirstArgs).
550
+ substitutevarsA2(Arguments,Vars1,Vars2,Vars3,FirstArgs1,FirstArgs2) :-
551
+ Arguments=[Variable|Variables],
552
+ ((getvalue(Variable,Value,Vars1),
553
+ Value=empty)->
554
+ ((append(Vars2,[Variable],Vars4)),
555
+ (isvar(Variable)->append(FirstArgs1,[Variable],
556
+ FirstArgs3);FirstArgs3=FirstArgs1));
557
+ (getvalue(Variable,Value,Vars1),
558
+ append(Vars2,[Value],Vars4)),
559
+ FirstArgs3=FirstArgs1),
560
+ substitutevarsA2(Variables,Vars1,Vars4,Vars3,FirstArgs3,FirstArgs2).
561
+
562
+ findresult3([],_Result,Result2,Result2).
563
+ findresult3(Arguments1,Result1,Result2,Result3) :-
564
+ Arguments1=[Value|Arguments2],
565
+ expressionnotatom(Value),
566
+ append(Result2,[Value],Result4),
567
+ findresult3(Arguments2,Result1,Result4,Result3).
568
+ findresult3(Arguments1,Result1,Result2,Result3) :-
569
+ Arguments1=[Variable|Arguments2],
570
+ isvar(Variable),
571
+ member([Variable,Value],Result1),
572
+ append(Result2,[Value],Result4),
573
+ findresult3(Arguments2,Result1,Result4,Result3).
574
+
575
+
576
+