, , , .
, generic-church. . , -
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
import Data.Church
data Nat = Z | S Nat
instance ChurchRep Nat
, Nat , Foldable.
GHCi -
*> (toChurch Z) 'a' (const 'b')
'a'
*> (toChurch $ S Z) 'a' (const 'b')
'b'
foldr
*> fromChurch (\a b -> a) :: Nat
Z
*> fromChurch (a b -> b Z) :: Nat
S Z
catamorphisms/folds , generic-church ( Real Soon Now). , , , Nat
type Nat' = forall c. c -> (Nat -> c) -> c
, generalFunc
generalFunc , maybe. toChurchP, generic-church-0.2, 5 , . , cabal update:)
{-# LANGUAGE DeriveGeneric, MultiParamTypeClasses, FlexibleInstances, TypeFamilies, ScopedTypeVariables #-}
module So where
import Data.Church
import GHC.Generics
import Data.Proxy
-- Generalize of const. Allows us to use more than just Either
-- for gmaybe
class GConst a r where
gconst :: r -> a
instance GConst r' r => GConst (a -> r') r where
gconst r = const (gconst r)
instance GConst r r where
gconst = id
gfail :: forall a e s e' r.
(ChurchRep a, GConst e' e, Church a r ~ (e' -> s -> r)) =>
e -> s -> a -> r
gfail e s a = toChurchP (Proxy :: Proxy r) a (gconst e :: e') s
, gconst , , e. -
> gfail id (const 'a') $ Left 'b'
'b'
-XIncoherentInstances (eek!). , , either, maybe.
, - .
*So> gfail True id (Just False)
False
*So> gfail True (id :: Bool -> Bool) Nothing
True
*So> gfail 'a' (const 'b') (Left ())
'a'
*So> gfail 'a' (const 'b') (Right ())
'b'
*So> :set -XDeriveGeneric
*So> data MyMaybe = Fail | Yay Int deriving (Show, Generic)
*So> instance ChurchRep MyMaybe
*So> gfail 'a' (const 'b') (Yay 1)
'b'