{-# OPTIONS -XTemplateHaskell -XMultiParamTypeClasses #-} import UpdatableViewStub data Bin = Z | O deriving Eq -- Zero or One incr' [] = [O] incr' (O:x) = Z:incr' x incr' (Z:x) = O:x decr' [] = [] decr' [O] = [] decr' (Z:x) = (O:decr' x) decr' (O:x) = Z:x {-# LINE 16 "../examples/msblsb.hs" #-} data MSB = MSB [Bin] deriving Eq {-# LINE 29 "../examples/msblsb.hs" #-} data LSB = LSB [Bin] deriving Eq instance UpdatableView (LSB ) (MSB ) where {-# LINE 52 "../examples/msblsb.hs" #-} view = $(foldT ''LSB) (reverseB) >>>> $(toC 'MSB) zero :: MSB zero = let zero = LSB [] in (toView :: (LSB) -> (MSB )) zero incr :: MSB -> MSB incr = let incr (LSB x) = LSB $ incr' x in (toView :: (LSB -> LSB ) -> (MSB -> MSB )) incr -- :: cr (MSB x) = MSB $ reverse $ incr' (reverse x)) decr :: MSB -> MSB decr = let decr (LSB x) = LSB $ decr' x in (toView :: (LSB -> LSB) -> (MSB -> MSB)) decr -- :: cr (LSB x) = LSB $ reverse $ decr' $ reverse x isZero :: MSB -> Bool isZero = let isZero (LSB []) = True isZero (LSB (Z:x)) = isZero (LSB x) isZero _ = False in (toView :: (LSB -> Bool ) -> (MSB -> Bool )) isZero binFromInteger :: Integer -> MSB binFromInteger = let binFromInteger n = LSB $ f n where f 0 = [] f n = if n `mod` 2 == 0 then Z:f (n `div` 2) else O:f (n `div` 2) in (toView :: (Integer -> LSB ) -> (Integer -> MSB)) binFromInteger {-# LINE 54 "../examples/msblsb.hs" #-} instance Show MSB where show n = show (f n) where f n | isZero n = 0 | otherwise = 1 + f (decr n) instance Num MSB where (+) = add (*) = mul fromInteger = binFromInteger abs = id signum n | isZero n = 0 | otherwise = 1 normal :: MSB -> MSB normal (MSB x) = MSB (normal' x) where normal' [] = [] normal' (O:x) = O:x normal' (Z:x) = normal' x add :: MSB -> MSB -> MSB add x y | isZero x = y | otherwise = add (decr x) (incr y) mul :: MSB -> MSB -> MSB mul x y | isZero x = zero | otherwise = add y (mul (decr x) y)