agda-spa/Showable.agda
Danila Fedorin 040c13caba Use instances to simplify printing code
Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
2024-03-11 12:50:05 -07:00

39 lines
1.1 KiB
Agda
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 "()" }