Description of our abstract machine

Components

This machine operates on some given monoid ; it contains a register X and a stack S; In order to simplify the proofs, we pack together these 2 components, and create a Set Config, using the Record feature.
Inductive Stack:Set := emptystack:Stack
                    |  push:M->Stack->Stack.

Record Config:Set :=
   config {
         config_X:M;
         config_S:Stack}.
As usual, we define functionally top:Stack->M and pop:Stack->Stack (using Case syntax).

Elementary instructions

We define a set Instr of instructions operating on the register and the stack:
Inductive Instr:Set :=
  MUL:Instr   
| SQR:Instr   
| PUSH:Instr  
| SWAP:Instr. 

The operational semantics of this set of instructions is given by a function Exec1:Instr->Config->Config defined by cases. We can describe this semantics in a more readable way than the source

MUL
transforms a configuration of the form {X=m, S=(m1,m2,...)} into
{X=m o m1, S=(m2,...)}
SQR
transforms a configuration of the form {X=m, S=(m1,...)} into
{X= m^2, S=(m1,...)}
PUSH
transforms a configuration of the form {X=m, S=(m1,...)} into
{X=m, S=(m,m1,...)}
SWAP
transforms a configuration of the form {X=m, S=(m1,m2,...)} into
{X=m, S=(m2,m1,...)}

Sequences of instructions

Then we define a Set Code which may be considered as the set of sequences of instructions, (we could have defined Code as (list Instr)):
Inductive Code:Set :=
   End:Code
|  seq:Instr->Code->Code.

With the help of the Coq extended grammars, we present code fragments as, for instance, (PUSH ;; PUSH ;; SQR ;; MUL ;; End)

Code appending is represented by the infix operator @@

Then it is easy to extend Exec1 in a primitive recursive function:

Recursive Definition Exec:Code->Config->Config:=
     End v => v
  |  (i ;;  c) v => (Exec c (Exec1 i v)).

A useful lemma

The module machine ends with a lemma which relates code appending with functional composition.
Lemma Exec_app: (c,c':Code)(v:Config)
                (Exec ( c @@  c') v)=(Exec c' (Exec c v)).
which is used in commands like "Rewrite Exec_app" in develop.v

Click here to see the source.

A Demo in Caml-Light

#load "Demo.ml";;
##open "Demo";;

#Exec1;;
- : 'a monoid -> Instr -> 'a Config -> 'a Config = <fun>

#Exec;;
- : 'a monoid -> Code -> 'a Config -> 'a Config = <fun>

#let  c1= Exec1 standard PUSH (config(2,emptystack));;
c1 : int Config = config (2, push (2, emptystack))

#let  c2= Exec1 standard PUSH c1;;
c2 : int Config = config (2, push (2, push (2, emptystack)))

#let  c3= Exec1 standard SQR c2;;
c3 : int Config = config (4, push (2, push (2, emptystack)))

#let  c4= Exec1 standard MUL c3;;
c4 : int Config = config (8, push (2, emptystack))

#let c5 =Exec standard 
              (seq(PUSH,seq(SWAP,seq(SQR,seq(MUL,End))))) 
              c4;;
c5 : int Config = config (128, push (8, emptystack))