forked from ucsd-progsys/liquidhaskell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemigroup_desugared.hs
More file actions
157 lines (127 loc) · 3.47 KB
/
semigroup_desugared.hs
File metadata and controls
157 lines (127 loc) · 3.47 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{-@ LIQUID "--reflection" @-}
module Semigroup where
import Prelude hiding (Semigroup(..), mappend)
infixl 3 ?
(?) :: a -> () -> a
x ? _ = x
{-# INLINE (?) #-}
infixl 3 ==.
-- {-@ reflect (==.) @-}
(==.) :: a -> a -> a
_ ==. x = x
{-# INLINE (==.) #-}
data QED = QED
-- {-@ reflect *** @-}
infixl 2 ***
x *** QED = ()
-- ==========
-- Desugaring
-- ==========
{-@
data SemigroupD a = SemigroupD {
sdMappend :: a -> a -> a
, sdLawAssociative :: x : a
-> y : a
-> z : a
-> {sdMappend (sdMappend x y) z = sdMappend x (sdMappend y z)}
}
@-}
data SemigroupD a = SemigroupD {
sdMappend :: a -> a -> a
, sdLawAssociative :: a -> a -> a -> ()
}
{-@ reflect mappendInt @-}
mappendInt :: Int -> Int -> Int
mappendInt a b = a + b
-- {-@ reflect semigroupInt @-}
-- {-@ lazy semigroupInt @-}
semigroupInt :: SemigroupD Int
semigroupInt = SemigroupD mappendInt lawAssociativeInt
-- {-@ reflect lawAssociativeInt @-}
{-@ lawAssociativeInt
:: x : Int
-> y : Int
-> z : Int
-> {mappendInt (mappendInt x y) z = mappendInt x (mappendInt y z)}
@-}
lawAssociativeInt x y z =
mappendInt (mappendInt x y) z
==. (x + y) + z
==. x + (y + z)
==. mappendInt x (mappendInt y z)
*** QED
-- JP: Why does the previous work? I expected something like the following:
--
-- {-@ lawAssociativeInt
-- :: x : Int
-- -> y : Int
-- -> z : Int
-- -> {mappend semigroupInt (mappend semigroupInt x y) z = mappend semigroupInt x (mappend semigroupInt y z)}
-- @-}
-- lawAssociativeInt x y z =
-- mappend semigroupInt (mappend semigroupInt x y) z
-- ==. (x + y) + z
-- ==. x + (y + z)
-- ==. mappend semigroupInt x (mappend semigroupInt y z)
-- *** QED
-- ======================================
-- Attempting to use desugared class laws
-- ======================================
{-@ assume lawAssociative
:: d : SemigroupD a
-> x : a
-> y : a
-> z : a
-> {sdMappend d (sdMappend d x y) z = sdMappend d x (sdMappend d y z)}
@-}
lawAssociative :: SemigroupD a -> a -> a -> a -> ()
lawAssociative _ _ _ _ = ()
{-@ testLemma
:: d : SemigroupD a
-> x : a
-> y : a
-> z : a
-> {sdMappend d (sdMappend d x y) z = sdMappend d x (sdMappend d y z) }
@-}
testLemma :: SemigroupD a -> a -> a -> a -> ()
testLemma d x y z = sdLawAssociative d x y z
-- JP: sdLawAssociative doesn't work, but replacing it with the assumed lawAssociative does.
-- {-@ testLemma'
-- :: d : SemigroupD a
-- -> w : a
-- -> x : a
-- -> y : a
-- -> z : a
-- -> {sdMappend d (sdMappend d w x) (sdMappend d y z) = sdMappend d w (sdMappend d x (sdMappend d y z)) }
-- @-}
-- testLemma' :: SemigroupD a -> a -> a -> a -> a -> ()
-- testLemma' d w x y z = lawAssociative d w x (sdMappend d y z)
-- -- sdMappend d (sdMappend d w x) r ? lawAssociative d w x r
-- -- ==. sdMappend d w (sdMappend d x r)
-- -- *** QED
--
-- where
-- r = sdMappend d y z
-- {-@ mappend
-- {-@ assume mappend
-- :: d : SemigroupD a
-- -> x : a
-- -> y : a
-- -> z : {a | z = sdMappend d x y}
-- @-}
--
-- {-@ reflect mappend @-}
-- mappend :: SemigroupD a -> a -> a -> a
-- mappend d x y = sdMappend d x y
--
-- -- {-@ reflect lawAssociative @-}
-- -- {-@ lawAssociative
-- {-@ assume lawAssociative
-- :: d : SemigroupD a
-- -> x : a
-- -> y : a
-- -> z : a
-- -> {mappend d (mappend d x y) z = mappend d x (mappend d y z)}
-- @-}
-- lawAssociative :: SemigroupD a -> a -> a -> a -> ()
-- lawAssociative d x y z = sdLawAssociative d x y z