Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

143
LINES

< > BotCompany Repo | #1017032 // Patterns Step 2 [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (7618L/55K).

1  
!7
2  
3  
srecord DollarVarPattern(S pat) {}
4  
srecord Definition(O lhs, O rhs) {}
5  
srecord Input(S s) {}
6  
srecord Statement(S s) {}
7  
8  
sS defs = [[
9  
A cup of water = A cup with water inside
10  
A cup of Peter = A cup that belongs to Peter
11  
A cup of glass = A cup that consists of glass
12  
A cup of style = A cup that has a lot of style
13  
a $a with $b inside       = [implies] $b is smaller than a $a
14  
a $a that belongs to $b   = [implies] $b is a person
15  
a $a that consists of $b  = [implies] $b is a material
16  
a $a that has a lot of $b = [implies] $b is a property
17  
]];
18  
19  
p-exp {
20  
  new ThoughtSpace ts;
21  
  ts.objects.add(Input("a cup of joe"));
22  
  
23  
  ts.addDefinitions(defs);
24  
  ts.think(false);
25  
  //ts.printDefinitionsForInput();
26  
  
27  
  for (Definition d : ts.definitionsForInput()) {
28  
    print("\nTrying definition " + sfu(d));
29  
    new ThoughtSpace ts2;
30  
    ts2.objects.add(Input((S) d.rhs));
31  
    ts2.addDefinitions(defs);
32  
    ts2.think(false);
33  
    ts2.removeInitialObjects();
34  
    
35  
    //pnlStruct(ts2.literalDefinitions());
36  
    pnlStruct(ts2.statements());
37  
  }
38  
}
39  
40  
sclass ThoughtSpace {
41  
  new LinkedHashSet objects;
42  
  int initialObjects;
43  
  
44  
  void think(bool print) {
45  
    initialObjects = l(objects);
46  
    long startTime = now();
47  
    int n = -1;
48  
    while (l(objects) > n) {
49  
      n = l(objects);
50  
      
51  
      // def + def -> patterned def [emerge pattern]
52  
      L<Definition> defs = instancesOf(Definition, objects);
53  
      for (Definition a : defs)
54  
        for (Definition b : defs)
55  
          if (a.lhs instanceof S && b.lhs instanceof S) {
56  
            S pat = combineToDollarVarPattern((S) a.lhs, (S) b.lhs);
57  
            if (containsDollarVars(pat))
58  
              objects.add(DollarVarPattern(pat));
59  
          }
60  
          
61  
      // pattern + def -> patterned def [generalize]
62  
      for (DollarVarPattern pat : instancesOf(DollarVarPattern, objects)) {
63  
        for (Definition a : defs) if (a.lhs instanceof S) {
64  
          SS match = matchDollarVarsIC(pat.pat, (S) a.lhs);
65  
          if (match != null)
66  
            generalizeDefinition(a, match);
67  
        }
68  
      }
69  
      
70  
      // input + patterned def -> def       [specialize]
71  
      // input + patterned def -> statement [implication]
72  
      for (Input i : instancesOf(Input, objects))
73  
        for (Definition a : defs) if (a.lhs instanceof DollarVarPattern) {
74  
          SS match = matchDollarVarsIC(((DollarVarPattern) a.lhs).pat, i.s);
75  
          new Matches m;
76  
          if (match != null) {
77  
            S rhs = ((DollarVarPattern) a.rhs).pat;
78  
            if (swic_trim(rhs, "[implies]", m))
79  
              objects.add(Statement(transformEachTokenUsingMap(m.rest(), match)));
80  
            else
81  
              specializeDefinition(a, match);
82  
          }
83  
        }
84  
    }
85  
    long time = now()-startTime;
86  
    
87  
    if (print)
88  
      pnlStruct(dropFirst(initialObjects, asList(objects)));
89  
    print("\n" + n2(l(objects)-initialObjects, "rewrite") + " in " + (now()-startTime) + " ms");
90  
  }
91  
  
92  
  void generalizeDefinition(Definition def, SS match) {
93  
    SS rev = reverseCIMap(match);
94  
    S lhs = transformEachTokenUsingMap((S) def.lhs, rev);
95  
    S rhs = transformEachTokenUsingMap((S) def.rhs, rev);
96  
    addIfNotNull(objects, Definition(DollarVarPattern(lhs), DollarVarPattern(rhs)));
97  
  }
98  
  
99  
  void specializeDefinition(Definition def, SS match) {
100  
    S lhs = transformEachTokenUsingMap(((DollarVarPattern) def.lhs).pat, match);
101  
    S rhs = transformEachTokenUsingMap(((DollarVarPattern) def.rhs).pat, match);
102  
    addIfNotNull(objects, Definition(lhs, rhs));
103  
  }
104  
  
105  
  void addDefinitions(S defs) {
106  
    for (S s : tlft(defs)) {
107  
      L<S> l = splitAtJavaToken(s, "=");
108  
      objects.add(Definition(first(l), second(l)));
109  
    }
110  
  }
111  
  
112  
  void printDefinitionsForInput() {
113  
    final Input in = firstInstanceOf(Input, objects);
114  
    if (in != null) {
115  
      L<Definition> definitions = definitionsForInput(in);
116  
      print("Got " + n2(definitions, "definition") + " for input " + quote(in.s) + (empty(definitions) ? "." : ":"));
117  
      printStructLinesIndent(definitions);
118  
    }
119  
  }
120  
  
121  
  Input input() {
122  
    ret firstInstanceOf(Input, objects);
123  
  }
124  
  
125  
  L<Definition> definitionsForInput() { ret definitionsForInput(input()); }
126  
  
127  
  L<Definition> definitionsForInput(final Input in) {
128  
    ret [Definition d : instancesOf(Definition, objects) | eqic_gen(d.lhs, in.s)];
129  
  }
130  
  
131  
  L<Definition> literalDefinitions() {
132  
    ret [Definition d : instancesOf(Definition, objects) | d.lhs instanceof S && d.rhs instanceof S];
133  
  }
134  
  
135  
  L<Statement> statements() {
136  
    ret instancesOf(Statement, objects);
137  
  }
138  
  
139  
  void removeInitialObjects {
140  
    replaceCollection(objects, dropFirst(initialObjects, asList(objects)));
141  
    initialObjects = 0;
142  
  }
143  
}

Author comment

Began life as a copy of #1017020

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1017032
Snippet name: Patterns Step 2 [OK]
Eternal ID of this version: #1017032/24
Text MD5: d4e3a9cfea6700355940daf45813fe49
Transpilation MD5: 0daf6fe1f6a9c0abf2688047ab192b44
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-07-11 03:16:47
Source code size: 4783 bytes / 143 lines
Pitched / IR pitched: No / No
Views / Downloads: 441 / 971
Version history: 23 change(s)
Referenced in: [show references]