A categorical programming language
修订版 | b6c821c54efd7859a51266ef4f8af16b74a59a42 (tree) |
---|---|
时间 | 2022-03-28 15:44:27 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Allow N to be fancy blackboard N in LaTeX.
@@ -1,10 +1,3 @@ | ||
1 | -# Frame | |
2 | - | |
3 | -The frame is a preprocessor written in OCaml which statically links together | |
4 | -multiple Cammy expressions. The frame takes any nullary functor which is not | |
5 | -standard, and treats it as a file path into a tree of Cammy expressions. Each | |
6 | -path is replaced with the expression at that path. | |
7 | - | |
8 | 1 | # Jelly |
9 | 2 | |
10 | 3 | The jelly optimizer is a Cammy-to-Cammy optimizer which uses e-graphs to |
@@ -46,7 +46,7 @@ class Atom(SExp): | ||
46 | 46 | return False |
47 | 47 | |
48 | 48 | def extractType(self, extractor, formatter, outerPrecedence): |
49 | - return self.symbol | |
49 | + return formatter.formatN if self.symbol == "N" else self.symbol | |
50 | 50 | |
51 | 51 | def buildArrow(self): |
52 | 52 | return buildUnary(self.symbol) |
@@ -1,7 +1,7 @@ | ||
1 | 1 | import os |
2 | 2 | import sys |
3 | 3 | |
4 | -from cammylib.hive import Hive | |
4 | +from cammylib.hive import Hive, MissingAtom | |
5 | 5 | from cammylib.sexp import BASIS_ATOMS, BASIS_FUNCTORS |
6 | 6 | |
7 | 7 | def comb(dag): |
@@ -28,7 +28,10 @@ def main(argv): | ||
28 | 28 | suffix = len(fullpath) - 6 |
29 | 29 | assert suffix >= 0, "proven by .endswith() check above" |
30 | 30 | atom = fullpath[prefix:suffix] |
31 | - hive.load(atom) | |
31 | + try: | |
32 | + hive.load(atom) | |
33 | + except MissingAtom as ma: | |
34 | + print "Couldn't find", ma.atom, "while trying to load", atom | |
32 | 35 | graph = comb(hive.dag) |
33 | 36 | write = True |
34 | 37 | if tatom in graph: |
@@ -13,6 +13,8 @@ from cammylib.types import ConstraintStore, TypeExtractor, TypeFormatter, Unific | ||
13 | 13 | LINE_BUFFER_LENGTH = 1024 |
14 | 14 | |
15 | 15 | class PlaintextTypeFormatter(TypeFormatter): |
16 | + formatN = "N" | |
17 | + | |
16 | 18 | def resetsBracketsFor(self, constructor): |
17 | 19 | return constructor in ("hom", "list") |
18 | 20 |
@@ -40,7 +42,13 @@ def command(hive, code, line): | ||
40 | 42 | sexp = sexp.canonicalize(hive) |
41 | 43 | sexp = jellify(sexp) |
42 | 44 | arrow = sexp.buildArrow() |
43 | - print arrow.run(T()).asStr() | |
45 | + cs = ConstraintStore() | |
46 | + domain, codomain = arrow.types(cs) | |
47 | + extractor = TypeExtractor(cs, PlaintextTypeFormatter()) | |
48 | + if extractor.extract(domain) != "1": | |
49 | + print "Arrow is not an element" | |
50 | + else: | |
51 | + print arrow.run(T()).asStr() | |
44 | 52 | |
45 | 53 | def repl(hive, stdin, stdout): |
46 | 54 | while True: |
@@ -13,6 +13,8 @@ from cammylib.types import ConstraintStore, TypeExtractor, TypeFormatter, Unific | ||
13 | 13 | SortFileNames = make_timsort_class() |
14 | 14 | |
15 | 15 | class LatexTypeFormatter(TypeFormatter): |
16 | + formatN = r"\mathbb{N}" | |
17 | + | |
16 | 18 | def resetsBracketsFor(self, constructor): |
17 | 19 | return constructor == "list" |
18 | 20 |
@@ -1,4 +1,4 @@ | ||
1 | -(comp (pair (f/addpair (f/divpair (comp pair/swap f-atan2) (fun/const (f/mulpair f-pi f/2))) (fun/const (comp f/2 f-recip))) (f/mulpair v2/norm (fun/const f/2))) hv2rgb) | |
1 | +(comp (pair (f/addpair (f/divpair (comp pair/swap f-atan2) (fun/const (f/mulpair f-pi f/2))) (fun/const (comp f/2 f-recip))) (f/mulpair v2/complex/norm (fun/const f/2))) hv2rgb) | |
2 | 2 | |
3 | 3 | Map a complex number to a color. The magnitude is mapped to luminance and the |
4 | 4 | angle is mapped to hue. |
@@ -1,9 +1,4 @@ | ||
1 | -(comp | |
2 | - (iter-fractal @0 @1) | |
3 | - (comp | |
4 | - (list/filter v2/c-mag-2) | |
5 | - (comp list/len | |
6 | - (f/divpair nat/to-f (fun/const (comp @1 nat/to-f)))))) | |
1 | +(comp (iter-fractal @0 @1) (comp (list/filter v2/complex/mag-2) (comp list/len (f/divpair nat/to-f (fun/const (comp @1 nat/to-f)))))) | |
7 | 2 | |
8 | 3 | Measure the degree to which a fractal diverges. Given a maximum number of |
9 | 4 | steps, we iterate the IFS for a fractal in the complex plane until its |
@@ -1,3 +1,3 @@ | ||
1 | -(comp (fun/tensor id mat2/trans) (pair (mat2/vecpair fst (comp snd fst)) (mat2/vecpair fst (comp snd snd)))) | |
1 | +(comp (pair/tensor id mat2/trans) (pair (mat2/vecpair fst (comp snd fst)) (mat2/vecpair fst (comp snd snd)))) | |
2 | 2 | |
3 | 3 | Multiply two matrices. |
@@ -1 +1,3 @@ | ||
1 | -(f/subpair v2/norm (fun/const f-one)) | |
1 | +(f/subpair v2/complex/norm (fun/const f-one)) | |
2 | + | |
3 | + |
@@ -1,13 +1,3 @@ | ||
1 | -(f/subpair | |
2 | - (fun/apppair | |
3 | - (comp @1 (fold | |
4 | - (fun/name (fun/const f-zero)) | |
5 | - (curry | |
6 | - (f/addpair | |
7 | - (f/divpair (comp (comp fst fst) snd) | |
8 | - (comp (comp | |
9 | - (pair (comp (comp fst fst) fst) snd) | |
10 | - (v2/map2 f/sub)) v2/norm)) | |
11 | - (fun/apppair (comp fst snd) snd))))) | |
12 | - id) | |
13 | - (fun/const @0)) | |
1 | +(f/subpair (fun/apppair (comp @1 (fold (fun/name (fun/const f-zero)) (curry (f/addpair (f/divpair (comp (comp fst fst) snd) (comp (comp (pair (comp (comp fst fst) fst) snd) (v2/map2 f/sub)) v2/complex/norm)) (fun/apppair (comp fst snd) snd))))) id) (fun/const @0)) | |
2 | + | |
3 | + |
@@ -1 +1,3 @@ | ||
1 | -(curry (comp (pair fst (comp (comp snd (v2/map f/abs)) v2/c-sqr)) (v2/map2 f-add))) | |
1 | +(curry (comp (pair fst (comp (comp snd (v2/map f/abs)) v2/complex/sqr)) (v2/map2 f-add))) | |
2 | + | |
3 | +An iteration of the Burning Ship fractal, a relative of the Mandelbrot set. |
@@ -1 +0,0 @@ | ||
1 | -(comp fun/dup v2/c-mul) |
@@ -0,0 +1,3 @@ | ||
1 | +A two-dimensional vector space can be interpreted as a type of complex | |
2 | +numbers via the [Cayley–Dickson | |
3 | +construction](https://en.wikipedia.org/wiki/Cayley%E2%80%93Dickson_construction). |
@@ -0,0 +1,4 @@ | ||
1 | +(pair f-zero f-one) | |
2 | + | |
3 | +The imaginary unit $i$. By convention, multiplication by $i$ corresponds to a | |
4 | +quarter turn counterclockwise on the Cartesian plane. |
@@ -1,3 +1,3 @@ | ||
1 | -(f/ltpair v2/norm (fun/const f/2)) | |
1 | +(f/ltpair v2/complex/norm (fun/const f/2)) | |
2 | 2 | |
3 | 3 | Whether a complex number's magnitude is less than 2. |
@@ -0,0 +1,3 @@ | ||
1 | +(comp (comp fun/dup f/dot2) f/sqrt-pos) | |
2 | + | |
3 | +The norm of a complex number. |
@@ -0,0 +1,3 @@ | ||
1 | +(comp fun/dup v2/complex/mul) | |
2 | + | |
3 | + |
@@ -1,8 +1,10 @@ | ||
1 | 1 | (comp |
2 | 2 | (comp v2/polar |
3 | 3 | (pair |
4 | - (comp fst (comp f-sqrt (case id f-zero))) | |
4 | + (comp fst f/sqrt-pos) | |
5 | 5 | (f/divpair snd (fun/const f/2)))) |
6 | 6 | (pair |
7 | 7 | (f/mulpair fst (comp snd f-cos)) |
8 | 8 | (f/mulpair fst (comp snd f-sin)))) |
9 | + | |
10 | +The square root of a complex number. |
@@ -1 +1,4 @@ | ||
1 | -(curry (comp (pair fst (comp snd v2/c-sqr)) (v2/map2 f-add))) | |
1 | +(curry (comp (pair fst (comp snd v2/complex/sqr)) (v2/map2 f-add))) | |
2 | + | |
3 | +Perform a Mandelbrot iteration. The Mandelbrot set is composed of points | |
4 | +which do not diverge under iteration. |
@@ -1 +1,3 @@ | ||
1 | 1 | (pair (comp fst @0) (comp snd @0)) |
2 | + | |
3 | +Map over both dimensions of a vector simultaneously. |
@@ -2,3 +2,5 @@ | ||
2 | 2 | (comp (pair (comp fst fst) (comp snd fst)) @0) |
3 | 3 | (comp (pair (comp fst snd) (comp snd snd)) @0)) |
4 | 4 | |
5 | + | |
6 | +Lift a binary operation to a vector space. |
@@ -1 +0,0 @@ | ||
1 | -(comp (comp (comp fun/dup f/dot2) f-sqrt) (case id f-zero)) |
@@ -1 +1,4 @@ | ||
1 | -(pair v2/norm f-atan2) | |
1 | +(pair v2/complex/norm f-atan2) | |
2 | + | |
3 | +Convert from rectangular coordinates to polar coordinates on the Cartesian | |
4 | +plane. |
@@ -1,3 +1,5 @@ | ||
1 | 1 | (f/mulpair |
2 | 2 | (comp (f/divpair id (fun/const @1)) @0) |
3 | 3 | (fun/const @2)) |
4 | + | |
5 | +Scale a vector. |
@@ -1,3 +1,5 @@ | ||
1 | 1 | (f/addpair |
2 | 2 | (comp (f/subpair id (fun/const @1)) @0) |
3 | 3 | (fun/const @2)) |
4 | + | |
5 | +Translate a vector. |