Firstly, this builds on https://www.schoolofhaskell.com/user/bartosz/understanding-algebras so please read for context if unfamiliar with algebras and recursion schemes.
Say I have a simple expression parser:
data Expr a = ... -- whatever
parse :: String -> Fix Expr
eval :: Algebra Expr (Either String Int)
It may or may not succeed. Example:
cata eval $ parse "3+4" -- Right 7
cata eval $ parse "3+x" -- Left "x not defined"
My question is, if one were to
1) update the parse
function to annotate nodes with parse positions
2) prefix error messages with the parsing positions
how could I integrate these new functions below, with those above?
type ParsePositions = (Int,Int)
parse' :: String -> Cofree Expr ParsePositions
prefixError :: ParsePositions -> String -> String
Example usage:
run "3+4" -- Right 7
run "3+x" -- Left "Error parsing [2,2]: x not defined"
Would this be a zygomorphism? Maybe a histomorphism? Both would need some type contortion.
Bonus points: should I be using an elgot algebra to short-circuit on failure seeing evaluation can return Left String
?