我可以提取Positive,Nat到int32,Z到int吗?

时间:2012-05-02 04:14:56

标签: ocaml coq coq-extraction

嗨我正在写一个从Coq到Ocaml的提取,我想转换类型:

positive --> int32
N -> int32

但我希望保持类型Zint

以下是我提取这些条件的代码:

Require Import ZArith NArith.
Require Import ExtrOcamlBasic.

(* Mapping of [positive], [N], [Z] into [int32]. *)
Extract Inductive positive => int32
[ "(fun p-> let two = Int32.add Int32.one Int32.one in
    Int32.add Int32.one (Int32.mul two p))"
  "(fun p->
    let two = Int32.add Int32.one Int32.one in Int32.mul two p)" "Int32.one" ]
  "(fun f2p1 f2p f1 p -> let two = Int32.add Int32.one Int32.one in
    if p <= Int32.one then f1 () else if Int32.rem p two = Int32.zero then
    f2p (Int32.div p two) else f2p1 (Int32.div p two))".

Extract Inductive N => int32 [ "Int32.zero" "" ]
"(fun f0 fp n -> if n=Int32.zero then f0 () else fp n)".

Extract Inductive Z => int [ "0" "" "(~-)" ]
"(fun f0 fp fn z -> if z=0 then f0 () else if z>0 then fp z else fn (-z))".

我无法保留Z -> int因为Coq库(BinInt.v)中Z的定义

Inductive Z : Set :=
  | Z0 : Z
  | Zpos : positive -> Z
  | Zneg : positive -> Z.

我收到错误:(函数coq_Zdouble_plus_one)

文件“BinInt.ml”,第38行,字符4-5:

错误:此表达式的类型为int,但表达式需要类型          INT32

BinInt.ml

open BinPos
open Datatypes

(** val coq_Z_rect :
    'a1 -> (int32 -> 'a1) -> (int32 -> 'a1) -> int -> 'a1 **)

let coq_Z_rect f f0 f1 z =
  (fun f0 fp fn z -> if z=0 then f0 () else if z>0 then fp z else fn (-z))
    (fun _ ->
    f)
    (fun x ->
    f0 x)
    (fun x ->
    f1 x)
    z

(** val coq_Z_rec : 'a1 -> (int32 -> 'a1) -> (int32 -> 'a1) -> int -> 'a1 **)

let coq_Z_rec f f0 f1 z =
  (fun f0 fp fn z -> if z=0 then f0 () else if z>0 then fp z else fn (-z))
    (fun _ ->
    f)
    (fun x ->
    f0 x)
    (fun x ->
    f1 x)
    z

(** val coq_Zdouble_plus_one : int -> int **)

let coq_Zdouble_plus_one x =
  (fun f0 fp fn z -> if z=0 then f0 () else if z>0 then fp z else fn (-z))
    (fun _ ->
    Int32.one)
    (fun p ->
    ((fun p-> let two = Int32.add Int32.one Int32.one in
    Int32.add Int32.one (Int32.mul two p))
    p))
    (fun p -> (~-)
    (coq_Pdouble_minus_one p))
    x

如果我提取Z -> int32,那就没关系,但这不是我想要的。

1 个答案:

答案 0 :(得分:1)

您的问题是Z内部构建于positive

Inductive Z : Set := Z0 : Z
                   | Zpos : positive -> Z
                   | Zneg : positive -> Z
                   .

这意味着每当您获得Z时,您就会获得positive和一些额外信息。

如果您确实要为Zpositive使用不同的类型,则必须在intint32之间插入转换函数。你可以用提取功能做到这一点,但我不确定 - 或者是否 - 这是可能的。

我看到的另一个问题是Z s上匹配内的代码会使positive处理,这意味着你将不断在类型之间进行转换并失去任何额外的精度类型可能超过另一个。如果可能的话,我会为两者使用相同的类型。