As a minimal example of the problem I'm having, here's a definition of natural numbers, a doubling function, and a type refined by an even-ness predicate:
data Nat' = Z | S Nat' deriving Show
{-@ reflect double' @-}
double' :: Nat' -> Nat'
double' Z = Z
double' (S x) = (S (S (double' x)))
{-@ type Even' = {v:Nat' | even' v} @-}
{-@ reflect even' @-}
even' :: Nat' -> Bool
even' Z = True
even' (S Z) = False
even' (S (S x)) = even' x
I'd like to first declare {-@ double' :: Nat' -> Even' @-}
and then prove this to be true, but I'm under the impression that I instead must first write the proof and then use castWithTheorem
(which itself has worked for me) as such:
{-@ even_double :: x:Nat' -> {even' (double' x)} @-}
even_double Z = even' (double' Z)
==. even' Z
==. True
*** QED
even_double (S x) = even' (double' (S x))
==. even' (S (S (double' x)))
==. even' (double' x)
? even_double x
==. True
*** QED
{-@ double :: Nat' -> Even' @-}
double x = castWithTheorem (even_double x) (double' x)
However, this gives fairly illegible errors like:
:1:1-1:1: Error
elaborate solver elabBE 177 "lq_anf$##7205759403792806976##d3tK" {lq_tmp$x##1556 : (GHC.Types.$126$$126$ (GHC.Prim.TYPE GHC.Types.LiftedRep) (GHC.Prim.TYPE GHC.Types.LiftedRep) bool bool) | [(lq_tmp$x##1556 = GHC.Types.Eq#)]} failed on:
lq_tmp$x##1556 == GHC.Types.Eq#
with error
Cannot unify (GHC.Types.$126$$126$ (GHC.Prim.TYPE GHC.Types.LiftedRep) (GHC.Prim.TYPE GHC.Types.LiftedRep) bool bool) with func(0 , [(GHC.Prim.$126$$35$ @(42) @(43) @(44) @(45));
(GHC.Types.$126$$126$ @(42) @(43) @(44) @(45))]) in expression: lq_tmp$x##1556 == GHC.Types.Eq#
because
Elaborate fails on lq_tmp$x##1556 == GHC.Types.Eq#
in environment
GHC.Types.Eq# := func(4 , [(GHC.Prim.$126$$35$ @(0) @(1) @(2) @(3));
(GHC.Types.$126$$126$ @(0) @(1) @(2) @(3))])
lq_tmp$x##1556 := (GHC.Types.$126$$126$ (GHC.Prim.TYPE GHC.Types.LiftedRep) (GHC.Prim.TYPE GHC.Types.LiftedRep) bool bool)
What am I doing wrong? From my experiments, it seems to be caused by trying to prove that some predicate function is true of some argument.
The issue was that I should have been using
NewProofCombinators
instead ofProofCombinators
. Then replacing==.
with===
andcastWithTheorem (even_double x) (double' x)
with(double' x) `withProof` (even_double x)
fixes the problem: http://goto.ucsd.edu:8090/index.html#?demo=permalink%2F1543595949_5844.hsAll the online resources I've found use
ProofCombinators
so hopefully this saves someone some pain.Source: https://github.com/ucsd-progsys/liquidhaskell/issues/1378#issuecomment-443262472