我试着在Idris中实现最简单的光学功能。Iso是一个函数,被认为在所有的程序中都是多态的。我认为这是正确的语法。
所有类型的检查,除了最后的测试。
interface Profunctor (p : Type -> Type -> Type) where
dimap : (a' -> a) -> (b -> b') -> p a b -> p a' b'
Iso : Type -> Type -> Type -> Type -> Type
Iso a b s t = {p : Type -> Type -> Type} -> Profunctor p => p a b -> p s t
-- A pair of functions
data PairFun : Type -> Type -> Type -> Type -> Type where
MkPair : (s -> a) -> (b -> t) -> PairFun a b s t
-- PairFun a b s t is a Profunctor in s t
Profunctor (PairFun a b) where
dimap f g (MkPair get set) = MkPair (get . f) (g . set)
-- Extract a pair of functions from an Iso
fromIso : Iso a b s t -> PairFun a b s t
fromIso iso = iso (MkPair id id)
-- Turn a pair of functions to an Iso
toIso : PairFun a b s t -> Iso a b s t
toIso (MkPair get set) = dimap get set
-- forall p. Profunctor p => p Int Int -> p Char String
myIso : Iso Int Int Char String
myIso = toIso (MkPair ord show)
x : PairFun Int Int Char String
x = fromIso myIso我得到了这个错误。看起来Idris是在抱怨p1是一个Profunctor的假设,但这是Iso定义中的约束。
Can't find implementation for Profunctor p1
Type mismatch between
p1 Int Int -> p1 Char String (Type of myIso p1
constraint)
and
p Int Int -> p Char String (Expected type)
Specifically:
Type mismatch between
p1 Int Int
and
p Int Int发布于 2021-12-02 07:44:27
下面的代码在Idris 2版本0.3中适用于我。这是Idris 2的一个相当老的版本,但它可能也适用于最近的版本。
interface Profunctor (0 p : Type -> Type -> Type) where
dimap : (a' -> a) -> (b -> b') -> p a b -> p a' b'
Iso : Type -> Type -> Type -> Type -> Type
Iso a b s t = {0 p : Type -> Type -> Type} -> Profunctor p => p a b -> p s t
data PairFun : Type -> Type -> Type -> Type -> Type where
MkPair : (s -> a) -> (b -> t) -> PairFun a b s t
Profunctor (PairFun a b) where
dimap f g (MkPair get set) = MkPair (get . f) (g . set)
fromIso : Iso a b s t -> PairFun a b s t
fromIso iso = iso (MkPair id id)
toIso : PairFun a b s t -> Iso a b s t
toIso (MkPair get set) = dimap get set
myIso : Iso Int Int Char String
myIso = toIso (MkPair ord show)
x : PairFun Int Int Char String
x = fromIso myIso不幸的是,我不知道如何在Idris 1中完成这项工作。问题似乎在于p的产生性:阐述者并不从p1 a b = p2 a b中推断出p1 = p2。在任何Idrises中,这通常都不成立,因为p1和p2可以是任意函数。Idris 2似乎在某种程度上继续使用p1 = p2;这是一个方便的特性,代价是推理的健壮性。
上述代码中关于p的无关注释与我刚才提到的生成性问题无关,它们只是需要重现Idris 1和GHC行为。在Idris 2中,隐式引入的变量总是具有0多重性,因此我们必须将p擦除,才能将其应用于0类型参数。此外,0 p匹配Idris 1/ GHC行为,其中类型通常被删除。在Idris 2中,类型只有在与0绑定时才会被擦除。
https://stackoverflow.com/questions/70178976
复制相似问题