-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFix.hs
More file actions
38 lines (28 loc) · 882 Bytes
/
Copy pathFix.hs
File metadata and controls
38 lines (28 loc) · 882 Bytes
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
{-# LANGUAGE GADTs #-}
module Fix where
import Data.Maybe
import KeyM hiding ( apply )
--------------------------------------------------------------------------------
data Box s where
Lock :: Key s a -> a -> Box s
unlock :: Key s a -> Box s -> Maybe a
unlock k (Lock k' x) =
case testEquality k k' of
Just Refl -> Just x
Nothing -> Nothing
--------------------------------------------------------------------------------
data D s a
= Fun (Box s -> D s a)
| Val a
fun :: Key s (D s a) -> (D s a -> D s a) -> D s a
fun k f = Fun (f . fromJust . unlock k)
apply :: Key s (D s a) -> D s a -> D s a -> D s a
apply k (Fun f) x = f (Lock k x)
fix :: (a -> a) -> a
fix f = runKeyM
(do k <- newKey
let f' = Val . f . unVal
wf = fun k (\x -> apply k (fun k f') (apply k x x))
return (unVal (apply k wf wf)))
where
unVal (Val x) = x