Debian Codegraph @

src/Text/Printf/Generic.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Generic printf function.
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Text.Printf.Generic where
10
11 open import Level using (Level; 0ℓ; _⊔_; Lift)
12 open import Data.List.Base as List using (List; []; _∷_)
13 open import Data.Char.Base
14 open import Data.Maybe.Base hiding (map)
15 open import Data.Nat.Base using (ℕ)
16 open import Data.Product.Base hiding (map)
17 open import Data.Product.Nary.NonDependent
18 open import Data.String.Base using (String)
19 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
20 open import Data.Unit.Base using (⊤)
21 open import Function.Nary.NonDependent
22 open import Function.Base
23
24 open import Text.Format.Generic
25
26 private
27 variable
28 ℓ : Level
29 A B : Set ℓ
30
31 ------------------------------------------------------------------------
32 -- Printf argument specifications.
33 -- Defines the rendering of chunks.
34
35 record PrintfSpec {ℓ} (spec : FormatSpec) (A : Set ℓ) : Set (Level.suc 0ℓ ⊔ ℓ) where
36
37 open FormatSpec spec public
38
39 field
40 renderArg : ∀ arg → ArgType arg → A
41 renderStr : String → A
42
43 module Type (spec : FormatSpec) where
44
45 open Format spec renaming (Error to FormatError)
46
47 record Error (e : FormatError) : Set ℓ where
48
49 private
50
51 Size : FormatError ⊎ Format → ℕ
52 Size (inj₁ err) = 0
53 Size (inj₂ fmt) = size fmt
54
55 Printf : ∀ pr → Set ℓ → Set (ℓ ⊔ ⨆ (Size pr) 0ℓs)
56 Printf (inj₁ err) _ = Error err
57 Printf (inj₂ fmt) B = Arrows _ ⟦ fmt ⟧ B
58
59 map : ∀ pr → (A → B) → Printf pr A → Printf pr B
60 map (inj₁ err) f p = _
61 map (inj₂ fmt) f p = mapₙ _ f p
62
63 module Render {spec : FormatSpec} (render : PrintfSpec spec A) where
64
65 open PrintfSpec render
66 open Type spec
67
68 open Format spec renaming (Error to FormatError)
69
70 assemble : ∀ fmt → Product⊤ _ ⟦ fmt ⟧ → List A
71 assemble [] vs = []
72 assemble (Arg a ∷ fmt) (x , vs) = renderArg a x ∷ assemble fmt vs
73 assemble (Raw str ∷ fmt) vs = renderStr str ∷ assemble fmt vs
74
75 private
76
77 printf′ : ∀ pr → Printf pr (List A)
78 printf′ (inj₁ err) = _
79 printf′ (inj₂ fmt) = curry⊤ₙ _ (assemble fmt)
80
81 printf : (input : String) → Printf (lexer input) (List A)
82 printf input = printf′ (lexer input)