Haskell extensions
Posted: 2016-12-25 , Modified: 2016-12-25
Tags: haskell, functional programming
Posted: 2016-12-25 , Modified: 2016-12-25
Tags: haskell, functional programming
a ~ Int.Constraint.Anything declared to have type constraint:
type family Typ a b :: Constraint
type instance Typ Int b = Show b
type instance Typ Bool b = Num b
func :: Typ a b => a -> b -> b
func = ...type Stringy a = (Read a, Show a)Nat goes from a type to a kind and Ze goes from a value of type Nat, to a type of kind Nat.data Nat = Ze | Su Nat data Vec :: * -> Nat -> * where Nil :: Vec a Ze Cons :: a -> Vec a n -> Vec a (Su n)class Mult a b c | a b -> cclass Extract container elem | container -> elemExample:
data List a where
Nil :: List a
Cons :: a -> List a -> List aExample that can’t be done with regular ADTs:
data Expr a where
I :: Int -> Expr Int
B :: Bool -> Expr Bool
Add :: Expr Int -> Expr Int -> Expr Int
Mul :: Expr Int -> Expr Int -> Expr Int
Eq :: Expr Int -> Expr Int -> Expr Bool>> and return in order to make do notation do - basically - whatever we want it to!Example: composition
(>>) = flip (.)
return = id(>>) = (<=<)Example:
setBind :: Ord b => Set a -> (a -> Set b) -> Set bdata Record = Record {a :: A, b :: b} and a function f r = g (a w) (b w) you can save writing the w by: f Record{..} = g a b.f r@Record{..} =Create a Record field by field
do
a <- ...
b <- ...
return Record{..}Allow nonempty contexts when deriving
data T m = MkT (m Int)
deriving instance Eq (m Int) => Eq (T m)data a + b = Plus a b.import M( type (+)).