• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A categorical programming language


Commit MetaInfo

修訂385e7011487f8ae1e80597e01ffb9430ba2d941c (tree)
時間2024-10-24 11:25:50
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

cammylib: Weaken composition.

Fairly easy compared to what's next.

Change Summary

差異

--- a/cammy/cammylib/arrows.py
+++ b/cammy/cammylib/arrows.py
@@ -7,6 +7,11 @@ from cammylib.elements import F, N, thePoint, theTrue, theFalse
77
88
99 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
1015
1116 class Arrow(object):
1217 _immutable_ = True
@@ -21,19 +26,18 @@ class Id(Arrow):
2126
2227 class Comp(Arrow):
2328 _immutable_ = True
24- def __init__(self, f, g):
25- self.f = f
26- self.g = g
29+ def __init__(self, fs): self.fs = fs
2730 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)
2932 def compileCAM(self, compiler):
30- self.f.compileCAM(compiler)
31- self.g.compileCAM(compiler)
33+ for f in self.fs: f.compileCAM(compiler)
3234 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
3741
3842 class Ignore(Arrow):
3943 _immutable_ = True
@@ -451,8 +455,7 @@ def buildUnary(name):
451455 raise BuildProblem("Invalid unary functor: " + name)
452456
453457 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)
456459 elif name == "pair" and len(args) == 2:
457460 return Pair(args[0], args[1])
458461 elif name == "case" and len(args) == 2:
--- a/cammy/cammylib/cam.py
+++ b/cammy/cammylib/cam.py
@@ -38,8 +38,7 @@ class TermOp(object):
3838 self.name = name
3939 self.action = action
4040
41- def apply(self, term):
42- return self.action(term)
41+ def apply(self, term): return self.action(term)
4342 register_code_object_class(TermOp, lambda op: "cam:%s:0:builtin" % op.name)
4443
4544 # Some term ops are too complicated for an inline lambda.
--- a/cammy/repl.py
+++ b/cammy/repl.py
@@ -73,18 +73,20 @@ class EvaluateElement(Command):
7373 doc = "Evaluate an element or sequence. Elements are arrows from 1; sequences are elements from N."
7474 def run(self, line):
7575 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
8890
8991 class PrintBytecode(Command):
9092 doc = "Compile to bytecode, then disassemble."