Functional programming 在有界自然和满足有界自然之间建立同构?
在Idris中,能否在Functional programming 在有界自然和满足有界自然之间建立同构?,functional-programming,numbers,dependent-type,idris,isomorphism,Functional Programming,Numbers,Dependent Type,Idris,Isomorphism,在Idris中,能否在Fin n和(x**So(x
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(sx您可以看到,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(sx事实上我收回了它——写
So(xx`LT`n
,甚至是假设的更简单的那么(x`LTE`n
证明是相当棘手的!@PyRulez:那么
现在清楚了你要如何将上面的两位结合到你想要的东西中吗?事实上我收回了——写So(xx`LT`n
,甚至是所谓的简单一点的So(x`LT`n
证明是相当棘手的!@PyRulez:So
现在清楚了,你该如何将上面的两位组合成你想要的吗?