haskell - Coq:haskell 的 Replicate 函数的强规范
问题描述
我在理解 Coq 中强规范和弱规范之间的区别时遇到了一些麻烦。例如,如果我想使用强规范方式编写复制函数(给定一个数字 n 和一个值 x,它会创建一个长度为 n 的列表,所有元素都等于 x),我将如何做到这一点? 显然我必须编写函数的归纳“版本”,但如何?
Haskell 中的定义:
myReplicate :: Int -> a -> [a]
myReplicate 0 _ = []
myReplicate n x | n > 0 = x:myReplicate (n-1) x
| otherwise = []
弱规范的定义:
用弱规范定义这些函数,然后添加伴随引理。例如,我们定义了一个函数 f : A->B 并证明了一个形式为 ∀ x:A, Rx ( fx ) 的语句,其中 R 是编码函数预期输入/输出行为的关系。
强规范的定义:
给出函数的强说明:这个函数的类型直接说明输入是类型 A 的值 x,输出是类型 B 的值 v 和 v 满足Rxv的证明的组合。这种规范通常依赖于依赖类型。
编辑:我收到了老师的回复,显然我必须做类似的事情,但对于复制案例:
“例如,如果我们想从它的规范中提取一个计算列表长度的函数,我们可以定义一个关系 RelLength 来建立预期输入和输出之间的关系,然后证明它。像这样:
Inductive RelLength (A:Type) : nat -> list A -> Prop :=
| len_nil : RelLength 0 nil
| len_cons : forall l x n, RelLength n l -> RelLength (S n) (x::l) .
Theorem len_corr : forall (A:Type) (l:list A), {n | RelLength n l}.
Proof.
…
Qed.
Recursive Extraction len_corr.
用于证明的函数必须直接使用列表“recursor”(这就是fixpoint 不会显示的原因——它隐藏在list_rect 中)。
所以你不需要编写函数本身,只需要编写关系,因为函数将由证明定义。”
知道了这一点,我如何将其应用于复制功能案例?
解决方案
只是为了好玩,这就是 Haskell 中的样子,所有依赖的东西都更烦人。这段代码使用了一些非常新的 GHC 特性,主要是为了使类型更加明确,但可以很容易地修改它以使用旧的 GHC 版本。
{-# language GADTs, TypeFamilies, PolyKinds, DataKinds, ScopedTypeVariables,
TypeOperators, TypeApplications, StandaloneKindSignatures #-}
{-# OPTIONS_GHC -Wincomplete-patterns #-}
module RelRepl where
import Data.Kind (Type)
import Data.Type.Equality ((:~:)(..))
-- | Singletons (borrowed from the `singletons` package).
type Sing :: forall (k :: Type). k -> Type
type family Sing
type instance Sing @Nat = SNat
type instance Sing @[a] = SList @a
-- The version of Sing in the singletons package has many more instances;
-- in any case, more can be added anywhere as needed.
-- Natural numbers, used at the type level
data Nat = Z | S Nat
-- Singleton representations of natural numbers, used
-- at the term level.
data SNat :: Nat -> Type where
SZ :: SNat 'Z
SS :: SNat n -> SNat ('S n)
-- Singleton lists
data SList :: forall (a :: Type). [a] -> Type where
SNil :: SList '[]
SCons :: Sing a -> SList as -> SList (a ': as)
-- The relation representing the `replicate` function.
data RelRepl :: forall (a :: Type). Nat -> a -> [a] -> Type where
Repl_Z :: forall x. RelRepl 'Z x '[]
Repl_S :: forall n x l. RelRepl n x l -> RelRepl ('S n) x (x ': l)
-- Dependent pairs, because those aren't natively supported.
data DPair :: forall (a :: Type). (a -> Type) -> Type where
MkDPair :: forall {a :: Type} (x :: a) (p :: a -> Type).
Sing x -> p x -> DPair @a p
-- Proof that every natural number and value produce a list
-- satisfying the relation.
repl_corr :: forall {a :: Type} (n :: Nat) (x :: a).
SNat n -> Sing x -> DPair @[a] (RelRepl n x)
repl_corr SZ _x = MkDPair SNil Repl_Z
repl_corr (SS n) x
| MkDPair l pf <- repl_corr n x
= MkDPair (SCons x l) (Repl_S pf)
-- Here's a proof that the relation indeed specifies
-- a *unique* function.
replUnique :: forall {a :: Type} (n :: Nat) (x :: a) (xs :: [a]) (ys :: [a]).
RelRepl n x xs -> RelRepl n x ys -> xs :~: ys
replUnique Repl_Z Repl_Z = Refl
replUnique (Repl_S pf1) (Repl_S pf2)
| Refl <- replUnique pf1 pf2
= Refl
推荐阅读
- ios - BLE 设备 - 相同的代码和相同的固件适用于 iphone SE,但在 iphone 7 plus 中效果不佳
- java - 构建命令失败,外部原生问题 android studio
- react-native - 在 react-native 中以编程方式将屏幕倒置
- javascript - 使用 Javascript 从 web 视图切换到应用视图
- amazon-s3 - 使用 Cloudfront 预签名的 S3 URL
- java - “内容处置”不起作用
- android - 抛出异常,尽管我有 catch 子句
- node.js - 一旦部署在heroku上,如何通过带有角度和节点的表单输入发送电子邮件?
- spring - 如何使用 jax-rs 服务更改和重新加载应用程序配置
- python - 使用 requests.post 提交表单