on master help sign in
Add Control.ST.Exception

Only possible to throw exceptions after all the state transitions have been done, meaning that we always know everything is in the final state when we get to the catch block. This is most likely to be useful for controlling things, such as backtracking parsers, so it's possible that 'Exception' is not really the right name for it. However...

commit148783275d1b34e25456c22e2dceeaad213fbd83 authorEdwin Brady <ecb10@st-andrews.ac.uk> dateSat 11 Mar 2017, 22:43 (10 years ago) parent51e26c4
2 files changed+39−1
libs/contrib/Control/ST/Exception.idr+38 libs/contrib/contrib.ipkg+1−1
libs/contrib/Control/ST/Exception.idrnew file+38
@@ -0,0 +1,38 @@
1+module Control.ST.ImplicitCall
2+
3+import Control.ST
4+import Control.Catchable
5+import Control.IOExcept
6+
7+public export
8+interface Exception (m : Type -> Type) errorType | m where
9+ throw : errorType -> STrans m a ctxt (const ctxt)
10+
11+ catch : STrans m a in_res (const out_res) ->
12+ (errorType -> STrans m a out_res (const out_res)) ->
13+ STrans m a in_res (const out_res)
14+
15+export
16+Exception (Either errorType) errorType where
17+ throw err = lift (Left err)
18+ catch prog f = do res <- runAs prog
19+ case res of
20+ Left err => f err
21+ Right ok => pure ok
22+
23+export
24+Exception Maybe () where
25+ throw err = lift Nothing
26+ catch prog f = do res <- runAs prog
27+ case res of
28+ Nothing => f ()
29+ Just ok => pure ok
30+
31+export
32+Exception (IOExcept errorType) errorType where
33+ throw err = lift (ioe_fail err)
34+ catch prog f = do io_res <- runAs prog
35+ res <- lift (catch (do r <- io_res
36+ pure (Right r))
37+ (\err => pure (Left err)))
38+ either (\err => f err) (\ok => pure ok) res
libs/contrib/contrib.ipkg+1−1
@@ -8,7 +8,7 @@ modules = CFFI, CFFI.Types, CFFI.Memory,
88 Control.Algebra.NumericImplementations,
99 Control.Isomorphism.Primitives,
1010 Control.Partial,
11− Control.ST, Control.ST.ImplicitCall,
11+ Control.ST, Control.ST.ImplicitCall, Control.ST.Exception,
1212
1313 Interfaces.Verified,
1414