-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSignalMLState.hs
More file actions
232 lines (196 loc) · 7.75 KB
/
SignalMLState.hs
File metadata and controls
232 lines (196 loc) · 7.75 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-|
Module : Control.SignalRunner.SignalMLState
Description : Runner for general ML-style state (supporting allocation, dereferencing, and assignment)
Copyright : (c) Danel Ahman, 2019
License : MIT
Maintainer : danel.ahman@eesti.ee
Stability : experimental
This module implements a runner that provides general ML-style state
that supports allocation of references, dereferencing references,
and assignment to references.
We allow a large class of Haskell values to be stored in our references,
as long as they are instances of the `Typeable` type class. We use this
restriction to be able to compare the types of references for equality,
so as to be able to define decidable equality for references (`refEq`),
which we in turn use when updating the values stored in the heap.
If one attempts to access a non-existent reference (for dereferencing
or assignment), then the corresponding co-operation is going to send a
(kill) signal, which one can catch with the signal finaliser of `run`.
-}
module Control.SignalRunner.SignalMLState
(
Ref, refEq, MLState(..), S(..), Heap,
alloc, (!), deref, (=:=), assign,
mlRunner, mlInitialiser, mlFinaliserVal, mlFinaliserExc, mlFinaliserSig, mlTopLevel,
Typeable
) where
import Control.SignalRunner
import Data.Typeable
-- | Type of (kill) signals.
data S where
-- | Signal sent when we observe that the given
-- reference is not in the heap during dereferencing.
RefNotInHeapInDerefSignal :: Ref a -> S
-- | Signal sent when we observe that the given
-- reference is not in the heap during assignment.
RefNotInHeapInAssignSignal :: Ref a -> S
instance Show S where
show (RefNotInHeapInDerefSignal r) = "RefNotInHeapInDerefSignal -- " ++ show r
show (RefNotInHeapInAssignSignal r) = "RefNotInHeapInAssignSignal -- " ++ show r
-- | Type of natural numbers that we use for the address of references.
data Nat where
Z :: Nat
S :: Nat -> Nat
instance Eq Nat where
Z == Z = True
(S n) == (S m) = n == m
_ == _ = False
instance Show Nat where
show Z = "Z"
show (S n) = "S " ++ show n
-- | Addresses of references (exposed because we need it to implement @MonotonicMLState@).
type Addr = Nat
-- | Type of references, restricted to only store values
-- of types satisfying the `Typeable` type class.
data Ref a where
R :: (Typeable a) => Addr -> Ref a
instance Eq (Ref a) where
R addr == R addr' = addr == addr'
instance Show (Ref a) where
show r = "ref. with address " ++ show (addrOf r)
-- | Exposing the address of a reference (private to this module).
addrOf :: Ref a -> Addr
addrOf (R r) = r
-- | Decidable equality on references (of possibly different types).
--
-- If the references are deemed to be equal, the equality test also
-- returns a proof that their types are (propositionally) equal.
refEq :: (Typeable a,Typeable b) => Ref a -> Ref b -> Maybe (a :~: b)
refEq (r :: Ref a) (r' :: Ref b) =
if (addrOf r == addrOf r')
then eqT @a @b
else Nothing
-- | Memory is a partial map from references to `Typeable` values.
type Memory = forall a . (Typeable a) => Ref a -> Maybe a
-- | Type of heaps. These comprise a partial map
-- from references to values, and the address of
-- the next fresh reference to be allocated.
data Heap = H { memory :: Memory, nextAddr :: Addr }
-- | Reading the value of a reference in the heap.
--
-- It returns an optional value, depending on whether
-- the reference was present in the heap or not.
heapSel :: (Typeable a) => Heap -> Ref a -> Maybe a
heapSel h r = memory h r
-- | Updating the value of a reference in the memory.
memUpd :: (Typeable a) => Memory -> Ref a -> a -> Memory
memUpd mem r x r' =
case refEq r r' of
Nothing -> mem r'
Just Refl -> Just x
-- | Updatring the value of a reference in the heap.
heapUpd :: (Typeable a) => Heap -> Ref a -> a -> Heap
heapUpd h r x = h { memory = memUpd (memory h) r x }
-- | Allocating a fresh reference in the heap,
-- with the given initial value.
heapAlloc :: (Typeable a) => Heap -> a -> (Ref a,Heap)
heapAlloc h init =
let r = R (nextAddr h) in
(r , H { memory = memUpd (memory h) r init ,
nextAddr = S (nextAddr h) })
-- | An effect for general ML-style state.
data MLState :: * -> * where
-- | Algebraic operation for allocating a fresh reference.
Alloc :: (Typeable a) => a -> MLState (Ref a)
-- | Algebraic operation for dereferencing a reference.
Deref :: (Typeable a) => Ref a -> MLState a
-- | Algebraic operation for assiging a value to a reference.
Assign :: (Typeable a) => Ref a -> a -> MLState ()
-- | Generic effect for allocating a fresh reference.
alloc :: (Typeable a,Member MLState sig) => a -> User sig e (Ref a)
alloc init = performU (Alloc init)
-- | Generic effect for dereferencing a reference.
(!) :: (Typeable a,Member MLState sig) => Ref a -> User sig e a
(!) r = performU (Deref r)
-- | Generic effect for dereferencing a reference (synonym of @(!)@).
deref r = (!) r -- used with qualified module names
-- | Generic effect for assigning a value to a reference.
(=:=) :: (Typeable a,Member MLState sig) => Ref a -> a -> User sig e ()
(=:=) r x = performU (Assign r x)
-- | Generic effect for assigning a value to a reference (synonym of @(=:=)@).
assign r x = r =:= x -- used with qualified module names
-- | The co-operations of the runner `mlRunner`.
mlCoOps :: MLState a -> Kernel sig Zero S Heap a
mlCoOps (Alloc init) =
do h <- getEnv;
(r,h') <- return (heapAlloc h init);
setEnv h';
return r
mlCoOps (Deref r) =
do h <- getEnv;
maybe
(kill (RefNotInHeapInDerefSignal r))
(\ x -> return x)
(heapSel h r)
mlCoOps (Assign r x) =
do h <- getEnv;
maybe
(kill (RefNotInHeapInAssignSignal r))
(\ _ -> setEnv (heapUpd h r x))
(heapSel h r)
-- | Runner that implements the `MLState` effect.
--
-- Its runtime state is a heap (see `Heap`), and its
-- co-operations call the corresponding allocation,
-- dereferencing, and assignment operations on the heap.
--
-- In the co-operation `Deref`, if the reference is
-- not present in the heap, the (kill) signal
-- `RefNotInHeapInDerefSignal` gets sent.
--
-- In the co-operation `Assign`, if the reference is
-- not present in the heap, the (kill) signal
-- `RefNotInHeapInAssignSignal` gets sent.
mlRunner :: Runner '[MLState] sig S Heap
mlRunner = mkRunner mlCoOps
-- | Initialiser for the runner `mlRunner` that
-- initialises the heap with the empty partial map,
-- and sets the next address to be allocated to zero.
mlInitialiser :: User sig Zero Heap
mlInitialiser = return (H { memory = \ _ -> Nothing , nextAddr = Z })
-- | Finaliser for return values for the runner `mlRunner`,
-- which discards the final value of the heap, and simply
-- passes on the return value.
mlFinaliserVal :: a -> Heap -> User sig Zero a
mlFinaliserVal x _ = return x
-- | Finaliser for exceptions for the runner `mlRunner`,
-- which is vacuously defined because there are
-- no exceptions (the exceptions index is `Zero`).
mlFinaliserExc :: Zero -> Heap -> User sig Zero a
mlFinaliserExc e _ = impossible e
-- | Finaliser for signals for the runner `mlRunner`,
-- which raises a Haskell runtime error to signify
-- that an uncaught signal reached the top level.
mlFinaliserSig :: S -> User sig Zero a
mlFinaliserSig s = error ("signal reached top level (" ++ show s ++ ")")
-- | Top level for running user code that can use ML-style state.
mlTopLevel :: User '[MLState] Zero a -> a
mlTopLevel m =
pureTopLevel (
run
mlRunner
mlInitialiser
m
mlFinaliserVal
mlFinaliserExc
mlFinaliserSig
)