forked from logpy/logpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm.py
42 lines (29 loc) · 840 Bytes
/
term.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from unification import unify, reify
from unification.core import _unify, _reify
from .dispatch import dispatch
@dispatch((tuple, list))
def arguments(seq):
return seq[1:]
@dispatch((tuple, list))
def operator(seq):
return seq[0]
@dispatch(object, (tuple, list))
def term(op, args):
return (op, ) + tuple(args)
def unifiable_with_term(cls):
_reify.add((cls, dict), reify_term)
_unify.add((cls, cls, dict), unify_term)
return cls
def reify_term(obj, s):
op, args = operator(obj), arguments(obj)
op = reify(op, s)
args = reify(args, s)
new = term(op, args)
return new
def unify_term(u, v, s):
u_op, u_args = operator(u), arguments(u)
v_op, v_args = operator(v), arguments(v)
s = unify(u_op, v_op, s)
if s is not False:
s = unify(u_args, v_args, s)
return s