如何为由Either和Maybe组成的Type创建Functor实例

问题描述 投票:0回答:2

我遇到一个类型的Functor实例有问题,该类型基本上只是嵌套的Either和Maybe。

data Tuple a b = Tuple a b
data Primitive = String String | Boolean Boolean | Number Number | Null
data JsonValue = Object (Map String JsonValue) | Array (List JsonValue) | Primitive
type Path = List String
data JsonGraphValue = JsonGraphObject (Map String JsonGraphValue) | Atom JsonValue | Ref Path | Error JsonValue | JsonPrimitive Primitive

newtype JsonGraphRecResult a = JsonGraphRecResult (Either String (Tuple (Maybe a) (List Path)))
instance jsonGraphRecResultFunctor :: Functor JsonGraphRecResult where
  map f (JsonGraphRecResult (Right (Tuple (Just value) paths))) = JsonGraphRecResult (Right (Tuple (Just (f value)) paths))
  map f value = value

我得到以下错误指向上面代码末尾的“值”字。

  Could not match type

    a1

  with type

    b0


while trying to match type JsonGraphRecResult a1
  with type JsonGraphRecResult b0
while checking that expression value
  has type JsonGraphRecResult b0
in value declaration jsonGraphRecResultFunctor

where b0 is a rigid type variable
      a1 is a rigid type variable

我不清楚为什么JsonGraphRecResult与以下编译好的Blah类型有什么不同:

newtype Blah a = Blah (Maybe a)
instance blahFunctor :: Functor Blah where
  map f (Blah (Just x)) = Blah (Just (f x))
  map f value = value

以下gist可以直接粘贴到“Try PureScript”在线REPL中,以便复制错误。

purescript
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.