我正在尝试在 Idris 中实现最简单的 profunctor optic。Iso 是一个在所有 profunctor 中都应该是多态的函数。我认为这是正确的语法。
一切都进行类型检查,除了最终测试。
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