Syntax of a toy imperative language
We always restrict ourselves to certain domains.
Arithmetic expressions
A numeral (here a natural number)
A random number between a1 and a2
An identifier, which should have been declared previously
And then the classical operations on numerals.
Boolean expressions
True or False
Classical boolean operations
We can also derive some other classical boolean operations :
Definition BGt (a1 a2 : aexp) :=
BNot (BLe a1 a2).
Definition BNeq (a1 a2 : aexp) :=
BNot (BEq a1 a2).
BNot (BLe a1 a2).
Definition BNeq (a1 a2 : aexp) :=
BNot (BEq a1 a2).
Commands
Inductive com : Type :=
| CSkip
| CAsgn (x : string) (a : aexp)
| CSeq (c1 c2 : com)
| CIf (b : bexp) (c1 c2 : com)
| CWhile (b : bexp) (c : com).
| CSkip
| CAsgn (x : string) (a : aexp)
| CSeq (c1 c2 : com)
| CIf (b : bexp) (c1 c2 : com)
| CWhile (b : bexp) (c : com).
Syntactic sugar and notations
<{
x := 0;
y := [0, 100];
while x < y do
x := x + y
end
}>
Coercion AId : string >-> aexp.
Coercion ANum : D >-> aexp.
Declare Scope com_scope.
Notation "<{ e }>" := e
(e custom com, format "'[hv' <{ '/ ' '[v' e ']' '/' }> ']'") : com_scope.
Notation "( x )" := x (in custom com, x at level 99).
Notation "x" := x (in custom com at level 0, x constr at level 0).
Notation "f x .. y" := (.. (f x) .. y)
(in custom com at level 0, only parsing,
f constr at level 0, x constr at level 1,
y constr at level 1).
Notation "x + y" := (APlus x y) (in custom com at level 50, left associativity).
Notation "[ x , y ]" := (ARandom x y) (in custom com at level 50).
Notation "x - y" := (AMinus x y) (in custom com at level 50, left associativity).
Notation "x * y" := (AMult x y) (in custom com at level 40, left associativity).
Notation "'true'" := true (at level 1).
Notation "'true'" := BTrue (in custom com at level 0).
Notation "'false'" := false (at level 1).
Notation "'false'" := BFalse (in custom com at level 0).
Notation "x <= y" := (BLe x y) (in custom com at level 70, no associativity).
Notation "x > y" := (BGt x y) (in custom com at level 70, no associativity).
Notation "x = y" := (BEq x y) (in custom com at level 70, no associativity).
Notation "x <> y" := (BNeq x y) (in custom com at level 70, no associativity).
Notation "x && y" := (BAnd x y) (in custom com at level 80, left associativity).
Notation "'~' b" := (BNot b) (in custom com at level 75, right associativity).
Open Scope com_scope.
Coercion ANum : D >-> aexp.
Declare Scope com_scope.
Notation "<{ e }>" := e
(e custom com, format "'[hv' <{ '/ ' '[v' e ']' '/' }> ']'") : com_scope.
Notation "( x )" := x (in custom com, x at level 99).
Notation "x" := x (in custom com at level 0, x constr at level 0).
Notation "f x .. y" := (.. (f x) .. y)
(in custom com at level 0, only parsing,
f constr at level 0, x constr at level 1,
y constr at level 1).
Notation "x + y" := (APlus x y) (in custom com at level 50, left associativity).
Notation "[ x , y ]" := (ARandom x y) (in custom com at level 50).
Notation "x - y" := (AMinus x y) (in custom com at level 50, left associativity).
Notation "x * y" := (AMult x y) (in custom com at level 40, left associativity).
Notation "'true'" := true (at level 1).
Notation "'true'" := BTrue (in custom com at level 0).
Notation "'false'" := false (at level 1).
Notation "'false'" := BFalse (in custom com at level 0).
Notation "x <= y" := (BLe x y) (in custom com at level 70, no associativity).
Notation "x > y" := (BGt x y) (in custom com at level 70, no associativity).
Notation "x = y" := (BEq x y) (in custom com at level 70, no associativity).
Notation "x <> y" := (BNeq x y) (in custom com at level 70, no associativity).
Notation "x && y" := (BAnd x y) (in custom com at level 80, left associativity).
Notation "'~' b" := (BNot b) (in custom com at level 75, right associativity).
Open Scope com_scope.
As we did for expressions, we can use a few Notation
declarations to make reading and writing Imp programs more
convenient.
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
End Syntax.