-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPS.curry
206 lines (168 loc) · 6.51 KB
/
CPS.curry
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
module CPS where
import Float
import FiniteMap
import SetFunctions
type Prob = Float
data VC a = V a | C (() -> PV a)
type PV a = (Prob, VC a)
pvUnit :: a -> PV a
pvUnit x = (1.0, V x)
pvBind :: PV a -> (a -> PV b) -> PV b
pvBind (p, V x) f = (p, C (\ () -> f x))
pvBind (p, C t) f = (p, C (\ () -> pvBind (t ()) f))
type PM a = (a -> PV Bool) -> PV Bool
type Arr a b = a -> (b -> PV Bool) -> PV Bool
b :: Bool -> PM Bool
b x = \k -> k x
dist :: [(Prob,a)] -> PM a
dist ch = \k -> foldr1 (?) (map (\ (p,v) -> (p, C (\ () -> k v))) ch)
{-
flip 0.5
=
dist [(0.5,True), (1 -. 0.5, False)]
=
dist [(0.5,True), (0.5, False)]
=
\k -> foldr1 (?) (map (\ (p,v) (p, C (\ () -> k v))) [(0.5,True), (0.5, False)])
=
\k -> foldr1 (?) [(0.5, C (\ () -> k True)),(0.5, C (\ () -> k False))]
=
\k -> (0.5, C (\ () -> k True)) ? (0.5, C (\ () -> k False))
reify0 (flip 0.5)
=
flip 0.5 pvUnit
=
(\k -> (0.5, C (\ () -> k True)) ? (0.5, C (\ () -> k False))) pvUnit
=
(0.5, C (\ () -> pvUnit True)) ? (0.5, C (\ () -> pvUnit False))
-}
neg :: PM Bool -> PM Bool
neg e = \k -> e (\v -> k (not v))
con :: PM Bool -> PM Bool -> PM Bool
con e1 e2 = \k -> e1 (\v1 -> if v1 then e2 k else b False k)
dis :: PM Bool -> PM Bool -> PM Bool
dis e1 e2 = \k -> e1 (\v1 -> if v1 then b True k else e2 k)
if_ :: PM Bool -> (() -> PM a) -> (() -> PM a) -> PM a
if_ et e1 e2 = \k -> et (\t -> if t then e1 () k else e2 () k)
lam :: (PM a -> PM b) -> PM (Arr a b)
lam e = \k -> k (\x -> e (\k2 -> k2 x))
app :: PM (Arr a b) -> PM a -> PM b
app e1 e2 = \k -> e1 (\f -> e2 (\x -> f x k))
reify0 :: PM Bool -> PV Bool
reify0 m = m pvUnit
-----------------
-- Exploration --
-----------------
-- explore :: Maybe Int -> PV a -> PV a
-- explore maxdepth cs =
-- let loop :: Float -> Int -> Bool -> PV a -> (FM a Float, PV a) -> (FM a Float, PV a)
-- loop p depth down choices answers@(ans, susp) =
-- case choices of
-- [] -> answers
-- (pt, V v) : rest -> loop p depth down rest (addToFM_C (+.) ans v (pt *. p), susp)
-- (pt, C t) : rest | down ->
-- let down' = maybe True (depth <) maxdepth
-- in loop p depth down rest (loop (pt *. p) (depth + 1) down' (t ()) answers)
-- (pt, c) : rest -> loop p depth down rest (ans, (pt *. p, c) ? susp)
-- (ans1,susp1) = loop 1.0 0 True cs (emptyFM (<), [])
-- in foldFM (\v p a -> (p, V v) ? a) susp1 ans1
explore :: Maybe Int -> [PV a] -> IO [PV a]
explore maxdepth cs =
let loop :: Float -> Int -> Bool -> [PV a] -> (FM a Float, [PV a]) -> IO (FM a Float, [PV a])
loop p depth down choices answers@(ans, susp) =
case choices of
[] -> return answers
(pt, V v) : rest -> loop p depth down rest (addToFM_C (+.) ans v (pt *. p), susp)
(pt, C t) : rest | down ->
let down' = maybe True (depth <) maxdepth
in values2list (set1 t ()) >>= \cs' -> loop (pt *. p) (depth + 1) down' cs' answers >>= loop p depth down rest
(pt, c) : rest -> loop p depth down rest (ans, (pt *. p, c) : susp)
in loop 1.0 0 True cs (emptyFM (<), []) >>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
{-
values2list (set1 reify0 (flip 0.5)) >>= explore (Just 1)
=
explore (Just 1) [(0.5, C (\ () -> pvUnit True)),(0.5, C (\ () -> pvUnit False))]
=
loop 1.0 0 True [(0.5, C (\ () -> pvUnit True)),(0.5, C (\ () -> pvUnit False))] (emptyFM (<), [])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
(values2list (set1 (\ () -> pvUnit True) ()) >>= \cs' -> loop (0.5 * 1.0) (0+1) (0 < 1) cs' (emptyFM (<), [])
>>= loop 1.0 0 True [(0.5, C (\ () -> pvUnit False))])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
(loop 0.5 1 True [(1.0, V True)] (emptyFM (<), [])
>>= loop 1.0 0 True [(0.5, C (\ () -> pvUnit False))])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
(loop 0.5 1 True [] (addToFM_C (+.) (emptyFM (<)) 1.0 0.5, [])
>>= loop 1.0 0 True [(0.5, C (\ () -> pvUnit False))])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
(loop 0.5 1 True [] ({True -> 0.5}, [])
>>= loop 1.0 0 True [(0.5, C (\ () -> pvUnit False))])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
loop 1.0 0 True [(0.5, C (\ () -> pvUnit False))] ({True -> 0.5}, [])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
(values2list (set1 (\ () -> pvUnit False) ()) >>= \cs' -> loop (0.5 *. 1.0) (0 + 1) (0 < 1) cs' ({True -> 0.5}, [])
>>= loop 1.0 0 True [])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
(loop 0.5 1 True [(1.0,V False)] ({True -> 0.5}, [])
>>= loop 1.0 0 True [])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
(loop 0.5 1 True [] ({False -> 0.5,True -> 0.5}, [])
>>= loop 1.0 0 True [])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
loop 1.0 0 True [] ({False -> 0.5,True -> 0.5}, [])
>>= \ (ans1,susp1) -> return (foldFM (\v p a -> (p, V v) : a) susp1 ans1)
=
return (foldFM (\v p a -> (p, V v) : a) [] {False -> 0.5, True -> 0.5})
=
[(0.5, V False), (0.5, V True)]
-}
-----------------
-- LAWN Model --
-----------------
flip :: Float -> PM Bool
flip p = dist [(p,True), (1 -. p, False)]
let_ :: PM a -> (PM a -> PM b) -> PM b
let_ e f = app (lam f) e
grassModel :: PM Bool
grassModel =
let_ (flip 0.3) (\rain ->
let_ (flip 0.5) (\sprinkler ->
let_ (dis (con (flip 0.9) rain)
(dis (con (flip 0.8) sprinkler)
(flip 0.1))) (\grassIsWet ->
if_ grassIsWet (const rain) (const (dist [])))))
scp1 :: PV Bool
scp1 = reify0 (grassModel)
scpe1 :: IO [PV Bool]
scpe1 = values2list (set1 reify0 grassModel) >>= explore (Just 1)
scpe4 :: IO [PV Bool]
scpe4 = values2list (set1 reify0 grassModel) >>= explore (Just 4)
-- uniform :: [a] -> PM a
-- uniform xs = uniformInterval (foldr1 (?) xs) count
-- where
-- count = length xs
-- uniformInterval :: a -> Int -> PM a
-- uniformInterval val p = dist [(1.0 /. (i2f p), val)]
-- birthday :: Int -> PM Int
-- birthday = \_ -> uniform [1..366]
-- pairEqual :: Int -> Int -> Int -> PM Bool
-- pairEqual max p1 p2 =
-- let_ (birthday p1) (\b1 ->
-- let_ (birthday p2) (\b2 ->
-- if p1 > max
-- then b False
-- else if p2 > max
-- then pairEqual max (p1 + 1) (p1 + 2)
-- else if (b1 <==> b2)
-- then b True
-- else pairEqual max p1 (p2 + 1)))
-- (<==>) :: PM a -> PM a -> Bool
-- x <==> y = snd (reify0 x) == snd (reify0 y)