skylabs.lang.cpp.semantics.sub_module

(*
 * Copyright (c) 2020-2026 BlueRock Security, Inc.
 * This software is distributed under the terms of the BedRock Open-Source License.
 * See the LICENSE-BedRock file in the repository root for details.
 *)

Require Import stdpp.fin_maps.
Require Export skylabs.prelude.base.
Require Import skylabs.prelude.avl.
Require Import skylabs.prelude.list.
Require Import skylabs.lang.cpp.syntax.

TODO rename sub_module since it is not actually about modules TODO use ⊆ as the "generic" name by declaring SubsetEq instances at each level.

Sub-module

sub_module a b states that the translation_unit a is included in the translation_unit b: each declaration of a has a compatible one in b, with at least as much information (see GlobDecl_le and ObjValue_le).
The motivating case is #include: if the source of b includes the source of a, then sub_module a b is meant to hold. That is the intent behind these orders, and it is not proved or tested here. The relation does not require an #include: it only compares the two translation units' contents.

(* Some tactics *)
#[local] Ltac do_bool_decide :=
  repeat match goal with
    | H : False |- _ => contradiction
    | H : Is_true _ |- _ => red in H
    | H : _ && _ = true |- _ => apply andb_true_iff in H; destruct H
    | H : bool_decide _ = false |- _ => apply bool_decide_eq_false_1 in H
    | H : bool_decide _ = true |- _ => apply bool_decide_eq_true_1 in H
    | |- _ && _ = true => apply andb_true_iff; split
    end.
#[local] Ltac smash :=
  intuition idtac; repeat (do_bool_decide; subst; repeat case_match);
  eauto using bool_decide_eq_true_2; try congruence.

Definitions for decidability

Section compat_le.
  Context {T : Type}.
  Variable (f : option T -> option T -> bool).

  (* NOTE: this is effectively Decision (map_inclusion),
     but is significantly more computationally efficient *)

  Definition compat_le (l r : NM.t T) : bool :=
    negb $ NM.find_any (fun k v => negb (f (Some v) (r !! k))) l.

  Lemma compat_le_sound : forall l r,
      (forall x, f None x = true) ->
      if compat_le l r then
        forall k, f (l !! k) (r !! k) = true
      else
        exists k, f (l !! k) (r !! k) = false.
  Proof.
    intros.
    unfold compat_le.
    generalize (NM.find_any_ok (λ k (v : T), negb (f (Some v) (r !! k))) l).
    generalize (NM.find_any (λ k (v : T), negb (f (Some v) (r !! k))) l).
    destruct b; simpl; intros.
    - destruct H0 as [ ? [ ? [ ? ? ] ] ].
      exists x. unfold lookup in *.
      apply negb_true_iff in H1.
      erewrite NM.find_1; eauto.
    - unfold lookup, NM.map_lookup.
      destruct (NM.find k l) eqn:Heq; eauto.
      apply NM.find_2 in Heq.
      eapply H0 in Heq.
      apply negb_false_iff in Heq. eauto.
  Qed.

End compat_le.

Inclusion of types (GlobDecl)

GlobDecl_le a b holds when a has compatible (but possibly less) information than b. For example, a might be a type declaration while b is a compatible definition.
Definition GlobDecl_le (a b : GlobDecl) : bool :=
  match a , b with
  | Gtype , Gtype
  | Gtype , Genum _ _
  | Gtype , Gunion _
  | Gtype , Gstruct _
  | Gtype , Gunsupported _
  | Gtype , Gopaque _ => true
  | Gunion _ , Gtype
  | Gstruct _ , Gtype => false
  | Gunion u , Gunion u' =>
    bool_decide (u = u')
  | Gstruct s , Gstruct s' =>
    bool_decide (s = s')
  | Genum e _ , Genum e' _ =>
    bool_decide (e = e')
  | Gconstant t (Some e) , Gconstant t' (Some e') =>
    bool_decide (t = t') && bool_decide (e = e')
  | Gconstant t (Some _) , Gconstant t' None => false
  | Gconstant t None , Gconstant t' (Some e') =>
    bool_decide (t = t')
  | Gconstant t None , Gconstant t' None =>
    bool_decide (t = t')
  | Gtypedef t , Gtypedef t' =>
    bool_decide (t = t')
  | Gunsupported m , Gunsupported m' => bool_decide (m = m')
  (* An opaque record is only compatible with itself. *)
  | Gopaque o , Gopaque o' => bool_decide (o = o')
  | _ , _ => false
  end.
Definition GlobDecl_ler := λ g1 g2, Is_true $ GlobDecl_le g1 g2.
Arguments GlobDecl_ler !_ _ /.

Section GlobDecl_ler.
  #[local] Notation GlobDecl_ler := (GlobDecl_ler).
  #[local] Instance GlobDecl_le_refl : Reflexive GlobDecl_ler.
  Proof.
    intros []; rewrite /= ?require_eq_refl; smash.
  Qed.

  #[local] Instance GlobDecl_le_trans : Transitive GlobDecl_ler.
  Proof.
    intros gd1 gd2 gd3; destruct gd1, gd2, gd3 => //=; smash.
  Qed.

  #[global] Instance: PreOrder GlobDecl_ler := {}.

  (* From this, it should follow that multiple translation units from the same
  genv must have compatible definitions. See sub_modules_agree_globdecl *)

  Lemma GlobDecl_ler_join gd1 gd2 gd3 :
    GlobDecl_ler gd1 gd3 -> GlobDecl_ler gd2 gd3 ->
    GlobDecl_ler gd1 gd2 \/ GlobDecl_ler gd2 gd1.
  Proof.
    destruct gd1, gd2 => //=; destruct gd3 => //=; smash.
    left; smash.
  Qed.
End GlobDecl_ler.

Compatibility of GlobDecl states that they contain consistent information. Since declarations can not be partially defined, it is sufficient to define this by saying that one subsumes the other.
Note this relation is reflexive and symmetric, but *not* transitive.
Definition GlobDecl_compat gd1 gd2 :=
  GlobDecl_ler gd1 gd2 \/ GlobDecl_ler gd2 gd1.

#[local] Instance GlobDecl_compat_refl : Reflexive GlobDecl_compat.
Proof. left. reflexivity. Qed.

#[local] Instance GlobDecl_compat_sym : Symmetric GlobDecl_compat.
Proof. red. rewrite /GlobDecl_compat; destruct 1; tauto. Qed.

TODO: consolidate with other definitions
Lemma enum_compat {t1 t2 a b} :
  GlobDecl_compat (Genum t1 a) (Genum t2 b) ->
  t1 = t2.
Proof.
  rewrite /GlobDecl_compat/GlobDecl_ler/=.
  destruct 1.
  case_bool_decide; eauto; contradiction.
  case_bool_decide; eauto; contradiction.
Qed.

Inclusion of type_tables


Definition type_table_le (te1 te2 : type_table) : Prop :=
  forall gn gv,
    te1 !! gn = Some gv ->
    exists gv', te2 !! gn = Some gv' /\ GlobDecl_ler gv gv'.

(* TODO: consider replacing type_table_le's definition with type_table_le_alt *)
Definition type_table_le_alt : type_table -> type_table -> Prop :=
  map_included (fun _ => GlobDecl_ler).
#[global] Instance: PreOrder type_table_le_alt.
Proof. apply @map_included_preorder, _. Qed.

Lemma type_table_le_equiv te1 te2 : type_table_le te1 te2 <-> type_table_le_alt te1 te2.
Proof.
  apply iff_forall => i; unfold option_relation.
  (* XXX TC inference produces different results here. Hacky fix. *)
  unfold type_table, globname, ident.
  repeat case_match; try naive_solver.
Qed.

#[global] Instance: PreOrder type_table_le.
Proof.
  eapply preorder_proper.
  apply: type_table_le_equiv.
  apply _.
Qed.

#[global,program]
Instance type_table_le_dec : RelDecision type_table_le :=
  fun a b =>
    match compat_le (fun l r => match l , r with
                             | None , _ => true
                             | Some _ , None => false
                             | Some l , Some r => GlobDecl_le l r
                             end) a b as X return _ = X -> _ with
    | true => fun pf => left _
    | false => fun pf => right _
    end eq_refl.
Next Obligation.
  intros.
  match goal with
  | _ : compat_le ?F _ _ = _ |- _ => generalize (@compat_le_sound _ F a b (fun _ => eq_refl))
  end; rewrite pf.
  rewrite /type_table_le.
  intros. specialize (H gn). rewrite H0 in H.
  case_match.
  - eexists; split; eauto. do 2 red. rewrite H. done.
  - congruence.
Qed.
Next Obligation.
  intros.
  match goal with
  | _ : compat_le ?F _ _ = _ |- _ => generalize (@compat_le_sound _ F a b (fun _ => eq_refl))
  end; rewrite pf.
  rewrite /type_table_le.
  intros. intro. destruct H.
  case_match; try congruence.
  destruct (H0 _ _ H1) as [ ? [ Heq ? ] ].
  rewrite Heq in H.
  do 2 red in H2. rewrite H in H2. assumption.
Qed.

Inclusion of ObjValue


Definition OrDefault_le {T : Set} `{EqDecision T} (ae be : exception_spec.t) (a b : OrDefault T) : bool :=
  match a , b with
  | Defaulted , Defaulted => bool_decide (ae ⊆ be)
  | Defaulted , CompilerProvided _ => bool_decide (ae ⊆ be)
  | CompilerProvided x , CompilerProvided y
  | UserDefined x , UserDefined y => bool_decide (ae = be /\ x = y)
  | _ , _ => false
  end.
Definition OrDefault_ler {T : Set} `{EqDecision T} : relation (exception_spec.t * OrDefault T) :=
  λ '(ae, a) '(be, b), OrDefault_le ae be a b = true.

Section OrDefault_ler.
  Context {T : Set} `{EqDecision T}.

  #[local] Instance OrDefault_le_refl : Reflexive (@OrDefault_ler T _).
  Proof.
    rewrite /OrDefault_ler/OrDefault_le.
    intros [? od]; destruct od => //=;
      apply bool_decide_eq_true_2; done.
  Qed.

  #[local] Instance OrDefault_le_trans : Transitive (@OrDefault_ler T _).
  Proof.
    rewrite /OrDefault_ler/OrDefault_le.
    intros [? x] [? y] [?z] Hxy Hyz.
    repeat case_match; subst; try congruence.
    all: repeat case_bool_decide; intuition (subst; try tauto).
    { exfalso. apply H1. etrans; eauto. }
    { exfalso. apply H1. etrans; eauto. }
  Qed.

  #[global] Instance: PreOrder (@OrDefault_ler T _) := {}.
End OrDefault_ler.

DtorBody_le is OrDefault_le for destructor bodies, except that Defaulted is not below CompilerProvided.
cpp2v prints every implicit or defaulted destructor as Defaulted, also where clang defines it, and rejects one whose body clang did not leave empty. A CompilerProvided body need not refine Defaulted.
Definition DtorBody_le (ae be : exception_spec.t) (a b : OrDefault Stmt) : bool :=
  match a , b with
  | Defaulted , CompilerProvided _ => false
  | _ , _ => OrDefault_le ae be a b
  end.
Definition DtorBody_ler : relation (exception_spec.t * OrDefault Stmt) :=
  λ '(ae, a) '(be, b), DtorBody_le ae be a b = true.

Section DtorBody_ler.
  #[local] Instance DtorBody_le_refl : Reflexive DtorBody_ler.
  Proof.
    rewrite /DtorBody_ler/DtorBody_le/OrDefault_le.
    intros [? od]; destruct od => //=;
      apply bool_decide_eq_true_2; done.
  Qed.

  #[local] Instance DtorBody_le_trans : Transitive DtorBody_ler.
  Proof.
    rewrite /DtorBody_ler/DtorBody_le/OrDefault_le.
    intros [? x] [? y] [?z] Hxy Hyz.
    repeat case_match; subst; try congruence.
    all: repeat case_bool_decide; intuition (subst; try tauto).
    all: exfalso; match goal with H : _ ⊆ _ → _ |- _ => apply H end; [etrans; eauto|..]; done.
  Qed.

  #[global] Instance: PreOrder DtorBody_ler := {}.
End DtorBody_ler.

var_type_le t t' holds when a variable declared with type t may be defined with type t'. basic.link lets array declarations differ in the presence of a major array bound, as in extern int a[]; against int a[3];; cpp2v puts cv-qualifiers on the element type. The array must not be zero-sized, since init_validR only gives those validR.
Definition var_type_le (t t' : type) : bool :=
  match t , t' with
  | Tincomplete_array e , Tarray e' _ => bool_decide (e = e') && negb (zero_sized_array t')
  | _ , _ => bool_decide (t = t')
  end.
Definition var_type_ler : relation type := λ t t', var_type_le t t' = true.

Section var_type_ler.
  #[local] Instance var_type_le_refl : Reflexive var_type_ler.
  Proof. rewrite /var_type_ler/var_type_le => t. case_match; by rewrite ?bool_decide_eq_true_2. Qed.

  #[local] Instance var_type_le_trans : Transitive var_type_ler.
  Proof.
    rewrite /var_type_ler/var_type_le => t t' t''.
    destruct t; try by [intros; repeat (rewrite ->bool_decide_eq_true in *; simplify_eq)];
      destruct t'; try by [intros; repeat (rewrite ->bool_decide_eq_true in *; simplify_eq)].
  Qed.

  #[global] Instance: PreOrder var_type_ler := {}.
End var_type_ler.

Lemma var_type_le_inv t t' :
  var_type_le t t' = true ->
  t = t' \/
  exists e n, t = Tincomplete_array e /\ t' = Tarray e n /\ zero_sized_array t' = false.
Proof.
  rewrite /var_type_le => H.
  destruct t; try by left; apply bool_decide_eq_true_1 in H.
  destruct t'; try by left; apply bool_decide_eq_true_1 in H.
  move: H => /andb_prop [/bool_decide_eq_true_1 -> /negb_true_iff Hz].
  right. by eexists _, _.
Qed.

ObjVal_le a b holds when a has compatible (but possibly less) information than b. For example, a might be a declaration while b is a compatible definition.
Note: We reduce && so that it is lazy in its second argument.
Definition ObjValue_le (a b : ObjValue) : bool := Eval cbv beta iota zeta delta [ andb ] in
  let drop_norm t := drop_qualifiers $ normalize_type t in
  match a , b with
  | Ovar t oe , Ovar t' oe' =>
    var_type_le t t' &&
      match oe , oe' with
      | global_init.NoInit , global_init.NoInit => true
      | global_init.ImplicitInit , global_init.ImplicitInit => true
      | global_init.ImplicitInit , global_init.Init _ => true
      | global_init.Extern , _ => true
      | global_init.Delayed , global_init.Delayed => true
      | global_init.Init l , global_init.Init r => bool_decide (l = r)
      | _ , _ => false
      end
  | Ofunction f , Ofunction f' =>
    bool_decide (f.(f_cc) = f'.(f_cc)) &&
    bool_decide (f.(f_arity) = f'.(f_arity)) &&
    bool_decide (f.(f_return) = f'.(f_return)) &&
      match f.(f_body) , f'.(f_body) with
      | None , _ =>
        bool_decide (List.map (fun b => drop_norm b.2) f.(f_params) =
                     List.map (fun b => drop_norm b.2) f'.(f_params))
      | Some b , Some b' =>
        bool_decide (f.(f_params) = f'.(f_params)) &&
        bool_decide (f.(f_attrs) = f'.(f_attrs)) &&
        bool_decide (b = b')
      | _ , None => false
      end
  | Omethod m , Omethod m' =>
    bool_decide (m.(m_cc) = m'.(m_cc)) &&
    bool_decide (m.(m_arity) = m'.(m_arity)) &&
    bool_decide (m.(m_class) = m'.(m_class)) &&
    bool_decide (m.(m_this_qual) = m'.(m_this_qual)) &&
    bool_decide (m.(m_return) = m'.(m_return)) &&
    match m.(m_body) , m'.(m_body) with
    | None , _ =>
      bool_decide (List.map (fun b => drop_norm b.2) m.(m_params) =
                   List.map (fun b => drop_norm b.2) m'.(m_params))
    | Some b , Some b' =>
      bool_decide (m.(m_params) = m'.(m_params)) &&
      bool_decide (m.(m_attrs) = m'.(m_attrs)) &&
      OrDefault_le m.(m_exception) m'.(m_exception) b b'
    | _ , None => false
    end
  | Oconstructor c , Oconstructor c' =>
    bool_decide (c.(c_cc) = c'.(c_cc)) &&
    bool_decide (c.(c_arity) = c'.(c_arity)) &&
    bool_decide (c.(c_class) = c'.(c_class)) &&
    match c.(c_body) , c'.(c_body) with
    | None , _ =>
      bool_decide (List.map (fun x => drop_norm x.2) c.(c_params) =
                   List.map (fun x => drop_norm x.2) c'.(c_params))
    | _ , None => false
    | Some x , Some y =>
      bool_decide (c.(c_params) = c'.(c_params)) &&
      OrDefault_le c.(c_exception) c'.(c_exception) x y
    end
  | Odestructor dd , Odestructor dd' =>
    bool_decide (dd.(d_cc) = dd'.(d_cc)) &&
    bool_decide (dd.(d_class) = dd'.(d_class)) &&
    match dd.(d_body) , dd'.(d_body) with
    | None , _ => true
    | _ , None => false
    | Some x , Some y => DtorBody_le dd.(d_exception) dd'.(d_exception) x y
    end
  | _ , _ => false
  end.
Definition ObjValue_ler : relation ObjValue := λ g1 g2, ObjValue_le g1 g2 = true. (* TODO use Is_true *)
Arguments ObjValue_ler !_ _ /.

Section ObjValue_ler.
  #[local] Instance ObjValue_le_refl : Reflexive ObjValue_ler.
  Proof.
    rewrite /ObjValue_ler/ObjValue_le/DtorBody_le/OrDefault_le => -[t e||||].
    { have -> : var_type_le t t = true by change (var_type_ler t t).
      destruct e => //=. exact: bool_decide_eq_true_2. }
    all: smash.
  Qed.

  #[local] Instance ObjValue_le_trans : Transitive ObjValue_ler.
  Proof.
    intros a b c.
    destruct a, b => //=; (destruct c => //=; intros;
    repeat lazymatch goal with
           | H : Func' |- _ => destruct H; simpl in *
           | H : Method' |- _ => destruct H; simpl in *
           | H : Ctor' |- _ => destruct H; simpl in *
           | H : Dtor' |- _ => destruct H; simpl in *
           | H : false = true |- _ => inversion H
           | H : true = false |- _ => inversion H
           | H : bool_decide _ = true |- _ => apply bool_decide_eq_true_1 in H; subst
           | H : OrDefault_le ?xe ?ye ?x ?y = true |- _ => change (OrDefault_ler (xe,x) (ye,y)) in H
           | H : DtorBody_le ?xe ?ye ?x ?y = true |- _ => change (DtorBody_ler (xe,x) (ye,y)) in H
           | H : var_type_le ?x ?y = true |- _ => change (var_type_ler x y) in H
           | H : context [ if @bool_decide ?P ?DEC then _ else _ ] |- _ =>
                destruct (@bool_decide_reflect P DEC); subst
           | H : match ?X with _ => _ end = _ |- _ => destruct X eqn:?
           | |- bool_decide _ = _ => apply bool_decide_eq_true_2
           | |- OrDefault_le ?xe ?ye ?x ?y = true => change (OrDefault_ler (xe,x) (ye,y))
           | |- DtorBody_le ?xe ?ye ?x ?y = true => change (DtorBody_ler (xe,x) (ye,y))
           | |- context [ var_type_le ?x ?y ] =>
                rewrite (_ : var_type_le x y = true); [ | by change (var_type_ler x y); etrans; eauto ]
           | |- context [ bool_decide ?X ] => rewrite (bool_decide_eq_true_2 X); [ | by etrans; eauto ]
           end; try solve [ eauto | etrans; eauto ]).
  Qed.

  #[global] Instance: PreOrder ObjValue_ler := {}.
End ObjValue_ler.

Inclusion of symbol_tables

(* Ditto. *)
Definition sym_table_le_alt : symbol_table -> symbol_table -> Prop :=
  map_included (fun _ => ObjValue_ler).
#[global] Instance: PreOrder sym_table_le_alt.
Proof. apply @map_included_preorder, _. Qed.

Definition sym_table_le (a b : symbol_table) :=
  forall on v,
      a !! on = Some v ->
      exists v', b !! on = Some v' /\
            ObjValue_ler v v'.

Lemma sym_table_le_equiv te1 te2 : sym_table_le te1 te2 <-> sym_table_le_alt te1 te2.
Proof.
  apply iff_forall => i; unfold option_relation.
  (* XXX TC inference produces different results here. Hacky fix, as above. *)
  unfold symbol_table, globname, obj_name, ident.
  repeat case_match; naive_solver.
Qed.

#[global] Instance: PreOrder sym_table_le.
Proof.
  eapply preorder_proper.
  apply: sym_table_le_equiv.
  apply _.
Qed.

#[global,program]
Instance sym_table_le_dec a b : Decision (sym_table_le a b) :=
  match compat_le (fun l r => match l , r with
                           | None , _ => true
                           | Some _ , None => false
                           | Some l , Some r => ObjValue_le l r
                           end) a b as X return _ = X -> _ with
  | true => fun pf => left _
  | false => fun pf => right _
  end eq_refl.
Next Obligation.
  intros.
  match goal with
  | _ : compat_le ?F _ _ = _ |- _ => generalize (@compat_le_sound _ F a b (fun _ => eq_refl))
  end; rewrite pf.
  rewrite /sym_table_le.
  intros. specialize (H on). rewrite H0 in H.
  case_match.
  - eexists; split; eauto.
  - congruence.
Qed.
Next Obligation.
  intros.
  match goal with
  | _ : compat_le ?F _ _ = _ |- _ => generalize (@compat_le_sound _ F a b (fun _ => eq_refl))
  end; rewrite pf.
  rewrite /sym_table_le.
  intros. intro. destruct H.
  case_match; try congruence.
  destruct (H0 _ _ H1) as [ ? [ Heq ? ] ].
  rewrite Heq in H.
  red in H2. rewrite H in H2. congruence.
Qed.

(* TODO: complete_decl *)
#[local] Hint Constructors complete_decl complete_basic_type complete_type
  complete_pointee_type wellscoped_type wellscoped_types : core.

Lemma complete_decl_respects_GlobDecl_le {te g1 g2} :
  GlobDecl_ler g1 g2 ->
  complete_decl te g1 ->
  complete_decl te g2.
Proof.
  intros Hle Hct; inversion Hct; simplify_eq; destruct g2 => //=;
     simpl in *; do_bool_decide; subst; smash.
Qed.

#[local] Definition complete_decl_respects te2 g := ∀ te1,
  type_table_le te2 te1 ->
  complete_decl te1 g.
#[local] Definition complete_basic_type_respects te2 t := ∀ te1,
  type_table_le te2 te1 ->
  complete_basic_type te1 t.
#[local] Definition complete_pointee_type_respects te2 t := ∀ te1,
  type_table_le te2 te1 ->
  complete_pointee_type te1 t.
#[local] Definition complete_type_respects te2 t := ∀ te1,
  type_table_le te2 te1 ->
  complete_type te1 t.
#[local] Definition wellscoped_type_respects te2 ts := ∀ te1,
  type_table_le te2 te1 ->
  wellscoped_type te1 ts.
#[local] Definition wellscoped_types_respects te2 ts := ∀ te1,
  type_table_le te2 te1 ->
  wellscoped_types te1 ts.

(* Actual mutual induction. *)
Lemma complete_respects_sub_table_mut te2 :
  (∀ g : GlobDecl, complete_decl te2 g → complete_decl_respects te2 g) ∧
  (∀ t : type, complete_basic_type te2 t → complete_basic_type_respects te2 t) ∧
  (∀ t : type, complete_pointee_type te2 t → complete_pointee_type_respects te2 t) ∧
  (∀ t : type, complete_type te2 t → complete_type_respects te2 t) ∧
  (∀ t : type, wellscoped_type te2 t → wellscoped_type_respects te2 t) ∧
  (∀ t : list type, wellscoped_types te2 t → wellscoped_types_respects te2 t).
Proof.
  apply complete_mut_ind; try solve [intros; red; repeat_on_hyps (fun H => red in H); eauto].
  - intros * Hlook ? Hsub.
    destruct (Hsub _ _ Hlook) as (st1 & Hlook1 & _).
    eapply (complete_pt_named _ Hlook1).
  - intros * Hlook Hct IH ? Hsub.
    destruct (Hsub _ _ Hlook) as (st1 & Hlook1 & Hle).
    do 3 red in Hle. smash.
  - intros * Hlook Hct IH ? Hsub.
    destruct (Hsub _ _ Hlook) as (st1 & Hlook1 & Hle).
    apply (complete_named _ Hlook1).
    apply (complete_decl_respects_GlobDecl_le Hle), IH, Hsub.
  - intros * Hlook ??? Hsub.
    destruct (Hsub _ _ Hlook) as (st1 & Hlook1 & Hle).
    do 3 red in Hle. smash.
Qed.

Lemma complete_type_respects_sub_table te1 te2 t :
  type_table_le te2 te1 ->
  complete_type te2 t → complete_type te1 t.
Proof. intros. by eapply complete_respects_sub_table_mut. Qed.

Inclusion of static_assert

Definition asserts_le (sa1 sa2 : list StaticAssert) : Prop :=
  List.Forall (fun x => x ∈ sa2) sa1.

#[global] Instance: PreOrder asserts_le.
Proof.
  constructor.
  { red. intros. red. apply Forall_forall. done. }
  { red; intros.
    apply Forall_forall; intros.
    eapply Forall_forall in H; eauto.
    eapply Forall_forall in H0; eauto. }
Qed.

Inclusion on translation_unit


Record sub_module (a b : translation_unit) : Prop :=
{ types_compat : type_table_le a.(types) b.(types)
; syms_compat : sym_table_le a.(symbols) b.(symbols)
; asserts_compat : asserts_le a.(asserts) b.(asserts)
; abi_compat : a.(abi) = b.(abi) }.

Section sub_module.
  #[local] Instance: Reflexive sub_module.
  Proof. done. Qed.

  #[local] Instance: Transitive sub_module.
  Proof. intros ??? [] []; split; by etrans. Qed.

  #[global] Instance: PreOrder sub_module := {}.
End sub_module.
#[global] Instance: RewriteRelation sub_module := {}.

Decidability

sub_module is decidable
Definition module_le (a b : translation_unit) : bool :=
  Eval cbv beta iota zeta delta [ andb ] in
  bool_decide (a.(abi) = b.(abi)) &&
  bool_decide (type_table_le a.(types) b.(types)) &&
  bool_decide (sym_table_le a.(symbols) b.(symbols)) &&
  bool_decide (asserts_le a.(asserts) b.(asserts)).

Theorem module_le_sound : forall a b,
    if module_le a b then
      sub_module a b
    else
      ~sub_module a b.
Proof.
  rewrite /module_le; intros.
  repeat case_bool_decide.
  { constructor; eauto. }
  { intro C; inversion C; eauto. }
  { intro C; inversion C; eauto. }
  { intro C; inversion C; eauto. }
  { intro C; inversion C; eauto. }
Qed.

Theorem module_le_spec : forall a b,
    Bool.reflect (sub_module a b) (module_le a b).
Proof.
  intros. generalize (module_le_sound a b).
  destruct (module_le a b); constructor; eauto.
Qed.

#[global]
Instance sub_module_dec : RelDecision sub_module :=
  fun l r => match module_le l r as X
                return (if X then sub_module l r else ~sub_module l r) -> {_} + {_}
          with
          | true => fun pf => left pf
          | false => fun pf => right pf
          end (module_le_sound l r).

Lemma sub_module_preserves_globdecl {m1 m2 gn g1} :
  sub_module m1 m2 ->
  m1.(types) !! gn = Some g1 ->
  ∃ g2, m2.(types) !! gn = Some g2 ∧ GlobDecl_ler g1 g2.
Proof. move=>/types_compat + Heq => /(_ _ _ Heq). eauto. Qed.

Lemma sub_modules_agree_globdecl tu1 tu2 tu3 nm gd1 gd2 :
  sub_module tu1 tu3 ->
  sub_module tu2 tu3 ->
  tu1.(types) !! nm = Some gd1 ->
  tu2.(types) !! nm = Some gd2 ->
  GlobDecl_ler gd1 gd2 \/ GlobDecl_ler gd2 gd1.
Proof.
  move=> Hs1 Hs2
    /(sub_module_preserves_globdecl Hs1) [] gd3 [Hlook Hl1]
    /(sub_module_preserves_globdecl Hs2) [] ? [? Hl2]; simplify_eq.
  exact: GlobDecl_ler_join.
Qed.

Lemma sub_module_preserves_gstruct m1 m2 gn st :
  sub_module m1 m2 ->
  m1.(types) !! gn = Some (Gstruct st) ->
  m2.(types) !! gn = Some (Gstruct st).
Proof.
  move=> Hsub /(sub_module_preserves_globdecl Hsub) {Hsub m1 m2} [g2 [->]].
  destruct g2 => //=. intros; do_bool_decide; subst; smash.
Qed.

Lemma sub_module_preserves_gunion m1 m2 gn un :
  sub_module m1 m2 ->
  m1.(types) !! gn = Some (Gunion un) ->
  m2.(types) !! gn = Some (Gunion un).
Proof.
  move=> Hsub /(sub_module_preserves_globdecl Hsub) {Hsub m1 m2} [g2 [->]].
  destruct g2 => //=; smash.
Qed.

Lemma sub_module_preserves_gopaque m1 m2 gn o :
  sub_module m1 m2 ->
  m1.(types) !! gn = Some (Gopaque o) ->
  m2.(types) !! gn = Some (Gopaque o).
Proof.
  move=> Hsub /(sub_module_preserves_globdecl Hsub) {Hsub m1 m2} [g2 [->]].
  destruct g2 => //=; smash.
Qed.

(* For enums, names1 and names2 need not be related, as specified by GlobDecl_le.
TODO: https://eel.is/c++draft/basic.def.odr13 restricts this to anonymous enums. *)

Lemma sub_module_preserves_genum m1 m2 gn ty names1 :
  sub_module m1 m2 ->
  m1.(types) !! gn = Some (Genum ty names1) ->
  exists names2, m2.(types) !! gn = Some (Genum ty names2).
Proof.
  move=> Hsub /(sub_module_preserves_globdecl Hsub) {Hsub m1 m2} [g2 [->]].
  destruct g2 => //=; smash.
Qed.

Lemma sub_module_preserves_gconstant m1 m2 gn t e :
  sub_module m1 m2 ->
  m1.(types) !! gn = Some (Gconstant t (Some e)) ->
  m2.(types) !! gn = Some (Gconstant t (Some e)).
Proof.
  move=> Hsub /(sub_module_preserves_globdecl Hsub) {Hsub m1 m2} [g2 [->]].
  rewrite /GlobDecl_ler /GlobDecl_le; smash.
Qed.

Lemma sub_module_preserves_gtypedef m1 m2 gn t :
  sub_module m1 m2 ->
  m1.(types) !! gn = Some (Gtypedef t) ->
  m2.(types) !! gn = Some (Gtypedef t).
Proof.
  move=> Hsub /(sub_module_preserves_globdecl Hsub) [g2 [->]].
  destruct g2 => //=; intros; smash.
Qed.

#[global] Instance byte_order_proper : Proper (sub_module ==> eq) byte_order.
Proof. rewrite /byte_order. destruct 1; congruence. Qed.
#[global] Instance byte_order_flip_proper : Proper (flip sub_module ==> eq) byte_order.
Proof. rewrite /byte_order. destruct 1; congruence. Qed.

Lemma complete_type_respects_sub_module tt1 tt2 t :
  sub_module tt2 tt1 ->
  complete_type tt2.(types) t -> complete_type tt1.(types) t.
Proof. move=> /types_compat Hsub Hct. exact: complete_type_respects_sub_table. Qed.

(* class_compatible a b c states that translation units a and b have
   the same definitions for the class cls (including all transitive
   base classes)

   this is necessary, e.g. when code in translation unit a wants to call
   via a virtual table that was constructed in translation unit b
 *)

Inductive class_compatible (a b : translation_unit) (cls : name) : Prop :=
| Class_compat {st}
               (_ : a.(types) !! cls = Some (Gstruct st))
               (_ : b.(types) !! cls = Some (Gstruct st))
               (_ : forall base, In base (map fst st.(s_bases)) ->
                            class_compatible a b base).

#[local]
Lemma arguments_same xs : forall ys,
    map (λ b : ident * type, drop_qualifiers (normalize_type b.2)) xs = map (λ b : ident * type, drop_qualifiers (normalize_type b.2)) ys ->
    map (λ t : type, to_arg_type (normalize_type t)) xs.*2 = map (λ t : type, to_arg_type (normalize_type t)) ys.*2.
Proof.
  induction xs; destruct ys; simpl; intros; try congruence.
  inversion H; clear H; subst.
  f_equal; eauto.
  revert H1; clear.
  rewrite /to_arg_type. rewrite !qual_norm_decompose_type.
  rewrite !drop_qualifiers_decompose_type.
  destruct (decompose_type (normalize_type a.2)), (decompose_type (normalize_type p.2)); simpl in *; intros; subst.
  auto.
Qed.

Only an array variable declared without its major bound changes type.
Lemma type_of_value_ObjValue_ler a b :
  ObjValue_ler a b ->
  type_of_value a = type_of_value b \/
  exists e n, type_of_value a = Tincomplete_array e /\ type_of_value b = Tarray e n.
Proof.
  destruct a, b; simpl; try congruence.
  { case/andb_prop => /var_type_le_inv [->|[e [n [-> [-> _]]]]] _; [by left|right].
    rewrite /type_of_value/=. by eexists _, _. }
  all: left; simpl; try congruence;
    repeat first [ case_bool_decide | case_match ]; subst; try congruence; simpl; eauto.
  all: intros; rewrite /type_of_value/=; try congruence.
  all: f_equal; f_equal; try congruence; try solve [eauto using arguments_same].
  all: f_equal; try congruence; eauto using arguments_same.
Qed.

Mismatches

module_le only says whether sub_module a b holds. When it does not, sub_module_mismatch.compute a b says why, with one field per conjunct of sub_module; sub_module_mismatch.spec shows that it is empty exactly when sub_module a b holds. Like module_le, it is meant for vm_compute on closed translation units, and makes one pass over each table.

Module sub_module_mismatch.

  Variant kind : Set :=
  | Missing
the name has no entry on the right
  | Incompatible
its entry on the right is not _le the one on the left
.
  #[global] Instance kind_eq_dec : EqDecision kind.
  Proof. solve_decision. Defined.

  Section table_mismatches.
    Context {T : Type} (le : T -> T -> bool).

    Definition step (b : NM.t T) (n : name) (v : T)
        (acc : list (name * kind)) : list (name * kind) :=
      match b !! n with
      | Some v' => if le v v' then acc else (n, Incompatible) :: acc
      | None => (n, Missing) :: acc
      end.

The names of table a that are missing from table b or not le their entry there. A fold, like compat_le, so that it allocates nothing when everything matches.
    Definition table_mismatches (a b : NM.t T) : list (name * kind) :=
      NM.fold (step b) a [].

    Definition ok (b : NM.t T) (p : name * T) : Prop :=
      exists v', b !! p.1 = Some v' /\ le p.2 v' = true.

    Lemma fold_step_nil b l acc :
      fold_left (fun acc p => step b p.1 p.2 acc) l acc = [] <->
      acc = [] /\ List.Forall (ok b) l.
    Proof.
      revert acc; induction l as [|[n v] l IH] => acc /=.
      - rewrite List.Forall_nil_iff. tauto.
      - rewrite IH List.Forall_cons_iff /step /ok /=.
        repeat case_match; try naive_solver.
        split; [by intros [? ?]|intros (_ & (? & [= <-] & ?) & _); congruence].
    Qed.

    Lemma table_mismatches_nil_iff a b :
      table_mismatches a b = [] <->
      forall n v, a !! n = Some v -> exists v', b !! n = Some v' /\ le v v' = true.
    Proof.
      rewrite /table_mismatches NM.fold_1 fold_step_nil List.Forall_forall.
      split.
      - intros [_ Hok] n v Hn.
        rewrite /lookup /NM.map_lookup in Hn.
        apply NM.find_2, NM.elements_1, SetoidList.InA_alt in Hn as [[n' v'] [[Hk Hv] Hin]].
        simpl in Hk, Hv; subst v'. apply NM.eqL in Hk; subst n'. exact (Hok _ Hin).
      - intros Hok; split; [done|]. intros [n v] Hin. apply Hok.
        rewrite /lookup /NM.map_lookup. apply NM.find_1, NM.elements_2, SetoidList.InA_alt.
        exists (n, v). split; [split; [apply NM.Key.eq_refl|done]|done].
    Qed.
  End table_mismatches.

  Record t : Set := {
    abi_mismatch : bool;
the ABIs differ
the left's asserts missing on the right
  }.
  #[global] Instance t_eq_dec : EqDecision t.
  Proof. solve_decision. Qed.

  Definition compute (a b : translation_unit) : t :=
    {| abi_mismatch := bool_decide (a.(abi) <> b.(abi));
       type_mismatches := table_mismatches GlobDecl_le a.(types) b.(types);
       symbol_mismatches := table_mismatches ObjValue_le a.(symbols) b.(symbols);
       assert_mismatches := filter (fun x => x ∉ b.(asserts)) a.(asserts) |}.

  Definition empty : t :=
    {| abi_mismatch := false; type_mismatches := []; symbol_mismatches := [];
       assert_mismatches := [] |}.

  Theorem spec a b :
    sub_module a b <-> compute a b = empty.
  Proof.
    rewrite /compute /empty. split.
    - intros [Ht Hs Ha Habi]. f_equal.
      + apply bool_decide_eq_false. tauto.
      + apply table_mismatches_nil_iff => n v Hn.
        destruct (Ht n v Hn) as (v' & ? & Hle). exists v'.
        rewrite /GlobDecl_ler in Hle. by destruct (GlobDecl_le v v').
      + apply table_mismatches_nil_iff => n v Hn. exact (Hs n v Hn).
      + by apply list_filter_not_empty_iff.
    - intros [= Habi Ht Hs Ha]. constructor.
      + intros gn gv Hg.
        destruct (proj1 (table_mismatches_nil_iff _ _ _) Ht gn gv Hg) as (gv' & ? & Hle).
        exists gv'. by rewrite /GlobDecl_ler Hle.
      + intros on v Hv. exact (proj1 (table_mismatches_nil_iff _ _ _) Hs on v Hv).
      + by move: Ha; rewrite list_filter_not_empty_iff.
      + apply bool_decide_eq_false in Habi. by destruct (decide (a.(abi) = b.(abi))).
  Qed.

  Lemma sound a b :
    compute a b = empty -> sub_module a b.
  Proof. apply spec. Qed.

  Lemma not_sub_module a b :
    compute a b <> empty -> ~ sub_module a b.
  Proof. by rewrite spec. Qed.

End sub_module_mismatch.