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

1  
!752
2  
3  
static class Var extends Lisp {
4  
  Lisp instance;
5  
  
6  
  *(S name) {
7  
    super(name);
8  
    instance = this;
9  
  }
10  
}
11  
12  
static boolean unify(Lisp thiz, Lisp t) {
13  
  if (thiz instanceof Var) {
14  
    Var v = cast thiz;
15  
    if (v.instance != v)
16  
      return unify(v.instance, t);
17  
    //Trail::Push(v);
18  
    v.instance = t;
19  
    return true;
20  
  }
21  
  
22  
  return unify2(t, thiz);
23  
}
24  
25  
static boolean unify2(Lisp thiz, Lisp t) { 
26  
  if (thiz instanceof Var)
27  
    return unify(thiz, t);
28  
 
29  
  int arity = thiz.size();
30  
  if (neq(thiz.head, t.head) || arity != t.size())
31  
    return false;
32  
  for (int i = 0; i < arity; i++)
33  
    if (!unify(thiz.get(i), t.get(i)))
34  
      return false;
35  
  return true;
36  
}
37  
38  
p {
39  
  Var x = new Var("X");
40  
  print(x + " = " + x.instance);
41  
  print(unify(x, lisp("hello")));
42  
  print(x + " = " + x.instance);
43  
}

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