source: src/common/IO.ma @ 789

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

Common definition for animation semantics, and factor out IO definitions.

File size: 2.2 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
22definition convert_eventval : ∀ev:eventval. ∀ty:typ. res val ≝
23λev,ty.
24match ty with
25[ ASTint ⇒ match ev with [ EVint i ⇒ OK ? (Vint i) | _ ⇒ Error ? ]
26| ASTfloat ⇒ match ev with [ EVfloat f ⇒ OK ? (Vfloat f) | _ ⇒ Error ? ]
27| _ ⇒ Error ?
28].
29
30definition check_eventval' : ∀v:val. ∀ty:typ. res eventval ≝
31λv,ty.
32match ty with
33[ ASTint ⇒ match v with [ Vint i ⇒ OK ? (EVint i) | _ ⇒ Error ? ]
34| ASTfloat ⇒ match v with [ Vfloat f ⇒ OK ? (EVfloat f) | _ ⇒ Error ? ]
35| _ ⇒ Error ?
36].
37
38let rec check_eventval_list (vs: list val) (tys: list typ) : res (list eventval) ≝
39match vs with
40[ nil ⇒ match tys with [ nil ⇒ OK ? (nil ?) | _ ⇒ Error ? ]
41| cons v vt ⇒
42  match tys with
43  [ nil ⇒ Error ?
44  | cons ty tyt ⇒
45    do ev ← check_eventval' v ty;
46    do evt ← check_eventval_list vt tyt;
47    OK ? (ev::evt)
48  ]
49].
50
51(* IO monad *)
52
53(* Interactions are function calls that return a value and do not change
54   the rest of the Clight program's state. *)
55record io_out : Type[0] ≝
56{ io_function: ident
57; io_args: list eventval
58; io_in_typ: typ
59}.
60
61definition io_in ≝ λo. eventval_type (io_in_typ o).
62
63definition do_io : ident → list eventval → ∀t:typ. IO io_out io_in (eventval_type t) ≝
64λfn,args,t. Interact ??? (mk_io_out fn args t) (λres. Value ??? res).
65
66definition ret: ∀T. T → (IO io_out io_in T) ≝
67λT,x.(Value ?? T x).
Note: See TracBrowser for help on using the repository browser.