aboutsummaryrefslogtreecommitdiff
path: root/ch09_09.2.example.hs
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2025-01-03 11:01:20 -0800
committerPaul Oliver <contact@pauloliver.dev>2025-01-05 09:59:10 -0800
commit6a0d7f5c434c3564d0119befb6799fd77581050a (patch)
treef20bc998290211d2a895523417ad32e297b31af0 /ch09_09.2.example.hs
InitialHEADmaster
Diffstat (limited to 'ch09_09.2.example.hs')
-rw-r--r--ch09_09.2.example.hs33
1 files changed, 33 insertions, 0 deletions
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 ""