• 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

修订版0bc7131ef99cb227b2b358ba4379526e8d2fa711 (tree)
时间2022-03-02 11:00:55
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Add :! command to REPL for evaluating elements.

I should probably add typechecking, because it also will try to evaluate
non-elements.

更改概述

差异

--- a/README.txt
+++ b/README.txt
@@ -36,3 +36,14 @@ The environment for developing the Cammy toolchain is contained within
3636 To update eggs for the shell environment:
3737
3838 $ egg2nix eggs.scm > eggs.nix
39+
40+# Hive Documentation
41+
42+The toolchain can generate documentation for hives. To generate PDF
43+documentation:
44+
45+ $ cammy-weave hive/ | pandoc -o doc.pdf
46+
47+And to generate HTML documentation:
48+
49+ $ cammy-weave hive/ | pandoc --mathml -o doc.html
--- a/cammy-rpy/cammylib/arrows.py
+++ b/cammy-rpy/cammylib/arrows.py
@@ -34,22 +34,19 @@ class Element(object):
3434
3535 class T(Element):
3636 _immutable_ = True
37+ def asStr(self): return "*"
3738
3839 class B(Element):
3940 _immutable_ = True
40- def __init__(self, b):
41- self._b = b
42-
43- def b(self):
44- return self._b
41+ def __init__(self, b): self._b = b
42+ def b(self): return self._b
43+ def asStr(self): return "true" if self._b else "false"
4544
4645 class N(Element):
4746 _immutable_ = True
48- def __init__(self, bi):
49- self._bi = bi
50-
51- def n(self):
52- return self._bi
47+ def __init__(self, bi): self._bi = bi
48+ def n(self): return self._bi
49+ def asStr(self): return self._bi.str()
5350
5451 class F(Element):
5552 _immutable_ = True
@@ -62,6 +59,8 @@ class F(Element):
6259 def f(self):
6360 return self._f
6461
62+ def asStr(self): return str(self._f)
63+
6564 class P(Element):
6665 _immutable_ = True
6766
@@ -75,6 +74,8 @@ class P(Element):
7574 def second(self):
7675 return self._y
7776
77+ def asStr(self): return "(%s, %s)" % (self._x.asStr(), self._y.asStr())
78+
7879 class L(Element):
7980 _immutable_ = True
8081
@@ -84,6 +85,9 @@ class L(Element):
8485 def tagged(self, f, g):
8586 return f.run(self._x)
8687
88+ def asStr(self):
89+ return "L(%s)" % self._x.asStr()
90+
8791 class R(Element):
8892 _immutable_ = True
8993
@@ -93,6 +97,9 @@ class R(Element):
9397 def tagged(self, f, g):
9498 return g.run(self._x)
9599
100+ def asStr(self):
101+ return "R(%s)" % self._x.asStr()
102+
96103 class H(Element):
97104 _immutable_ = True
98105 def __init__(self, f, x):
@@ -100,6 +107,8 @@ class H(Element):
100107 self._x = x
101108 def apply(self, y):
102109 return self._f.run(P(self._x, y))
110+ def asStr(self):
111+ return "%s @ %s" % (self._f, self._x.asStr())
103112
104113 class Xs(Element):
105114 _immutable_ = True
@@ -107,6 +116,8 @@ class Xs(Element):
107116 def __init__(self, xs):
108117 self.xs = xs
109118 def l(self): return self.xs
119+ def asStr(self):
120+ return "[%s]" % ", ".join([x.asStr() for x in self.xs])
110121
111122 class Arrow(object):
112123 _immutable_ = True
--- a/cammy-rpy/repl.py
+++ b/cammy-rpy/repl.py
@@ -1,11 +1,10 @@
11 # Inspired by https://www.pypy.org/posts/2018/11/guest-post-implementing-calculator-repl-6271483514675006846.html
22
3-import os
43 import sys
54
65 from rpython.rlib.rfile import create_stdio
76
8-from cammylib.arrows import BuildProblem
7+from cammylib.arrows import BuildProblem, T
98 from cammylib.hive import Hive, MissingAtom
109 from cammylib.jelly import jellify
1110 from cammylib.parser import parse
@@ -13,10 +12,15 @@ from cammylib.types import ConstraintStore, TypeExtractor, UnificationFailed
1312
1413 LINE_BUFFER_LENGTH = 1024
1514
16-def command(code, line):
17- if code == "e":
18- # Edit a file
19- os.system("$EDITOR")
15+def command(hive, code, line):
16+ # XXX code e: Edit a file
17+ # os.system("$EDITOR")
18+ if code == "!":
19+ sexp, trail = parse(line)
20+ sexp = sexp.canonicalize(hive)
21+ sexp = jellify(sexp)
22+ arrow = sexp.buildArrow()
23+ print arrow.run(T()).asStr()
2024
2125 def repl(hive, stdin, stdout):
2226 while True:
@@ -28,7 +32,8 @@ def repl(hive, stdin, stdout):
2832 if line.startswith(":"):
2933 if len(line) < 2:
3034 print "Not a full command"
31- command(line[1], line[3:])
35+ command(hive, line[1], line[3:])
36+ continue
3237 sexp, trail = parse(line)
3338 print "Got:", line
3439 print "S-expression:", sexp.asStr()
--- a/cammy-simplify.sh
+++ /dev/null
@@ -1,12 +0,0 @@
1-#!@bash@/bin/bash
2-
3-# set -x
4-set -eu -o pipefail
5-
6-frame="@frame@/bin/frame"
7-jelly="@jelly@/bin/jelly"
8-
9-hive="$1"
10-
11-# Do it all.
12-exec $frame $hive | $jelly
--- a/cammy-weave.py
+++ /dev/null
@@ -1,32 +0,0 @@
1-#!@python3@/bin/python3
2-
3-import sys
4-
5-def offset_list(s):
6- count = 0
7- for i, c in enumerate(s):
8- if c == '(':
9- count += 1
10- elif c == ')':
11- count -= 1
12- if not count:
13- return i + 1
14- raise Exception("Runaway Cammy expression has %d unclosed parentheses" % count)
15-
16-def offset_atom(s):
17- for i, c in enumerate(s):
18- if c == ' ' or c == '\n':
19- return i
20- return len(s)
21-
22-def untangle(s):
23- offset = offset_list(s) if s.startswith('(') else offset_atom(s)
24- return s[:offset], s[offset:].strip()
25-
26-if __name__ == "__main__":
27- expr, doc = untangle(sys.stdin.read())
28- command = sys.argv[1]
29- if command == "strip":
30- print(expr)
31- else:
32- raise Exception("Unknown command %s" % command)
--- a/default.nix
+++ b/default.nix
@@ -37,17 +37,13 @@ in pkgs.stdenv.mkDerivation {
3737 --subst-var frame --subst-var jelly --subst-var movelist
3838 chmod +x $out/bin/cammy-build
3939
40- substitute $src/cammy-simplify.sh $out/bin/cammy-simplify \
41- --subst-var bash \
42- --subst-var frame --subst-var jelly --subst-var movelist
43- chmod +x $out/bin/cammy-simplify
44-
45- substitute $src/cammy-weave.py $out/bin/cammy-weave \
46- --subst-var python3
47- chmod +x $out/bin/cammy-weave
48-
40+ # To be removed
4941 makeWrapper ${movelist}/bin/movelist $out/bin/cammy-movelist
5042
43+ # jelly
44+ makeWrapper ${jelly}/bin/jelly $out/bin/cammy-jelly
45+
46+ # RPython tools
5147 makeWrapper ${cammy-draw}/bin/cammy-draw $out/bin/cammy-draw
5248 makeWrapper ${cammy-frame}/bin/cammy-frame $out/bin/cammy-frame
5349 makeWrapper ${cammy-repl}/bin/cammy-repl $out/bin/cammy-repl
--- a/hive/f/dot2.cammy
+++ b/hive/f/dot2.cammy
@@ -1,3 +1,5 @@
11 (f/addpair
22 (f/mulpair (comp fst fst) (comp snd fst))
33 (f/mulpair (comp fst snd) (comp snd snd)))
4+
5+The dot product of two two-dimensional vectors.
--- /dev/null
+++ b/hive/f/dot2pair.cammy
@@ -0,0 +1 @@
1+(comp (pair @0 @1) f/dot2)
--- /dev/null
+++ b/hive/fun/tensor.cammy
@@ -0,0 +1,11 @@
1+(pair (comp fst @0) (comp snd @1))
2+
3+Compose two arrows in parallel, acting on pairs of values.
4+
5+In categorical jargon, the [tensor
6+product](https://ncatlab.org/nlab/show/tensor+product) is a functor from pairs
7+of arrows to arrows:
8+
9+$$
10+\bigotimes : C \times C \to C
11+$$
--- /dev/null
+++ b/hive/mat2/id.cammy
@@ -0,0 +1,5 @@
1+(pair
2+ (pair f-one f-zero)
3+ (pair f-zero f-one))
4+
5+An identity matrix.
--- /dev/null
+++ b/hive/mat2/mul.cammy
@@ -0,0 +1,7 @@
1+(comp
2+ (fun/tensor id mat2/trans)
3+ (pair
4+ (mat2/vecpair fst (comp snd fst))
5+ (mat2/vecpair fst (comp snd snd))))
6+
7+Multiply two matrices.
--- /dev/null
+++ b/hive/mat2/trans.cammy
@@ -0,0 +1,5 @@
1+(pair
2+ (pair (comp fst fst) (comp snd fst))
3+ (pair (comp fst snd) (comp snd snd)))
4+
5+Transpose a matrix.
--- /dev/null
+++ b/hive/mat2/vec.cammy
@@ -0,0 +1,5 @@
1+(pair
2+ (f/dot2pair (comp fst fst) snd)
3+ (f/dot2pair (comp fst snd) snd))
4+
5+Apply a matrix to a column vector.
--- /dev/null
+++ b/hive/mat2/vecpair.cammy
@@ -0,0 +1 @@
1+(comp (pair @0 @1) mat2/vec)
--- a/hive/nat/is_even.cammy
+++ b/hive/nat/is_even.cammy
@@ -1 +1,3 @@
11 (pr t not)
2+
3+Whether a natural number is even. Zero is even.
--- /dev/null
+++ b/hive/nat/is_odd.cammy
@@ -0,0 +1,3 @@
1+(comp nat/is_even not)
2+
3+Whether a natural number is odd.
--- a/todo.txt
+++ b/todo.txt
@@ -4,14 +4,13 @@
44 - cammy-weave $HIVE: document a hive
55 * cammy-tangle $HIVE: take an expression on stdin, return framed optimized
66 expression on stdout
7- * cammy-frame $HIVE | jelly
7+ - cammy-frame $HIVE | cammy-jelly
88 - cammy-draw ...: take an expression and canvas params, make a PNG
99 - cammy-frame $HIVE: take an expression on stdin, return framed expression
1010 on stdout
11- * cammy-jelly $EXPR: print an optimized expression on stdout
12- * $EXPR is a path!
13- * This is wholly due to RPython issues
14- * When $EXPR is "-", read from stdin
11+ - cammy-jelly: print an optimized expression on stdout
12+ - cammy-djinn $TY $TY: synthesize a function with the given signature
13+ - uses last bit of movelist code
1514 * REPL improvements
1615 * allow mid-functor newlines
1716 * jellification
@@ -30,18 +29,16 @@
3029 * list/append : [X] × [X] → [X]
3130 * rat
3231 * refactoring from the bikeshed: ignore -> !
33-* Typechecker needs to handle holes
34-* Typechecker should handle polymorphism better
35-* Typechecker errors are inscrutable
36-* Trigonometric functions
37- * f-atan2 added, need f-sin and f-cos?
38- * Optimizations for odd/even functions?
32+ * Might not be important since fun/const is the only user!
3933 * Transcendental and other constants
4034 * Euler's constant e
4135 * Euler's constant gamma
4236 * Remove movelist djinn, maybe?
4337 * Double-negation monad for CPS?
38+ * Cont is available but not fully developed
39+ * monads/cont/join, monads/cont/bind, etc.
4440 * fun/name should always start from 1
41+ * Requires changing some callers
4542 * Prove that nat/add is smallest with its behavior
4643 * Streams
4744 * Monad: X → [N, X]
@@ -58,3 +55,4 @@
5855 * Moore transducers: [Q × S, Q] -> [Q × S, Q]
5956 * Mealy machines: For state type Q, input type S, and output type L, Q × S -> Q × L
6057 * Jets
58+* f/dot2pair