on master help sign in
Fix conflicts and compile errors in recent merge
commit3edef47da81116df92a6d283bdd6058ca109d4d2 authorEdwin Brady <eb@cs.st-andrews.ac.uk> dateSat 25 Feb 2012, 19:05 (15 years ago) parent605aea3efc04a3
5 files changed+78−6
lib/builtins.idr+5−4 lib/checkall.idr+1 lib/prelude.idr+5−2 lib/prelude/complex.idr+65 lib/prelude/nat.idr+2
lib/builtins.idr+5−4
@@ -186,14 +186,12 @@ instance (Ord a, Ord b) => Ord (a, b) where
186186 else compare xr yr
187187
188188
189−class (Eq a, Ord a) => Num a where
189+class Eq a => Num a where
190190 (+) : a -> a -> a
191191 (-) : a -> a -> a
192192 (*) : a -> a -> a
193193
194194 abs : a -> a
195− abs x = if (x < 0) then (-x) else x
196−
197195 fromInteger : Int -> a
198196
199197
@@ -203,7 +201,8 @@ instance Num Int where
203201 (-) = prim__subInt
204202 (*) = prim__mulInt
205203
206− fromInteger = id -- prim__bigIntToInt
204+ fromInteger = id
205+ abs x = if x<0 then -x else x
207206
208207
209208instance Num Integer where
@@ -211,6 +210,7 @@ instance Num Integer where
211210 (-) = prim__subBigInt
212211 (*) = prim__mulBigInt
213212
213+ abs x = if x<0 then -x else x
214214 fromInteger = prim__intToBigInt
215215
216216
@@ -219,6 +219,7 @@ instance Num Float where
219219 (-) = prim__subFloat
220220 (*) = prim__mulFloat
221221
222+ abs x = if x<0 then -x else x
222223 fromInteger = prim__intToFloat
223224
224225
lib/checkall.idr+1
@@ -21,6 +21,7 @@ import prelude.vect
2121import prelude.strings
2222import prelude.char
2323import prelude.heap
24+import prelude.complex
2425
2526import network.cgi
2627
lib/prelude.idr+5−2
@@ -143,6 +143,9 @@ acos x = prim__floatACos x
143143atan : Float -> Float
144144atan x = prim__floatATan x
145145
146+atan2 : Float -> Float -> Float
147+atan2 y x = atan (y/x)
148+
146149sqrt : Float -> Float
147150sqrt x = prim__floatSqrt x
148151
@@ -154,11 +157,11 @@ ceiling x = prim__floatCeil x
154157
155158---- Ranges
156159
157−count : Num a => a -> a -> a -> List a
160+count : (Ord a, Num a) => a -> a -> a -> List a
158161count a inc b = if a <= b then a :: count (a + inc) inc b
159162 else []
160163
161−countFrom : Num a => a -> a -> List a
164+countFrom : (Ord a, Num a) => a -> a -> List a
162165countFrom a inc = a :: lazy (countFrom (a + inc) inc)
163166
164167syntax "[" [start] ".." [end] "]"
lib/prelude/complex.idrnew file+65
@@ -0,0 +1,65 @@
1+module prelude.complex
2+
3+import builtins
4+
5+
6+------------------------------ Rectangular form
7+
8+infix 6 :+
9+data Complex a = (:+) a a
10+
11+realPart : Complex a -> a
12+realPart (r:+i) = r
13+
14+imagPart : Complex a -> a
15+imagPart (r:+i) = i
16+
17+instance Eq a => Eq (Complex a) where
18+ (==) a b = realPart a == realPart b && imagPart a == imagPart b
19+
20+instance Show a => Show (Complex a) where
21+ show (r:+i) = "("++show r++":+"++show i++")"
22+
23+
24+
25+-- when we have a type class 'Fractional' (which contains Float and Double),
26+-- we can do:
27+{-
28+instance Fractional a => Fractional (Complex a) where
29+ (/) (a:+b) (c:+d) = let
30+ real = (a*c+b*d)/(c*c+d*d)
31+ imag = (b*c-a*d)/(c*c+d*d)
32+ in
33+ (real:+imag)
34+-}
35+
36+
37+
38+------------------------------ Polarform
39+
40+mkPolar : Float -> Float -> Complex Float
41+mkPolar radius angle = radius * cos angle :+ radius * sin angle
42+
43+cis : Float -> Complex Float
44+cis angle = cos angle :+ sin angle
45+
46+magnitude : Complex Float -> Float
47+magnitude (r:+i) = sqrt (r*r+i*i)
48+
49+phase : Complex Float -> Float
50+phase (x:+y) = atan2 y x
51+
52+
53+------------------------------ Conjugate
54+
55+conjugate : Num a => Complex a -> Complex a
56+conjugate (r:+i) = (r :+ (0-i))
57+
58+-- We can't do "instance Num a => Num (Complex a)" because
59+-- we need "abs" which needs "magnitude" which needs "sqrt" which needs Float
60+instance Num (Complex Float) where
61+ (+) (a:+b) (c:+d) = ((a+b):+(c+d))
62+ (-) (a:+b) (c:+d) = ((a-b):+(c-d))
63+ (*) (a:+b) (c:+d) = ((a*c-b*d):+(b*c+a*d))
64+ fromInteger x = (fromInteger x:+0)
65+ abs (a:+b) = (magnitude (a:+b):+0)
lib/prelude/nat.idr+2
@@ -113,6 +113,8 @@ instance Num Nat where
113113 (-) = minus
114114 (*) = mult
115115
116+ abs x = x
117+
116118 fromInteger = fromInteger'
117119 where
118120 %assert_total