• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-cqtcocoa誰得pythonphprubygameguibathyscaphec翻訳計画中(planning stage)omegatframeworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

A categorical programming language


Commit MetaInfo

修订版f1ce494419f15ecbc21a4174094990cb4fc0f433 (tree)
时间2021-11-04 02:50:35
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Start doing trigonometry.

Halfway to HSL and graphing complex functions!

更改概述

差异

--- a/cammy-run/main.py
+++ b/cammy-run/main.py
@@ -107,46 +107,30 @@ class H(Element):
107107
108108
109109 class Arrow(object):
110- pass
110+ _immutable_ = True
111111
112112 class Id(Arrow):
113-
114113 _immutable_ = True
115-
116- def run(self, x):
117- return x
114+ def run(self, x): return x
118115
119116 class Comp(Arrow):
120-
121117 _immutable_ = True
122-
123118 def __init__(self, f, g):
124119 self.f = f
125120 self.g = g
126-
127- def run(self, x):
128- return self.g.run(self.f.run(x))
121+ def run(self, x): return self.g.run(self.f.run(x))
129122
130123 class Ignore(Arrow):
131-
132124 _immutable_ = True
133-
134- def run(self, x):
135- return T()
125+ def run(self, x): return T()
136126
137127 class First(Arrow):
138-
139128 _immutable_ = True
140-
141- def run(self, x):
142- return x.first()
129+ def run(self, x): return x.first()
143130
144131 class Second(Arrow):
145-
146132 _immutable_ = True
147-
148- def run(self, x):
149- return x.second()
133+ def run(self, x): return x.second()
150134
151135 class Pair(Arrow):
152136 _immutable_ = True
@@ -157,18 +141,12 @@ class Pair(Arrow):
157141 return P(self.f.run(x), self.g.run(x))
158142
159143 class Left(Arrow):
160-
161144 _immutable_ = True
162-
163- def run(self, x):
164- return L(x)
145+ def run(self, x): return L(x)
165146
166147 class Right(Arrow):
167-
168148 _immutable_ = True
169-
170- def run(self, x):
171- return R(x)
149+ def run(self, x): return R(x)
172150
173151 class Case(Arrow):
174152 _immutable_ = True
@@ -207,6 +185,10 @@ class FOne(Arrow):
207185 _immutable_ = True
208186 def run(self, x): return F(1.0)
209187
188+class FPi(Arrow):
189+ _immutable_ = True
190+ def run(self, x): return F(math.pi)
191+
210192 def sign(f): return math.copysign(1.0, f) > 0.0
211193 class FSign(Arrow):
212194 _immutable_ = True
@@ -252,6 +234,10 @@ class FSqrt(Arrow):
252234 f = x.f()
253235 return L(F(math.sqrt(f))) if sign(f) else R(T())
254236
237+class FATan2(Arrow):
238+ _immutable_ = True
239+ def run(self, x): return F(math.atan2(x.first().f(), x.second().f()))
240+
255241
256242 class BuildProblem(Exception):
257243 def __init__(self, message):
@@ -267,6 +253,7 @@ unaryFunctors = {
267253 "either": Either(),
268254 "f-zero": FZero(),
269255 "f-one": FOne(),
256+ "f-pi": FPi(),
270257 "f-sign": FSign(),
271258 "f-negate": FNegate(),
272259 "f-recip": FRecip(),
@@ -274,6 +261,7 @@ unaryFunctors = {
274261 "f-add": FAdd(),
275262 "f-mul": FMul(),
276263 "f-sqrt": FSqrt(),
264+ "f-atan2": FATan2(),
277265 }
278266
279267 def buildUnary(name):
--- a/frame/frame.ml
+++ b/frame/frame.ml
@@ -5,7 +5,8 @@ module CodeCache = Map.Make (String)
55 let primitives =
66 "id comp ignore fst snd pair left right case curry uncurry \
77 zero succ pr nil cons fold t f not conj disj either \
8- f-zero f-one f-sign f-negate f-recip f-lt f-add f-mul f-sqrt"
8+ f-zero f-one f-pi \
9+ f-sign f-negate f-recip f-lt f-add f-mul f-sqrt f-atan2"
910
1011 let filter =
1112 List.fold_left
--- a/hive/f/10.cammy
+++ b/hive/f/10.cammy
@@ -1 +1 @@
1-(comp (pair f-one f/9) f-add)
1+(f/addpair f-one f/9)
--- a/hive/f/2.cammy
+++ b/hive/f/2.cammy
@@ -1 +1 @@
1-(comp (pair f-one f-one) f-add)
1+(f/addpair f-one f-one)
--- a/hive/f/3.cammy
+++ b/hive/f/3.cammy
@@ -1 +1 @@
1-(comp (pair f-one f/2) f-add)
1+(f/addpair f-one f/2)
--- a/hive/f/4.cammy
+++ b/hive/f/4.cammy
@@ -1 +1 @@
1-(comp (pair f/2 f/2) f-add)
1+(f/addpair f/2 f/2)
--- /dev/null
+++ b/hive/f/addpair.cammy
@@ -0,0 +1 @@
1+(comp (pair @0 @1) f-add)
--- a/hive/f/div.cammy
+++ /dev/null
@@ -1 +0,0 @@
1-(comp (pair fst (comp snd f-recip)) f-mul)
--- /dev/null
+++ b/hive/f/divpair.cammy
@@ -0,0 +1 @@
1+(f/mulpair @0 (comp @1 f-recip))
--- a/hive/f/dot2.cammy
+++ b/hive/f/dot2.cammy
@@ -1,5 +1,3 @@
1-(comp
2- (pair
3- (comp (pair (comp fst fst) (comp snd fst)) f-mul)
4- (comp (pair (comp fst snd) (comp snd snd)) f-mul))
5- f-add)
1+(f/addpair
2+ (f/mulpair (comp fst fst) (comp snd fst))
3+ (f/mulpair (comp fst snd) (comp snd snd)))
--- a/hive/f/error.cammy
+++ b/hive/f/error.cammy
@@ -1 +1 @@
1-(comp (pair (comp fst @0) (comp snd f-negate)) f-add)
1+(f/addpair (comp fst @0) (comp snd f-negate))
--- /dev/null
+++ b/hive/f/mulpair.cammy
@@ -0,0 +1 @@
1+(comp (pair @0 @1) f-mul)
--- a/hive/f/sqr.cammy
+++ b/hive/f/sqr.cammy
@@ -1 +1 @@
1-(comp fun/dup f-mul)
1+(f/mulpair id id)
--- /dev/null
+++ b/hive/f/subpair.cammy
@@ -0,0 +1 @@
1+(f/addpair @0 (comp @1 f-negate))
--- /dev/null
+++ b/hive/f/sum.cammy
@@ -0,0 +1 @@
1+(fold f-zero f-add)
--- /dev/null
+++ b/hive/monads/maybe/join.cammy
@@ -0,0 +1 @@
1+(case id right)
--- /dev/null
+++ b/hive/monads/maybe/lift.cammy
@@ -0,0 +1 @@
1+(comp @0 left)
--- /dev/null
+++ b/hive/monads/maybe/unit.cammy
@@ -0,0 +1 @@
1+left
--- /dev/null
+++ b/hive/monads/reader/join.cammy
@@ -0,0 +1 @@
1+(curry (comp (pair fun/app snd) fun/app))
--- /dev/null
+++ b/hive/monads/reader/unit.cammy
@@ -0,0 +1 @@
1+(curry fst)
--- /dev/null
+++ b/hive/monads/state/unit.cammy
@@ -0,0 +1 @@
1+(curry id)
--- a/hive/v2/c-mul.cammy
+++ b/hive/v2/c-mul.cammy
@@ -1,7 +1,7 @@
11 (pair
2- (comp (pair
2+ (f/subpair
33 (comp (pair (comp fst fst) (comp snd fst)) f-mul)
4- (comp (pair (comp fst snd) (comp snd snd)) f-mul)) f/sub)
5- (comp (pair
4+ (comp (pair (comp fst snd) (comp snd snd)) f-mul))
5+ (f/addpair
66 (comp (pair (comp fst snd) (comp snd fst)) f-mul)
7- (comp (pair (comp fst fst) (comp snd snd)) f-mul)) f-add))
7+ (comp (pair (comp fst fst) (comp snd snd)) f-mul)))
--- a/hive/v2/scale.cammy
+++ b/hive/v2/scale.cammy
@@ -1,3 +1,3 @@
1-(comp (pair (comp (comp (pair id (fun/const @1)) f/div) @0)
2- (fun/const @2)) f-mul)
3-
1+(f/mulpair
2+ (comp (f/divpair id (fun/const @1)) @0)
3+ (fun/const @2))
--- a/hive/v2/translate.cammy
+++ b/hive/v2/translate.cammy
@@ -1,2 +1,3 @@
1-(comp (pair (comp (comp (pair id (fun/const @1)) f/sub) @0)
2- (fun/const @2)) f-add)
1+(f/addpair
2+ (comp (f/subpair id (fun/const @1)) @0)
3+ (fun/const @2))
--- a/movelist/movelist.scm
+++ b/movelist/movelist.scm
@@ -19,6 +19,7 @@
1919 ((== expr 'f-add) (== s (list 'pair 'F 'F)) (== t 'F))
2020 ((== expr 'f-mul) (== s (list 'pair 'F 'F)) (== t 'F))
2121 ((== expr 'f-sqrt) (== s 'F) (== t (list 'sum 'F '1)))
22+ ((== expr 'f-atan2) (== s (list 'pair 'F 'F)) (== t 'F))
2223 ; Compound before trivial.
2324 ((== expr 'succ) (== s 'N) (== t 'N))
2425 ((== expr 'zero) (== s '1) (== t 'N))
@@ -27,6 +28,7 @@
2728 ((== expr 'not) (== s '2) (== t '2))
2829 ((== expr 'f-zero) (== s '1) (== t 'F))
2930 ((== expr 'f-one) (== s '1) (== t 'F))
31+ ((== expr 'f-pi) (== s '1) (== t 'F))
3032 ((== expr 'f-sign) (== s 'F) (== t '2))
3133 ((== expr 'f-negate) (== s 'F) (== t 'F))
3234 ((== expr 'f-recip) (== s 'F) (== t 'F))
--- a/todo.txt
+++ b/todo.txt
@@ -8,3 +8,12 @@
88 * Typechecker needs to handle holes
99 * Typechecker should handle polymorphism better
1010 * Typechecker errors are inscrutable
11+* Draw complex functions
12+ * hsl2rgb
13+ * complex to HSL
14+* Trigonometric functions
15+ * f-atan2 added, need f-sin and f-cos?
16+* Transcendental and other constants
17+ * Euler's constant e
18+ * pi
19+ * Euler's constant gamma