我写了一个简短的函数:
swapMaybe : Monad m => Maybe (m a) -> m (Maybe a)
swapMaybe Nothing = pure Nothing
swapMaybe (Just x) = map Just x
然后我试图证明其特性之一:
import Interfaces.Verified
swapMaybePreservesMap : VerifiedMonad m => (g : a -> b) -> (x : Maybe (m a)) ->
swapMaybe (map (map g) x) = map (map g) (swapMaybe x)
swapMaybePreservesMap g Nothing = ?hswapMaybePreservesMapNothing
swapMaybePreservesMap g (Just x) = ?hswapMaybePreservesMapJust
(functorComposition x g Just)
(functorComposition x Just (map g))
:t hswapMaybePreservesMapJust
给出以下类型:
a : Type
b : Type
g : a -> b
m : Type -> Type
x : m a
constraint : VerifiedMonad m
--------------------------------------
hswapMaybePreservesMapJust : (map (\x1 => Just (g x1)) x = map Just (map g x)) ->
(map (\x1 => Just (g x1)) x = map (\meth => Prelude.Maybe implementation of Prelude.Functor.Functor, method map g meth) (map Just x)) ->
map Just (map g x) = map (\meth => Prelude.Maybe implementation of Prelude.Functor.Functor, method map g meth) (map Just x)
map (\x1 => Just (g x1)) x
的两个参数在视觉上相同的左侧hswapMaybePreservesMapJust
看起来很有希望应用rewrite
。
但是,它失败了:
swapMaybePreservesMap g (Just x) = rewrite (functorComposition x g Just) in
(functorComposition x Just (map g))
错误:
When checking right hand side of swapMaybePreservesMap with expected type
swapMaybe (map (map g) (Just x)) = map (map g) (swapMaybe (Just x))
rewriting map (\x1 => Just (g x1)) x to map Just (map g x) did not change type map Just (map g x) =
map (\meth => Prelude.Maybe implementation of Prelude.Functor.Functor, method map g meth) (map Just x)
我检查了this question,并尝试使用the
明确指定结果类型,将rewrite
参数移至let
和eta展开map g
。但是也没有成功:
swapEq : {x : a} -> {y : a} -> x = y -> y = x
swapEq eq = rewrite eq in Refl
swapMaybePreservesMap : VerifiedMonad n => (g : a -> b) -> (x : Maybe (n a)) ->
swapMaybe (map (\z => map g z) x) = map (\z => map g z) (swapMaybe x)
swapMaybePreservesMap g Nothing = ?hswapMaybePreservesMapNothing
swapMaybePreservesMap g xx@(Just x) = let
pr1 = (swapEq $ functorComposition x g Just)
pr2 = (functorComposition x Just (\z => map g z))
in the (swapMaybe (map (\z => map g z) xx) = map (\z => map g z) (swapMaybe xx))
(rewrite pr1 in pr2)
...,并显示以下消息:
When checking right hand side of swapMaybePreservesMap with expected type
swapMaybe (map (\z => map g z) (Just x)) = map (\z8 => map g z8) (swapMaybe (Just x))
When checking argument value to function Prelude.Basics.the:
rewriting map Just (map g x) to map (\x1 => Just (g x1)) x did not change type map Just (map g x) =
map (\z11 => Prelude.Maybe implementation of Prelude.Functor.Functor, method map g z11) (map Just x)
我在docs中读过:
rewrite prf in expr
如果我们有
prf : x = y
,并且expr
的必需类型是 x的属性,rewrite ... in
语法将在 必需的x
类型,并用expr
替换。
为什么在这种情况下y
不起作用?
是因为参数左侧的值实际上不同(也许未显示某些隐式参数)还是其他原因?也许最好显式地应用一些rewrite
策略脚本?
答案 0 :(得分:0)
最后我找到了原因-swapMaybe
和swapMaybePreservesMap
中的类型约束不同。以下实现给出了明确的错误:
swapMaybePreservesMap : VerifiedMonad m => (g : a -> b) -> (x : Maybe (m a)) ->
swapMaybe (map (map g) x) = map (map g) (swapMaybe x)
swapMaybePreservesMap g Nothing = ?outOfScope
swapMaybePreservesMap g (Just x) = trans
(sym $ functorComposition x g Just)
(functorComposition x Just (map g))
消息:
When checking right hand side of swapMaybePreservesMap with expected type
swapMaybe (map (map g) (Just x)) = map (map g) (swapMaybe (Just x))
When checking an application of function trans:
Type mismatch between
map (map g . Just) x = (map (map g) . map Just) x (Type of functorComposition x Just (map g))
and
map (\x1 => Just (g x1)) x = map (\meth => Prelude.Maybe implementation of Prelude.Functor.Functor, method map g meth) (map Just x) (Expected type)
Specifically:
Type mismatch between
map (map g) (map Just x)
and
map (\meth => Prelude.Maybe implementation of Prelude.Functor.Functor, method map g meth) (map Just x)
将VerifiedMonad
类型约束应用于swapMaybe
而不是Monad
会使类型检查器满意(上面的swapMaybePreservesMap
实现):
swapMaybe : Monad m => Maybe (m a) -> m (Maybe a)
swapMaybe Nothing = pure Nothing
swapMaybe (Just x) = map Just x
但是,最好证明一个函数的某些属性,并使用某些Monad
(仅适用于VerifiedMonad
)。