gpt4 book ai didi

haskell - Coq:haskell 的 Replicate 函数的强规范

转载 作者:行者123 更新时间:2023-12-04 11:29:50 26 4
gpt4 key购买 nike

我在理解 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”(这就是修复点不会出现的原因——它隐藏在 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

关于haskell - Coq:haskell 的 Replicate 函数的强规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67661349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com