A categorical programming language
修订版 | 09a112ec6d9740e181d405b6fb8355b7c8dcb27d (tree) |
---|---|
时间 | 2022-03-30 11:50:39 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Call djinn from REPL.
@@ -1,5 +1,5 @@ | ||
1 | 1 | entrypoint: exe: enableJIT: |
2 | -{ nixpkgs ? import <nixpkgs> {}, jelly ? null }: | |
2 | +{ nixpkgs ? import <nixpkgs> {}, jelly ? null, movelist ? null }: | |
3 | 3 | let |
4 | 4 | inherit (nixpkgs.pkgs) |
5 | 5 | fetchFromGitLab stdenv |
@@ -34,6 +34,7 @@ in | ||
34 | 34 | ]; |
35 | 35 | |
36 | 36 | JELLY_PATH = "${jelly}/bin/jelly"; |
37 | + MOVELIST_PATH = "${movelist}/bin/movelist"; | |
37 | 38 | |
38 | 39 | buildPhase = '' |
39 | 40 | source $stdenv/setup |
@@ -0,0 +1,15 @@ | ||
1 | +import os | |
2 | + | |
3 | +from rpython.rlib.rfile import create_popen_file | |
4 | + | |
5 | +from cammylib.parser import parse | |
6 | + | |
7 | +movelist_path = os.environ["MOVELIST_PATH"] | |
8 | + | |
9 | +def askDjinn(dom, cod): | |
10 | + "Synthesize a Cammy expression with the given domain and codomain." | |
11 | + command = "%s '%s' '%s' 1" % (movelist_path, dom.asStr(), cod.asStr()) | |
12 | + with create_popen_file(command, "r") as handle: | |
13 | + s = handle.read() | |
14 | + sexp, trail = parse(s) | |
15 | + return sexp |
@@ -52,9 +52,16 @@ class CammyParser(object): | ||
52 | 52 | |
53 | 53 | |
54 | 54 | def parse(s): |
55 | + "Parse a Cammy expression, preserving any trailing text." | |
55 | 56 | parser = CammyParser(s) |
56 | 57 | sexp = parser.takeExpression() |
57 | 58 | # Parser is now fast-forwarded to the end of the S-expression, so we can |
58 | 59 | # slice from that point and get the docstring/etc. |
59 | 60 | trail = s[parser.position():] |
60 | 61 | return sexp, trail |
62 | + | |
63 | + | |
64 | +def parseTypes(s): | |
65 | + "Parse two types separated by whitespace." | |
66 | + parser = CammyParser(s) | |
67 | + return parser.takeExpression(), parser.takeExpression() |
@@ -6,8 +6,9 @@ from rpython.rlib.rfile import create_stdio | ||
6 | 6 | |
7 | 7 | from cammylib.arrows import BuildProblem, T |
8 | 8 | from cammylib.hive import Hive, MissingAtom |
9 | +from cammylib.djinn import askDjinn | |
9 | 10 | from cammylib.jelly import jellify |
10 | -from cammylib.parser import parse | |
11 | +from cammylib.parser import parse, parseTypes | |
11 | 12 | from cammylib.types import ConstraintStore, TypeExtractor, TypeFormatter, UnificationFailed |
12 | 13 | |
13 | 14 | LINE_BUFFER_LENGTH = 1024 |
@@ -49,6 +50,11 @@ def command(hive, code, line): | ||
49 | 50 | print "Arrow is not an element" |
50 | 51 | else: |
51 | 52 | print arrow.run(T()).asStr() |
53 | + elif code == "d": | |
54 | + # Invoke djinn. | |
55 | + dom, cod = parseTypes(line) | |
56 | + sexp = askDjinn(dom, cod) | |
57 | + print sexp.asStr() | |
52 | 58 | |
53 | 59 | def repl(hive, stdin, stdout): |
54 | 60 | while True: |
@@ -3,12 +3,12 @@ let | ||
3 | 3 | inherit (nixpkgs) pkgs; |
4 | 4 | jelly = (import jelly/Cargo.nix { pkgs = nixpkgs; }).rootCrate.build; |
5 | 5 | movelist = import ./movelist { inherit nixpkgs; }; |
6 | - cammy-comb = import ./cammy-rpy/comb.nix { inherit nixpkgs jelly; }; | |
7 | - cammy-draw = import ./cammy-rpy/draw.nix { inherit nixpkgs jelly; }; | |
8 | - cammy-frame = import ./cammy-rpy/frame.nix { inherit nixpkgs jelly; }; | |
9 | - cammy-repl = import ./cammy-rpy/repl.nix { inherit nixpkgs jelly; }; | |
10 | - cammy-rename = import ./cammy-rpy/rename.nix { inherit nixpkgs jelly; }; | |
11 | - cammy-weave = import ./cammy-rpy/weave.nix { inherit nixpkgs jelly; }; | |
6 | + cammy-comb = import ./cammy-rpy/comb.nix { inherit nixpkgs jelly movelist; }; | |
7 | + cammy-draw = import ./cammy-rpy/draw.nix { inherit nixpkgs jelly movelist; }; | |
8 | + cammy-frame = import ./cammy-rpy/frame.nix { inherit nixpkgs jelly movelist; }; | |
9 | + cammy-repl = import ./cammy-rpy/repl.nix { inherit nixpkgs jelly movelist; }; | |
10 | + cammy-rename = import ./cammy-rpy/rename.nix { inherit nixpkgs jelly movelist; }; | |
11 | + cammy-weave = import ./cammy-rpy/weave.nix { inherit nixpkgs jelly movelist; }; | |
12 | 12 | in pkgs.stdenv.mkDerivation { |
13 | 13 | name = "cammy"; |
14 | 14 | version = "0.2"; |
@@ -1,9 +1,3 @@ | ||
1 | -(comp | |
2 | - (sdf2/metaballs (comp f/4 f-recip) | |
3 | - (fun/const | |
4 | - (comp | |
5 | - (pair (pair (comp (comp f/4 f-recip) fun/dup) (comp f/3 f-recip)) | |
6 | - (comp | |
7 | - (pair (pair (pair (comp f-one f-negate) f-zero) (comp f/2 f-recip)) | |
8 | - nil) cons)) cons))) | |
9 | - sdf2d) | |
1 | +(comp (sdf2/metaballs (comp f/4 f-recip) (fun/const (comp (pair (pair (comp (comp f/4 f-recip) pair/dup) (comp f/3 f-recip)) (comp (pair (pair (pair (comp f-one f-negate) f-zero) (comp f/2 f-recip)) nil) cons)) cons))) sdf2d) | |
2 | + | |
3 | + |
@@ -1,3 +1,3 @@ | ||
1 | -(comp (pair id (comp fun/dup f-mul)) f-mul) | |
1 | +(comp (pair id (comp pair/dup f-mul)) f-mul) | |
2 | 2 | |
3 | 3 | The cube of a floating-point number. |
@@ -0,0 +1,3 @@ | ||
1 | +(pair (case fst fst) (case (comp snd left) (comp snd right))) | |
2 | + | |
3 | +Factor out a product from a sum. |
@@ -0,0 +1,3 @@ | ||
1 | +(pair (case (comp fst left) (comp fst right)) (case snd snd)) | |
2 | + | |
3 | +Factor out a product from a sum. |
@@ -1,8 +1,4 @@ | ||
1 | -(comp | |
2 | - (fun/apppair | |
3 | - (fun/const (comp @1 nonempty/unfold)) | |
4 | - (pair (comp (fun/const f-zero) fun/dup) @0)) | |
5 | - snd) | |
1 | +(comp (fun/apppair (fun/const (comp @1 nonempty/unfold)) (pair (comp (fun/const f-zero) pair/dup) @0)) snd) | |
6 | 2 | |
7 | 3 | Iterate an [IFS](https://en.wikipedia.org/wiki/Iterated_function_system) for a |
8 | 4 | given number of steps. |
@@ -3,6 +3,4 @@ | ||
3 | 3 | (pair/mapsnd (comp (case cons nil) left)) |
4 | 4 | (comp fun/distribr (sum/mapright ignore)))) |
5 | 5 | |
6 | -[X] -> (X x [X]) + 1 | |
7 | - | |
8 | 6 | Try to decompose a list at its extremal cell. |
@@ -1,3 +1,3 @@ | ||
1 | -(comp (comp fun/dup (pair fst (comp snd nat/sqr))) nat/mul) | |
1 | +(comp (comp pair/dup (pair fst (comp snd nat/sqr))) nat/mul) | |
2 | 2 | |
3 | 3 | The cube of a natural number. |
@@ -1,3 +1,3 @@ | ||
1 | -(comp fun/dup nat/add) | |
1 | +(comp pair/dup nat/add) | |
2 | 2 | |
3 | 3 | Double a natural number by adding it to itself. |
@@ -1,3 +1,3 @@ | ||
1 | -(comp (pr (comp zero fun/dup) (comp fst (pair succ id))) snd) | |
1 | +(comp (pr (comp zero pair/dup) (comp fst (pair succ id))) snd) | |
2 | 2 | |
3 | 3 | The predecessor of a natural number. Zero is mapped to itself. |
@@ -1 +1,3 @@ | ||
1 | -(comp fun/dup nat/mul) | |
1 | +(comp pair/dup nat/mul) | |
2 | + | |
3 | + |
@@ -1,3 +1,3 @@ | ||
1 | -(comp (comp fun/dup f/dot2) f/sqrt-pos) | |
1 | +(comp (comp pair/dup f/dot2) f/sqrt-pos) | |
2 | 2 | |
3 | 3 | The norm of a complex number. |
@@ -1,3 +1,3 @@ | ||
1 | -(comp fun/dup v2/complex/mul) | |
1 | +(comp pair/dup v2/complex/mul) | |
2 | 2 | |
3 | 3 |
@@ -1 +1,3 @@ | ||
1 | -(fun/const (comp @0 fun/dup)) | |
1 | +(fun/const (comp @0 pair/dup)) | |
2 | + | |
3 | + |
@@ -1 +1,3 @@ | ||
1 | -(comp (comp (comp fun/dup f/dot3) f-sqrt) (case id f-zero)) | |
1 | +(comp (comp (comp pair/dup f/dot3) f-sqrt) (case id f-zero)) | |
2 | + | |
3 | + |