forked from purescript/purescript-prelude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Show.purs
71 lines (58 loc) · 2.36 KB
/
Show.purs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Data.Show
( class Show, show
, class ShowRecordFields, showRecordFields
) where
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Prim.RowList as RL
import Record.Unsafe (unsafeGet)
import Type.Data.RowList (RLProxy(..))
-- | The `Show` type class represents those types which can be converted into
-- | a human-readable `String` representation.
-- |
-- | While not required, it is recommended that for any expression `x`, the
-- | string `show x` be executable PureScript code which evaluates to the same
-- | value as the expression `x`.
class Show a where
show :: a -> String
instance showBoolean :: Show Boolean where
show true = "true"
show false = "false"
instance showInt :: Show Int where
show = showIntImpl
instance showNumber :: Show Number where
show = showNumberImpl
instance showChar :: Show Char where
show = showCharImpl
instance showString :: Show String where
show = showStringImpl
instance showArray :: Show a => Show (Array a) where
show = showArrayImpl show
instance showRecord :: (RL.RowToList rs ls, ShowRecordFields ls rs) => Show (Record rs) where
show record = case showRecordFields (RLProxy :: RLProxy ls) record of
[] -> "{}"
fields -> join " " ["{", join ", " fields, "}"]
-- | A class for records where all fields have `Show` instances, used to
-- | implement the `Show` instance for records.
class ShowRecordFields rowlist row where
showRecordFields :: RLProxy rowlist -> Record row -> Array String
instance showRecordFieldsNil :: ShowRecordFields RL.Nil row where
showRecordFields _ _ = []
instance showRecordFieldsCons
:: ( IsSymbol key
, ShowRecordFields rowlistTail row
, Show focus
)
=> ShowRecordFields (RL.Cons key focus rowlistTail) row where
showRecordFields _ record
= cons (join ": " [ key, show focus ]) tail
where
key = reflectSymbol (SProxy :: SProxy key)
focus = unsafeGet key record :: focus
tail = showRecordFields (RLProxy :: RLProxy rowlistTail) record
foreign import showIntImpl :: Int -> String
foreign import showNumberImpl :: Number -> String
foreign import showCharImpl :: Char -> String
foreign import showStringImpl :: String -> String
foreign import showArrayImpl :: forall a. (a -> String) -> Array a -> String
foreign import cons :: forall a. a -> Array a -> Array a
foreign import join :: String -> Array String -> String