【发布时间】:2019-09-11 18:28:12
【问题描述】:
我有以下版本的isPrime 用 Coq 编写(并证明)。
-
Compute (isPrime 330)大约需要30秒 在我的机器上完成。 - 提取的 Haskell 代码大约需要
1秒来验证9767是否为素数。
根据this post的评论, 时间差异没有任何意义,但我想知道这是为什么? 提取 Coq 代码时还有其他方法可以预测性能吗?毕竟,有时性能确实很重要,一旦你努力证明它是正确的,就很难更改 Coq 源代码。 这是我的 Coq 代码:
(***********)
(* IMPORTS *)
(***********)
Require Import Coq.Arith.PeanoNat.
(************)
(* helper'' *)
(************)
Fixpoint helper' (p m n : nat) : bool :=
match m with
| 0 => false
| 1 => false
| S m' => (orb ((mult m n) =? p) (helper' p m' n))
end.
(**********)
(* helper *)
(**********)
Fixpoint helper (p m : nat) : bool :=
match m with
| 0 => false
| S m' => (orb ((mult m m) =? p) (orb (helper' p m' m) (helper p m')))
end.
(***********)
(* isPrime *)
(***********)
Fixpoint isPrime (p : nat) : bool :=
match p with
| 0 => false
| 1 => false
| S p' => (negb (helper p p'))
end.
(***********************)
(* Compute isPrime 330 *)
(***********************)
Compute (isPrime 330).
(********************************)
(* Extraction Language: Haskell *)
(********************************)
Extraction Language Haskell.
(***************************)
(* Use Haskell basic types *)
(***************************)
Require Import ExtrHaskellBasic.
(****************************************)
(* Use Haskell support for Nat handling *)
(****************************************)
Require Import ExtrHaskellNatNum.
Extract Inductive Datatypes.nat => "Prelude.Integer" ["0" "succ"]
"(\fO fS n -> if n Prelude.== 0 then fO () else fS (n Prelude.- 1))".
(***************************)
(* Extract to Haskell file *)
(***************************)
Extraction "/home/oren/GIT/CoqIt/FOLDER_2_PRESENTATION/FOLDER_2_EXAMPLES/EXAMPLE_03_PrintPrimes_Performance_Haskell.hs" isPrime.
【问题讨论】:
-
仅供参考,Haskell 有
Numeric.Natural.Natural,基本上就是Integer,但未签名。
标签: performance haskell coq