git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update prelude classes
commit9bda0e4f82fe11fe34406c72cc6d76f6fec181ea authorGary Burgess <gary.burgess@gmail.com> dateSun 15 Feb 2015, 01:33 (12 years ago) parent9aa8b92
1 file changed+43−27
prelude/prelude.purs+43−27
prelude/prelude.purs+43−27
@@ -13,8 +13,12 @@ module Prelude
1313 , Applicative, pure, liftA1
1414 , Bind, (>>=)
1515 , Monad, return, liftM1, ap
16− , Num, (+), (-), (*), (/), (%)
16+ , Semiring, (+), zero, (*), one
17+ , Ring, (-)
18+ -- , (%)
1719 , negate
20+ , DivisionRing, (/)
21+ , Num
1822 , Eq, (==), (/=), refEq, refIneq
1923 , Ord, Ordering(..), compare, (<), (>), (<=), (>=)
2024 , Bits, (.&.), (.|.), (.^.), shl, shr, zshr, complement
@@ -24,28 +28,28 @@ module Prelude
2428 , Unit(..), unit
2529 ) where
2630
27− -- | An alias for `true`, which can be useful in guard clauses:
28− -- |
31+ -- | An alias for `true`, which can be useful in guard clauses:
32+ -- |
2933 -- | E.g.
30− -- |
31− -- | max x y | x >= y = x
34+ -- |
35+ -- | max x y | x >= y = x
3236 -- | | otherwise = y
3337 otherwise :: Boolean
3438 otherwise = true
3539
36− -- | Flips the order of the arguments to a function of two arguments.
40+ -- | Flips the order of the arguments to a function of two arguments.
3741 flip :: forall a b c. (a -> b -> c) -> b -> a -> c
3842 flip f b a = f a b
3943
40− -- | Returns its first argument and ignores its second.
44+ -- | Returns its first argument and ignores its second.
4145 const :: forall a b. a -> b -> a
4246 const a _ = a
4347
4448 -- | This function returns its first argument, and can be used to assert type equalities.
45− -- | This can be useful when types are otherwise ambiguous.
46− -- |
49+ -- | This can be useful when types are otherwise ambiguous.
50+ -- |
4751 -- | E.g.
48− -- |
52+ -- |
4953 -- | main = print $ [] `asTypeOf` [0]
5054 -- |
5155 -- | If instead, we had written `main = print []`, the type of the argument `[]` would have
@@ -200,18 +204,31 @@ module Prelude
200204
201205 infixl 7 *
202206 infixl 7 /
203− infixl 7 %
207+ -- infixl 7 %
204208
205209 infixl 6 -
206210 infixl 6 +
207211
208− class Num a where
209− (+) :: a -> a -> a
212+ -- | Addition and multiplication
213+ class Semiring a where
214+ (+) :: a -> a -> a
215+ zero :: a
216+ (*) :: a -> a -> a
217+ one :: a
218+
219+ -- | Addition, multiplication, and subtraction
220+ class (Semiring a) <= Ring a where
210221 (-) :: a -> a -> a
211− (*) :: a -> a -> a
222+
223+ negate :: forall a. (Ring a) => a -> a
224+ negate a = zero - a
225+
226+ -- | Ring with division (possibly non-commutative field)
227+ class (Ring a) <= DivisionRing a where
212228 (/) :: a -> a -> a
213− (%) :: a -> a -> a
214− negate :: a -> a
229+
230+ -- | Definitely commutative field
231+ class (DivisionRing a) <= Num a
215232
216233 foreign import numAdd
217234 """
@@ -258,20 +275,19 @@ module Prelude
258275 }
259276 """ :: Number -> Number -> Number
260277
261− foreign import numNegate
262− """
263− function numNegate(n) {
264− return -n;
265− }
266− """ :: Number -> Number
267−
268− instance numNumber :: Num Number where
278+ instance semiringNumber :: Semiring Number where
269279 (+) = numAdd
270− (-) = numSub
280+ zero = 0
271281 (*) = numMul
282+ one = 1
283+
284+ instance ringNumber :: Ring Number where
285+ (-) = numSub
286+
287+ instance divisionRingNumber :: DivisionRing Number where
272288 (/) = numDiv
273− (%) = numMod
274− negate = numNegate
289+
290+ instance numNumber :: Num Number
275291
276292 newtype Unit = Unit {}
277293