• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-cqtcocoa誰得pythonphprubygameguibathyscaphec翻訳計画中(planning stage)omegatframeworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

A categorical programming language


Commit MetaInfo

修订版f36a6db0034bf03bb209dc9539ebb04e3e04d075 (tree)
时间2022-02-21 08:24:56
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Refactor canonicalization.

更改概述

差异

--- a/cammy-rpy/cammylib/hive.py
+++ b/cammy-rpy/cammylib/hive.py
@@ -1,18 +1,6 @@
11 import os.path
22
33 from cammylib.parser import parse
4-from cammylib.sexp import Atom, Functor, Hole
5-
6-BASIS_ATOMS = (
7- "id", "ignore", "fst", "snd", "left", "right",
8- "zero", "succ", "nil", "cons", "t", "f", "not", "conj", "disj",
9- "f-zero", "f-one", "f-pi", "f-sign", "f-floor", "f-negate", "f-recip",
10- "f-lt", "f-add", "f-mul", "f-sqrt", "f-sin", "f-cos", "f-atan2"
11-)
12-
13-BASIS_FUNCTORS = (
14- "comp", "pair", "case", "curry", "uncurry", "pr", "fold", "either",
15-)
164
175
186 class Hive(object):
@@ -28,25 +16,7 @@ class Hive(object):
2816 fullpath = os.path.join(self.hivepath, filename)
2917 with open(fullpath, "r") as handle:
3018 sexp, trail = parse(handle.read())
31- sexp = self.canonicalize(sexp)
19+ sexp = sexp.canonicalize(self)
3220 self.exprs[atom] = sexp
3321 print "Loaded", atom, sexp
3422 return sexp
35-
36- def canonicalize(self, expr):
37- if isinstance(expr, Atom):
38- if expr.symbol in BASIS_ATOMS:
39- return expr
40- else:
41- return self.load(expr.symbol)
42- elif isinstance(expr, Functor):
43- args = [self.canonicalize(arg) for arg in expr.arguments]
44- if expr.constructor in BASIS_FUNCTORS:
45- return Functor(expr.constructor, args)
46- else:
47- functor = self.load(expr.constructor)
48- return functor.substitute(args)
49- elif isinstance(expr, Hole):
50- return expr
51- else:
52- assert False, "not ready yet"
--- a/cammy-rpy/cammylib/sexp.py
+++ b/cammy-rpy/cammylib/sexp.py
@@ -1,6 +1,19 @@
1+BASIS_ATOMS = (
2+ "id", "ignore", "fst", "snd", "left", "right",
3+ "zero", "succ", "nil", "cons", "t", "f", "not", "conj", "disj",
4+ "f-zero", "f-one", "f-pi", "f-sign", "f-floor", "f-negate", "f-recip",
5+ "f-lt", "f-add", "f-mul", "f-sqrt", "f-sin", "f-cos", "f-atan2"
6+)
7+
8+BASIS_FUNCTORS = (
9+ "comp", "pair", "case", "curry", "uncurry", "pr", "fold", "either",
10+)
11+
12+
113 class SExp(object):
214 "An S-expression."
315
16+
417 class Atom(SExp):
518 "An S-expression atom."
619
@@ -15,6 +28,13 @@ class Atom(SExp):
1528 def substitute(self, args):
1629 return self
1730
31+ def canonicalize(self, hive):
32+ if self.symbol in BASIS_ATOMS:
33+ return self
34+ else:
35+ return hive.load(self.symbol)
36+
37+
1838 class Functor(SExp):
1939 "A list of S-expressions with a distinguished head."
2040
@@ -32,6 +52,15 @@ class Functor(SExp):
3252 return Functor(self.constructor,
3353 [arg.substitute(args) for arg in self.arguments])
3454
55+ def canonicalize(self, hive):
56+ args = [arg.canonicalize(hive) for arg in self.arguments]
57+ if self.constructor in BASIS_FUNCTORS:
58+ return Functor(self.constructor, args)
59+ else:
60+ functor = hive.load(self.constructor)
61+ return functor.substitute(args)
62+
63+
3564 class Hole(SExp):
3665 "A hole where an S-expression could be."
3766
@@ -45,3 +74,6 @@ class Hole(SExp):
4574
4675 def substitute(self, args):
4776 return args[self.index]
77+
78+ def canonicalize(self, hive):
79+ return self
--- a/cammy-rpy/repl.py
+++ b/cammy-rpy/repl.py
@@ -19,7 +19,7 @@ def repl(hive, stdin, stdout):
1919 print "Got:", line
2020 print "S-expression:", sexp.asStr()
2121 print "Trail:", trail
22- sexp = hive.canonicalize(sexp)
22+ sexp = sexp.canonicalize(hive)
2323 print "Canonicalized:", sexp.asStr()
2424 try:
2525 arrow = buildArrow(sexp)