-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathLec_1_16.lhs
101 lines (70 loc) · 1.99 KB
/
Lec_1_16.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
\begin{code}
{-@ LIQUID "--short-names" @-}
{-# LANGUAGE PartialTypeSignatures #-}
module Lec_1_16 where
import Prelude hiding (sum)
import ProofCombinators
{-@ assert :: {p:Bool | p } -> a -> a @-}
assert :: Bool -> a -> a
assert True x = x
assert False _ = impossible "Yikes assert failed!"
test0 = assert (1 < 10)
{-@ lazy mj @-}
mj :: Int -> Int -> Int
mj x 0 = 1
mj x n = x * mj x (n-1)
{-@ plus :: x:_ -> y:_ -> {v:Int | v = x + y } @-}
plus :: Int -> Int -> Int
plus x y = x + y
test1 x = assert (x < plus x 1)
\end{code}
\begin{code}
-- 1. sum 0 == 0
-- 2. sum n >= 0
tests = [ assert (sum 0 == 0) -- VC 1
, assert (sum 1 == 1)
, assert (sum 2 == 3)
, assert (sum 3 == 6)
]
{-@ theorem_stuff :: _ -> { v: _ | sum 2 == 3 } @-}
theorem_stuff :: _
theorem_stuff _ = sum 2
=== 2 + sum 1
=== 2 + 1 + sum 0
=== 2 + 1 + 0
=== 3
{-@ another_theorem :: _ -> { v: _ | sum 3 == 6 } @-}
another_theorem :: _
another_theorem _ = sum 3
=== 3 + sum 2
? theorem_stuff ()
=== 6
anilTests :: _
anilTests =
[ assert (sum 2 == 2 + sum 1)
, assert (sum 1 == 1 + sum 0)
, assert (sum 0 == 0)
]
{-@ reflect sum @-}
{-@ sum :: n:Int -> {v:Nat | n <= v} @-}
sum :: Int -> Int
sum n = if n <= 0
then 0 -- "base" case
else n + sum (n-1) -- "recursive" case
-- FORALL n. n <= sum n
{-@ theorem_MJ :: n:Int -> { sum n >= n } @-}
theorem_MJ :: Int -> Proof
theorem_MJ n
| n <= 0 = sum n
=>= 0
*** QED
| otherwise = sum n
=>= n + sum (n-1)
=>= n
*** QED
-- {-@ (=>=) :: x:a -> y:{a | x >= y} -> {v:a | v == y} @-}
-- x =>= y = y
-- testMJ n = assert (n <= sum n)
-- (===) :: x:a -> y:{a | y == x} -> {v:a | v == x && v == y}
-- TRUE => (1 + 3 == 4)
\end{code}