aboutsummaryrefslogtreecommitdiff
path: root/ch10_10.2-ii.hs
blob: d8b1275e5cec126e1725b1b12b39370a6c3b48bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)))