[Haskell-cafe] Excercise on tagless final interpreters

Brent Yorgey byorgey at seas.upenn.edu
Thu Mar 21 16:26:12 CET 2013


On Thu, Mar 21, 2013 at 11:32:21AM +0100, matteo vezzola wrote:
> I'm playing with tagless final interpreters reading [1], using a very simple language:
> 
> >>> class Ints repr where
> >>>     int :: Integer -> repr Integer
> >>>     (.+.) :: repr Integer -> repr Integer -> repr Integer
> >>>     (.*.) :: repr Integer -> repr Integer -> repr Integer
> >>>     (.-.) :: repr Integer -> repr Integer
> >>>     (.<=.) :: repr Integer -> repr Integer -> repr Bool
> 
> >>> newtype P repr t = P { unP :: Bool -> repr t }
> >>> instance Ints repr => Ints (P repr) where
> >>>     int n = P $ \ s -> if s then int n else (.-.) (int n)
> >>>     (.-.) n = P $ unP n . not
> >>>     n .+. m = P $ \ s -> unP n s .+. unP m s

> >>>     n .*. m = P $ \ s -> unP n s .*. unP m s
> >>>     n .<=. m = P $ \ s -> unP n s .<=. unP m s

Incidentally, these last two lines seem wrong.  It is not the case
that -(n*m) = (-n)*(-m) or that  n <= m  iff  (-n) <= (-m).

-Brent



More information about the Haskell-Cafe mailing list