Abstract Refinements
A Tricky Problem
Problem
Require context-dependent invariants & summaries for HOFs.
Example
How to summarize iteration-dependence for higher-order loop
?
Problem Is Pervasive
Lets distill it to a simple example...
(First, a few aliases)
68: {-@ type Odd = {v:Int | (v mod 2) = 1} @-}
69: {-@ type Even = {v:Int | (v mod 2) = 0} @-}
Example: maxInt
Compute the larger of two Int
s:
81: maxInt :: Int -> Int -> Int
82: maxInt x y = if y <= x then x else y
Example: maxInt
Has many incomparable refinement types/summaries
93: maxInt :: Nat -> Nat -> Nat
94: maxInt :: Even -> Even -> Even
95: maxInt :: Odd -> Odd -> Odd
Which should we use?
Refinement Polymorphism
maxInt
returns one of its two inputs x
and y
If |
: |
the inputs satisfy a property |
Then |
: |
the output satisfies that property |
Above holds for all properties!
Need to abstract properties over types
Parametric Refinements
Enable quantification over refinements ...
Type says: for any p
that is a property of Int
,
Parametric Refinements
Enable quantification over refinements ...
167: {-@ maxInt :: forall <p :: Int -> Prop>.
168: Int<p> -> Int<p> -> Int<p> @-}
169:
170: maxInt x y = if x <= y then y else x
Key idea: Int<p>
is just \(\reft{v}{\Int}{p(v)}\)
Abstract Refinement is uninterpreted function in SMT logic
Parametric Refinements
186: {-@ maxInt :: forall <p :: Int -> Prop>.
187: Int<p> -> Int<p> -> Int<p> @-}
188:
189: maxInt x y = if x <= y then y else x
Check Implementation via SMT
Parametric Refinements
200: {-@ maxInt :: forall <p :: Int -> Prop>.
201: Int<p> -> Int<p> -> Int<p> @-}
202:
203: maxInt x y = if x <= y then y else x
Check Implementation via SMT
\[\begin{array}{rll}
\ereft{x}{\Int}{p(x)},\ereft{y}{\Int}{p(y)} & \vdash \reftx{v}{v = y} & \subty \reftx{v}{p(v)} \\
\ereft{x}{\Int}{p(x)},\ereft{y}{\Int}{p(y)} & \vdash \reftx{v}{v = x} & \subty \reftx{v}{p(v)} \\
\end{array}\]
Parametric Refinements
221: {-@ maxInt :: forall <p :: Int -> Prop>.
222: Int<p> -> Int<p> -> Int<p> @-}
223:
224: maxInt x y = if x <= y then y else x
Check Implementation via SMT
\[\begin{array}{rll}
{p(x)} \wedge {p(y)} & \Rightarrow {v = y} & \Rightarrow {p(v)} \\
{p(x)} \wedge {p(y)} & \Rightarrow {v = x} & \Rightarrow {p(v)} \\
\end{array}\]
Using Abstract Refinements
If we call maxInt
with args satisfying common property,
Then p
instantiated property, result gets same property.
Infer Instantiation by Liquid Typing
At call-site, instantiate
p
with unknown
\(\kvar{p}\) and solve!
Recap
- Refinements: Types + Predicates
- Subtyping: SMT Implication
- Measures: Strengthened Constructors
- Abstract Refinements over Types
Abstract Refinements decouple invariants from code ...
[continue]