Qcert.Translation.NNRCtoDNNRC


Section NNRCtoDNNRC.



  Context {fruntime:foreign_runtime}.

  Fixpoint nnrc_to_dnnrc {A:Set} {plug_type:Set} (annot:A) (tenv:list (var×dlocalization)) (n:nnrc) : dnnrc A plug_type :=
    match n with
    | NNRCVar v
      match lookup equiv_dec tenv v with
      | NoneDNNRCVar annot v
      | Some VlocalDNNRCVar annot v
      | Some VdistrDNNRCCollect annot (DNNRCVar annot v)
      end
    | NNRCConst dDNNRCConst annot d
    | NNRCBinop b n1 n2DNNRCBinop annot b (nnrc_to_dnnrc annot tenv n1) (nnrc_to_dnnrc annot tenv n2)
    | NNRCUnop u n1DNNRCUnop annot u (nnrc_to_dnnrc annot tenv n1)
    | NNRCLet v n1 n2DNNRCLet annot v (nnrc_to_dnnrc annot tenv n1) (nnrc_to_dnnrc annot ((v,Vlocal)::tenv) n2)
    | NNRCFor v n1 n2DNNRCFor annot v (nnrc_to_dnnrc annot tenv n1) (nnrc_to_dnnrc annot ((v,Vlocal)::tenv) n2)
    | NNRCIf n1 n2 n3DNNRCIf annot (nnrc_to_dnnrc annot tenv n1) (nnrc_to_dnnrc annot tenv n2) (nnrc_to_dnnrc annot tenv n3)
    | NNRCEither n0 v1 n1 v2 n2
      DNNRCEither annot (nnrc_to_dnnrc annot tenv n0) v1 (nnrc_to_dnnrc annot ((v1,Vlocal)::tenv) n1) v2 (nnrc_to_dnnrc annot ((v2,Vlocal)::tenv) n2)
    | NNRCGroupBy g sl n1
      DNNRCGroupBy annot g sl (nnrc_to_dnnrc annot tenv n1)
    end.

  Definition wf_localization (tl:option dlocalization) (dl:option ddata) :=
    match tl, dl with
    | Some Vlocal, Some (Dlocal _) ⇒ True
    | Some Vlocal, Some (Ddistr _) ⇒ False
    | Some Vdistr, Some (Ddistr _) ⇒ True
    | Some Vdistr, Some (Dlocal _) ⇒ False
    | _, _False
    end.

  Definition wf_denv (tenv:list (var×dlocalization)) (denv:list (var×ddata)) :=
    (domain tenv = domain denv)
     v, wf_localization (lookup equiv_dec tenv v) (lookup equiv_dec denv v).

  Lemma wf_denv_cons v d tenv denv :
    wf_denv tenv denv wf_denv ((v, Vlocal) :: tenv) ((v, Dlocal d) :: denv).

  Lemma lookup_denv_local v d denv :
    lookup equiv_dec denv v = Some (Dlocal d)
    lift Dlocal (lookup equiv_dec (localize_denv denv) v) =
    lookup equiv_dec denv v.

  Lemma lookup_denv_distr v l denv :
    lookup equiv_dec denv v = Some (Ddistr l)
    lift Dlocal (lookup equiv_dec (localize_denv denv) v) =
    lift Dlocal (olift checkDistrAndCollect (lookup equiv_dec denv v)).

  Lemma rmap_nnrc_to_dnnrc_correct {A:Set} {plug_type:Set} {plug:AlgPlug plug_type} (h:brand_relation_t) (annot:A) tenv denv v l n2 :
    wf_denv tenv denv
    ( (tenv : list (var × dlocalization))
            (denv : list (var × ddata)),
        wf_denv tenv denv
        lift Dlocal (nnrc_core_eval h (localize_denv denv) n2) =
        dnnrc_eval h denv (nnrc_to_dnnrc annot tenv n2))
    rmap
      (fun d1 : data
         olift checkLocal
               (dnnrc_eval h ((v, Dlocal d1) :: denv)
                          (nnrc_to_dnnrc annot ((v, Vlocal) :: tenv) n2))) l = rmap (fun d1 : datannrc_core_eval h ((v, d1) :: localize_denv denv) n2) l.

  Lemma nnrc_to_dnnrc_correct {A plug_type:Set} (annot:A) {plug:AlgPlug plug_type} h (tenv:list (var×dlocalization)) (n:nnrc) :
     denv:list (var×ddata),
      wf_denv tenv denv
      lift Dlocal (nnrc_core_eval h (localize_denv denv) n) = dnnrc_eval h denv (nnrc_to_dnnrc annot tenv n).

  Context {ftype: ForeignType.foreign_type}.
  Definition nnrc_to_dnnrc_dataset {A:Set} (annot:A) (tenv:list (var×dlocalization)) (n:nnrc) := @nnrc_to_dnnrc A dataset annot (mkConstants tenv) n.

End NNRCtoDNNRC.