summaryrefslogtreecommitdiff
path: root/src/gen_lib/state.lem
diff options
context:
space:
mode:
Diffstat (limited to 'src/gen_lib/state.lem')
-rw-r--r--src/gen_lib/state.lem23
1 files changed, 13 insertions, 10 deletions
diff --git a/src/gen_lib/state.lem b/src/gen_lib/state.lem
index 1ca25b74..cbec7204 100644
--- a/src/gen_lib/state.lem
+++ b/src/gen_lib/state.lem
@@ -19,18 +19,18 @@ type sequential_state 'regs =
and exception type 'e. *)
type ME 'regs 'a 'e = sequential_state 'regs -> list ((either 'a 'e) * sequential_state 'regs)
-(* Most of the time, we don't distinguish between different types of exceptions *)
-type M 'regs 'a = ME 'regs 'a unit
+(* By default, we use strings to distinguish between different types of exceptions *)
+type M 'regs 'a = ME 'regs 'a string
(* For early return, we abuse exceptions by throwing and catching
- the return value. The exception type is "maybe 'r", where "Nothing"
- represents a proper exception and "Just r" an early return of value "r". *)
-type MR 'regs 'a 'r = ME 'regs 'a (maybe 'r)
+ the return value. The exception type is "either 'r string", where "Right e"
+ represents a proper exception and "Left r" an early return of value "r". *)
+type MR 'regs 'a 'r = ME 'regs 'a (either 'r string)
val liftR : forall 'a 'r 'regs. M 'regs 'a -> MR 'regs 'a 'r
let liftR m s = List.map (function
| (Left a, s') -> (Left a, s')
- | (Right (), s') -> (Right Nothing, s')
+ | (Right e, s') -> (Right (Right e), s')
end) (m s)
val return : forall 'regs 'a 'e. 'a -> ME 'regs 'a 'e
@@ -48,17 +48,20 @@ val (>>): forall 'regs 'b 'e. ME 'regs unit 'e -> ME 'regs 'b 'e -> ME 'regs 'b
let inline (>>) m n = m >>= fun _ -> n
val exit : forall 'regs 'e 'a. 'e -> M 'regs 'a
-let exit _ s = [(Right (), s)]
+let exit _ s = [(Right "exit", s)]
+
+val assert_exp : forall 'regs. bool -> string -> M 'regs unit
+let assert_exp exp msg s = if exp then [(Left (), s)] else [(Right msg, s)]
val early_return : forall 'regs 'a 'r. 'r -> MR 'regs 'a 'r
-let early_return r s = [(Right (Just r), s)]
+let early_return r s = [(Right (Left r), s)]
val catch_early_return : forall 'regs 'a. MR 'regs 'a 'a -> M 'regs 'a
let catch_early_return m s =
List.map
(function
- | (Right (Just a), s') -> (Left a, s')
- | (Right Nothing, s') -> (Right (), s')
+ | (Right (Left a), s') -> (Left a, s')
+ | (Right (Right e), s') -> (Right e, s')
| (Left a, s') -> (Left a, s')
end) (m s)