Library proc

Require Export smallstep Ensembles map Relations.
Set Implicit Arguments.
Unset Strict Implicit.

CPP 2015 submission: Section IV. Procedural semantics

Section Semworld.

CPP 2015 submission: Definition 5. Language with function calls

Here we separately introduce the notion of "semantic world" sufficient to define behaviors, and actually independent of the actual languages.
The paper includes those elements in the definition of a language, which should not be the case.

Record semworld: Type := Semworld {
  
Function names
  funcname: Type;

 
Events
  event: Type;

 
Memory states
  heap: Type;

Appendix B:
Relation over memory states to ensure that they evolve monotonically.
  heap_le: heap -> heap -> Prop;
 
Preorder properties
  heap_le_refl: forall h, heap_le h h;
  heap_le_trans: forall h1 h2, heap_le h1 h2 -> forall h3, heap_le h2 h3 -> heap_le h1 h3;

 
Function arguments
  args: Type;
 
Return values
  ret: Type;
 
the following is necessary to show that an external function call is not stuck in the compositional semantics
  ret_inh: inhabited ret
}.

Variable sw: semworld.

Let funcname := funcname sw.
Let event := event sw.
Let heap := heap sw.
Let args := args sw.
Let ret := ret sw.

Let trace := list event.
Let traceinf := Stream event.

Function trace_of_option_event (oe: option event): trace :=
  match oe with
    | None => nil
    | Some e => e :: nil
  end.

Let behavior := behavior event (heap * ret).

Inductive special_local_kind: Type :=
| Call (func : funcname) (a: args)
| Return (r: ret)
.

Section Language.

Language with function calls.

Record language : Type := Language {
  
Local state
  local: Type;

 
Internal transition
  eval_internal: heap -> local -> option event -> heap -> local -> Prop;

Appendix B: internal transition is compatible with memory state monotonic evolution
  eval_internal_le: forall h l tr h' l',
    eval_internal h l tr h' l' ->
    heap_le h h';


 
Local state kind
  local_kind: local -> option special_local_kind;

  
Internal transition only defined from Normal local states
  eval_internal_kind: forall h l tr h' l',
    eval_internal h l tr h' l' ->
    local_kind l = None;

 
Continuation stack frame
  frame: Type;

 
Backup
  caller_call: args -> heap -> local -> (heap * frame);

 
Appendix B: Backup is compatible with memory state monotonic evolution
  caller_call_le: forall a h tr h' fr,
    caller_call a h tr = (h', fr) ->
    heap_le h h';

 
Restore
  caller_return: ret -> heap -> frame -> (heap * local);

 
Appendix B: Restore is compatible with memory state monotonic evolution
  caller_return_le: forall r h fr h' l',
    caller_return r h fr = (h', l') ->
    heap_le h h';

 
Function code
  code: Type;

 
Init
  callee_init: args -> heap -> code -> (heap * local);

 
Appendix B: Init is compatible with memory state monotonic evolution
  callee_init_le: forall a h c h' l',
    callee_init a h c = (h', l') ->
    heap_le h h'
}.

Variable lang: language.

Let local := local lang.
Let frame := frame lang.

CPP 2015 submission: Definitions 6 and 9. Programs, modules or compilation units


Definition module : Type := Map funcname (code lang).

CPP 2015 submission: Definition 7. Procedural semantics

Configuration
Inductive config : Type := Config
  {
    c_heap: heap;
    c_local: local;
    c_stack: list frame
  }.

Variable m : module.

Transition relation
Inductive step : config -> trace -> config -> Prop :=
| step_internal l
    h tr h' l'
    (Hstep: eval_internal h l tr h' l')
    tr' (Htr': tr' = trace_of_option_event tr)
    s:
    step (Config h l s) tr' (Config h' l' s)
| step_call: forall l func ar
  (Hl: local_kind l = Some (Call func ar))
  cmd
  (Hfunc: lookup m func cmd)
  h h' fr
  (Hframe: caller_call ar h l = (h', fr))
  h'' l''
  (Hl'': callee_init ar h' cmd = (h'', l''))
  s,
  step (Config h l s) nil (Config h'' l'' (fr :: s))
| step_return: forall l r
  (Hcmd: (local_kind l) = Some (Return r)) h
  fr h' l'
  (Hreturn: caller_return r h fr = (h', l'))
  s,
  step (Config h l (fr :: s)) nil (Config h' l' s)
.

Lemma step_le: forall h l s tr h' l' s'
  (Hstep: step (Config h l s) tr (Config h' l' s')),
  heap_le h h'.
Proof.
  inversion 1; subst; eauto using eval_internal_le, caller_call_le, callee_init_le, heap_le_trans, caller_return_le.
Qed.

Final states
Inductive config_final: config -> heap * ret -> Prop :=
| config_final_intro l r
  (Hcmd: (local_kind l) = Some (Return r)) h :
  config_final (Config h l nil) (h, r)
.

Big-step semantics
Inductive semantics (f: funcname) (ar: args) (h: heap) : behavior -> Prop :=
| semantics_intro cmd (Hcmd: lookup m f cmd)
  h' l'
  (Hinit: callee_init ar h cmd = (h', l'))
  beh
  (Hbeh: config_behaviors step config_final (Config h' l' nil) beh) :
  semantics f ar h beh
  .

End Language.

End Semworld.