Skip to content

Instantly share code, notes, and snippets.

@urso
Created November 7, 2010 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urso/666121 to your computer and use it in GitHub Desktop.
Save urso/666121 to your computer and use it in GitHub Desktop.
AwesomePrelude example with associated data families
{-# 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