我一直在尝试使用So
类型来实现SortedTwoElems
证明类型和功能isSortedTwoElems
。我不确定如何实施proveUnsortedTwoElems
案例。
以下是完整示例:
import Data.So
data SortedTwoElems : List e -> Type where
MkSortedTwoElems : Ord e => {x : e} -> {y : e} -> (prf : So (x <= y))-> SortedTwoElems [x, y]
proveUnsortedTwoElems : Ord e => {x : e} -> {y : e} -> So (not (x <= y)) -> SortedTwoElems [x, y] -> Void
proveUnsortedTwoElems = ?how_to_implement_this
isSortedTwoElems : Ord e => {x : e} -> {y : e} -> (xs : List e) -> Dec (SortedTwoElems xs)
isSortedTwoElems [] = No (\(MkSortedTwoElems _) impossible)
isSortedTwoElems (x :: []) = No (\(MkSortedTwoElems _) impossible)
isSortedTwoElems (x :: (y :: (z :: xs))) = No (\(MkSortedTwoElems _) impossible)
isSortedTwoElems (x :: (y :: [])) =
case choose (x <= y) of
(Left prf) => Yes (MkSortedTwoElems prf)
(Right prfNot) => No (proveUnsortedTwoElems prfNot)
使用时执行:
proveUnsortedTwoElems (MkSortedTwoElems _) impossible
类型检查器抱怨:
proveUnsortedTwoElems (MkSortedTwoElems _) is a valid case
答案 0 :(得分:4)
我从一个中间引理开始,当你发现两个相互矛盾的So
时,它应该允许结束:
soNotTrue : So b -> So (not b) -> Void
soNotTrue {b = True} prf prf' = absurd prf'
soNotTrue {b = False} prf prf' = absurd prf
然后你可以尝试写:
unsortedTwoElems : Ord e => So (not (x <= y)) -> SortedTwoElems [x, y] -> Void
unsortedTwoElems prf (MkSortedTwoElems prf') = soNotTrue prf' prf
此处的错误消息应该提醒您:Ord e
中使用的So (not (x <= y))
约束是unsortedTwoElems
中绑定的约束,而MkSortedTwoElems
内部使用的约束是有界的通过它。
无法保证这两个Ord
兼容。
我建议的解决方案:重新定义SortedTwoElems
,以明确它正在使用的Ord
。
import Data.So
data SortedTwoElems : Ord e -> List e -> Type where
MkSortedTwoElems : {o : Ord e} -> So (x <= y) -> SortedTwoElems o [x, y]
soNotTrue : So b -> So (not b) -> Void
soNotTrue {b = True} prf prf' = absurd prf'
soNotTrue {b = False} prf prf' = absurd prf
unsortedTwoElems : (o : Ord e) => So (not (x <= y)) -> SortedTwoElems o [x, y] -> Void
unsortedTwoElems prf (MkSortedTwoElems prf') = soNotTrue prf' prf