Library liblayers.compcertx.GenSem

Require Import compcert.lib.Coqlib.
Require Import compcert.lib.Integers.
Require Import compcert.common.Values.
Require Import compcert.common.AST.
Require Import compcert.common.Memtype.
Require Import compcert.cfrontend.Ctypes.
Require Export liblayers.lib.OptionMonad.

Generic semantics from abstract functions


Inductive Z64 := VZ64 (z: Z).

Function Z642Z (z: Z64) :=
  match z with
    | VZ64
  end.

Class Semof (data: Type) T (targs: typelist) (tres: type) :=
  semof : T → (list valdatavaldataProp).

Class Semprops {data} T `{Tsemof: Semof data T} :=
{
  semprops_well_typed f vargs d vres :
    semof f vargs d vres
    Val.has_type vres (typ_of_type tres);

  semprops_arity f vargs d vres :
    semof f vargs d vres
    length vargs = length (typlist_of_typelist targs);

  semprops_lessdef f vargs vargs´ d vres :
    semof f vargs d vres
    Val.lessdef_list vargs vargs´
    vargs´ = vargs;

  semprops_inject_neutral f vargs d vres j:
    semof f vargs d vres
    val_inject j vres vres;

  semprops_determ f vargs d vres1 vres2 d1 d2:
    semof f vargs d vres1 d1
    semof f vargs d vres2 d2
    vres1 = vres2 d1 = d2;

  semprops_inject f ι vargs vargs´ d vres :
    semof f vargs d vres
    val_list_inject ι vargs vargs´
    vargs´ = vargs
}.

Basic instances


Section INSTANCES.
  Context {data : Type}.

  Notation type_int32u := (Tint I32 Unsigned noattr).
  Notation type_int64u := (Tlong Unsigned noattr).

  Inductive semof_nil_void: Semof data (dataoption data) Tnil Tvoid :=
    | semof_nil_void_intro f d :
        f d = ret
        semof f nil d Vundef .

  Global Existing Instance semof_nil_void.

  Global Instance semof_nil_void_props: Semprops (dataoption data).
  Proof.
    split.
    +
      intros ? ? ? ? ? H.
      inv H.
      simpl.
      tauto.
    +
      intros ? ? ? ? ? H.
      inv H;
      reflexivity.
    +
      intros ? ? ? ? ? ? H Hl.
      inv H;
      inv Hl;
      reflexivity.
    +
      inversion 1; subst.
      constructor.
    +
      inversion 1.
      inversion 1.
      unfold bind, ret in *; simpl in ×.
      split; congruence.
    +
      inversion 1.
      inversion 1.
      reflexivity.
  Qed.

  Inductive semof_nil_int: Semof data (dataoption (data × Z)) Tnil type_int32u :=
    | semof_nil_int_intro f d z :
        f d = ret (, Int.unsigned z)
        semof f nil d (Vint z) .

  Global Existing Instance semof_nil_int.

  Global Instance semof_nil_int_props: Semprops (dataoption (data × Z)).
  Proof.
    split.
    +
      intros ? ? ? ? ? H.
      inv H.
      simpl.
      tauto.
    +
      intros ? ? ? ? ? H.
      inv H;
      reflexivity.
    +
      intros ? ? ? ? ? ? H Hl.
      inv H;
      inv Hl;
      reflexivity.
    +
      inversion 1; subst.
      constructor.
    +
      inversion 1.
      inversion 1.
      unfold bind, ret in *; simpl in ×.
      split; try congruence.
      rewrite H0 in H7.
      inv H7.
      rewrite <- Int.repr_unsigned with z.
      rewrite <- Int.repr_unsigned with z0.
      congruence.
    +
      inversion 1.
      inversion 1.
      reflexivity.
  Qed.


  Local Notation lift_nil_int_pure f :=
    (fun dz <- f d; ret (d, z)).

  Global Instance semof_nil_int_pure: Semof data (dataoption Z) Tnil type_int32u :=
    fun fsemof (lift_nil_int_pure f).

  Global Instance semof_nil_int_pure_props: Semprops (dataoption Z).
  Proof.
    split; intro f.
    + exact (semprops_well_typed (lift_nil_int_pure f)).
    + exact (semprops_arity (lift_nil_int_pure f)).
    + exact (semprops_lessdef (lift_nil_int_pure f)).
    + exact (semprops_inject_neutral (lift_nil_int_pure f)).
    + exact (semprops_determ (lift_nil_int_pure f)).
    + exact (semprops_inject (lift_nil_int_pure f)).
  Qed.


  Local Notation lift_nil_int_pure_total f :=
    (fun dret (f d)).

  Global Instance semof_nil_int_pure_total: Semof data (dataZ) Tnil type_int32u :=
    fun fsemof (lift_nil_int_pure_total f).

  Global Instance semof_nil_int_pure_total_props: Semprops (dataZ).
  Proof.
    split; intro f.
    + exact (semprops_well_typed (lift_nil_int_pure_total f)).
    + exact (semprops_arity (lift_nil_int_pure_total f)).
    + exact (semprops_lessdef (lift_nil_int_pure_total f)).
    + exact (semprops_inject_neutral (lift_nil_int_pure_total f)).
    + exact (semprops_determ (lift_nil_int_pure_total f)).
    + exact (semprops_inject (lift_nil_int_pure_total f)).
  Qed.

  Inductive semof_cons `{Semof data}: Semof data (ZT) (Tcons type_int32u targs) tres :=
    semof_cons_intro f (i: int) l d v :
      semof (f (Int.unsigned i)) l d v
      semof f (Vint i :: l) d v .

  Global Existing Instance semof_cons.

  Global Instance semof_cons_props {T} `(HT: Semprops data T): Semprops (ZT).
  Proof.
    split.
    +
      intros ? ? ? ? ? H.
      inv H.
      apply (semprops_well_typed (targs := targs) (f (Int.unsigned i)) l d vres ).
      eassumption.
    +
      intros ? ? ? ? ? H.
      inv H.
      simpl.
      f_equal.
      eapply semprops_arity.
      eassumption.
    +
      intros until .
      intros H Hl.
      inv H.
      inv Hl.
      inv H2.
      f_equal.
      eapply semprops_lessdef;
      eassumption.
    +
      inversion 1; subst.
      eapply semprops_inject_neutral.
      eassumption.
    +
      inversion 1.
      inversion 1.
      eapply semprops_determ; eassumption.
    +
      intros.
      inv H.
      inv H0.
      inv H3.
      f_equal.
      eapply semprops_inject; eassumption.
  Qed.

  Inductive semof_nil_int64: Semof data (dataoption (data × Z64)) Tnil type_int64u :=
    | semof_nil_int64_intro f d z :
        f d = ret (, VZ64 (Int64.unsigned z))
        semof f nil d (Vlong z) .

  Global Existing Instance semof_nil_int64.

  Global Instance semof_nil_int64_props: Semprops (dataoption (data × Z64)).
  Proof.
    split.
    +
      intros ? ? ? ? ? H.
      inv H.
      simpl.
      tauto.
    +
      intros ? ? ? ? ? H.
      inv H;
      reflexivity.
    +
      intros ? ? ? ? ? ? H Hl.
      inv H;
      inv Hl;
      reflexivity.
    +
      inversion 1; subst.
      constructor.
    +
      inversion 1.
      inversion 1.
      unfold bind, ret in *; simpl in ×.
      split; try congruence.
      rewrite H0 in H7.
      inv H7.
      rewrite <- Int64.repr_unsigned with z.
      rewrite <- Int64.repr_unsigned with z0.
      congruence.
    +
      inversion 1.
      inversion 1.
      reflexivity.
  Qed.

  Global Instance semof_nil_int64_pure: Semof data (dataoption Z64) Tnil type_int64u :=
    fun fsemof (lift_nil_int_pure f).

  Global Instance semof_nil_int64_pure_props: Semprops (dataoption Z64).
  Proof.
    split; intro f.
    + exact (semprops_well_typed (lift_nil_int_pure f)).
    + exact (semprops_arity (lift_nil_int_pure f)).
    + exact (semprops_lessdef (lift_nil_int_pure f)).
    + exact (semprops_inject_neutral (lift_nil_int_pure f)).
    + exact (semprops_determ (lift_nil_int_pure f)).
    + exact (semprops_inject (lift_nil_int_pure f)).
  Qed.

  Global Instance semof_nil_int64_pure_total: Semof data (dataZ64) Tnil type_int64u :=
    fun fsemof (lift_nil_int_pure_total f).

  Global Instance semof_nil_int64_pure_total_props: Semprops (dataZ64).
  Proof.
    split; intro f.
    + exact (semprops_well_typed (lift_nil_int_pure_total f)).
    + exact (semprops_arity (lift_nil_int_pure_total f)).
    + exact (semprops_lessdef (lift_nil_int_pure_total f)).
    + exact (semprops_inject_neutral (lift_nil_int_pure_total f)).
    + exact (semprops_determ (lift_nil_int_pure_total f)).
    + exact (semprops_inject (lift_nil_int_pure_total f)).
  Qed.

  Inductive semof_cons64 `{Semof data}: Semof data (Z64T) (Tcons type_int64u targs) tres :=
    semof_cons64_intro f (i: int64) l d v :
      semof (f (VZ64 (Int64.unsigned i))) l d v
      semof f (Vlong i :: l) d v .

  Global Existing Instance semof_cons64.

  Global Instance semof_cons64_props {T} `(HT: Semprops data T): Semprops (Z64T).
  Proof.
    split.
    +
      intros ? ? ? ? ? H.
      inv H.
      apply (semprops_well_typed (targs := targs) (f (VZ64 (Int64.unsigned i))) l d vres ).
      eassumption.
    +
      intros ? ? ? ? ? H.
      inv H.
      simpl.
      f_equal.
      eapply semprops_arity.
      eassumption.
    +
      intros until .
      intros H Hl.
      inv H.
      inv Hl.
      inv H2.
      f_equal.
      eapply semprops_lessdef;
      eassumption.
    +
      inversion 1; subst.
      eapply semprops_inject_neutral.
      eassumption.
    +
      inversion 1.
      inversion 1.
      eapply semprops_determ; eassumption.
    +
      intros.
      inv H.
      inv H0.
      inv H3.
      f_equal.
      eapply semprops_inject; eassumption.
  Qed.

End INSTANCES.