Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Haskell instances for all AwesomePrelude type classes.

  • Loading branch information...
commit ba5261ff11d7bd288fbe772a4012fcb0275f5a5c 1 parent 33eae5e
Tom Lokhorst authored February 07, 2010
2  src/Generic/Data/Num.hs
@@ -7,14 +7,12 @@ import qualified Prelude
7 7
 
8 8
 infixl 6 +
9 9
 infixl 7 *
10  
-infixl 7 /
11 10
 infixl 6 -
12 11
 
13 12
 class Num j a where
14 13
   (+)         :: j a -> j a -> j a
15 14
   (-)         :: j a -> j a -> j a
16 15
   (*)         :: j a -> j a -> j a
17  
-  (/)         :: j a -> j a -> j a
18 16
   fromInteger :: Prelude.Integer -> j a
19 17
 
20 18
 -- Terrible hack to get number literals working.
45  src/Lang/Haskell.hs
@@ -2,7 +2,6 @@
2 2
 
3 3
 module Lang.Haskell where
4 4
 
5  
-import Prelude ((++))
6 5
 import qualified Prelude as P
7 6
 
8 7
 import Generic.Prelude
@@ -11,6 +10,7 @@ type family H a :: *
11 10
 
12 11
 newtype Haskell a = Hs { runHaskell :: H a }
13 12
 
  13
+
14 14
 -- * Haskell instances for AwesomePrelude 'data types'.
15 15
 
16 16
 instance NameC Haskell where
@@ -26,7 +26,46 @@ instance FunC Haskell where
26 26
 type instance H Bool = P.Bool
27 27
 
28 28
 instance BoolC Haskell where
29  
-  false = Hs P.False
30  
-  true  = Hs P.True
  29
+  false           = Hs P.False
  30
+  true            = Hs P.True
31 31
   bool x y (Hs b) = if b then y else x
32 32
 
  33
+type instance H (Maybe a) = P.Maybe (H a)
  34
+
  35
+instance MaybeC Haskell where
  36
+  nothing           = Hs P.Nothing
  37
+  just (Hs x)       = Hs (P.Just x)
  38
+  maybe n f (Hs mx) = P.maybe n (\x -> f (Hs x)) mx
  39
+
  40
+type instance H (a, b) = (H a, H b)
  41
+
  42
+instance TupleC Haskell where
  43
+  mkTuple (Hs x) (Hs y) = Hs (x, y)
  44
+  tuple f (Hs (x, y))   = f (Hs x) (Hs y)
  45
+
  46
+type instance H (Either a b) = P.Either (H a) (H b)
  47
+
  48
+instance EitherC Haskell where
  49
+  left  (Hs x)      = Hs (P.Left x)
  50
+  right (Hs y)      = Hs (P.Right y)
  51
+  either l r (Hs e) = P.either (\x -> l (Hs x)) (\y -> r (Hs y)) e
  52
+
  53
+type instance H [a] = [H a]
  54
+
  55
+instance ListC Haskell where
  56
+  nil                 = Hs []
  57
+  cons (Hs x) (Hs xs) = Hs (x:xs)
  58
+  list n c (Hs xs)    = case xs of { [] -> n; y:ys -> c (Hs y) (Hs ys) }
  59
+
  60
+
  61
+-- * Haskell instances of AwesomePrelude type classes.
  62
+
  63
+instance (P.Num a) => Num Haskell a where
  64
+  (+) = (P.+)
  65
+  (-) = (P.-)
  66
+  (*) = (P.*)
  67
+  fromInteger = P.fromInteger
  68
+
  69
+instance (P.Eq a) => Eq Haskell a where
  70
+  x == y = if x P.== y then true else false
  71
+
19  src/Lang/JavaScript.hs
@@ -11,6 +11,7 @@ import Lang.Value
11 11
 data JS
12 12
 type JavaScript a = Val JS a
13 13
 
  14
+
14 15
 -- * JavaScript instances for AwesomePrelude 'data types'.
15 16
 
16 17
 instance NameC (Val JS) where
@@ -26,15 +27,6 @@ instance BoolC (Val JS) where
26 27
   true       = Con "true"
27 28
   bool x y z = fun3 "bool" (\[f, t, b] -> b ++ " ? " ++ t ++ "(/*force*/) : " ++ f ++ "(/*force*/)") (lam (const x)) (lam (const y)) z
28 29
 
29  
-data Number
30  
-
31  
-instance Num (Val JS) Number where
32  
-  (+) = fun2 "add" (\[a, b] -> a ++ " + " ++ b)
33  
-  (-) = fun2 "sub" (\[a, b] -> a ++ " - " ++ b)
34  
-  (*) = fun2 "mul" (\[a, b] -> a ++ " * " ++ b)
35  
-  (/) = fun2 "div" (\[a, b] -> a ++ " / " ++ b)
36  
-  fromInteger x = Con (P.show x)
37  
-
38 30
 instance MaybeC (Val JS) where
39 31
   nothing   = Con "{ nothing : 1 }"
40 32
   just      = fun1 "just" (\[x] -> "{ just : " ++ x ++ " }")
@@ -54,8 +46,17 @@ instance ListC (Val JS) where
54 46
   cons        = fun2 "cons" (\[x, xs] -> "{ head : " ++ x ++ ", tail : " ++ xs ++ " }")
55 47
   list b f    = fun3 "list" (\[n, c, xs] -> xs ++ ".nil ? " ++ n ++ " : " ++ c ++ "(" ++ xs ++ ".head)(" ++ xs ++ ".tail)") b (lam2 f)
56 48
 
  49
+
57 50
 -- * JavaScript instances of AwesomePrelude type classes.
58 51
 
  52
+data Number
  53
+
  54
+instance Num (Val JS) Number where
  55
+  (+) = fun2 "add" (\[a, b] -> a ++ " + " ++ b)
  56
+  (-) = fun2 "sub" (\[a, b] -> a ++ " - " ++ b)
  57
+  (*) = fun2 "mul" (\[a, b] -> a ++ " * " ++ b)
  58
+  fromInteger x = Con (P.show x)
  59
+
59 60
 instance Eq (Val JS) Bool where
60 61
   (==) = fun2 "eq"  (\[a, b] -> a ++ " == " ++ b)
61 62
   (/=) = fun2 "neq" (\[a, b] -> a ++ " /= " ++ b)

0 notes on commit ba5261f

Please sign in to comment.
Something went wrong with that request. Please try again.