source: src/common/IO.ma @ 797

Last change on this file since 797 was 797, checked in by campbell, 9 years ago

Add error messages wherever the error monad is used.
Sticks to CompCert? style strings+identifiers for the moment.
Use axioms for strings as we currently have no representation or literals
for them - still *very* useful for animation in the proof assistant.

File size: 2.4 KB
Line 
1
2include "common/IOMonad.ma".
3include "common/Events.ma".
4
5(* Checking types of values given to / received from an external function call. *)
6
7definition eventval_type : ∀ty:typ. Type[0] ≝
8λty. match ty with [ ASTint ⇒ int | ASTptr _ ⇒ False | ASTfloat ⇒ float ].
9
10definition mk_eventval: ∀ty:typ. eventval_type ty → eventval ≝
11λty:typ. match ty return λty'.eventval_type ty' → eventval with [ ASTint ⇒ λv.EVint v | ASTptr r ⇒ ? | ASTfloat ⇒ λv.EVfloat v ].
12*; qed.
13
14definition mk_val: ∀ty:typ. eventval_type ty → val ≝
15λty:typ. match ty return λty'.eventval_type ty' → val with [ ASTint ⇒ λv.Vint v | ASTptr r ⇒ ? | ASTfloat ⇒ λv.Vfloat v ].
16*; qed.
17
18lemma mk_val_correct: ∀ty:typ. ∀v:eventval_type ty.
19  eventval_match (mk_eventval ty v) ty (mk_val ty v).
20#ty cases ty; normalize; // * *; qed.
21
22axiom IllTypedEvent : String.
23
24definition convert_eventval : ∀ev:eventval. ∀ty:typ. res val ≝
25λev,ty.
26match ty with
27[ ASTint ⇒ match ev with [ EVint i ⇒ OK ? (Vint i) | _ ⇒ Error ? (msg IllTypedEvent)]
28| ASTfloat ⇒ match ev with [ EVfloat f ⇒ OK ? (Vfloat f) | _ ⇒ Error ? (msg IllTypedEvent)]
29| _ ⇒ Error ? (msg IllTypedEvent)
30].
31
32definition check_eventval' : ∀v:val. ∀ty:typ. res eventval ≝
33λv,ty.
34match ty with
35[ ASTint ⇒ match v with [ Vint i ⇒ OK ? (EVint i) | _ ⇒ Error ? (msg IllTypedEvent) ]
36| ASTfloat ⇒ match v with [ Vfloat f ⇒ OK ? (EVfloat f) | _ ⇒ Error ? (msg IllTypedEvent) ]
37| _ ⇒ Error ? (msg IllTypedEvent)
38].
39
40let rec check_eventval_list (vs: list val) (tys: list typ) : res (list eventval) ≝
41match vs with
42[ nil ⇒ match tys with [ nil ⇒ OK ? (nil ?) | _ ⇒ Error ? (msg IllTypedEvent) ]
43| cons v vt ⇒
44  match tys with
45  [ nil ⇒ Error ? (msg IllTypedEvent)
46  | cons ty tyt ⇒
47    do ev ← check_eventval' v ty;
48    do evt ← check_eventval_list vt tyt;
49    OK ? (ev::evt)
50  ]
51].
52
53(* IO monad *)
54
55(* Interactions are function calls that return a value and do not change
56   the rest of the Clight program's state. *)
57record io_out : Type[0] ≝
58{ io_function: ident
59; io_args: list eventval
60; io_in_typ: typ
61}.
62
63definition io_in ≝ λo. eventval_type (io_in_typ o).
64
65definition do_io : ident → list eventval → ∀t:typ. IO io_out io_in (eventval_type t) ≝
66λfn,args,t. Interact ??? (mk_io_out fn args t) (λres. Value ??? res).
67
68definition ret: ∀T. T → (IO io_out io_in T) ≝
69λT,x.(Value ?? T x).
Note: See TracBrowser for help on using the repository browser.