• 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

修订版ec83ededb658caa34976ba08e197fb26a396b8c1 (tree)
时间2022-02-28 08:18:51
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Typecheck functors with holes.

It might feel a little hacky to allow non-exectuable arrow subclasses,
but I'm thinking of it this way: execution is only one thing an arrow
can do, and symbolic logic doesn't end after arrows are built.

更改概述

差异

--- a/cammy-rpy/cammylib/arrows.py
+++ b/cammy-rpy/cammylib/arrows.py
@@ -520,6 +520,18 @@ def buildCompound(name, args):
520520 else:
521521 raise BuildProblem("Invalid compound functor: " + name)
522522
523+
524+class Given(Arrow):
525+ """
526+ A formal parameter for a function.
527+
528+ An given arrow is not executable, but it can still be typechecked.
529+ """
530+ _immutable_ = True
531+ def __init__(self, index): self.index = index
532+ def run(self, x): raise BuildProblem("given arrow cannot be run")
533+ def types(self, cs): return cs.givens(self.index)
534+
523535 def buildArrow(sexp):
524536 if isinstance(sexp, Atom):
525537 return buildUnary(sexp.symbol)
@@ -527,6 +539,6 @@ def buildArrow(sexp):
527539 args = [buildArrow(arg) for arg in sexp.arguments]
528540 return buildCompound(sexp.constructor, args)
529541 elif isinstance(sexp, Hole):
530- raise BuildProblem("Holes cannot become arrows")
542+ return Given(sexp.index)
531543 else:
532544 assert False, "inconceivable"
--- a/cammy-rpy/cammylib/sexp.py
+++ b/cammy-rpy/cammylib/sexp.py
@@ -38,7 +38,7 @@ class Atom(SExp):
3838 class Functor(SExp):
3939 "A list of S-expressions with a distinguished head."
4040
41- _immutable_fields_ = "constructor", "arguments[:]",
41+ _immutable_fields_ = "constructor", "arguments[:]"
4242
4343 def __init__(self, constructor, arguments):
4444 self.constructor = constructor
--- a/cammy-rpy/cammylib/types.py
+++ b/cammy-rpy/cammylib/types.py
@@ -33,6 +33,12 @@ class ConstraintStore(object):
3333 def __init__(self):
3434 self.i = 0
3535 self.constraints = []
36+ self.knownGivens = []
37+
38+ def givens(self, index):
39+ while len(self.knownGivens) <= index:
40+ self.knownGivens.append((self.fresh(), self.fresh()))
41+ return self.knownGivens[index]
3642
3743 def fresh(self):
3844 rv = self.i
--- a/cammy-rpy/repl.py
+++ b/cammy-rpy/repl.py
@@ -43,6 +43,10 @@ def repl(hive, stdin, stdout):
4343 cs = ConstraintStore()
4444 domain, codomain = arrow.types(cs)
4545 extractor = TypeExtractor()
46+ for i, (gdom, gcod) in enumerate(cs.knownGivens):
47+ print "Given @%d : %s -> %s" % (i,
48+ extractor.extractType(cs, gdom),
49+ extractor.extractType(cs, gcod))
4650 print "Type:", extractor.extractType(cs, domain), "->", extractor.extractType(cs, codomain)
4751 except BuildProblem as bp:
4852 print "Couldn't build arrow:", bp.message
--- a/cammy-rpy/weave.py
+++ b/cammy-rpy/weave.py
@@ -39,6 +39,10 @@ def main(argv):
3939 cs = ConstraintStore()
4040 domain, codomain = arrow.types(cs)
4141 extractor = TypeExtractor()
42+ for i, (gdom, gcod) in enumerate(cs.knownGivens):
43+ doc.append("Given @%d : %s -> %s" % (i,
44+ extractor.extractType(cs, gdom),
45+ extractor.extractType(cs, gcod)))
4246 doc.append("Type: %s -> %s" % (extractor.extractType(cs, domain),
4347 extractor.extractType(cs, codomain)))
4448 except MissingAtom as ma: