• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javaandroidc++linuxc#windowsobjective-ccocoaqtpython誰得phprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A categorical programming language


Commit MetaInfo

修订版09a112ec6d9740e181d405b6fb8355b7c8dcb27d (tree)
时间2022-03-30 11:50:39
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Call djinn from REPL.

更改概述

差异

--- a/cammy-rpy/builder.nix
+++ b/cammy-rpy/builder.nix
@@ -1,5 +1,5 @@
11 entrypoint: exe: enableJIT:
2-{ nixpkgs ? import <nixpkgs> {}, jelly ? null }:
2+{ nixpkgs ? import <nixpkgs> {}, jelly ? null, movelist ? null }:
33 let
44 inherit (nixpkgs.pkgs)
55 fetchFromGitLab stdenv
@@ -34,6 +34,7 @@ in
3434 ];
3535
3636 JELLY_PATH = "${jelly}/bin/jelly";
37+ MOVELIST_PATH = "${movelist}/bin/movelist";
3738
3839 buildPhase = ''
3940 source $stdenv/setup
--- /dev/null
+++ b/cammy-rpy/cammylib/djinn.py
@@ -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
--- a/cammy-rpy/cammylib/parser.py
+++ b/cammy-rpy/cammylib/parser.py
@@ -52,9 +52,16 @@ class CammyParser(object):
5252
5353
5454 def parse(s):
55+ "Parse a Cammy expression, preserving any trailing text."
5556 parser = CammyParser(s)
5657 sexp = parser.takeExpression()
5758 # Parser is now fast-forwarded to the end of the S-expression, so we can
5859 # slice from that point and get the docstring/etc.
5960 trail = s[parser.position():]
6061 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()
--- a/cammy-rpy/repl.py
+++ b/cammy-rpy/repl.py
@@ -6,8 +6,9 @@ from rpython.rlib.rfile import create_stdio
66
77 from cammylib.arrows import BuildProblem, T
88 from cammylib.hive import Hive, MissingAtom
9+from cammylib.djinn import askDjinn
910 from cammylib.jelly import jellify
10-from cammylib.parser import parse
11+from cammylib.parser import parse, parseTypes
1112 from cammylib.types import ConstraintStore, TypeExtractor, TypeFormatter, UnificationFailed
1213
1314 LINE_BUFFER_LENGTH = 1024
@@ -49,6 +50,11 @@ def command(hive, code, line):
4950 print "Arrow is not an element"
5051 else:
5152 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()
5258
5359 def repl(hive, stdin, stdout):
5460 while True:
--- a/default.nix
+++ b/default.nix
@@ -3,12 +3,12 @@ let
33 inherit (nixpkgs) pkgs;
44 jelly = (import jelly/Cargo.nix { pkgs = nixpkgs; }).rootCrate.build;
55 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; };
1212 in pkgs.stdenv.mkDerivation {
1313 name = "cammy";
1414 version = "0.2";
--- a/hive/demo/metaball.cammy
+++ b/hive/demo/metaball.cammy
@@ -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+
--- a/hive/f/cube.cammy
+++ b/hive/f/cube.cammy
@@ -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)
22
33 The cube of a floating-point number.
--- /dev/null
+++ b/hive/fun/factorl.cammy
@@ -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.
--- /dev/null
+++ b/hive/fun/factorr.cammy
@@ -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.
--- a/hive/iter-fractal.cammy
+++ b/hive/iter-fractal.cammy
@@ -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)
62
73 Iterate an [IFS](https://en.wikipedia.org/wiki/Iterated_function_system) for a
84 given number of steps.
--- a/hive/list/uncons.cammy
+++ b/hive/list/uncons.cammy
@@ -3,6 +3,4 @@
33 (pair/mapsnd (comp (case cons nil) left))
44 (comp fun/distribr (sum/mapright ignore))))
55
6-[X] -> (X x [X]) + 1
7-
86 Try to decompose a list at its extremal cell.
--- a/hive/nat/cube.cammy
+++ b/hive/nat/cube.cammy
@@ -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)
22
33 The cube of a natural number.
--- a/hive/nat/double.cammy
+++ b/hive/nat/double.cammy
@@ -1,3 +1,3 @@
1-(comp fun/dup nat/add)
1+(comp pair/dup nat/add)
22
33 Double a natural number by adding it to itself.
--- a/hive/nat/pred.cammy
+++ b/hive/nat/pred.cammy
@@ -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)
22
33 The predecessor of a natural number. Zero is mapped to itself.
--- a/hive/nat/sqr.cammy
+++ b/hive/nat/sqr.cammy
@@ -1 +1,3 @@
1-(comp fun/dup nat/mul)
1+(comp pair/dup nat/mul)
2+
3+
--- a/hive/v2/complex/norm.cammy
+++ b/hive/v2/complex/norm.cammy
@@ -1,3 +1,3 @@
1-(comp (comp fun/dup f/dot2) f/sqrt-pos)
1+(comp (comp pair/dup f/dot2) f/sqrt-pos)
22
33 The norm of a complex number.
--- a/hive/v2/complex/sqr.cammy
+++ b/hive/v2/complex/sqr.cammy
@@ -1,3 +1,3 @@
1-(comp fun/dup v2/complex/mul)
1+(comp pair/dup v2/complex/mul)
22
33
--- a/hive/v2/const.cammy
+++ b/hive/v2/const.cammy
@@ -1 +1,3 @@
1-(fun/const (comp @0 fun/dup))
1+(fun/const (comp @0 pair/dup))
2+
3+
--- a/hive/v3/norm.cammy
+++ b/hive/v3/norm.cammy
@@ -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+