1 | srecord DollarVarPattern(S pat) {} |
2 | srecord Definition(O lhs, O rhs) {} |
3 | srecord Input(S s) {} |
4 | srecord Statement(S s) {} |
5 | srecord ProposeRewrite(S a, S b) {} |
6 | |
7 | sclass ThoughtSpace1 { |
8 | new LinkedHashSet objects; |
9 | int initialObjects; |
10 | static bool printStats; |
11 | new L<VF1<ThoughtSpace1>> steppers; |
12 | new Set<Class> lookingFor; |
13 | |
14 | void think(bool print) { |
15 | initialObjects = l(objects); |
16 | long startTime = now(); |
17 | int n = -1; |
18 | while (l(objects) > n) { |
19 | n = l(objects); |
20 | |
21 | // def + def -> patterned def [emerge pattern] |
22 | L<Definition> defs = instancesOf(Definition); |
23 | for (Definition a : defs) |
24 | for (Definition b : defs) |
25 | if (a.lhs instanceof S && b.lhs instanceof S) { |
26 | S pat = combineToDollarVarPattern((S) a.lhs, (S) b.lhs); |
27 | if (containsDollarVars(pat)) |
28 | objects.add(DollarVarPattern(pat)); |
29 | } |
30 | |
31 | // pattern + def -> patterned def [generalize] |
32 | for (DollarVarPattern pat : instancesOf(DollarVarPattern)) { |
33 | for (Definition a : defs) if (a.lhs instanceof S) { |
34 | SS match = matchDollarVarsIC(pat.pat, (S) a.lhs); |
35 | if (match != null) |
36 | generalizeDefinition(a, match); |
37 | } |
38 | } |
39 | |
40 | // input + patterned def -> def [specialize] |
41 | // input + patterned def -> statement [implication] |
42 | for (Input i : instancesOf(Input)) |
43 | for (Definition a : defs) if (a.lhs instanceof DollarVarPattern) { |
44 | SS match = matchDollarVarsIC(((DollarVarPattern) a.lhs).pat, i.s); |
45 | new Matches m; |
46 | if (match != null) { |
47 | S rhs = ((DollarVarPattern) a.rhs).pat; |
48 | if (swic_trim(rhs, "[implies]", m)) |
49 | objects.add(Statement(transformEachTokenUsingMap(m.rest(), match))); |
50 | else |
51 | specializeDefinition(a, match); |
52 | } |
53 | } |
54 | |
55 | for (VF1<ThoughtSpace1> stepper : concurrentlyIterateList(steppers)) |
56 | callF(stepper, this); |
57 | } |
58 | long time = now()-startTime; |
59 | |
60 | if (print) |
61 | pnlStruct(dropFirst(initialObjects, asList(objects))); |
62 | |
63 | if (printStats) |
64 | print("\n" + n2(l(objects)-initialObjects, "rewrite") + " in " + (now()-startTime) + " ms"); |
65 | } |
66 | |
67 | void generalizeDefinition(Definition def, SS match) { |
68 | SS rev = reverseCIMap(match); |
69 | S lhs = transformEachTokenUsingMap((S) def.lhs, rev); |
70 | S rhs = transformEachTokenUsingMap((S) def.rhs, rev); |
71 | addIfNotNull(objects, Definition(DollarVarPattern(lhs), DollarVarPattern(rhs))); |
72 | } |
73 | |
74 | void specializeDefinition(Definition def, SS match) { |
75 | S lhs = transformEachTokenUsingMap(((DollarVarPattern) def.lhs).pat, match); |
76 | S rhs = transformEachTokenUsingMap(((DollarVarPattern) def.rhs).pat, match); |
77 | addIfNotNull(objects, Definition(lhs, rhs)); |
78 | } |
79 | |
80 | void addDefinitions(S defs) { |
81 | for (S s : tlft(defs)) { |
82 | L<S> l = splitAtJavaToken(s, "="); |
83 | objects.add(Definition(first(l), second(l))); |
84 | } |
85 | } |
86 | |
87 | void printDefinitionsForInput() { |
88 | final Input in = firstInstanceOf(Input, objects); |
89 | if (in != null) { |
90 | L<Definition> definitions = definitionsForInput(in); |
91 | print("Got " + n2(definitions, "definition") + " for input " + quote(in.s) + (empty(definitions) ? "." : ":")); |
92 | printStructLinesIndent(definitions); |
93 | } |
94 | } |
95 | |
96 | Input input() { |
97 | ret firstInstanceOf(Input, objects); |
98 | } |
99 | |
100 | S getInput() { |
101 | Input i = input(); |
102 | ret i == null ? null : i.s; |
103 | } |
104 | |
105 | L<Definition> definitionsForInput() { ret definitionsForInput(input()); } |
106 | |
107 | L<Definition> definitionsForInput(final Input in) { |
108 | ret [Definition d : instancesOf(Definition) | eqic_gen(d.lhs, in.s)]; |
109 | } |
110 | |
111 | L<S> getDefinitionTexts(fS s) { |
112 | new L<S> l; |
113 | for (Definition d : instancesOf(Definition)) |
114 | if (eqic_gen(d.lhs, s)) |
115 | l.add((S) d.rhs); |
116 | ret l; |
117 | } |
118 | |
119 | L<Definition> literalDefinitions() { |
120 | ret [Definition d : instancesOf(Definition) | d.lhs instanceof S && d.rhs instanceof S]; |
121 | } |
122 | |
123 | L<Statement> statements() { |
124 | ret instancesOf(Statement); |
125 | } |
126 | |
127 | void removeInitialObjects { |
128 | replaceCollection(objects, dropFirst(initialObjects, asList(objects))); |
129 | initialObjects = 0; |
130 | } |
131 | |
132 | <A> L<A> instancesOf(Class<A> c) { |
133 | ret main.instancesOf(c, objects); |
134 | } |
135 | |
136 | void removeObjectsOfType(Class type) { |
137 | removeAll(objects, instancesOf(type)); |
138 | } |
139 | |
140 | void setInput(S s) { |
141 | removeObjectsOfType(Input); |
142 | objects.add(Input(s)); |
143 | } |
144 | |
145 | void add(O o) { objects.add(o); } |
146 | |
147 | void addStepper(VF1<ThoughtSpace1> s) { steppers.add(s); } |
148 | |
149 | bool found() { |
150 | ret containsExactInstanceOfAny(objects, lookingFor); |
151 | } |
152 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1017040 |
Snippet name: | ThoughtSpace1 - "direct" thought space (directly handling Java objects) |
Eternal ID of this version: | #1017040/17 |
Text MD5: | eade9ed25a1021a5eb20adc067917383 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-07-11 14:30:23 |
Source code size: | 4791 bytes / 152 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 414 / 943 |
Version history: | 16 change(s) |
Referenced in: | [show references] |