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

43
LINES

< > BotCompany Repo | #1002818 // Test unifiable variables

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

Libraryless. Click here for Pure Java version (916L/6K/22K).

!752

static class Var extends Lisp {
  Lisp instance;
  
  *(S name) {
    super(name);
    instance = this;
  }
}

static boolean unify(Lisp thiz, Lisp t) {
  if (thiz instanceof Var) {
    Var v = cast thiz;
    if (v.instance != v)
      return unify(v.instance, t);
    //Trail::Push(v);
    v.instance = t;
    return true;
  }
  
  return unify2(t, thiz);
}

static boolean unify2(Lisp thiz, Lisp t) { 
  if (thiz instanceof Var)
    return unify(thiz, t);
 
  int arity = thiz.size();
  if (neq(thiz.head, t.head) || arity != t.size())
    return false;
  for (int i = 0; i < arity; i++)
    if (!unify(thiz.get(i), t.get(i)))
      return false;
  return true;
}

p {
  Var x = new Var("X");
  print(x + " = " + x.instance);
  print(unify(x, lisp("hello")));
  print(x + " = " + x.instance);
}

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: #1002818
Snippet name: Test unifiable variables
Eternal ID of this version: #1002818/1
Text MD5: f8646792cfb6cfefb2c79425c88c7730
Transpilation MD5: c7c61322a624538ae32da77230fb753c
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:43:19
Source code size: 844 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 607 / 668
Referenced in: #1002819 - Prolog Interpreter (v1, without class), superseded by #1002820
#3000382 - Answer for ferdie (>> t = 1, f = 0)