module Generic.Semantics.Printing where
open import Codata.Thunk
open import Codata.Stream as Stream using (Stream; _∷_)
open import Data.Unit
open import Data.Bool
open import Data.Product
open import Data.Nat.Base
open import Data.Nat.Show as Nat
open import Data.List.Base using (List; []; _∷_)
open import Data.List.NonEmpty as List⁺ using (List⁺; _∷_)
open import Data.Char
open import Data.String using (String ; _++_ ; fromList ; toList)
open import Category.Monad
open import Category.Monad.State
open import Function
open module ST = RawMonadState (StateMonadState (Stream String _))
M = State (Stream String _)
open import var hiding (get)
open import environment as E
open import varlike
open import Generic.Syntax as S
open import Generic.Semantics
module _ {I : Set} where
record Name (i : I) (Γ : List I) : Set where
constructor mkN; field getN : String
open Name public
record Printer (i : I) (Γ : List I) : Set where
constructor mkP; field getP : M String
open Printer public
module _ {I : Set} where
fresh : {i : I} {Γ : List I} → M (Name i Γ)
fresh = get >>= λ nms →
put (Stream.tail nms) >>= λ _ →
return $ mkN $ Stream.head nms
vl^StName : VarLike (λ i Γ → M (Name i Γ))
new vl^StName = fresh
th^𝓥 vl^StName = λ st _ → mkN ∘ getN ST.<$> st
module _ {I : Set} (d : Desc I) where
Pieces : List I → I ─Scoped
Pieces [] i Γ = String
Pieces Δ i Γ = (Δ ─Env) (λ _ _ → String) [] × String
record Display : Set where
constructor mkD
field getD : ∀ {i Γ} → ⟦ d ⟧ Pieces i Γ → String
open Display public
module _ {I : Set} {d : Desc I} where
printing : Display d → Sem d Name Printer
Sem.th^𝓥 (printing dis) n = const $ mkN (getN n)
Sem.var (printing dis) n = mkP (return (getN n))
Sem.alg (printing dis) {i} {Γ} v = mkP $ getD dis ST.<$> ih where
reify^M : {Γ : List I} (Δ : List I) (i : I) →
Kripke Name Printer Δ i Γ →
M (Pieces d Δ i Γ)
reify^M [] i = getP
reify^M Δ@(_ ∷ _) i = λ f → let σ = freshˡ vl^StName _
in E.traverse rawIApplicative σ >>= λ ρ →
getP (f (freshʳ vl^Var Δ) ρ) >>= λ b →
return ((getN E.<$> ρ) , b)
ih : M (⟦ d ⟧ (Pieces d) i Γ)
ih = S.traverse rawIApplicative d (fmap d reify^M v)
print : Display d → {i : I} → TM d i → String
print dis t = proj₁ $ getP (Sem.closed (printing dis) t) names where
alphabetWithSuffix : String → List⁺ String
alphabetWithSuffix suffix = List⁺.map (λ c → fromList (c ∷ []) ++ suffix)
$′ 'a' ∷ toList "bcdefghijklmnopqrstuvwxyz"
allNats : Stream ℕ _
allNats = Stream.iterate suc 0
names : Stream String _
names = Stream.concat
$′ Stream.map alphabetWithSuffix
$′ "" ∷ λ where .force → Stream.map Nat.show allNats