Qcert.NRAEnv.Typing.TcNRAEnvInfer


Section TcNRAEnvInfer.




Type inference for NRA when given the type of the input



  Context {m:basic_model}.
  Contextconstants:list (string×rtype)).

  Fixpoint infer_cnraenv_type (e:cnraenv) (τenv τin:rtype) : option rtype :=
    match e with
      | ANIDSome τin
      | ANConst dinfer_data_type (normalize_data brand_relation_brands d)
      | ANBinop b op1 op2
        let binf τ:rtype) := infer_binop_type b τ τ in
        olift2 binf (infer_cnraenv_type op1 τenv τin) (infer_cnraenv_type op2 τenv τin)
      | ANUnop u op1
        let unf:rtype) := infer_unop_type u τ in
        olift unf (infer_cnraenv_type op1 τenv τin)
      | ANMap op1 op2
        let mapf:rtype) :=
            olift (fun xlift (fun yColl y) (infer_cnraenv_type op1 τenv x)) (tuncoll τ)
        in
        olift mapf (infer_cnraenv_type op2 τenv τin)
      | ANMapConcat op1 op2
        let mapconcatf:list (string×rtype)) :=
            match RecMaybe Closed τ with
              | NoneNone
              | Some τr₁
                match olift (tmapConcatOutput τ) (infer_cnraenv_type op1 τenv τr₁) with
                  | NoneNone
                  | Some τSome (Coll τ)
                end
            end
        in
        olift mapconcatf (olift tmapConcatInput (infer_cnraenv_type op2 τenv τin))
      | ANProduct op1 op2
        let mapconcatf:list (string×rtype)) :=
            match RecMaybe Closed τ with
              | NoneNone
              | Some τr₁
                match olift (tmapConcatOutput τ) (infer_cnraenv_type op2 τenv τin) with
                  | NoneNone
                  | Some τSome (Coll τ)
                end
            end
        in
        olift mapconcatf (olift tmapConcatInput (infer_cnraenv_type op1 τenv τin))
      | ANSelect op1 op2
        let selectf:rtype) :=
            match tuncoll τ with
              | Some τ₁'
                match infer_cnraenv_type op1 τenv τ₁' with
                  | Some τ
                    match `τ with
                      | Bool₀Some (Coll τ₁')
                      | _None
                    end
                  | NoneNone
                end
              | NoneNone
            end
        in
        olift selectf (infer_cnraenv_type op2 τenv τin)
      | ANDefault op1 op2
        match ((infer_cnraenv_type op1 τenv τin), (infer_cnraenv_type op2 τenv τin)) with
            | (Some τ₁', Some τ₂')
              match (tuncoll τ₁', tuncoll τ₂') with
                | (Some τ₁₀, Some τ₂₀)
                  if (`τ₁₀ == `τ₂₀) then Some τ₁' else None
                | _None
              end
            | (_, _)None
        end
      | ANEither op1 op2
        match tuneither τin with
        | Some (τl, τr)
          match ((infer_cnraenv_type op1 τenv τl), (infer_cnraenv_type op2 τenv τr)) with
          | (Some τ₁', Some τ₂')
            if (rtype_eq_dec τ₁' τ₂')
            then Some τ₁'
            else None
          | (_, _)None
          end
        | _None
        end
      | ANEitherConcat op1 op2
        match (infer_cnraenv_type op1 τenv τin, infer_cnraenv_type op2 τenv τin) with
        | (Some τeither, Some τrecplus)
          match tuneither τeither with
          | Some (τl, τr)
            match (trecConcat τl τrecplus, trecConcat τr τrecplus) with
            | (Some τrecl, Some τrecr)
              Some (Either τrecl τrecr)
            | (_, _)None
            end
          | NoneNone
          end
        | (_, _)None
        end
      | ANApp op1 op2
        let appf:rtype) := infer_cnraenv_type op1 τenv τ in
        olift appf (infer_cnraenv_type op2 τenv τin)
      | ANGetConstant s
        tdot τconstants s
      | ANEnv
        Some τenv
      | ANAppEnv op1 op2
        let appf:rtype) := infer_cnraenv_type op1 τ τin in
        olift appf (infer_cnraenv_type op2 τenv τin)
      | ANMapEnv op1
        let mapfenv':rtype) :=
            lift Coll (infer_cnraenv_type op1 τenv' τin)
        in
        olift mapf (tuncoll τenv)
    end.

  Lemma infer_cnraenv_type_correctenv τin τout:rtype) (e:cnraenv) :
    infer_cnraenv_type e τenv τin = Some τout
    cnraenv_type τconstants e τenv τin τout.


End TcNRAEnvInfer.