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

144
LINES

< > BotCompany Repo | #1017190 // Develop From Examples v3 [with function composition, OK]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Libraryless. Click here for Pure Java version (18077L/132K).

1  
!7
2  
3  
set flag DynModule. // for transpilation
4  
5  
sclass DevelopFromExamples > DynTextArea {
6  
  S whatToTry;
7  
  transient ReliableSingleThread rst = new(r think);
8  
  transient volatile PlanInMotion<Runnable> currentPlan;
9  
  transient int delay = 0; //2000; // to see stuff
10  
11  
  visualize {
12  
    final JLabel lblStatus = jlabel();
13  
    awtEvery(lblStatus, 100, r { main.setText(lblStatus, planStatus()) });
14  
    ret withCenteredButtons(centerAndSouthWithMargins(
15  
      jhsplit(jSection("Examples (struct => struct)", super.visualize()), dm_printLogComponent()),
16  
      vstackWithSpacing(
17  
        jsection("Standard functions to try", onEnter(dm_fieldTextField('whatToTry), rst)),
18  
        lblStatus
19  
      )), "Think", rst, "Fresh", rThread fresh, "[disabled] Cancel", r cancel);
20  
  }
21  
  
22  
  S planStatus() {
23  
    PlanInMotion plan = currentPlan;
24  
    if (plan == null) ret " ";
25  
    ret "Steps done: " + l(plan.doneSteps) + ". Steps planned: " + l(plan.plannedSteps);
26  
  }
27  
  
28  
  void cancel {
29  
    PlanInMotion plan = currentPlan;
30  
    if (plan != null) plan.cancelled = true;
31  
  }
32  
  
33  
  void fresh {
34  
    dm_refreshTranspiler();
35  
    forgetTranspiledStandardFunctions(words(whatToTry));
36  
    loadFunctions_clearCache();
37  
    rst.trigger();
38  
  }
39  
  
40  
  class Thinker {
41  
    new PlanInMotion<Runnable> plan;
42  
    new L<Pair<S>> examples;
43  
    new L<Pair<O>> unstructedExamples;
44  
    F2<O, O, Bool> equalsFunction;
45  
    new LinkedHashSet<S> thingsThatWork;
46  
    
47  
    void go {
48  
      currentPlan = plan;
49  
      temp tempAfterwards(r { currentPlan = null; });
50  
      temp tempEnableButton(findButton(dm_vis(), "Cancel"));
51  
      
52  
      clearPrintLog();
53  
      loadFunctions_preferDiskCache();
54  
    
55  
      S text = rtrimAll(getText());
56  
      Pair<File, Bool> examplesFile = ai_makeExamplesFile(text);
57  
      if (examplesFile.b) print("Wrote: " + f2s(examplesFile.a));
58  
      bool eqic = false;
59  
    
60  
      L<S> lines = tlftj(text);
61  
      if (eq(first(lines), "splitAtEmptyLines"))
62  
        lines = splitAtEmptyLines(text);
63  
        
64  
      for (S line : lines) pcall {
65  
        if (eqic(line, "eqic")) eqic = true;
66  
        L<S> l = splitAtDoubleArrow_bothDirections(line);
67  
        if (l(l) == 2) {
68  
          Pair<S> p = pair(
69  
            tok_multiLineQuotesToStandardQuotes(first(l)),
70  
            safeCanonicalizeStructure(tok_multiLineQuotesToStandardQuotes(second(l))));
71  
          add(examples, p);
72  
          add(unstructedExamples, pair(safeUnstruct(p.a), safeUnstruct(p.b));
73  
        }
74  
      }
75  
      
76  
      final File logFile = replaceExtension(examplesFile.a, ".log");
77  
      final LineBuffer lineBuffer = new(voidfunc(S line) { appendToFile(logFile, line + "\n") });
78  
      temp tempInterceptPrint(func(S s) -> Bool { lineBuffer.append(s); true; });
79  
      
80  
      print(localDateWithMilliseconds());
81  
      print("Thinking about " + n2(examples, "example"));
82  
      
83  
      equalsFunction = eqic
84  
          ? func(O a, O b) -> Bool { eqic((S) a, (S) b) }
85  
          : func(O a, O b) -> Bool { eq(a, b) };
86  
  
87  
      // Make plan
88  
      fL<S> functionsToTry = uniquify(standardFunctionsOnly(words(whatToTry)));
89  
      
90  
      for (fS s : functionsToTry) addToPlan(s);
91  
      for (fS a : functionsToTry)
92  
        for (fS b : listMinus(functionsToTry, a))
93  
          addToPlan(a + "°" + b);
94  
      
95  
      for (Runnable r : plan.master())
96  
        callF(r);
97  
        
98  
      print("\n" + (plan.cancelled ? "Cancelled" : "Done") + " after " + n2(plan.doneSteps, "step") + ". " +
99  
        (empty(thingsThatWork) ? (plan.cancelled ? "Nothing worked so far." : "Nothing works.") : (plan.cancelled ? "Things that worked so far: " : "Things that work: ") + joinWithComma(thingsThatWork)));
100  
      print();
101  
    }
102  
    
103  
    void addToPlan(fS s) {
104  
      plan.add(r {
105  
        sleep(delay);
106  
        F1<S, S> f = functionFromText(s);
107  
        func(S in) -> S { struct(callAndMake_orDirect(s, unstruct(in))) };
108  
        T3<S, S, O> counterExample = worksOnAllExamples_returnCounterExample(f, examples, equalsFunction);
109  
        if (counterExample == null)
110  
          print("WORKS: " + addAndReturn(thingsThatWork, s));
111  
        else {
112  
          print("Nope: " + s);
113  
          bool exc = counterExample.c instanceof Throwable;
114  
          print((exc ? "Exception on:    "
115  
                     : "Wrong result on: ") + sfu(counterExample.a));
116  
          print(       "Expected:        " + sfu(counterExample.b));
117  
          if (exc)
118  
            printStackTrace((Throwable) counterExample.c);
119  
          else
120  
            print(     "         Result: " + sfu(counterExample.c));
121  
        }
122  
      });
123  
    }
124  
  }
125  
  
126  
  void think {
127  
    if (currentPlan != null) ret;
128  
    new Thinker().go();
129  
  }
130  
  
131  
  // Allows composed functions ("g°f") or simple function names
132  
  F1<S, S> functionFromText(fS s) {
133  
    fL<S> l = splitAtJavaToken(s, "°");
134  
    if (l(l) == 2)
135  
      ret func(S in) -> S {
136  
        O o = unstruct(in);
137  
        o = callAndMake_orDirect(second(l), o);
138  
        o = callAndMake_orDirect(first(l), o);
139  
        ret struct(o);
140  
      };
141  
    else
142  
      ret func(S in) -> S { struct(callAndMake_orDirect(s, unstruct(in))) };
143  
  }
144  
}

Author comment

Began life as a copy of #1017185

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: #1017190
Snippet name: Develop From Examples v3 [with function composition, OK]
Eternal ID of this version: #1017190/16
Text MD5: 27e0b7783028cc96b591935627eee783
Transpilation MD5: e488af1198ebe30f609d66c397f28729
Author: stefan
Category: javax / a.i.
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-08-20 11:13:52
Source code size: 5164 bytes / 144 lines
Pitched / IR pitched: No / No
Views / Downloads: 295 / 710
Version history: 15 change(s)
Referenced in: [show references]