tsoding / haskell-json

Source Code for JSON Parser Video
https://www.youtube.com/watch?v=N9RUqGYuGfw
MIT License
168 stars 26 forks source link

Either ParserError should not be an Alternative #23

Closed t-c-acc closed 2 months ago

t-c-acc commented 2 months ago

In the code

instance Alternative (Either ParserError) where
  empty = Left $ ParserError 0 "empty"
  Left _ <|> e2 = e2
  e1 <|> _ = e1

But empty is not a right identity: Left example <|> empty == empty.

So it is not a monoid under <|>...

class Applicative f => Alternative (f :: Type -> Type) where

A monoid on applicative functors.

[ ... ]

empty :: f a

The identity of <|> empty <|> a == a a <|> empty == a

P.S. Sorry for the spam on 5 year old youtube project: insomnia...

t-c-acc commented 2 months ago

Ok, I assume you "pretend" all Left values are "the same" and their contents are just for logging errors...