-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
executable file
·60 lines (55 loc) · 1.66 KB
/
convert.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import re, sys, os
from ptp import factsAndRules
from readrules import readterm, ops
from terms import proving, failed, VARIABLE, unify, toList
from useful import *
def groupRules(rules):
functors = {}
for rule in rules:
f = rule.consequent.functor
try:
functors[f].append(rule)
except:
functors[f] = [rule]
s = ""
for f in functors:
print f, functors[f]
l = functors[f]
if len(l) > 1:
for i in range(len(l)):
l[i].consequent.functor += "_%s"%(i)
for r in l:
defn = r.defn()
print defn
s += "%s\n"%(defn)
exec defn in globals()
if len(l) > 1:
defn = "def %s(A, CONT, INDENT, RULES):\n %s\n"%(f, " or ".join(["%s(A, CONT, INDENT, RULES)"%(x.consequent.functor) for x in l]))
print defn
exec defn in globals()
fn = globals()['%s'%(f)]
fn.src = functors[f]
fn.defn = defn
functors[f] = fn
return functors, s
def readFactsAndRules(s=factsAndRules, debug=False):
factsandrules = []
while not s == "":
t, s = readterm(s, ".", debug=debug)
s = s.strip()[1:]
factsandrules.append(t)
return factsandrules
from terms import SUCCESS
def success(g):
raise SUCCESS(str(g))
def prove(g, r):
g, s = readterm("%s."%(g))
if not s == ".":
raise TermException("XXX")
print "Proving %s"%(g)
try:
r[g.functor](tuple([toList(a) for a in g.args]), (lambda:success(g.args)), '', r)
except SUCCESS as s:
print "Proved %s: %s"%(g, s.msg)
return
print "no"