{-# OPTIONS --safe --sized-types #-}
module Generic.Semantics.Printing {I : Set} where
open import Codata.Stream using (Stream)
open import Size
open import Data.Product
open import Data.List.Base using (List; []; _∷_; _++_)
open import Data.String using (String)
open import Category.Monad
open import Category.Monad.State
open import Function
open import Relation.Unary
open import StateOfTheArt.ACMM using (module Printer)
open Printer using (Fresh; Wrap; Name; Printer; MkW; getW; map^Wrap; th^Wrap; fresh; names)
private
variable
Γ Δ : List I
σ : I
i : Size
module ST = RawMonadState (StateMonadState (Stream String _))
open ST renaming (rawIApplicative to ApplicativeM)
hiding (_<$>_)
open import Data.Var hiding (get; _<$>_)
open import Data.Environment hiding (_>>_; sequenceA; _<$>_)
open import Data.Var.Varlike
open import Generic.Syntax hiding (sequenceA)
open import Generic.Semantics
vl^FreshName : VarLike (λ (σ : I) → Fresh ∘ (Name σ))
vl^FreshName = record
{ th^𝓥 = th^Functor functor^M th^Wrap
; new = fresh _
}
where open ST renaming (rawFunctor to functor^M)
Pieces : List I → I ─Scoped
Pieces [] i Γ = String
Pieces Δ i Γ = (Δ ─Env) Name (Δ ++ Γ) × String
reify^Pieces : ∀ Δ i → Kripke Name Printer Δ i Γ → Fresh (Pieces Δ i Γ)
reify^Pieces [] i p = getW p
reify^Pieces Δ@(_ ∷ _) i f = do
ρ ← sequenceA (freshˡ vl^FreshName _)
b ← getW (f (freshʳ vl^Var Δ) ρ)
return (ρ , b)
where open Data.Environment
instance _ = ApplicativeM
Display : Desc I → Set
Display d = ∀ {i Γ} → ⟦ d ⟧ Pieces i Γ → String
open Semantics
module _ {d : Desc I} where
Printing : Display d → Semantics d Name Printer
Printing dis .th^𝓥 = th^Wrap
Printing dis .var = map^Wrap return
Printing dis .alg = λ v → MkW $ dis <$> mapA d reify^Pieces v
where open Generic.Syntax
open ST
instance _ = ApplicativeM
open Data.Environment
instance _ = ApplicativeM
print : Display d → Tm d i σ Γ → String
print dis t = proj₁ (printer names) where
printer : Fresh String
printer = do
init ← sequenceA (base vl^FreshName)
getW (Semantics.semantics (Printing dis) init t)