• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A categorical programming language


Commit MetaInfo

修订版b5514dfa0e756af6866ce9e10358a3638bc1418b (tree)
时间2022-07-03 23:20:13
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Add the ability to display sequences, not just elements.

更改概述

差异

--- a/cammy-rpy/repl.py
+++ b/cammy-rpy/repl.py
@@ -4,10 +4,11 @@ import time
44
55 from rpython.rlib.rfile import create_stdio
66
7-from cammylib.arrows import BuildProblem, T
7+from cammylib.arrows import BuildProblem
88 from cammylib.cam import compileArrow
99 from cammylib.hive import Hive, MissingAtom
1010 from cammylib.djinn import askDjinn
11+from cammylib.elements import T, N
1112 from cammylib.jelly import jellify
1213 from cammylib.parser import parse, parseTypes
1314 from cammylib.types import ConstraintStore, TypeExtractor, TypeFormatter, UnificationFailed
@@ -38,6 +39,13 @@ class PlaintextTypeFormatter(TypeFormatter):
3839 class PromptContext(object):
3940 mostRecentExpr = None
4041
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+
4149 def command(hive, code, line, context):
4250 # XXX code e: Edit a file
4351 # os.system("$EDITOR")
@@ -49,25 +57,33 @@ def command(hive, code, line, context):
4957 arrow = sexp.buildArrow()
5058 cs = ConstraintStore()
5159 domain, codomain = arrow.types(cs)
60+
61+ start = time.time()
5262 # 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"))
5865 # Compile the arrow and run it.
5966 cam = compileArrow(arrow)
6067 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)
7187 elif code == "n":
7288 # Normalize an arrow.
7389 sexp, trail = parse(line)