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

77
LINES

< > BotCompany Repo | #1002816 // GNU Prolog Test 3 (var assignments)

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

Uses 419K of libraries. Click here for Pure Java version (958L/7K/24K).

1  
!752
2  
!1002813 // gnu prolog
3  
4  
import gnu.prolog.vm.*;
5  
import gnu.prolog.term.*;
6  
import gnu.prolog.database.*;
7  
8  
static S prolog = [[
9  
  here([]).
10  
  here2([hello]).
11  
]];
12  
13  
static int query(Interpreter intp, Term term) ctex {
14  
  try {
15  
    Interpreter.Goal goal = intp.prepareGoal(term);
16  
	  ret intp.execute(goal);
17  
	  // todo: stop goal?
18  
	 } catch (Exception e) {
19  
	   print("fail " + e);
20  
	   ret 0;
21  
	 }
22  
}
23  
24  
static Interpreter interpreter;
25  
26  
p {
27  
  new Environment env;
28  
  
29  
  S streamName = "someinput";
30  
  Reader reader = new StringReader(prolog);
31  
  
32  
  new PrologTextLoader(env.getPrologTextLoaderState(), reader, streamName);
33  
  
34  
	List<PrologTextLoaderError> errors = env.getLoadingErrors();
35  
	for (PrologTextLoaderError error : errors) {
36  
		printStackTrace(error);
37  
	}
38  
39  
  interpreter = env.createInterpreter();
40  
  env.runInitialization(interpreter);
41  
  
42  
  getIt("here");
43  
  getIt("here2");
44  
}
45  
46  
static void getIt(S function) {
47  
  VariableTerm x = new VariableTerm("X");
48  
  printVar(x);
49  
  Term term = new CompoundTerm(function, new Term[] {x});
50  
  print("Term: " + term);
51  
  print(query(interpreter, term));
52  
  printVar(x);
53  
}
54  
55  
static void printVar(VariableTerm x) {
56  
  Term t = x.dereference();
57  
  print(x.name + " = " + t + " (" + t.getClass().getName() + ")");
58  
  print("  Lisp: " + toLisp(t));
59  
}
60  
61  
static Lisp toLisp(Term term) {
62  
  if (term instanceof AtomTerm)
63  
    ret lisp(((AtomTerm) term).value);
64  
    
65  
  if (term instanceof VariableTerm)
66  
    ret lisp(((VariableTerm) term).name);
67  
    
68  
  if (term instanceof CompoundTerm) {
69  
    CompoundTerm c = cast term;
70  
    Lisp l = new Lisp(c.tag.functor.value);
71  
    for (Term arg : c.args)
72  
      l.add(toLisp(arg));
73  
    ret l;
74  
  }
75  
  
76  
  ret lisp("? " + term.getClass().getName());
77  
}

Author comment

Began life as a copy of #1002815

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002816
Snippet name: GNU Prolog Test 3 (var assignments)
Eternal ID of this version: #1002816/1
Text MD5: ad7f47ac35c86d5a321a6966df89070f
Transpilation MD5: 7135e8035670f34845c62ac046bde778
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-27 02:18:57
Source code size: 1770 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 571 / 584
Referenced in: [show references]