Qcert.NRAEnv.Typing.TcNRAEnv


Section TcNRAEnv.




Typing for NRA
  Section typ.
    Context {m:basic_model}.
    Contextconstants:tbindings).

  Inductive cnraenv_type : cnraenv rtype rtype rtype Prop :=
  | ANTIDenv τ} :
      cnraenv_type ANID τenv τ τ
  | ANTConstenv τin τout} c :
      data_type (normalize_data brand_relation_brands c) τout cnraenv_type (ANConst c) τenv τin τout
  | ANTBinopenv τin τ τ τout} b op1 op2 :
      binOp_type b τ τ τout
      cnraenv_type op1 τenv τin τ
      cnraenv_type op2 τenv τin τ
      cnraenv_type (ANBinop b op1 op2) τenv τin τout
  | ANTUnopenv τin τ τout} u op :
      unaryOp_type u τ τout
      cnraenv_type op τenv τin τ
      cnraenv_type (ANUnop u op) τenv τin τout
  | ANTMapenv τin τ τ} op1 op2 :
      cnraenv_type op1 τenv τ τ
      cnraenv_type op2 τenv τin (Coll τ)
      cnraenv_type (ANMap op1 op2) τenv τin (Coll τ)
  | ANTMapConcatenv τin τ τ τ} op1 op2 pf1 pf2 pf3 :
      cnraenv_type op1 τenv (Rec Closed τ pf1) (Coll (Rec Closed τ pf2))
      cnraenv_type op2 τenv τin (Coll (Rec Closed τ pf1))
      rec_concat_sort τ τ = τ
      cnraenv_type (ANMapConcat op1 op2) τenv τin (Coll (Rec Closed τ pf3))
  | ANTProductenv τin τ τ τ} op1 op2 pf1 pf2 pf3 :
      cnraenv_type op1 τenv τin (Coll (Rec Closed τ pf1))
      cnraenv_type op2 τenv τin (Coll (Rec Closed τ pf2))
      rec_concat_sort τ τ = τ
      cnraenv_type (ANProduct op1 op2) τenv τin (Coll (Rec Closed τ pf3))
  | ANTSelectenv τin τ} op1 op2 :
      cnraenv_type op1 τenv τ Bool
      cnraenv_type op2 τenv τin (Coll τ)
      cnraenv_type (ANSelect op1 op2) τenv τin (Coll τ)
  | ANTDefaultenv τin τ} op1 op2 :
      cnraenv_type op1 τenv τin (Coll τ)
      cnraenv_type op2 τenv τin (Coll τ)
      cnraenv_type (ANDefault op1 op2) τenv τin (Coll τ)
  | ANTEitherenv τl τr τout} opl opr :
      cnraenv_type opl τenv τl τout
      cnraenv_type opr τenv τr τout
      cnraenv_type (ANEither opl opr) τenv (Either τl τr) τout
  | ANTEitherConcatenv τin rll pfl rlr pfr rlo pfo lo ro} op1 op2 pflo pfro :
      cnraenv_type op1 τenv τin (Either (Rec Closed rll pfl) (Rec Closed rlr pfr))
      cnraenv_type op2 τenv τin (Rec Closed rlo pfo)
      rec_concat_sort rll rlo = lo
      rec_concat_sort rlr rlo = ro
      cnraenv_type (ANEitherConcat op1 op2) τenv τin (Either (Rec Closed lo pflo) (Rec Closed ro pfro))
  | ANTAppenv τin τ1 τ2} op2 op1 :
      cnraenv_type op1 τenv τin τ1
      cnraenv_type op2 τenv τ1 τ2
      cnraenv_type (ANApp op2 op1) τenv τin τ2
  | ANTGetConstantenv τin τout} s :
      tdot τconstants s = Some τout
      cnraenv_type (ANGetConstant s) τenv τin τout
  | ANTEnvenv τin} :
      cnraenv_type ANEnv τenv τin τenv
  | ANTAppEnvenv τenv' τin τ2} op2 op1 :
      cnraenv_type op1 τenv τin τenv'
      cnraenv_type op2 τenv' τin τ2
      cnraenv_type (ANAppEnv op2 op1) τenv τin τ2
  | ANTMapEnvenv τin τ} op1 :
      cnraenv_type op1 τenv τin τ
      cnraenv_type (ANMapEnv op1) (Coll τenv) τin (Coll τ).
  End typ.

  Notation "Op ▷ A >=> B ⊣ C ; E" := (cnraenv_type C Op E A B) (at level 70).

Type lemmas for individual cnraenvebraic expressions

  Context {m:basic_model}.

  Lemma rmap_typedc} {τenv τ τ : rtype} c (op1 : cnraenv) (env:data) (dl : list data) :
    (bindings_type c τc)
    (env τenv)
    (Forall (fun d : datadata_type d τ) dl)
    (op1 τ >=> τ τc; τenv)
    ( d : data,
       data_type d τ x : data, brand_relation_brands ⊢ₑ op1 @ₑ d c; env = Some x x τ)
     x : list data, (rmap (cnraenv_eval brand_relation_brands c op1 env) dl = Some x) data_type (dcoll x) (Coll τ).

  Lemma rmap_env_typedc} {τenv τ τ : rtype} c (op1 : cnraenv) (x0:data) (dl : list data) :
    bindings_type c τc
    (x0 τ)
    (Forall (fun d : datadata_type d τenv) dl)
    (op1 τ >=> τ τc;τenv)
    ( env : data,
       data_type env τenv
        d : data,
         data_type d τ
          x : data, brand_relation_brands ⊢ₑ op1 @ₑ d c;env = Some x data_type x τ)
     x : list data, (rmap (fun env' ⇒ (cnraenv_eval brand_relation_brands c op1 env' x0)) dl = Some x) data_type (dcoll x) (Coll τ).

  Lemma recover_rec k d r τ pf:
    data_type d r
    ` r =
    Rec₀ k
      (map
         (fun x : string × {τ : rtype₀ | wf_rtype₀ τ = true}
            (fst x, ` (snd x))) τ)
    data_type d (Rec k τ pf).

  Lemma recover_rec_forall k l r τ pf:
    Forall (fun d : datadata_type d r) l
    ` r =
    Rec₀ k
      (map
         (fun x : string × {τ : rtype₀ | wf_rtype₀ τ = true}
            (fst x, ` (snd x))) τ)
    Forall (fun d : datadata_type d (Rec k τ pf)) l.

  Lemma omap_concat_typed_env
        (τenv:rtype) (τ τ τ: list (string × rtype)) (env:data) (dl2: list data)
        (x : list (string × data)) pf1 pf2 pf3:
    (data_type env τenv)
    ( x : data, In x dl2 data_type x (Rec Closed τ pf2))
    rec_concat_sort τ τ = τ
    data_type (drec x) (Rec Closed τ pf1)
    ( y : list data,
       rmap (fun x1 : dataorecconcat (drec x) x1) dl2
       = Some y data_type (dcoll y) (Coll (Rec Closed τ pf3))).

  Lemma rproduct_typed_envenv:rtype} {τ τ τ: list (string × rtype)} (env:data) (dl dl0: list data) pf1 pf2 pf3:
    data_type env τenv
    Forall (fun d : datadata_type d (Rec Closed τ pf2)) dl0
    Forall (fun d : datadata_type d (Rec Closed τ pf1)) dl
    rec_concat_sort τ τ = τ
     x : list data, (rproduct dl dl0 = Some x) data_type (dcoll x) (Coll (Rec Closed τ pf3)).

  Lemma data_type_concat l1 l2 τ:
    data_type (dcoll l1) (Coll τ)
    data_type (dcoll l2) (Coll τ)
    data_type (dcoll (l1 ++ l2)) (Coll τ).

  Lemma rmap_concat_typed_envc} {τenv:rtype} {τ τ τ : list (string × rtype)} (op1 : cnraenv) c (env:data) (dl: list data) pf1 pf2 pf3:
    bindings_type c τc
    env τenv
    rec_concat_sort τ τ = τ
    Forall (fun d : datadata_type d (Rec Closed τ pf1)) dl
    (op1 Rec Closed τ pf1 >=> Coll (Rec Closed τ pf2) τc;τenv)
    ( d : data,
                data_type d (Rec Closed τ pf1)
                 x : data,
                   brand_relation_brands ⊢ₑ op1 @ₑ d c;env = Some x data_type x (Coll (Rec Closed τ pf2)))
     x : list data, (rmap_concat (cnraenv_eval brand_relation_brands c op1 env) dl = Some x) data_type (dcoll x) (Coll (Rec Closed τ pf3)).

  Lemma rmap_concat_typed2_envc} {τenv:rtype} {τ τ τ : list (string × rtype)} τin c (op1 : cnraenv) (env:data) y (dl: list data) pf1 pf2 pf3:
    bindings_type c τc
    env τenv
    rec_concat_sort τ τ = τ
    Forall (fun d : datadata_type d (Rec Closed τ pf1)) dl
    (op1 τin >=> Coll (Rec Closed τ pf2) τc;τenv)
    ( d : data,
                data_type d (Rec Closed τ pf1)
                 x : data,
                   brand_relation_brands ⊢ₑ op1 @ₑ y c;env = Some x data_type x (Coll (Rec Closed τ pf2)))
     x : list data, (rmap_concat (fun zbrand_relation_brands ⊢ₑ op1@ y c;env) dl = Some x) data_type (dcoll x) (Coll (Rec Closed τ pf3)).

Main typing soundness theorem for NRAEnv
Corrolaries of the main type soudness theorem

  Definition typed_cnraenv_totalc} {τenv τin τout} (op:cnraenv) (HOpT: op τin >=> τout τc;τenv) c (env:data) (d:data)
    (dt_c: bindings_type c τc) :
    (env τenv)
    (d τin)
    { x:data | x τout }.

  Definition tcnraenv_evalc} {τenv τin τout} (op:cnraenv) (HOpT: op τin >=> τout τc;τenv) c (env:data) (d:data)
             (dt_c: bindings_type c τc) :
    (env τenv) (d τin) data.


Auxiliary lemmas specific to some of the NRA expressions used in the translation

  Definition pat_context_type tconst tbind tpid : rtype :=
    Rec Closed (("PBIND"%string,tbind) :: ("PCONST"%string,tconst) :: ("PDATA"%string,tpid) :: nil) (eq_refl _).

  Lemma ATdot {p s τin τ pf τout}:
      p τin >=> Rec Closed τ pf
      tdot τ s = Some τout
      AUnop (ADot s) p τin >=> τout.

  Lemma ATdot_inv {p s τin τout}:
      AUnop (ADot s) p τin >=> τout
       τ pf k,
      p τin >=> Rec k τ pf
      tdot τ s = Some τout.

  Lemma ATpat_data τc τ τin :
    pat_data pat_context_type τc τ τin >=> τin.

  Lemma ATpat_data_inv' k τc τ τin pf τout:
      pat_data Rec k [("PBIND"%string, τ); ("PCONST"%string, τc); ("PDATA"%string, τin)] pf >=> τout
      τin = τout.

  Lemma ATpat_data_inv τc τ τin τout:
    pat_data pat_context_type τc τ τin >=> τout
    τin = τout.


  Lemma ATunnest_two (s1 s2:string) (op:NRA.nra) τin τ pf1 τs τrem pf2 :
    op τin >=> (Coll (Rec Closed τ pf1))
    tdot τ s1 = Some (Coll τs)
    τrem = (rremove (rec_concat_sort τ ((s2,τs)::nil)) s1)
    NRAExt.unnest_two s1 s2 op
               τin >=> Coll (Rec Closed τrem pf2).


  Lemma ATunnest_two_inv (s1 s2:string) (op:NRA.nra) τin rec :
    unnest_two s1 s2 op
                       τin >=> Coll rec
     τ pf1 τs τrem pf2,
    op τin >=> (Coll (Rec Closed τ pf1))
    tdot τ s1 = Some (Coll τs)
    rec = (Rec Closed τrem pf2)
    τrem = (rremove (rec_concat_sort τ ((s2,τs)::nil)) s1).

    Lemma ATRecEither s τl τr pf1 pf2:
      nra_type (ARecEither s) (Either τl τr)
               (Either
                  (Rec Closed ((s,τl)::nil) pf1)
                  (Rec Closed ((s,τr)::nil) pf2)).

    Theorem typed_cnraenv_to_typed_nrac} pfenv τin τout} (op:cnraenv):
    (cnraenv_type τc op τenv τin τout) (nra_type (nra_of_cnraenv op) (pat_context_type (Rec Closed τc pf) τenv τin) τout).

  Lemma fold_pat_context_type (c env d: {τ : rtype₀ | wf_rtype₀ τ = true}) pf :
    Rec Closed [("PBIND"%string, env); ("PCONST"%string, c); ("PDATA"%string, d)] pf
    = pat_context_type c env d.




      Lemma UIP_bool {a b:bool} (pf1 pf2:a = b) : pf1 = pf2.




  Lemma typed_cnraenv_to_typed_nra_inv' {k τc pf0 τenv τin τout pf} (op:cnraenv):
    nra_type (nra_of_cnraenv op) (Rec k [("PBIND"%string, τenv); ("PCONST"%string, (Rec Closed τc pf0)); ("PDATA"%string, τin)] pf) τout
    cnraenv_type τc op τenv τin τout.

  Theorem typed_cnraenv_to_typed_nra_invc} pfenv τin τout} (op:cnraenv):
    nra_type (nra_of_cnraenv op) (pat_context_type (Rec Closed τc pf) τenv τin) τout
    cnraenv_type τc op τenv τin τout.

  Lemma typed_cnraenv_const_sort_fc op τenv τin τout} :
    cnraenv_type (rec_sort τc) op τenv τin τout
    cnraenv_type τc op τenv τin τout.

  Lemma typed_cnraenv_const_sort_bc op τenv τin τout} :
      cnraenv_type τc op τenv τin τout
      cnraenv_type (rec_sort τc) op τenv τin τout.

  Lemma typed_cnraenv_const_sort τc op τenv τin τout :
    cnraenv_type (rec_sort τc) op τenv τin τout
    cnraenv_type τc op τenv τin τout.

End TcNRAEnv.


Notation "Op ▷ A >=> B ⊣ C ; E" := (cnraenv_type C Op E A B) (at level 70).
Notation "Op @▷ d ⊣ C ; e" := (tcnraenv_eval C Op e d) (at level 70).