• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqt誰得windowscocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

A categorical programming language


Commit MetaInfo

修订版f34a2fd449b5186b9e39ba03af1cceceec590d17 (tree)
时间2022-05-17 13:21:38
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Draw normal vectors as colors.

更改概述

差异

--- a/cammy-rpy/cammylib/arrows.py
+++ b/cammy-rpy/cammylib/arrows.py
@@ -469,7 +469,7 @@ class Given(Arrow):
469469 """
470470 A formal parameter for a function.
471471
472- An given arrow is not executable, but it can still be typechecked.
472+ A given arrow is not executable, but it can still be typechecked.
473473 """
474474 _immutable_ = True
475475 def __init__(self, index): self.index = index
--- a/cammy-rpy/cammylib/cam.py
+++ b/cammy-rpy/cammylib/cam.py
@@ -3,8 +3,9 @@ from math import atan2, cos, exp, sin
33 from rpython.rlib.jit import JitDriver
44 from rpython.rlib.rvmprof import get_unique_id, register_code, register_code_object_class, vmprof_execute_code
55
6-from cammylib.elements import B, F, L, N, P, R, T, equal
76 from cammylib.arrows import add, floor, mul, recip, sign, sqrt
7+from cammylib.elements import B, F, L, N, P, R, T, equal
8+from cammylib.sexp import equalArrows
89
910 # A basic CAM, as in the original paper:
1011 # https://doi.org/10.1016/0167-6423(87)90020-7
@@ -249,6 +250,8 @@ class Compiler(object):
249250 self.jumpLabels = []
250251 # Queue of curried arrows to compile.
251252 self.curryQueue = []
253+ # List of arrows which have already been compiled into subroutines.
254+ self.compiledArrows = []
252255
253256 def emit(self, inst):
254257 self.insts.append(inst)
@@ -311,10 +314,19 @@ class Compiler(object):
311314 self.halt()
312315 while self.curryQueue:
313316 jump, arrow = self.curryQueue.pop()
314- # XXX if already seen this arrow, then jump to it instead
315- self.jumpHere(jump)
316- arrow.compile(self)
317- self.ret()
317+ # XXX use a hashmap instead of a list
318+ for seen, there in self.compiledArrows:
319+ if equalArrows(arrow, seen):
320+ # Use that arrow instead of this one.
321+ self.insts[jump] = there
322+ break
323+ else:
324+ here = len(self.insts)
325+ # Save the address of the subroutine for later.
326+ self.compiledArrows.append((arrow, here))
327+ self.jumpHere(jump)
328+ arrow.compile(self)
329+ self.ret()
318330
319331 def makeMachine(self):
320332 rv = CAM(self.insts[:], self.constants[:])
--- a/cammy-rpy/cammylib/sexp.py
+++ b/cammy-rpy/cammylib/sexp.py
@@ -164,3 +164,21 @@ class Hole(SExp):
164164 def unhole(sexp):
165165 assert isinstance(sexp, Hole), "implementation error"
166166 return sexp.index
167+
168+def equalArrows(a1, a2):
169+ if a1 is a2:
170+ return True
171+ elif isinstance(a1, Atom) and isinstance(a2, Atom):
172+ return a1.symbol == a2.symbol
173+ elif isinstance(a1, Functor) and isinstance(a2, Functor):
174+ if a1.constructor != a2.constructor:
175+ return False
176+ if len(a1.args) != len(a2.args):
177+ return False
178+ for i, arg in enumerate(a1.args):
179+ if not equalArrows(arg, a2.args[i]):
180+ return False
181+ return True
182+ elif isinstance(a1, Hole) and isinstance(a2, Hole):
183+ return a1.index == a2.index
184+ return False
--- a/cammy-rpy/draw.py
+++ b/cammy-rpy/draw.py
@@ -6,7 +6,7 @@ from rpython.rlib.jit import JitDriver, unroll_safe #, set_param
66 from rpython.rlib.rfile import create_stdio
77 from rpython.rlib.rfloat import string_to_float
88 from rpython.rlib.rstring import StringBuilder, split
9-from rpython.rlib import rvmprof
9+# from rpython.rlib import rvmprof
1010
1111 from cammylib.cam import compileArrow
1212 from cammylib.elements import F, P, TypeFail
@@ -112,7 +112,7 @@ def multisample(program, radius, x, y):
112112
113113 def drawPixels(size, program, window):
114114 _, stdout, _ = create_stdio()
115- sb = StringBuilder()
115+ sb = StringBuilder(size * 3)
116116 cost = Welford()
117117 i = 0
118118 while i < size:
@@ -140,8 +140,8 @@ def drawPNG(program, filename, corners, width, height):
140140 def doDraw(prog, window, width, height, out):
141141 # set_param(None, "trace_limit", 50001)
142142
143- profiler = open("vmprof.dat", "wb")
144- rvmprof.enable(profiler.fileno(), 0.001)
143+ # profiler = open("vmprof.dat", "wb")
144+ # rvmprof.enable(profiler.fileno(), 0.001)
145145
146146 window = [string_to_float(s) for s in split(window)]
147147 width = int(width)
@@ -156,7 +156,7 @@ def doDraw(prog, window, width, height, out):
156156 print "Type failure:", tf.reason
157157 raise
158158
159- rvmprof.disable()
160- profiler.close()
159+ # rvmprof.disable()
160+ # profiler.close()
161161
162162 return 0
--- a/hive/discriminant.cammy
+++ /dev/null
@@ -1 +0,0 @@
1-(f/subpair (comp (comp snd fst) (comp pair/dup f-mul)) (f/mulpair (fun/const f/4) (f/mulpair fst (comp snd snd))))
\ No newline at end of file
--- /dev/null
+++ b/hive/sum/par.cammy
@@ -0,0 +1 @@
1+(case (comp @0 left) (comp @1 right))
\ No newline at end of file
--- /dev/null
+++ b/hive/weekend/discriminant.cammy
@@ -0,0 +1,3 @@
1+(f/subpair (comp (comp snd fst) (comp pair/dup f-mul)) (f/mulpair (fun/const f/4) (f/mulpair fst (comp snd snd))))
2+
3+Compute the discriminant for the quadratic formula.
--- /dev/null
+++ b/hive/weekend/discriminant2.cammy
@@ -0,0 +1,4 @@
1+(f/subpair (comp (comp snd fst) (comp pair/dup f-mul)) (f/mulpair fst (comp snd snd)))
2+
3+Compute the discriminant for the quadratic formula. Note that $b$ is replaced
4+with $h$, where $b = 2h$.
--- /dev/null
+++ b/hive/weekend/finish-quadratic.cammy
@@ -0,0 +1,5 @@
1+(comp
2+ (comp (pair (pair fst (comp snd fst)) (comp weekend/discriminant f-sqrt)) fun/distribr)
3+ (sum/par (f/divpair (f/subpair (comp (comp fst snd) f-negate) snd) (f/mulpair (comp fst fst) (fun/const f/2))) ignore))
4+
5+Compute the quadratic formula.
--- /dev/null
+++ b/hive/weekend/finish-quadratic2.cammy
@@ -0,0 +1,5 @@
1+(comp
2+ (comp (pair (pair fst (comp snd fst)) (comp weekend/discriminant2 f-sqrt)) fun/distribr)
3+ (sum/par (f/divpair (f/subpair (comp (comp fst snd) f-negate) snd) (comp fst fst)) ignore))
4+
5+Compute the quadratic formula, replacing $b$ with $h$ where $b = 2h$.
--- a/hive/weekend/hit_sphere.cammy
+++ b/hive/weekend/hit_sphere.cammy
@@ -3,3 +3,6 @@
33 (v3/triple (comp snd (comp pair/dup v3/dot)) (f/mulpair (fun/const f/2)
44 v3/dot) (f/subpair (comp fst (comp pair/dup v3/dot)) (comp (fun/const @1) (comp pair/dup
55 f-mul)))))
6+
7+Test whether a ray hits a sphere, generating coefficients for the quadratic
8+formula.
--- /dev/null
+++ b/hive/weekend/hit_sphere2.cammy
@@ -0,0 +1,12 @@
1+(comp
2+ (pair/mapfst (comp (pair id (fun/const @0)) v3/sub))
3+ (v3/triple
4+ (comp snd v3/length_squared)
5+ v3/dot
6+ (f/subpair
7+ (comp fst v3/length_squared)
8+ (comp (fun/const @1) f/sqr))))
9+
10+Test whether a ray hits a sphere, generating coefficients for the quadratic
11+formula. The coefficients are not $a$, $b$, and $c$, but $a$, $h$, and $c$,
12+where $b = 2h$.
--- /dev/null
+++ b/hive/weekend/hit_sphere2?.cammy
@@ -0,0 +1,3 @@
1+(comp (weekend/hit_sphere @0 @1) weekend/finish-quadratic)
2+
3+
--- /dev/null
+++ b/hive/weekend/hit_sphere3?.cammy
@@ -0,0 +1,4 @@
1+(comp (weekend/hit_sphere2 @0 @1) weekend/finish-quadratic2)
2+
3+Basically the same as hit_sphere2?, but now the quadratic formula is rewritten
4+in terms of $h$, where $b = 2h$.
--- a/hive/weekend/hit_sphere?.cammy
+++ b/hive/weekend/hit_sphere?.cammy
@@ -1 +1,3 @@
1-(comp (pair (fun/const f-zero) (comp weekend/hit_sphere discriminant)) f-lt)
\ No newline at end of file
1+(comp (pair (fun/const f-zero) (comp weekend/hit_sphere weekend/discriminant)) f-lt)
2+
3+
--- /dev/null
+++ b/hive/weekend/image4.cammy
@@ -0,0 +1 @@
1+(comp (pair (fun/const weekend/origin) weekend/aim-ray) weekend/ray_color4)
--- /dev/null
+++ b/hive/weekend/make-normal.cammy
@@ -0,0 +1 @@
1+(pair/of v3/sub weekend/ray_at (fun/const (v3/triple f-zero f-zero (comp f-one f-negate))))
\ No newline at end of file
--- a/hive/weekend/ray_color2.cammy
+++ b/hive/weekend/ray_color2.cammy
@@ -1,5 +1,9 @@
11 (comp
2- (pair (weekend/hit_sphere? (v3/triple f-zero f-zero (comp f-one f-negate)) f/2) id)
2+ (pair
3+ (weekend/hit_sphere?
4+ (v3/triple f-zero f-zero (comp f-one f-negate))
5+ f/half)
6+ id)
37 (uncurry (bool/if (fun/const weekend/red)
48 (comp
59 (comp
--- /dev/null
+++ b/hive/weekend/ray_color3.cammy
@@ -0,0 +1,3 @@
1+(comp (pair id (weekend/hit_sphere2? (v3/triple f-zero f-zero (comp f-one f-negate)) f/half)) (comp fun/distribr (case (comp weekend/make-normal (v3/map f/disc-to-interval)) (comp (comp (comp (comp (comp fst snd) v3/normalise) (comp snd fst)) (comp f/disc-to-interval f/invert-interval)) (v3/lerp weekend/white weekend/sky)))))
2+
3+
--- /dev/null
+++ b/hive/weekend/ray_color4.cammy
@@ -0,0 +1,3 @@
1+(comp (pair id (weekend/hit_sphere3? (v3/triple f-zero f-zero (comp f-one f-negate)) f/half)) (comp fun/distribr (case (comp weekend/make-normal (v3/map f/disc-to-interval)) (comp (comp (comp (comp (comp fst snd) v3/normalise) (comp snd fst)) (comp f/disc-to-interval f/invert-interval)) (v3/lerp weekend/white weekend/sky)))))
2+
3+
--- a/todo.txt
+++ b/todo.txt
@@ -129,6 +129,7 @@
129129 backwards and cancel out or amend previous instructions
130130 * Taking a label would create a code barrier, to avoid having to
131131 recompute jumps
132+ * PUSH; SWAP seen in the wild; occurs when compiling fun/graph
132133 * TCO, or really any call optimizations
133134 * snoc and other backwards versions of ops
134135 * there's no place to emit snoc