ectx_language.v 4.84 KB
Newer Older
1
2
(** An axiomatization of evaluation-context based languages, including a proof
    that this gives rise to a "language" in the Iris sense. *)
3
From iris.algebra Require Export base.
4
From iris.program_logic Require Import language.
5
6
7

(* We need to make thos arguments indices that we want canonical structure
   inference to use a keys. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
8
Class EctxLanguage (expr val ectx state : Type) := {
9
10
11
12
13
  of_val : val  expr;
  to_val : expr  option val;
  empty_ectx : ectx;
  comp_ectx : ectx  ectx  ectx;
  fill : ectx  expr  expr;
14
  head_step : expr  state  expr  state  list expr  Prop;
15
16
17

  to_of_val v : to_val (of_val v) = Some v;
  of_to_val e v : to_val e = Some v  of_val v = e;
18
  val_stuck e1 σ1 e2 σ2 efs : head_step e1 σ1 e2 σ2 efs  to_val e1 = None;
19
20
21

  fill_empty e : fill empty_ectx e = e;
  fill_comp K1 K2 e : fill K1 (fill K2 e) = fill (comp_ectx K1 K2) e;
Robbert Krebbers's avatar
Robbert Krebbers committed
22
  fill_inj K :> Inj (=) (=) (fill K);
23
24
  fill_not_val K e : to_val e = None  to_val (fill K e) = None;

Robbert Krebbers's avatar
Robbert Krebbers committed
25
26
27
  (* There are a whole lot of sensible axioms (like associativity, and left and
  right identity, we could demand for [comp_ectx] and [empty_ectx]. However,
  positivity suffices. *)
28
  ectx_positive K1 K2 :
Robbert Krebbers's avatar
Robbert Krebbers committed
29
    comp_ectx K1 K2 = empty_ectx  K1 = empty_ectx  K2 = empty_ectx;
30

31
  step_by_val K K' e1 e1' σ1 e2 σ2 efs :
32
33
    fill K e1 = fill K' e1' 
    to_val e1 = None 
34
    head_step e1' σ1 e2 σ2 efs 
35
36
    exists K'', K' = comp_ectx K K'';
}.
Robbert Krebbers's avatar
Robbert Krebbers committed
37

38
39
40
41
42
43
44
45
46
47
48
49
50
Arguments of_val {_ _ _ _ _} _.
Arguments to_val {_ _ _ _ _} _.
Arguments empty_ectx {_ _ _ _ _}.
Arguments comp_ectx {_ _ _ _ _} _ _.
Arguments fill {_ _ _ _ _} _ _.
Arguments head_step {_ _ _ _ _} _ _ _ _ _.

Arguments to_of_val {_ _ _ _ _} _.
Arguments of_to_val {_ _ _ _ _} _ _ _.
Arguments val_stuck {_ _ _ _ _} _ _ _ _ _ _.
Arguments fill_empty {_ _ _ _ _} _.
Arguments fill_comp {_ _ _ _ _} _ _ _.
Arguments fill_not_val {_ _ _ _ _} _ _ _.
51
Arguments ectx_positive {_ _ _ _ _} _ _ _.
52
53
54
Arguments step_by_val {_ _ _ _ _} _ _ _ _ _ _ _ _ _ _ _.

(* From an ectx_language, we can construct a language. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
55
56
Section ectx_language.
  Context {expr val ectx state} {Λ : EctxLanguage expr val ectx state}.
57
58
  Implicit Types (e : expr) (K : ectx).

59
  Definition head_reducible (e : expr) (σ : state) :=
60
     e' σ' efs, head_step e σ e' σ' efs.
61

62
  Inductive prim_step (e1 : expr) (σ1 : state)
63
      (e2 : expr) (σ2 : state) (efs : list expr) : Prop :=
Robbert Krebbers's avatar
Robbert Krebbers committed
64
65
    Ectx_step K e1' e2' :
      e1 = fill K e1'  e2 = fill K e2' 
66
      head_step e1' σ1 e2' σ2 efs  prim_step e1 σ1 e2 σ2 efs.
67

68
69
  Lemma val_prim_stuck e1 σ1 e2 σ2 efs :
    prim_step e1 σ1 e2 σ2 efs  to_val e1 = None.
70
71
72
73
74
  Proof. intros [??? -> -> ?]; eauto using fill_not_val, val_stuck. Qed.

  Canonical Structure ectx_lang : language := {|
    language.expr := expr; language.val := val; language.state := state;
    language.of_val := of_val; language.to_val := to_val;
Robbert Krebbers's avatar
Robbert Krebbers committed
75
    language.prim_step := prim_step;
76
    language.to_of_val := to_of_val; language.of_to_val := of_to_val;
Robbert Krebbers's avatar
Robbert Krebbers committed
77
    language.val_stuck := val_prim_stuck;
78
79
  |}.

80
  (* Some lemmas about this language *)
81
82
  Lemma head_prim_step e1 σ1 e2 σ2 efs :
    head_step e1 σ1 e2 σ2 efs  prim_step e1 σ1 e2 σ2 efs.
Robbert Krebbers's avatar
Robbert Krebbers committed
83
84
85
  Proof. apply Ectx_step with empty_ectx; by rewrite ?fill_empty. Qed.

  Lemma head_prim_reducible e σ : head_reducible e σ  reducible e σ.
86
  Proof. intros (e'&σ'&efs&?). eexists e', σ', efs. by apply head_prim_step. Qed.
87

Robbert Krebbers's avatar
Robbert Krebbers committed
88
  Lemma ectx_language_atomic e :
89
    ( σ e' σ' efs, head_step e σ e' σ' efs  is_Some (to_val e')) 
Robbert Krebbers's avatar
Robbert Krebbers committed
90
91
    ( K e', e = fill K e'  to_val e' = None  K = empty_ectx) 
    atomic e.
92
  Proof.
93
    intros Hatomic_step Hatomic_fill σ e' σ' efs [K e1' e2' -> -> Hstep].
Robbert Krebbers's avatar
Robbert Krebbers committed
94
95
    assert (K = empty_ectx) as -> by eauto 10 using val_stuck.
    rewrite fill_empty. eapply Hatomic_step. by rewrite fill_empty.
96
97
  Qed.

98
99
100
  Lemma head_reducible_prim_step e1 σ1 e2 σ2 efs :
    head_reducible e1 σ1  prim_step e1 σ1 e2 σ2 efs 
    head_step e1 σ1 e2 σ2 efs.
101
  Proof.
102
103
    intros (e2''&σ2''&efs''&?) [K e1' e2' -> -> Hstep].
    destruct (step_by_val K empty_ectx e1' (fill K e1') σ1 e2'' σ2'' efs'')
Robbert Krebbers's avatar
Robbert Krebbers committed
104
      as [K' [-> _]%symmetry%ectx_positive];
105
106
107
108
      eauto using fill_empty, fill_not_val, val_stuck.
    by rewrite !fill_empty.
  Qed.

109
110
111
112
113
114
115
116
  (* Every evaluation context is a context. *)
  Global Instance ectx_lang_ctx K : LanguageCtx ectx_lang (fill K).
  Proof.
    split.
    - eauto using fill_not_val.
    - intros ????? [K' e1' e2' Heq1 Heq2 Hstep].
      by exists (comp_ectx K K') e1' e2'; rewrite ?Heq1 ?Heq2 ?fill_comp.
    - intros e1 σ1 e2 σ2 ? Hnval [K'' e1'' e2'' Heq1 -> Hstep].
117
      destruct (step_by_val K K'' e1 e1'' σ1 e2'' σ2 efs) as [K' ->]; eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
118
      rewrite -fill_comp in Heq1; apply (inj (fill _)) in Heq1.
119
120
121
      exists (fill K' e2''); rewrite -fill_comp; split; auto.
      econstructor; eauto.
  Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
122
End ectx_language.
123

124
Arguments ectx_lang _ {_ _ _ _}.