From 6a0d7f5c434c3564d0119befb6799fd77581050a Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Fri, 3 Jan 2025 11:01:20 -0800 Subject: Initial --- ch09_09.2.example.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ch09_09.2.example.hs (limited to 'ch09_09.2.example.hs') diff --git a/ch09_09.2.example.hs b/ch09_09.2.example.hs new file mode 100644 index 0000000..bcecf7a --- /dev/null +++ b/ch09_09.2.example.hs @@ -0,0 +1,33 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE TypeFamilies #-} + +import Data.Kind (Type) +import Data.Proxy (Proxy (Proxy)) +import GHC.TypeLits (KnownSymbol, Symbol, symbolVal) + +data (a :: k1) :<< (b :: k2) + +infixr 5 :<< + +class HasPrintf a where + type Printf a :: Type + format :: String -> Proxy a -> Printf a + +instance KnownSymbol text => HasPrintf (text :: Symbol) where + type Printf text = String + format s _ = s <> symbolVal (Proxy @text) + +instance (HasPrintf a, KnownSymbol text) => HasPrintf ((text :: Symbol) :<< a) where + type Printf (text :<< a) = Printf a + format s _ = format (s <> symbolVal (Proxy @text)) (Proxy @a) + +instance (HasPrintf a, Show param) => HasPrintf ((param :: Type) :<< a) where + type Printf (param :<< a) = param -> Printf a + format s _ param = format (s <> show param) (Proxy @a) + +instance {-# OVERLAPPING #-} HasPrintf a => HasPrintf (String :<< a) where + type Printf (String :<< a) = String -> Printf a + format s _ param = format (s <> param) (Proxy @a) + +printf :: HasPrintf a => Proxy a -> Printf a +printf = format "" -- cgit v1.2.1