Qcert.DNNRC.Typing.TOpsInferSub


Section TOpsInferSub.



  Context {fdata:foreign_data}.
  Context {ftype:foreign_type}.
  Context {fdtyping:foreign_data_typing}.
  Context {m:brand_model}.

  Section b.

    Context {fbop:foreign_binary_op}.
    Context {fboptyping:foreign_binary_op_typing}.


    Definition infer_binop_type_sub
               (b:binOp) (τ τ:rtype) : option (rtype×rtype×rtype) :=
      match b with
      | AEq
        let τcommon := τ τ in
        Some (Bool, τcommon, τcommon)
      | AConcat
        match τ ==b , τ ==b with
        | true, trueSome (, , )
        | true, false
           lift (fun _(τ, , τ)) (tunrec τ)
        | false, true
           lift (fun _(τ, τ, )) (tunrec τ)
        | false, false
          lift (fun τ ⇒ (τ, τ, τ)) (trecConcat τ τ)
        end
      | AMergeConcat
        match τ ==b , τ ==b with
        | true, trueSome (Coll , , )
        | true, false
           lift (fun _(Coll τ, , τ)) (tunrec τ)
        | false, true
           lift (fun _(Coll τ, τ, )) (tunrec τ)
        | false, false
          lift (fun τ ⇒ (Coll τ, τ, τ)) (tmergeConcat τ τ)
        end
      | AAnd
        match subtype_dec τ Bool, subtype_dec τ Bool with
        | left _, left _Some (Bool, Bool, Bool)
        | _, _None
      end
      | AOr
        match subtype_dec τ Bool, subtype_dec τ Bool with
        | left _, left _Some (Bool, Bool, Bool)
        | _, _None
        end
      | ABArith _
        match subtype_dec τ Nat, subtype_dec τ Nat with
        | left _, left _Some (Nat, Nat, Nat)
        | _, _None
        end
      | ALt
      | ALe
        match subtype_dec τ Nat, subtype_dec τ Nat with
        | left _, left _Some (Bool, Nat, Nat)
        | _, _None
        end
      | AUnion | AMinus | AMin | AMax
        let τcommon := τ τ in
        if (tuncoll τcommon)
        then Some (τcommon, τcommon, τcommon)
        else None
      
      | AContains
        if τ ==b
        then Some (Bool, τ, τ)
        else lift (fun τ₂'
                     let τ := τ τ₂' in
                     (Bool, τ, Coll τ))
                  (tuncoll τ)
      | ASConcat
        match subtype_dec τ String, subtype_dec τ String with
        | left _, left _Some (String, String, String)
        | _, _None
        end
      | AForeignBinaryOp fb
        foreign_binary_op_typing_infer_sub fb τ τ
      end.

  End b.

  Section u.
    Context {fuop:foreign_unary_op}.
    Context {fuoptyping:foreign_unary_op_typing}.


    Definition infer_unop_type_sub (u:unaryOp) (τ:rtype) : option (rtype×rtype) :=
      match u with
      | AIdOpSome (τ,τ)
      | ANeg
        if subtype_dec τ Bool
        then Some (Bool, Bool)
        else None
      | ACollSome (Coll τ,τ)
      | ASingleton
        let τ₁' := τ (Coll ) in
        lift (fun τ ⇒ (τ, τ₁')) (tsingleton τ₁')
      | AFlatten
        let τ₁' := τ (Coll (Coll )) in
        bind (tuncoll τ₁')
             (fun τ₁in
                lift (fun _(τ₁in, τ₁'))
                     (tuncoll τ₁in))
      | ADistinct
        let τ₁' := τ (Coll ) in
        lift (fun τ ⇒ (Coll τ, τ₁')) (tuncoll τ₁')
      | AOrderBy sl
        let τ₁' := τ (Coll ) in
        match (tuncoll τ₁') with
        | Some τ₁₀
          match tunrecsortable (List.map fst sl) τ₁₀ with
          | Some _Some (τ₁', τ₁')
          | NoneNone
          end
        | NoneNone
        end
      | ARec sSome (Rec Closed ((s, τ)::nil) eq_refl, τ)
        
      | ADot s
        if τ ==
        then Some (, )
        else lift (fun τ ⇒ (τ, τ)) (tunrecdot s τ)
      | ARecRemove s
        if τ ==
        then Some (, )
        else lift (fun τ ⇒ (τ, τ)) (tunrecremove s τ)
      | ARecProject sl
        if τ ==
        then Some (, )
        else lift (fun τ ⇒ (τ, τ)) (tunrecproject sl τ)
      | ACount
        let τ₁' := τ (Coll ) in
        lift (fun τ ⇒ (Nat, τ₁')) (tuncoll τ₁')
      | ASum
      | ANumMin
      | ANumMax
      | AArithMean
        if subtype_dec τ (Coll Nat)
        then Some (Nat, Coll Nat)
        else None
      | AToString
        Some (String, τ)
      | ASubstring _ _
        if (subtype_dec τ String)
        then Some (String, String)
        else None
      | ALike _ _
        if (subtype_dec τ String)
        then Some (Bool, String)
        else None
      | ALeft
        Some (Either τ , τ)
      | ARight
        Some (Either τ, τ)
      | ABrand b
        if (subtype_dec τ (brands_type b))
        then Some (Brand b, τ)
        else None
    | AUnbrand
        if τ ==
        then
          Some (, )
        else
          match `τ with
          | Brand₀ bSome (brands_type b, τ)
          | _None
          end
    | ACast b
      if τ ==
      then
        Some (, )
      else
        match `τ with
        | Brand₀ _Some (Option (Brand b), τ)
        | _None
        end
    | AUArith op
      if subtype_dec τ Nat
      then Some (Nat, Nat)
      else None
    | AForeignUnaryOp fu
        foreign_unary_op_typing_infer_sub fu τ
      end.

  End u.
End TOpsInferSub.