Exercises part 3
In these exercises, you are going to implement Foldable
for different data types. Make sure to try and manually implement all six functions of the interface.
-
Implement
Foldable
forCrud i
:data Crud : (i : Type) -> (a : Type) -> Type where Create : (value : a) -> Crud i a Update : (id : i) -> (value : a) -> Crud i a Read : (id : i) -> Crud i a Delete : (id : i) -> Crud i a
-
Implement
Foldable
forResponse e i
:data Response : (e, i, a : Type) -> Type where Created : (id : i) -> (value : a) -> Response e i a Updated : (id : i) -> (value : a) -> Response e i a Found : (values : List a) -> Response e i a Deleted : (id : i) -> Response e i a Error : (err : e) -> Response e i a
-
Implement
Foldable
forList01
. Use tail recursion in the implementations oftoList
,foldMap
, andfoldl
.data List01 : (nonEmpty : Bool) -> Type -> Type where Nil : List01 False a (::) : a -> List01 False a -> List01 ne a
-
Implement
Foldable
forTree
. There is no need to use tail recursion in your implementations, but your functions must be accepted by the totality checker, and you are not allowed to cheat by usingassert_smaller
orassert_total
.Hint: You can test the correct behavior of your implementations by running the same folds on the result of
treeToVect
and verify that the outcome is the same. -
Like
Functor
andApplicative
,Foldable
composes: The product and composition of two foldable container types are again foldable container types. Proof this by implementingFoldable
forComp
andProduct
:record Comp (f,g : Type -> Type) (a : Type) where constructor MkComp unComp : f (g a) record Product (f,g : Type -> Type) (a : Type) where constructor MkProduct fst : f a snd : g a