Commands.v 2.36 KB
Newer Older
1
2
3
(**
 Formalization of the Abstract Syntax Tree of a subset used in the Daisy framework
 **)
Heiko Becker's avatar
Heiko Becker committed
4
Require Import Coq.Reals.Reals Coq.QArith.QArith.
5
Require Import Daisy.Expressions.
6
7
Require Export Daisy.Infra.ExpressionAbbrevs Daisy.Infra.NatSet.

8
9
(**
  Next define what a program is.
10
11
  Currently no loops, or conditionals.
  Only assignments and return statement
12
13
**)
Inductive cmd (V:Type) :Type :=
14
Let: mType -> nat -> exp V -> cmd V -> cmd V
15
| Ret: exp V -> cmd V.
16

Raphaël Monat's avatar
Raphaël Monat committed
17
18
19
20
21
22
Fixpoint getRetExp (V:Type) (f:cmd V) :=
  match f with
  |Let m x e g => getRetExp g
  | Ret e => e
  end.

23
24
25
26
27
28
29
30
31

Fixpoint toRCmd (f:cmd Q) :=
  match f with
  |Let m x e g => Let m x (toRExp e) (toRCmd g)
  |Ret e => Ret (toRExp e)
  end.

Fixpoint toREvalCmd (f:cmd R) :=
  match f with
32
  |Let m x e g => Let M0 x (toREval e) (toREvalCmd g)
33
34
35
  |Ret e => Ret (toREval e)
  end.

36
37
(*
UNUSED!
Heiko Becker's avatar
Heiko Becker committed
38
   Small Step semantics for Daisy language
39
40
41
42
43
44
45
46
Inductive sstep : cmd R -> env -> R -> cmd R -> env -> Prop :=
  let_s x e s E v eps:
    eval_exp eps E e v ->
    sstep (Let x e s) E eps s (updEnv x v E)
 |ret_s e E v eps:
    eval_exp eps E e v ->
    sstep (Ret e) E eps (Nop R) (updEnv 0 v E).
 *)
47
48

(**
49
50
  Define big step semantics for the Daisy language, terminating on a "returned"
  result value
51
 **)
52
53
54
Inductive bstep : cmd R -> env -> (nat -> option mType) -> R -> mType -> Prop :=
  let_b m m' x e s E v res defVars:
    eval_exp E defVars  e v m ->
55
56
    defVars x = Some m ->
    bstep s (updEnv x v E) defVars res m' ->
57
58
59
60
    bstep (Let m x e s) E defVars res m'
 |ret_b m e E v defVars:
    eval_exp E defVars e v m ->
    bstep (Ret e) E defVars v m.
Heiko Becker's avatar
Heiko Becker committed
61

Raphaël Monat's avatar
Raphaël Monat committed
62

Heiko Becker's avatar
Heiko Becker committed
63

64
65
66
67
(**
  The free variables of a command are all used variables of expressions
  without the let bound variables
**)
68
Fixpoint freeVars V (f:cmd V) :NatSet.t :=
Heiko Becker's avatar
Heiko Becker committed
69
  match f with
70
71
  | Let _ x e1 g => NatSet.remove x (NatSet.union (Expressions.usedVars e1) (freeVars g))
  | Ret e => Expressions.usedVars e
Heiko Becker's avatar
Heiko Becker committed
72
73
  end.

74
75
76
(**
  The defined variables of a command are all let bound variables
**)
77
Fixpoint definedVars V (f:cmd V) :NatSet.t :=
Heiko Becker's avatar
Heiko Becker committed
78
  match f with
79
  | Let _ x _ g => NatSet.add x (definedVars g)
80
  | Ret _ => NatSet.empty
81
82
83
84
85
86
87
88
  end.

(**
  The live variables of a command are all variables which occur on the right
  hand side of an assignment or at a return statement
 **)
Fixpoint liveVars V (f:cmd V) :NatSet.t :=
  match f with
89
  | Let _ _ e g => NatSet.union (usedVars e) (liveVars g)
90
  | Ret e => usedVars e
91
  end.