Packages

The Phi Programming Language

Current section

Files

Jump to
phi lib Data Show.hm
Raw

lib/Data/Show.hm

-----------------------------------------------------------------------------
-- |
-- Module : Data.Show
-- Copyright : (c) 2020-2021 EMQ Technologies Co., Ltd.
-- License : BSD-style (see the LICENSE file)
--
-- Maintainer : Feng Lee, feng@emqx.io
-- Yang M, yangm@emqx.io
-- Stability : experimental
-- Portability : portable
--
-- The Show typeclass.
--
-----------------------------------------------------------------------------
module Data.Show
( class Show, show, showAny
) where
import Data.Maybe (Maybe(..))
import Data.Unit (Unit)
import Data.List ((++))
import Data.Binary as B
import Data.Ordering (Ordering(..))
class Show a where
show :: a -> String
instance Show Atom where
show = showAtomImpl
instance Show Boolean where
show true = "true"
show false = "false"
instance Show Integer where
show = showIntImpl
instance Show Float where
show f = showFloatImpl f
instance Show Char where
show c = showCharImpl c
instance Show [Char] where
show x = "\"" ++ x ++ "\""
else instance Show a => Show [a] where
show x = "[" ++ showListImpl x ++ "]"
instance Show Unit where
show _ = "unit"
instance Show a => Show (Maybe a) where
show Nothing = "Nothing"
show (Just x) = "Just " ++ show x
instance Show Binary where
show x = show (B.toList x)
instance Show Ordering where
show LT = "LT"
show GT = "GT"
show EQ = "EQ"
foreign import showAtomImpl :: Atom -> String
foreign import showIntImpl :: Integer -> String
foreign import showFloatImpl :: Float -> String
foreign import showCharImpl :: Char -> String
foreign import showAny :: forall a. a -> String
showListImpl :: forall a. (Show a) => [a] -> String
showListImpl [] = ""
showListImpl [x] = show x
showListImpl [x | xs] = show x ++ ", " ++ showListImpl xs
instance (Show a, Show b) => Show (a, b) where
show (a', b') = "(" ++ show a' ++ ", " ++ show b' ++ ")"