Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Functional programming 在有界自然和满足有界自然之间建立同构?_Functional Programming_Numbers_Dependent Type_Idris_Isomorphism - Fatal编程技术网

Functional programming 在有界自然和满足有界自然之间建立同构?

Functional programming 在有界自然和满足有界自然之间建立同构?,functional-programming,numbers,dependent-type,idris,isomorphism,Functional Programming,Numbers,Dependent Type,Idris,Isomorphism,在Idris中,能否在Fin n和(x**So(x

在Idris中,能否在
Fin n
(x**So(x
之间建立同构关系?(我实际上不知道Idris,所以这些类型可能无效。一般的想法是,我们有一个数据类型,通过构造保证小于
n
,另一个数据类型通过测试保证小于
n

这是Idris 0.10.2中的一个证明,如您所见,
roundtrip2
是唯一棘手的证明

import Data.Fin
%default total

Bound : Nat -> Type
Bound n = DPair Nat (\x => x `LT` n)

bZ : Bound (S n)
bZ = (0 ** LTESucc LTEZero)

bS : Bound n -> Bound (S n)
bS (x ** bound) = (S x ** LTESucc bound)

fromFin : Fin n -> Bound n
fromFin FZ = bZ
fromFin (FS k) = bS (fromFin k)

toFin : Bound n -> Fin n
toFin (Z ** LTEZero) impossible
toFin {n = S n} (Z ** bound) = FZ
toFin (S x ** LTESucc bound) = FS (toFin (x ** bound))

roundtrip1 : {n : Nat} -> (k : Bound n) -> fromFin (toFin k) = k
roundtrip1 (Z ** LTEZero) impossible
roundtrip1 {n = S n} (Z ** LTESucc LTEZero) = Refl
roundtrip1 (S x ** LTESucc bound) = rewrite (roundtrip1 (x ** bound)) in Refl

roundtrip2 : {n : Nat} -> (k : Fin n) -> toFin (fromFin k) = k
roundtrip2 FZ = Refl
roundtrip2 (FS k) = rewrite (lemma (fromFin k)) in cong {f = FS} (roundtrip2 k)
  where
    lemma : {n : Nat} -> (k : Bound n) -> toFin (bS k) = FS (toFin k)
    lemma (x ** pf) = Refl
如果您拥有的是非命题的
So(x
而不是
x`LT`n
,则需要将其转换为证明形式。这一次我可以这样做:

import Data.So

%default total

stepBack : So (S x < S y) -> So (x < y)
stepBack {x = x} {y = y} so with (compare x y)
  | LT = so
  | EQ = so
  | GT = so

correct : So (x < y) -> x `LT` y
correct {x = Z}   {y = Z}     Oh impossible
correct {x = S _} {y = Z}     Oh impossible
correct {x = Z}   {y = S _} so = LTESucc LTEZero
correct {x = S x} {y = S y} so = LTESucc $ correct $ stepBack so
导入数据。所以 %默认总数 后退:So(sxSo(xx`LT`y 更正{x=Z}{y=Z}哦,不可能 纠正{x=S}{y=Z}哦,不可能 正确的{x=Z}{y=S_}so=LTESUCH LTERZERO correct{x=sx}{y=sy}so=LTESucc$correct$stepBack so
您可以看到,Idris 0.10.2中有一个证明,
roundtrip2
是唯一棘手的证明

import Data.Fin
%default total

Bound : Nat -> Type
Bound n = DPair Nat (\x => x `LT` n)

bZ : Bound (S n)
bZ = (0 ** LTESucc LTEZero)

bS : Bound n -> Bound (S n)
bS (x ** bound) = (S x ** LTESucc bound)

fromFin : Fin n -> Bound n
fromFin FZ = bZ
fromFin (FS k) = bS (fromFin k)

toFin : Bound n -> Fin n
toFin (Z ** LTEZero) impossible
toFin {n = S n} (Z ** bound) = FZ
toFin (S x ** LTESucc bound) = FS (toFin (x ** bound))

roundtrip1 : {n : Nat} -> (k : Bound n) -> fromFin (toFin k) = k
roundtrip1 (Z ** LTEZero) impossible
roundtrip1 {n = S n} (Z ** LTESucc LTEZero) = Refl
roundtrip1 (S x ** LTESucc bound) = rewrite (roundtrip1 (x ** bound)) in Refl

roundtrip2 : {n : Nat} -> (k : Fin n) -> toFin (fromFin k) = k
roundtrip2 FZ = Refl
roundtrip2 (FS k) = rewrite (lemma (fromFin k)) in cong {f = FS} (roundtrip2 k)
  where
    lemma : {n : Nat} -> (k : Bound n) -> toFin (bS k) = FS (toFin k)
    lemma (x ** pf) = Refl
如果您拥有的是非命题的
So(x
而不是
x`LT`n
,则需要将其转换为证明形式。这一次我可以这样做:

import Data.So

%default total

stepBack : So (S x < S y) -> So (x < y)
stepBack {x = x} {y = y} so with (compare x y)
  | LT = so
  | EQ = so
  | GT = so

correct : So (x < y) -> x `LT` y
correct {x = Z}   {y = Z}     Oh impossible
correct {x = S _} {y = Z}     Oh impossible
correct {x = Z}   {y = S _} so = LTESucc LTEZero
correct {x = S x} {y = S y} so = LTESucc $ correct $ stepBack so
导入数据。所以 %默认总数 后退:So(sxSo(xx`LT`y 更正{x=Z}{y=Z}哦,不可能 纠正{x=S}{y=Z}哦,不可能 正确的{x=Z}{y=S_}so=LTESUCH LTERZERO correct{x=sx}{y=sy}so=LTESucc$correct$stepBack so
事实上我收回了它——写
So(xx`LT`n
,甚至是假设的更简单的
那么(x`LTE`n
证明是相当棘手的!@PyRulez:
那么
现在清楚了你要如何将上面的两位结合到你想要的东西中吗?事实上我收回了——写
So(xx`LT`n
,甚至是所谓的简单一点的
So(x`LT`n
证明是相当棘手的!@PyRulez:
So
现在清楚了,你该如何将上面的两位组合成你想要的吗?