Qcert.Basic.Operators.RUnaryOpsSem


Section RUnaryOpsSem.




  Context {fdata:foreign_data}.

  Definition lifted_zbag (l : list data) : option (list Z) :=
    rmap (ondnat (fun xx)) l.
  Definition lifted_min (l : list data) : option data :=
    lift dnat (lift bnummin (lifted_zbag l)).
  Definition lifted_max (l : list data) : option data :=
    lift dnat (lift bnummax (lifted_zbag l)).

  Definition fun_of_arithuop (op:ArithUOp) : Z Z
  := match op with
         | ArithAbsZ.abs
         | ArithLog2Z.log2
         | ArithSqrtZ.sqrt
     end.

  Context {fuop:foreign_unary_op}.

  Context (h:brand_relation_t).

  Definition fun_of_unaryop (uop:unaryOp) : data option data :=
    match uop with
    | AIdOp
      fun dSome d
    | ANeg
      fun dunudbool negb d
    | AColl
      fun dSome (dcoll (d :: nil))
    | ASingleton
      fun d
        match d with
        | dcoll (d'::nil) ⇒ Some (dsome d')
        | dcoll _Some dnone
        | _None
        end
    | AFlatten
      fun dlift_oncoll (fun l ⇒ (lift dcoll (rflatten l))) d
    | ADistinct
      fun drondcoll (@bdistinct data data_eq_dec) d
    | AOrderBy sc
      fun ddata_sort sc d
    | ARec s
      fun dSome (drec ((s,d) :: nil))
    | ADot s
      fun d
        match d with
        | drec redot r s
        | _None
        end
    | ARecRemove s
      fun d
        match d with
        | drec rSome (drec (rremove r s))
        | _None
        end
    | ARecProject sl
      fun d
        match d with
        | drec rSome (drec (rproject r sl))
        | _None
        end
    | ACount
      fun dlift dnat (ondcoll (fun zZ_of_nat (bcount z)) d)
    | ASum
      fun dlift dnat (lift_oncoll dsum d)
    | ANumMin
      fun dmatch d with
               | dcoll llifted_min l
               | _None
               end
    | ANumMax
      fun dmatch d with
               | dcoll llifted_max l
               | _None
               end
    | AArithMean
      fun dlift dnat (lift_oncoll darithmean d)
    | AToString
      fun dSome (dstring (dataToString d))
    | ASubstring start olen
      fun d
                (match d with
                 | dstring s
                   Some (dstring (
                   let real_start :=
                       (match start with
                        | 0%Z ⇒ 0
                     | Z.pos pPos.to_nat p
                     | Z.neg n(String.length s) - (Pos.to_nat n)
                        end) in
                   let real_olen :=
                       match olen with
                       | Some len
                         match len with
                         | 0%Z ⇒ 0
                         | Z.pos pPos.to_nat p
                         | Z.neg n ⇒ 0
                         end
                       | None(String.length s) - real_start
                       end in
                   (substring real_start real_olen s)))
                 | _None
                 end)
    | ALike pat oescape
      fun d
        match d with
        | dstring sSome (dbool (string_like s pat oescape))
        | _None
        end
    | ALeft
      fun dSome (dleft d)
    | ARight
      fun dSome (dright d)
    | ABrand b
      fun dSome (dbrand (canon_brands h b) d)
    | AUnbrand
      fun dmatch d with
               | dbrand _ d'Some d'
               | _None
               end
    | ACast b
      fun d
        match d with
        | dbrand b' _
          if (sub_brands_dec h b' b)
          then
            Some (dsome d)
          else
            Some (dnone)
        | _None
        end
    | AUArith op
      fun d
        match d with
        | dnat nSome (dnat (fun_of_arithuop op n))
        | _None
        end
    | AForeignUnaryOp fuforeign_unary_op_interp h fu
    end.

  Lemma data_normalized_edot l s o :
    edot l s = Some o
    data_normalized h (drec l)
    data_normalized h o.

  Lemma data_normalized_filter l :
    data_normalized h (drec l)
     f, data_normalized h (drec (filter f l)).

  Lemma data_normalized_rremove l :
    data_normalized h (drec l)
     s, data_normalized h (drec (rremove l s)).

   Lemma data_normalized_rproject l :
    data_normalized h (drec l)
     l2, data_normalized h (drec (rproject l l2)).

  Lemma data_normalized_bdistinct l :
    data_normalized h (dcoll l) data_normalized h (dcoll (bdistinct l)).

  Lemma dnnone : data_normalized h dnone.

  Lemma dnsome d :
    data_normalized h d
    data_normalized h (dsome d).

  Lemma fun_of_unaryop_normalized {u d o} :
    fun_of_unaryop u d = Some o
    data_normalized h d
    data_normalized h o.

End RUnaryOpsSem.