diff options
Diffstat (limited to 'ch10_10.2-ii.hs')
-rw-r--r-- | ch10_10.2-ii.hs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/ch10_10.2-ii.hs b/ch10_10.2-ii.hs new file mode 100644 index 0000000..d8b1275 --- /dev/null +++ b/ch10_10.2-ii.hs @@ -0,0 +1,18 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE UndecidableInstances #-} + +-- Exercise 10.2-ii +-- Defunctionalize `foldr :: (a -> b -> b) -> b -> [a] -> b`. + +import Data.Kind (Type) + +type Exp a = a -> Type + +type family Eval (e :: Exp a) :: a + +data Foldr :: (a -> b -> Exp b) -> b -> [a] -> Exp b + +type instance Eval (Foldr f z '[]) = z + +type instance Eval (Foldr f z (x ': xs)) = Eval (f x (Eval (Foldr f z xs))) |