Permalink
Browse files

Haskell instances for all AwesomePrelude type classes.

  • Loading branch information...
1 parent 33eae5e commit ba5261ff11d7bd288fbe772a4012fcb0275f5a5c @tomlokhorst committed Feb 7, 2010
Showing with 52 additions and 14 deletions.
  1. +0 −2 src/Generic/Data/Num.hs
  2. +42 −3 src/Lang/Haskell.hs
  3. +10 −9 src/Lang/JavaScript.hs
View
2 src/Generic/Data/Num.hs
@@ -7,14 +7,12 @@ import qualified Prelude
infixl 6 +
infixl 7 *
-infixl 7 /
infixl 6 -
class Num j a where
(+) :: j a -> j a -> j a
(-) :: j a -> j a -> j a
(*) :: j a -> j a -> j a
- (/) :: j a -> j a -> j a
fromInteger :: Prelude.Integer -> j a
-- Terrible hack to get number literals working.
View
45 src/Lang/Haskell.hs
@@ -2,7 +2,6 @@
module Lang.Haskell where
-import Prelude ((++))
import qualified Prelude as P
import Generic.Prelude
@@ -11,6 +10,7 @@ type family H a :: *
newtype Haskell a = Hs { runHaskell :: H a }
+
-- * Haskell instances for AwesomePrelude 'data types'.
instance NameC Haskell where
@@ -26,7 +26,46 @@ instance FunC Haskell where
type instance H Bool = P.Bool
instance BoolC Haskell where
- false = Hs P.False
- true = Hs P.True
+ false = Hs P.False
+ true = Hs P.True
bool x y (Hs b) = if b then y else x
+type instance H (Maybe a) = P.Maybe (H a)
+
+instance MaybeC Haskell where
+ nothing = Hs P.Nothing
+ just (Hs x) = Hs (P.Just x)
+ maybe n f (Hs mx) = P.maybe n (\x -> f (Hs x)) mx
+
+type instance H (a, b) = (H a, H b)
+
+instance TupleC Haskell where
+ mkTuple (Hs x) (Hs y) = Hs (x, y)
+ tuple f (Hs (x, y)) = f (Hs x) (Hs y)
+
+type instance H (Either a b) = P.Either (H a) (H b)
+
+instance EitherC Haskell where
+ left (Hs x) = Hs (P.Left x)
+ right (Hs y) = Hs (P.Right y)
+ either l r (Hs e) = P.either (\x -> l (Hs x)) (\y -> r (Hs y)) e
+
+type instance H [a] = [H a]
+
+instance ListC Haskell where
+ nil = Hs []
+ cons (Hs x) (Hs xs) = Hs (x:xs)
+ list n c (Hs xs) = case xs of { [] -> n; y:ys -> c (Hs y) (Hs ys) }
+
+
+-- * Haskell instances of AwesomePrelude type classes.
+
+instance (P.Num a) => Num Haskell a where
+ (+) = (P.+)
+ (-) = (P.-)
+ (*) = (P.*)
+ fromInteger = P.fromInteger
+
+instance (P.Eq a) => Eq Haskell a where
+ x == y = if x P.== y then true else false
+
View
19 src/Lang/JavaScript.hs
@@ -11,6 +11,7 @@ import Lang.Value
data JS
type JavaScript a = Val JS a
+
-- * JavaScript instances for AwesomePrelude 'data types'.
instance NameC (Val JS) where
@@ -26,15 +27,6 @@ instance BoolC (Val JS) where
true = Con "true"
bool x y z = fun3 "bool" (\[f, t, b] -> b ++ " ? " ++ t ++ "(/*force*/) : " ++ f ++ "(/*force*/)") (lam (const x)) (lam (const y)) z
-data Number
-
-instance Num (Val JS) Number where
- (+) = fun2 "add" (\[a, b] -> a ++ " + " ++ b)
- (-) = fun2 "sub" (\[a, b] -> a ++ " - " ++ b)
- (*) = fun2 "mul" (\[a, b] -> a ++ " * " ++ b)
- (/) = fun2 "div" (\[a, b] -> a ++ " / " ++ b)
- fromInteger x = Con (P.show x)
-
instance MaybeC (Val JS) where
nothing = Con "{ nothing : 1 }"
just = fun1 "just" (\[x] -> "{ just : " ++ x ++ " }")
@@ -54,8 +46,17 @@ instance ListC (Val JS) where
cons = fun2 "cons" (\[x, xs] -> "{ head : " ++ x ++ ", tail : " ++ xs ++ " }")
list b f = fun3 "list" (\[n, c, xs] -> xs ++ ".nil ? " ++ n ++ " : " ++ c ++ "(" ++ xs ++ ".head)(" ++ xs ++ ".tail)") b (lam2 f)
+
-- * JavaScript instances of AwesomePrelude type classes.
+data Number
+
+instance Num (Val JS) Number where
+ (+) = fun2 "add" (\[a, b] -> a ++ " + " ++ b)
+ (-) = fun2 "sub" (\[a, b] -> a ++ " - " ++ b)
+ (*) = fun2 "mul" (\[a, b] -> a ++ " * " ++ b)
+ fromInteger x = Con (P.show x)
+
instance Eq (Val JS) Bool where
(==) = fun2 "eq" (\[a, b] -> a ++ " == " ++ b)
(/=) = fun2 "neq" (\[a, b] -> a ++ " /= " ++ b)

0 comments on commit ba5261f

Please sign in to comment.