理解代码背后的语义

时间:2013-05-06 02:00:54

标签: ocaml coq

我有一个OCaml代码,我很难将函数mi_pol形式化为Coq,因为我不清楚这段代码到底是什么,例如在

aux (vec_add add const (vector ci v)) args ps 

args.(i-1) <- mat_add add args.(i-1) (matrix ci m); aux const args ps

aux (vec_0 z dim) (Array.make n (mat_0 z dim)) ps

这是代码:

let vector = List.map;;

let clist x =
  let rec aux k = if k <= 0 then [] else x :: aux (k-1) in aux;;

let vec_add add v1 v2 =
  try List.map2 add v1 v2
  with Invalid_argument _ ->
    error_fmt "sum of two vectors of different size";;

let mat_add add m1 m2 =
  try List.map2 (vec_add add) m1 m2
  with Invalid_argument _ ->
    error_fmt "sum of two matrices of different size";;

(*vector zero *)
let vec_0 z dim = clist z dim;;  

(* matrix zero *)
let mat_0 z dim = clist (vec_0 z dim) dim;;
let comp f g x = f (g x);;

(* matrix transpose *)
let transpose ci =
  let rec aux = function
    | [] | [] :: _ -> []
    | cs -> List.map (comp ci List.hd) cs :: aux (List.map List.tl cs)
  in aux;;  

(* matrix *)
let matrix ci m =
  try transpose ci m
  with Failure _ -> error_fmt "ill-formed matrix";;

let mi_pol z add ci =
  let rec aux const args = function
    | [] -> { mi_const = const; mi_args = Array.to_list args }
    | Polynomial_sum qs :: ps -> aux const args (qs @ ps)
    | Polynomial_coefficient (Coefficient_matrix [v]) :: ps
    | Polynomial_coefficient (Coefficient_vector v) :: ps ->
      aux (vec_add add const (vector ci v)) args ps
    | Polynomial_product [p] :: ps -> aux const args (p :: ps)
    | Polynomial_product [Polynomial_coefficient (Coefficient_matrix m); 
                          Polynomial_variable i] :: ps ->
      args.(i-1) <- mat_add add args.(i-1) (matrix ci m);
      aux const args ps
    | _ -> not_supported "todo"
  in fun dim n -> function
    | Polynomial_sum ps -> aux (vec_0 z dim) (Array.make n (mat_0 z dim)) ps
    | _ -> not_supported
      "todo";;

任何帮助都非常感谢。如果你有mi_pol的Coq代码,它会对我有很大帮助。

1 个答案:

答案 0 :(得分:3)

似乎在向量空间上采用多项式,并计算附加到每个变量的所有(转置)(矩阵)系数的总和。 args是一个数组,args.(i)i - 变量上所有系数的总和,const是常量标量的总和。

我不知道这个操作的含义是什么,但我怀疑它在一般情况下没有多大意义(在......的产品总和的任意产品上工作;这将导致奇怪的非线性/同质行为)。我想这个多项式类型的实际值的形状存在隐式约束,例如它在所有变量中都可能是线性的。