A categorical programming language
修訂 | 385e7011487f8ae1e80597e01ffb9430ba2d941c (tree) |
---|---|
時間 | 2024-10-24 11:25:50 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
cammylib: Weaken composition.
Fairly easy compared to what's next.
@@ -7,6 +7,11 @@ from cammylib.elements import F, N, thePoint, theTrue, theFalse | ||
7 | 7 | |
8 | 8 | |
9 | 9 | def arrowEq(arr1, arr2): return arr1 is arr2 or arr1.eq(arr2) |
10 | +def arrowsEq(arrs1, arrs2): | |
11 | + if len(arrs1) != len(arrs2): return False | |
12 | + for i in range(len(arrs1)): | |
13 | + if not arrowEq(arrs1[i], arrs2[i]): return False | |
14 | + return True | |
10 | 15 | |
11 | 16 | class Arrow(object): |
12 | 17 | _immutable_ = True |
@@ -21,19 +26,18 @@ class Id(Arrow): | ||
21 | 26 | |
22 | 27 | class Comp(Arrow): |
23 | 28 | _immutable_ = True |
24 | - def __init__(self, f, g): | |
25 | - self.f = f | |
26 | - self.g = g | |
29 | + def __init__(self, fs): self.fs = fs | |
27 | 30 | def eq(self, other): |
28 | - return isinstance(other, Comp) and arrowEq(self.f, other.f) and arrowEq(self.g, other.g) | |
31 | + return isinstance(other, Comp) and arrowsEq(self.fs, other.fs) | |
29 | 32 | def compileCAM(self, compiler): |
30 | - self.f.compileCAM(compiler) | |
31 | - self.g.compileCAM(compiler) | |
33 | + for f in self.fs: f.compileCAM(compiler) | |
32 | 34 | def types(self, cs): |
33 | - fdom, fcod = self.f.types(cs) | |
34 | - gdom, gcod = self.g.types(cs) | |
35 | - cs.unify(fcod, gdom) | |
36 | - return fdom, gcod | |
35 | + dom, cod = self.fs[0].types(cs) | |
36 | + for f in self.fs[1:]: | |
37 | + x, y = f.types(cs) | |
38 | + cs.unify(cod, x) | |
39 | + cod = y | |
40 | + return dom, cod | |
37 | 41 | |
38 | 42 | class Ignore(Arrow): |
39 | 43 | _immutable_ = True |
@@ -451,8 +455,7 @@ def buildUnary(name): | ||
451 | 455 | raise BuildProblem("Invalid unary functor: " + name) |
452 | 456 | |
453 | 457 | def buildCompound(name, args): |
454 | - if name == "comp" and len(args) == 2: | |
455 | - return Comp(args[0], args[1]) | |
458 | + if name == "comp" and len(args) >= 2: return Comp(args) | |
456 | 459 | elif name == "pair" and len(args) == 2: |
457 | 460 | return Pair(args[0], args[1]) |
458 | 461 | elif name == "case" and len(args) == 2: |
@@ -38,8 +38,7 @@ class TermOp(object): | ||
38 | 38 | self.name = name |
39 | 39 | self.action = action |
40 | 40 | |
41 | - def apply(self, term): | |
42 | - return self.action(term) | |
41 | + def apply(self, term): return self.action(term) | |
43 | 42 | register_code_object_class(TermOp, lambda op: "cam:%s:0:builtin" % op.name) |
44 | 43 | |
45 | 44 | # Some term ops are too complicated for an inline lambda. |
@@ -73,18 +73,20 @@ class EvaluateElement(Command): | ||
73 | 73 | doc = "Evaluate an element or sequence. Elements are arrows from 1; sequences are elements from N." |
74 | 74 | def run(self, line): |
75 | 75 | sexp, trail = parse(line) |
76 | - arrow = sexp.buildArrow() | |
77 | - # If the arrow is polymorphic, monomorphize it. | |
78 | - if fixDomain(arrow, Atom("1")): | |
79 | - # Compile the arrow and run it. | |
80 | - cam = compileArrow(arrow) | |
81 | - printElement(cam) | |
82 | - elif fixDomain(arrow, Atom("N")): | |
83 | - # Maybe it's a sequence? | |
84 | - cam = compileArrow(arrow) | |
85 | - printSequence(cam) | |
86 | - else: | |
87 | - print "Couldn't unify domain with 1 or N, so can't display this arrow" | |
76 | + try: | |
77 | + arrow = sexp.buildArrow() | |
78 | + # If the arrow is polymorphic, monomorphize it. | |
79 | + if fixDomain(arrow, Atom("1")): | |
80 | + # Compile the arrow and run it. | |
81 | + cam = compileArrow(arrow) | |
82 | + printElement(cam) | |
83 | + elif fixDomain(arrow, Atom("N")): | |
84 | + # Maybe it's a sequence? | |
85 | + cam = compileArrow(arrow) | |
86 | + printSequence(cam) | |
87 | + else: | |
88 | + print "Couldn't unify domain with 1 or N, so can't display this arrow" | |
89 | + except BuildProblem as bp: print "Couldn't build arrow:", bp.message | |
88 | 90 | |
89 | 91 | class PrintBytecode(Command): |
90 | 92 | doc = "Compile to bytecode, then disassemble." |