{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies, FlexibleContexts, UndecidableInstances, OverlappingInstances, IncoherentInstances #-}
class SubtypeOf a b | a -> b where
upcast :: a -> b
downcastSafe
:: b
-> Maybe a
downcast :: b -> a
downcast b = case downcastSafe b of
Nothing
-> error $ "can not downcast the value" Just a -> a
class ( Ord a
) => Rangable t a
| t
-> a
where lowLim :: t -> a
highLim :: t -> a
class Packable t a | t -> a where
pack :: a -> t
unpack :: t -> a
class MultipleTo t a | t -> a where
multiple :: t -> a
instance ( Num a
, Ord a
, Rangable range a
, Packable range a
) => SubtypeOf range a
where upcast = unpack
downcastSafe b | b >= ( lowLim $ pb) && b <= ( highLim $ pb) = Just $ pb
where
pb = pack b
instance ( Integral a
, Packable range a
, MultipleTo range a
) => SubtypeOf range a
where upcast = unpack
downcastSafe b
| b `
mod `
( multiple pb
) == 0 = Just pb
where
pb = pack b
newtype Range1 a = Range1 { unRange1 :: a}
instance ( Num a
, Ord a
) => Rangable
( Range1 a
) a
where lowLim _ = 0
highLim _ = 10
instance ( Num a
, Ord a
) => Packable
( Range1 a
) a
where pack = Range1
unpack = unRange1
newtype Range2 a = Range2 { unRange2 :: a}
instance ( Num a
, Ord a
) => Rangable
( Range2 a
) a
where lowLim _ = - 10
highLim _ = 200
instance ( Num a
, Ord a
) => Packable
( Range2 a
) a
where pack = Range2
unpack = unRange2
ey0jIExBTkdVQUdFIE11bHRpUGFyYW1UeXBlQ2xhc3NlcywgRmxleGlibGVJbnN0YW5jZXMsIEZ1bmN0aW9uYWxEZXBlbmRlbmNpZXMsIEZsZXhpYmxlQ29udGV4dHMsIFVuZGVjaWRhYmxlSW5zdGFuY2VzLCBPdmVybGFwcGluZ0luc3RhbmNlcywgSW5jb2hlcmVudEluc3RhbmNlcyAjLX0KCmNsYXNzIFN1YnR5cGVPZiBhIGIgfCBhIC0+IGIgd2hlcmUKICB1cGNhc3QgOjogYSAtPiBiCiAgZG93bmNhc3RTYWZlIDo6I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AgICAgIHBiID0gcGFjayBiCgpuZXd0eXBlIFJhbmdlMSBhID0gUmFuZ2UxIHt1blJhbmdlMSA6OiBhfQogICAgICAgICAgICAgICAgIGRlcml2aW5nIFNob3cKCmluc3RhbmNlIChOdW0gYSwgT3JkIGEpID0+IFJhbmdhYmxlIChSYW5nZTEgYSkgYSB3aGVyZQogIGxvd0xpbSBfID0gMAogIGhpZ2hMaW0gXyA9IDEwCgppbnN0YW5jZSAoTnVtIGEsIE9yZCBhKSA9PiBQYWNrYWJsZSAoUmFuZ2UxIGEpIGEgd2hlcmUKICBwYWNrID0gUmFuZ2UxCiAgdW5wYWNrID0gdW5SYW5nZTEKCm5ld3R5cGUgUmFuZ2UyIGEgPSBSYW5nZTIge3VuUmFuZ2UyIDo6IGF9CiAgICAgICAgICAgICAgICAgICBkZXJpdmluZyBTaG93CgppbnN0YW5jZSAoTnVtIGEsIE9yZCBhKSA9PiBSYW5nYWJsZSAoUmFuZ2UyIGEpIGEgd2hlcmUKICBsb3dMaW0gXyA9IC0xMAogIGhpZ2hMaW0gXyA9IDIwMAoKaW5zdGFuY2UgKE51bSBhLCBPcmQgYSkgPT4gUGFja2FibGUgKFJhbmdlMiBhKSBhIHdoZXJlCiAgcGFjayA9IFJhbmdlMgogIHVucGFjayA9IHVuUmFuZ2Uy
compilation info
[1 of 1] Compiling Main ( prog.hs, prog.o )
prog.hs:22:0:
Duplicate instance declarations:
instance [incoherent] (Num a,
Ord a,
Rangable range a,
Packable range a) =>
SubtypeOf range a
-- Defined at prog.hs:(22,0)-(27,16)
instance [incoherent] (Integral a,
Packable range a,
MultipleTo range a) =>
SubtypeOf range a
-- Defined at prog.hs:(29,0)-(34,16)
stdout