Use instances to simplify printing code

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
2024-03-11 12:50:05 -07:00
parent 56da61b339
commit 040c13caba
5 changed files with 78 additions and 15 deletions

38
Showable.agda Normal file
View File

@@ -0,0 +1,38 @@
module Showable where
open import Data.String using (String; _++_)
open import Data.Nat using ()
open import Data.Nat.Show using () renaming (show to showNat)
open import Data.Fin using (Fin)
open import Data.Fin.Show using () renaming (show to showFin)
open import Data.Product using (_×_; _,_)
open import Data.Unit using (; tt)
record Showable {a} (A : Set a) : Set a where
field
show : A String
open Showable {{ ... }} public
instance
showableString : Showable String
showableString = record { show = λ s "\"" ++ s ++ "\"" }
showableNat : Showable
showableNat = record { show = showNat }
showableFin : {n : } Showable (Fin n)
showableFin = record { show = showFin }
showableProd : {a b} {A : Set a} {B : Set b}
{{ showableA : Showable A }} {{ showableB : Showable B }}
Showable (A × B)
showableProd {{ showableA }} {{ showableB }} = record
{ show = λ (a , b)
"(" ++ show a ++
", " ++ show b ++
")"
}
showableUnit : Showable
showableUnit = record { show = λ tt "()" }