A categorical programming language
Revisión | b5514dfa0e756af6866ce9e10358a3638bc1418b (tree) |
---|---|
Tiempo | 2022-07-03 23:20:13 |
Autor | Corbin <cds@corb...> |
Commiter | Corbin |
Add the ability to display sequences, not just elements.
@@ -4,10 +4,11 @@ import time | ||
4 | 4 | |
5 | 5 | from rpython.rlib.rfile import create_stdio |
6 | 6 | |
7 | -from cammylib.arrows import BuildProblem, T | |
7 | +from cammylib.arrows import BuildProblem | |
8 | 8 | from cammylib.cam import compileArrow |
9 | 9 | from cammylib.hive import Hive, MissingAtom |
10 | 10 | from cammylib.djinn import askDjinn |
11 | +from cammylib.elements import T, N | |
11 | 12 | from cammylib.jelly import jellify |
12 | 13 | from cammylib.parser import parse, parseTypes |
13 | 14 | from cammylib.types import ConstraintStore, TypeExtractor, TypeFormatter, UnificationFailed |
@@ -38,6 +39,13 @@ class PlaintextTypeFormatter(TypeFormatter): | ||
38 | 39 | class PromptContext(object): |
39 | 40 | mostRecentExpr = None |
40 | 41 | |
42 | +def printElement(cam): | |
43 | + print cam.execute(T()).asStr() | |
44 | + | |
45 | +def printSequence(cam): | |
46 | + for i in range(5): | |
47 | + print i, "|->", cam.execute(N(i)).asStr() | |
48 | + | |
41 | 49 | def command(hive, code, line, context): |
42 | 50 | # XXX code e: Edit a file |
43 | 51 | # os.system("$EDITOR") |
@@ -49,25 +57,33 @@ def command(hive, code, line, context): | ||
49 | 57 | arrow = sexp.buildArrow() |
50 | 58 | cs = ConstraintStore() |
51 | 59 | domain, codomain = arrow.types(cs) |
60 | + | |
61 | + start = time.time() | |
52 | 62 | # If the arrow is polymorphic, monomorphize it. |
53 | - cs.unify(domain, cs.concrete("1")) | |
54 | - extractor = TypeExtractor(cs, PlaintextTypeFormatter()) | |
55 | - if extractor.extract(domain) != "1": | |
56 | - print "Arrow is not an element" | |
57 | - else: | |
63 | + try: | |
64 | + cs.unify(domain, cs.concrete("1")) | |
58 | 65 | # Compile the arrow and run it. |
59 | 66 | cam = compileArrow(arrow) |
60 | 67 | start = time.time() |
61 | - print cam.execute(T()).asStr() | |
62 | - duration = time.time() - start | |
63 | - unit = "s" | |
64 | - if duration < 1.0: | |
65 | - duration *= 1000 | |
66 | - unit = "ms" | |
67 | - if duration < 1.0: | |
68 | - duration *= 1000 | |
69 | - unit = "µs" | |
70 | - print "(%f %s)" % (duration, unit) | |
68 | + printElement(cam) | |
69 | + except UnificationFailed: | |
70 | + # Maybe it's a sequence? | |
71 | + try: | |
72 | + cs.unify(domain, cs.concrete("N")) | |
73 | + cam = compileArrow(arrow) | |
74 | + start = time.time() | |
75 | + printSequence(cam) | |
76 | + except UnificationFailed: | |
77 | + print "Couldn't unify domain with 1 or N, so can't display this element" | |
78 | + duration = time.time() - start | |
79 | + unit = "s" | |
80 | + if duration < 1.0: | |
81 | + duration *= 1000 | |
82 | + unit = "ms" | |
83 | + if duration < 1.0: | |
84 | + duration *= 1000 | |
85 | + unit = "µs" | |
86 | + print "(%f %s)" % (duration, unit) | |
71 | 87 | elif code == "n": |
72 | 88 | # Normalize an arrow. |
73 | 89 | sexp, trail = parse(line) |