public
Created

AwesomePrelude example with associated data families

  • Download Gist
gistfile1.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
{-# 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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.