Created
November 7, 2010 13:27
-
-
Save urso/666121 to your computer and use it in GitHub Desktop.
AwesomePrelude example with associated data families
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TypeSynonymInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE TypeFamilies #-} | |
module MyLang where | |
import Prelude (($),(.)) | |
import qualified Prelude as P | |
import Generic.Data.Bool | |
import Generic.Data.Num | |
newtype MyLang a = ML {unML :: a} | |
type Bool = MyLang (TBool MyLang) | |
type Number a = MyLang (TNum MyLang a) | |
type Integer = Number P.Integer | |
instance BoolC MyLang where | |
data TBool MyLang = MyTrue | MyFalse | |
true = ML MyTrue | |
false = ML MyFalse | |
bool t _ (ML MyTrue) = t | |
bool _ e (ML MyFalse) = e | |
instance (P.Num a) => Num MyLang a where | |
data TNum MyLang a = MyNumber a | |
fromInteger = ML . MyNumber . P.fromInteger | |
abs = liftNum P.abs | |
signum = liftNum P.signum | |
(+) = liftNum2 (P.+) | |
(-) = liftNum2 (P.-) | |
(*) = liftNum2 (P.*) | |
negate = liftNum P.negate | |
liftNum f (ML (MyNumber a)) = ML $ MyNumber $ f a | |
liftNum2 f (ML (MyNumber a)) (ML (MyNumber b)) = ML $ MyNumber $ f a b | |
test :: Integer | |
test = if' true (3 + 4) (4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment