A categorical programming language
Revisión | f1ce494419f15ecbc21a4174094990cb4fc0f433 (tree) |
---|---|
Tiempo | 2021-11-04 02:50:35 |
Autor | Corbin <cds@corb...> |
Commiter | Corbin |
Start doing trigonometry.
Halfway to HSL and graphing complex functions!
@@ -107,46 +107,30 @@ class H(Element): | ||
107 | 107 | |
108 | 108 | |
109 | 109 | class Arrow(object): |
110 | - pass | |
110 | + _immutable_ = True | |
111 | 111 | |
112 | 112 | class Id(Arrow): |
113 | - | |
114 | 113 | _immutable_ = True |
115 | - | |
116 | - def run(self, x): | |
117 | - return x | |
114 | + def run(self, x): return x | |
118 | 115 | |
119 | 116 | class Comp(Arrow): |
120 | - | |
121 | 117 | _immutable_ = True |
122 | - | |
123 | 118 | def __init__(self, f, g): |
124 | 119 | self.f = f |
125 | 120 | 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)) | |
129 | 122 | |
130 | 123 | class Ignore(Arrow): |
131 | - | |
132 | 124 | _immutable_ = True |
133 | - | |
134 | - def run(self, x): | |
135 | - return T() | |
125 | + def run(self, x): return T() | |
136 | 126 | |
137 | 127 | class First(Arrow): |
138 | - | |
139 | 128 | _immutable_ = True |
140 | - | |
141 | - def run(self, x): | |
142 | - return x.first() | |
129 | + def run(self, x): return x.first() | |
143 | 130 | |
144 | 131 | class Second(Arrow): |
145 | - | |
146 | 132 | _immutable_ = True |
147 | - | |
148 | - def run(self, x): | |
149 | - return x.second() | |
133 | + def run(self, x): return x.second() | |
150 | 134 | |
151 | 135 | class Pair(Arrow): |
152 | 136 | _immutable_ = True |
@@ -157,18 +141,12 @@ class Pair(Arrow): | ||
157 | 141 | return P(self.f.run(x), self.g.run(x)) |
158 | 142 | |
159 | 143 | class Left(Arrow): |
160 | - | |
161 | 144 | _immutable_ = True |
162 | - | |
163 | - def run(self, x): | |
164 | - return L(x) | |
145 | + def run(self, x): return L(x) | |
165 | 146 | |
166 | 147 | class Right(Arrow): |
167 | - | |
168 | 148 | _immutable_ = True |
169 | - | |
170 | - def run(self, x): | |
171 | - return R(x) | |
149 | + def run(self, x): return R(x) | |
172 | 150 | |
173 | 151 | class Case(Arrow): |
174 | 152 | _immutable_ = True |
@@ -207,6 +185,10 @@ class FOne(Arrow): | ||
207 | 185 | _immutable_ = True |
208 | 186 | def run(self, x): return F(1.0) |
209 | 187 | |
188 | +class FPi(Arrow): | |
189 | + _immutable_ = True | |
190 | + def run(self, x): return F(math.pi) | |
191 | + | |
210 | 192 | def sign(f): return math.copysign(1.0, f) > 0.0 |
211 | 193 | class FSign(Arrow): |
212 | 194 | _immutable_ = True |
@@ -252,6 +234,10 @@ class FSqrt(Arrow): | ||
252 | 234 | f = x.f() |
253 | 235 | return L(F(math.sqrt(f))) if sign(f) else R(T()) |
254 | 236 | |
237 | +class FATan2(Arrow): | |
238 | + _immutable_ = True | |
239 | + def run(self, x): return F(math.atan2(x.first().f(), x.second().f())) | |
240 | + | |
255 | 241 | |
256 | 242 | class BuildProblem(Exception): |
257 | 243 | def __init__(self, message): |
@@ -267,6 +253,7 @@ unaryFunctors = { | ||
267 | 253 | "either": Either(), |
268 | 254 | "f-zero": FZero(), |
269 | 255 | "f-one": FOne(), |
256 | + "f-pi": FPi(), | |
270 | 257 | "f-sign": FSign(), |
271 | 258 | "f-negate": FNegate(), |
272 | 259 | "f-recip": FRecip(), |
@@ -274,6 +261,7 @@ unaryFunctors = { | ||
274 | 261 | "f-add": FAdd(), |
275 | 262 | "f-mul": FMul(), |
276 | 263 | "f-sqrt": FSqrt(), |
264 | + "f-atan2": FATan2(), | |
277 | 265 | } |
278 | 266 | |
279 | 267 | def buildUnary(name): |
@@ -5,7 +5,8 @@ module CodeCache = Map.Make (String) | ||
5 | 5 | let primitives = |
6 | 6 | "id comp ignore fst snd pair left right case curry uncurry \ |
7 | 7 | 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" | |
9 | 10 | |
10 | 11 | let filter = |
11 | 12 | List.fold_left |
@@ -1 +1 @@ | ||
1 | -(comp (pair f-one f/9) f-add) | |
1 | +(f/addpair f-one f/9) |
@@ -1 +1 @@ | ||
1 | -(comp (pair f-one f-one) f-add) | |
1 | +(f/addpair f-one f-one) |
@@ -1 +1 @@ | ||
1 | -(comp (pair f-one f/2) f-add) | |
1 | +(f/addpair f-one f/2) |
@@ -1 +1 @@ | ||
1 | -(comp (pair f/2 f/2) f-add) | |
1 | +(f/addpair f/2 f/2) |
@@ -0,0 +1 @@ | ||
1 | +(comp (pair @0 @1) f-add) |
@@ -1 +0,0 @@ | ||
1 | -(comp (pair fst (comp snd f-recip)) f-mul) |
@@ -0,0 +1 @@ | ||
1 | +(f/mulpair @0 (comp @1 f-recip)) |
@@ -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))) |
@@ -1 +1 @@ | ||
1 | -(comp (pair (comp fst @0) (comp snd f-negate)) f-add) | |
1 | +(f/addpair (comp fst @0) (comp snd f-negate)) |
@@ -0,0 +1 @@ | ||
1 | +(comp (pair @0 @1) f-mul) |
@@ -1 +1 @@ | ||
1 | -(comp fun/dup f-mul) | |
1 | +(f/mulpair id id) |
@@ -0,0 +1 @@ | ||
1 | +(f/addpair @0 (comp @1 f-negate)) |
@@ -0,0 +1 @@ | ||
1 | +(fold f-zero f-add) |
@@ -0,0 +1 @@ | ||
1 | +(case id right) |
@@ -0,0 +1 @@ | ||
1 | +(comp @0 left) |
@@ -0,0 +1 @@ | ||
1 | +left |
@@ -0,0 +1 @@ | ||
1 | +(curry (comp (pair fun/app snd) fun/app)) |
@@ -0,0 +1 @@ | ||
1 | +(curry fst) |
@@ -0,0 +1 @@ | ||
1 | +(curry id) |
@@ -1,7 +1,7 @@ | ||
1 | 1 | (pair |
2 | - (comp (pair | |
2 | + (f/subpair | |
3 | 3 | (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 | |
6 | 6 | (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))) |
@@ -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)) |
@@ -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)) |
@@ -19,6 +19,7 @@ | ||
19 | 19 | ((== expr 'f-add) (== s (list 'pair 'F 'F)) (== t 'F)) |
20 | 20 | ((== expr 'f-mul) (== s (list 'pair 'F 'F)) (== t 'F)) |
21 | 21 | ((== expr 'f-sqrt) (== s 'F) (== t (list 'sum 'F '1))) |
22 | + ((== expr 'f-atan2) (== s (list 'pair 'F 'F)) (== t 'F)) | |
22 | 23 | ; Compound before trivial. |
23 | 24 | ((== expr 'succ) (== s 'N) (== t 'N)) |
24 | 25 | ((== expr 'zero) (== s '1) (== t 'N)) |
@@ -27,6 +28,7 @@ | ||
27 | 28 | ((== expr 'not) (== s '2) (== t '2)) |
28 | 29 | ((== expr 'f-zero) (== s '1) (== t 'F)) |
29 | 30 | ((== expr 'f-one) (== s '1) (== t 'F)) |
31 | + ((== expr 'f-pi) (== s '1) (== t 'F)) | |
30 | 32 | ((== expr 'f-sign) (== s 'F) (== t '2)) |
31 | 33 | ((== expr 'f-negate) (== s 'F) (== t 'F)) |
32 | 34 | ((== expr 'f-recip) (== s 'F) (== t 'F)) |
@@ -8,3 +8,12 @@ | ||
8 | 8 | * Typechecker needs to handle holes |
9 | 9 | * Typechecker should handle polymorphism better |
10 | 10 | * 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 |