Structural Induction
How we express and prove properties on data types?
The list data type
A user defined list,
with its anchored size function.
Reflection of ADTs into the logic
The Liquid pragma
Automatically creates checker and selector measures:
isN :: L a -> Bool
isC :: L a -> Bool
select_C_1 :: L a -> a
select_C_2 :: L a -> L a
Question: Do these function types look familiar?
Reflection of Structural Inductive Functions
With the above measures,
map
reflects into logic! The body of
map
reflects in its result type map :: f:(a->b) -> xs:L a
-> {v:L a | v == map f xs
&& v == if isN xs then N else
C (f (select_C_1 xs)
(map f (select_C_2 xs))
}
Prove fancy lists properties
- Functor Laws
- Identity:
map id == id
- Distribution:
map (compose f g) == compose (map f) (map g)
- Identity:
- Monoid Laws
- Left Identity:
append empty x == x
- Right Identity:
append x empty == x
- Associativity:
append xs (append ys zs) == append (append xs ys) zs
- Left Identity:
- Monad Laws
- Left Identity:
bind (return x) f == f x
- Right Identity:
bind x return == x
- Associativity:
bind (bind m f) g == bind m (\x:a -> (bind (f x) g))
- Left Identity:
Recap
Termination: | Well-founded Metrics |
Reflection: | Allow Haskell functions in Logic |
tructural Induction: | Proving Theorems on Lists |
Next: Case Study: MapReduce: Program Properties that matter!
Appendix: Reflection of Non Recursive Functions
Non-recursive functions reflect too!
Get automatically the "singleton" Liquid Types:
id :: x:a -> {v:a | v == id x && v == x}
compose :: f:(b -> c) -> g:(a -> b) -> x:a
-> {v:c | v == compose f g x && v == f (g x)}
Proving Map-Identity
Optimization property: to map
identity do not transverse the list!
Proof by case splitting and recursive call.
Automation: Proving Map-Identity
Pretty Verbose Proof: Proof Automation!
Proof Generation:
- Automatic Unfolding, but
- Manual case splitting.
Proving Map-Fusion
Optimization property: transverse the list only once!
Exercise: Can you prove map-fusion?
Onto Monoid Laws
Reflect the monoid list operators
Monoid Laws: Left Identity
Lets prove the left identity monoid law!
Monoid Laws: Right Identity
Lets prove the right identity monoid law!
Monoid Laws: Associativity
Lets prove the associativity monoid law!
Onto Monad Laws!
Define monad list operators
Monoid Laws: Left Identity
Lets prove the left identity monad law!
Monoid Laws: Right Identity
Lets prove the right identity monad law!
Monoid Laws: Associativity
To prove associativity, lets assume a helper lemma!
- Bind distribution
Monoid Laws: Associativity
Lets prove the associativity monad law!