微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 PureScript 中为 SimpleJSON 创建自定义解析器 Monad?

如何解决如何在 PureScript 中为 SimpleJSON 创建自定义解析器 Monad?

我有以下内容,直到我尝试定义 readJSON' 为止:

newtype JSONWithErr a = JSONWithErr (Writer (Array Foreign.ForeignError) a)

derive newtype instance jsonWithErrApply :: Apply JSONWithErr
derive newtype instance jsonWithErrapplicative :: applicative JSONWithErr
derive newtype instance jsonWithErrFunctor :: Functor JSONWithErr
derive newtype instance jsonWithErrBind :: Bind JSONWithErr
derive newtype instance jsonWithErrMonad :: Monad JSONWithErr
derive newtype instance jsonWithErrTell :: MonadTell (Array Foreign.ForeignError) JSONWithErr
derive newtype instance jsonWithErrWriter :: MonadWriter (Array Foreign.ForeignError) JSONWithErr

newtype JSONParse a = JSONParse (ExceptT (NonEmptyList ForeignError) JSONWithErr a)

derive newtype instance jsonParseApply :: Apply JSONParse
derive newtype instance jsonParseapplicative :: applicative JSONParse
derive newtype instance jsonParseFunctor :: Functor JSONParse
derive newtype instance jsonParseBind :: Bind JSONParse
derive newtype instance jsonParseMonad :: Monad JSONParse
derive newtype instance jsonParseTell :: MonadTell (Array Foreign.ForeignError) JSONParse
derive newtype instance jsonParseWriter :: MonadWriter (Array Foreign.ForeignError) JSONParse
derive newtype instance jsonParseThrow :: MonadThrow (NonEmptyList ForeignError) JSONParse

generalize :: forall m a. Monad m => Identity a -> m a
generalize = unwrap >>> pure

-- type Except e = ExceptT e Identity
genExcept :: forall m e a. Monad m => ExceptT e Identity a -> ExceptT e m a
genExcept = unwrap >>> generalize >>> ExceptT

readJSON' :: forall a. JSON.ReadForeign a => String -> JSONParse a
readJSON' s = JSONParse $ genExcept $ (pure >>> JSONWithErr) <$> (JSON.readJSON' s) -}

这里的错误涵盖了 readJSON' 的整个定义,是:

Could not match type

    JSONWithErr t2

  with type

    a0


while trying to match type JSONParse t1
  with type JSONParse a0
while checking that expression (apply JSONParse) ((apply genExcept) ((map (...)) (readJSON' s)))
  has type JSONParse a0
in value declaration readJSON'

where a0 is a rigid type variable
        bound at (line 0,column 0 - line 0,column 0)
      t1 is an unkNown type
      t2 is an unkNown type

我试图简化它,但我似乎遇到了约束问题;在下面的简化中,键入的孔由于较早的错误而不起作用:

tmp :: forall a. JSON.ReadForeign a => String -> ExceptT (NonEmptyList ForeignError) JSONWithErr a
tmp s = ?help (JSON.readJSON' s)

错误

  No type class instance was found for

    Simple.JSON.ReadForeign t0

  The instance head contains unkNown type variables. Consider adding a type annotation.

while applying a function readJSON'
  of type ReadForeign t0 => String -> ExceptT (NonEmptyList ForeignError) Identity t0
  to argument s
while inferring the type of readJSON' s
in value declaration tmp

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。