-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathMonads_D.thy
82 lines (67 loc) · 2.14 KB
/
Monads_D.thy
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
(*
* Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
*
* SPDX-License-Identifier: GPL-2.0-only
*)
(*
* The basic monads used in capDL
*)
theory Monads_D
imports
Types_D
Monads.Nondet_In_Monad
Monads.Nondet_VCG
begin
(* Kernel state monad *)
type_synonym 'a k_monad = "(cdl_state, 'a) nondet_monad"
datatype cdl_except_error = ExceptError
datatype cdl_preempt_error = PreemptError
datatype cdl_fault_error = FaultError
(* Exception monad, no further exception information *)
type_synonym 'a except_monad = "(cdl_state, cdl_except_error + 'a) nondet_monad"
(* Exception monad, no further exception information *)
type_synonym 'a preempt_monad = "(cdl_state, cdl_preempt_error + 'a) nondet_monad"
(* Exception monad, no further exception information *)
type_synonym 'a fault_monad = "(cdl_state, cdl_fault_error + 'a) nondet_monad"
abbreviation
throw :: "(cdl_state, 'a + 'b) nondet_monad" where
"throw == throwError undefined"
text \<open>Allow preemption at this point.\<close>
definition
preemption_point :: "unit preempt_monad" where
"preemption_point \<equiv> throw \<sqinter> returnOk ()"
(*
* Convert an exception monad with aribtrary type into a
* new exception monad with unit type.
*)
definition
unify_failure :: "('f + 'a) k_monad \<Rightarrow> (unit + 'a) k_monad" where
"unify_failure m \<equiv> handleE' m (\<lambda>x. throwError ())"
text \<open>
Convert a fault monad into an exception monad.
\<close>
definition
fault_to_except :: "'a fault_monad \<Rightarrow> 'a except_monad"
where
"fault_to_except m \<equiv> handleE' m (\<lambda>x. throw)"
(*
* Non-deterministically select an item from the given set.
* If the set if empty, return 'None'.
*)
definition
option_select :: "'a set \<Rightarrow> ('s, 'a option) nondet_monad"
where
"option_select S \<equiv>
if S = {} then
return None
else
select S >>= (\<lambda>a. return (Some a))"
(* Return the given object, throwing an error if it is 'None'. *)
definition
throw_on_none :: "'a option \<Rightarrow> ('e + 'a) k_monad"
where
"throw_on_none x \<equiv>
case x of
None \<Rightarrow> throw
| Some y \<Rightarrow> returnOk y"
end