-
Notifications
You must be signed in to change notification settings - Fork 0
/
casttest2.hs
39 lines (30 loc) · 1.02 KB
/
casttest2.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE FlexibleInstances #-}
import Data.Ratio
import Data.Word
import Unsafe.Coerce
newtype TString = MkTString ([Char]) -- deriving (Show)
newtype Replicator = MkReplicator (Int -> String) -- deriving (Show)
class Thing a where
thing :: a
instance Thing Bool where thing = True
instance Thing Int where thing = 16
instance Thing String where thing = "hello, world"
instance Thing (Int -> String) where thing n = replicate n '*'
class Three a where
three :: a
-- for base >= 4.10.0.0, can import GHC.Float (castWord64ToDouble)
-- more generally, we can use this unsafe coercion:
castWord64ToDouble :: Word64 -> Double
castWord64ToDouble w = unsafeCoerce w
instance Three Int where
three = length "aaa"
instance Three Double where
three = castWord64ToDouble (0x4008000000000000)
instance Three Rational where
three = (6 :: Integer) % (2 :: Integer)
-- main = do
-- print (three :: Int)
-- print (three :: Double)
-- print (three :: Rational)
-- print $ take three "abcdef"
-- print $ (sqrt three :: Double)