[Haskell-cafe] A functor for two Peano systems

Galaxy Being borgauf at gmail.com
Mon Apr 5 05:17:23 UTC 2021


I'm just not understanding the concept of a functor in this context: I have
this

plus :: Int -> Int -> Int
plus n m = if (n == 0)
           then m
           else sCessor (plus (pCessor n) m)
                where sCessor x = x + 1
                      pCessor x = if (x == 0)
                                  then error "too small"
                                  else (x - 1)

and this

data MyNum = MNZero | OneMoreThan MyNum deriving (Show,Eq,Ord)

plus2 :: MyNum -> MyNum -> MyNum
plus2 n m = if (n == MNZero)
            then m
            else sCessor (plus2 (pCessor n) m)
                 where sCessor x = (OneMoreThan x)
                       pCessor x = if (x == MNZero)
                                   then (error "too small")
                                   else (oneLess x)
                       oneLess MNZero = MNZero
                       oneLess (OneMoreThan myn) = myn

It seems there should be just one plus, function that would handle both an
Int-based Peano and the MyNum-based Peano, not two. But in this definition

fmap :: (a -> b) -> f a -> f b

The (a -> b) should be "lifted" over the f a -> f b But I can't conceive of
how this should all fit together, i.e., to create just one generic plus
that would handle both the plus :: Int -> Int -> Int and the plus2 :: MyNum
-> MyNum -> MyNum. Trying to get started, I would assume I need some sort
of instance Functor MyNum describing, yeah, what? Or am I barking up the
completely wrong tree here, i.e., this isn't a functor issue?

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20210405/ff3c835e/attachment.html>


More information about the Haskell-Cafe mailing list