Data Types
Example: Lists
Lets define our own List
data type:
Specifying the size of a List
Measure
Haskell function with a single equation per constructor
Specifying the size of a List
Measure
Strengthens type of data constructor
data List a where
Emp :: {v:List a | size v = 0}
(:::) :: x:a -> xs:List a -> {v:List a|size v = 1 + size xs}
Using Measures
Exercise: Partial Functions
Fear head
and tail
no more!
Q: Write types for head
and tail
that verify impossible
.
Naming Non-Empty Lists
A convenient type alias
A Useful Partial Function: Fold / Reduce
Fold or Reduce f
over any list using seed acc
A Useful Partial Function: Fold / Reduce
Fold or Reduce f
over non-empty list using first element as seed
Q: How shall we fix foldr1
?
Exercise: average
Q: What is a safe input type for average'
?
Refining Data Types
Make illegal states unrepresentable
Example: Year is 12 Months
Legal Values: Lists of 12
elements, e.g.
"jan" ::: "feb" ::: ... ::: "dec" ::: Emp"
Example: Year is 12 Months
Refine Type to Legal Values
Lists Of A Given Size
Make illegal states unrepresentable
Exercise: map
Q: Can you fix map
to verify tempAverage
?
Exercise: init
Q: Can you fix the type of init
so that sanDiegoTemp
is accepted?
Exercise: init'
Q: For bonus points, fix init'
so sanDiegoTemp'
is accepted?
Recap
Refinements: | Types + Predicates |
Specification: | Refined Input/Output Types |
Verification: | SMT-based Predicate Subtyping |
Measures: | Specify Properties of Data |
Case Study: Insertion Sort