[Haskell-cafe] Excercise on tagless final interpreters
matteo vezzola
id074519 at studenti.univr.it
Thu Mar 21 11:32:21 CET 2013
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
After pushing down negations I'd like to distribute (.*.) over (.+.). [1] leaves it as an exercise, so it can't be that hard, but I don't get it...
Anyone knows how I could do it?
[1]: <http://okmij.org/ftp/tagless-final/course/lecture.pdf>
thanks,
--
matteo
More information about the Haskell-Cafe
mailing list