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).

!752

!1002813 // gnu prolog

import gnu.prolog.vm.*;
import gnu.prolog.term.*;
import gnu.prolog.database.*;

static S prolog = [[
  here([]).
  here2([hello]).
]];

static int query(Interpreter intp, Term term) ctex {
  try {
    Interpreter.Goal goal = intp.prepareGoal(term);
	  ret intp.execute(goal);
	  // todo: stop goal?
	 } catch (Exception e) {
	   print("fail " + e);
	   ret 0;
	 }
}

static Interpreter interpreter;

p {
  new Environment env;
  
  S streamName = "someinput";
  Reader reader = new StringReader(prolog);
  
  new PrologTextLoader(env.getPrologTextLoaderState(), reader, streamName);
  
	List<PrologTextLoaderError> errors = env.getLoadingErrors();
	for (PrologTextLoaderError error : errors) {
		printStackTrace(error);
	}

  interpreter = env.createInterpreter();
  env.runInitialization(interpreter);
  
  getIt("here");
  getIt("here2");
}

static void getIt(S function) {
  VariableTerm x = new VariableTerm("X");
  printVar(x);
  Term term = new CompoundTerm(function, new Term[] {x});
  print("Term: " + term);
  print(query(interpreter, term));
  printVar(x);
}

static void printVar(VariableTerm x) {
  Term t = x.dereference();
  print(x.name + " = " + t + " (" + t.getClass().getName() + ")");
  print("  Lisp: " + toLisp(t));
}

static Lisp toLisp(Term term) {
  if (term instanceof AtomTerm)
    ret lisp(((AtomTerm) term).value);
    
  if (term instanceof VariableTerm)
    ret lisp(((VariableTerm) term).name);
    
  if (term instanceof CompoundTerm) {
    CompoundTerm c = cast term;
    Lisp l = new Lisp(c.tag.functor.value);
    for (Term arg : c.args)
      l.add(toLisp(arg));
    ret l;
  }
  
  ret lisp("? " + term.getClass().getName());
}

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: 566 / 580
Referenced in: [show references]