Debian Codegraph @

src/Reflection/AST/Show.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Converting reflection machinery to strings
5 ------------------------------------------------------------------------
6
7 -- Note that Reflection.termErr can also be used directly in tactic
8 -- error messages.
9
10 {-# OPTIONS --cubical-compatible --safe #-}
11
12 module Reflection.AST.Show where
13
14 import Data.Char.Base as Char
15 import Data.Float.Base as Float
16 open import Data.List.Base hiding (_++_; intersperse)
17 import Data.Nat.Show as ℕ
18 open import Data.String.Base as String using (String; _++_; intersperse; braces; parens; _<+>_)
19 open import Data.String as String using (parensIfSpace)
20 open import Data.Product.Base using (_×_; _,_)
21 import Data.Word64.Base as Word64
22 open import Function.Base using (id; _∘′_; case_of_)
23 open import Relation.Nullary.Decidable.Core using (yes; no)
24
25 open import Reflection.AST.Abstraction hiding (map)
26 open import Reflection.AST.Argument hiding (map)
27 open import Reflection.AST.Argument.Quantity
28 open import Reflection.AST.Argument.Relevance
29 open import Reflection.AST.Argument.Visibility
30 open import Reflection.AST.Argument.Modality
31 open import Reflection.AST.Argument.Information
32 open import Reflection.AST.Definition
33 open import Reflection.AST.Literal
34 open import Reflection.AST.Pattern
35 open import Reflection.AST.Term
36
37 ------------------------------------------------------------------------
38 -- Re-export primitive show functions
39
40 open import Agda.Builtin.Reflection public
41 using () renaming
42 ( primShowMeta to showMeta
43 ; primShowQName to showName
44 )
45
46 ------------------------------------------------------------------------
47 -- Non-primitive show functions
48
49 showRelevance : Relevance → String
50 showRelevance relevant = "relevant"
51 showRelevance irrelevant = "irrelevant"
52
53 showRel : Relevance → String
54 showRel relevant = ""
55 showRel irrelevant = "."
56
57 showVisibility : Visibility → String
58 showVisibility visible = "visible"
59 showVisibility hidden = "hidden"
60 showVisibility instance′ = "instance"
61
62 showQuantity : Quantity → String
63 showQuantity quantity-0 = "quantity-0"
64 showQuantity quantity-ω = "quantity-ω"
65
66 showLiteral : Literal → String
67 showLiteral (nat x) = ℕ.show x
68 showLiteral (word64 x) = ℕ.show (Word64.toℕ x)
69 showLiteral (float x) = Float.show x
70 showLiteral (char x) = Char.show x
71 showLiteral (string x) = String.show x
72 showLiteral (name x) = showName x
73 showLiteral (meta x) = showMeta x
74
75 private
76 -- add appropriate parens depending on the given visibility
77 visibilityParen : Visibility → String → String
78 visibilityParen visible s = parensIfSpace s
79 visibilityParen hidden s = braces s
80 visibilityParen instance′ s = braces (braces s)
81
82 mutual
83
84 showTerms : List (Arg Term) → String
85 showTerms [] = ""
86 showTerms (arg i t ∷ ts) = visibilityParen (visibility i) (showTerm t) <+> showTerms ts
87
88 showTerm : Term → String
89 showTerm (var x args) = "var" <+> ℕ.show x <+> showTerms args
90 showTerm (con c args) = showName c <+> showTerms args
91 showTerm (def f args) = showName f <+> showTerms args
92 showTerm (lam v (abs s x)) = "λ" <+> visibilityParen v s <+> "→" <+> showTerm x
93 showTerm (pat-lam cs args) =
94 "λ {" <+> showClauses cs <+> "}" <+> showTerms args
95 showTerm (Π[ x ∶ arg i a ] b) =
96 "Π (" ++ visibilityParen (visibility i) x <+> ":" <+>
97 parensIfSpace (showTerm a) ++ ")" <+> parensIfSpace (showTerm b)
98 showTerm (sort s) = showSort s
99 showTerm (lit l) = showLiteral l
100 showTerm (meta x args) = showMeta x <+> showTerms args
101 showTerm unknown = "unknown"
102
103 showSort : Sort → String
104 showSort (set t) = "Set" <+> parensIfSpace (showTerm t)
105 showSort (lit n) = "Set" ++ ℕ.show n -- no space to disambiguate from set t
106 showSort (prop t) = "Prop" <+> parensIfSpace (showTerm t)
107 showSort (propLit n) = "Prop" ++ ℕ.show n -- no space to disambiguate from prop t
108 showSort (inf n) = "Setω" ++ ℕ.show n
109 showSort unknown = "unknown"
110
111 showPatterns : List (Arg Pattern) → String
112 showPatterns [] = ""
113 showPatterns (a ∷ ps) = showArg a <+> showPatterns ps
114 where
115 -- Quantities are ignored.
116 showArg : Arg Pattern → String
117 showArg (arg (arg-info h (modality r _)) p) =
118 braces? (showRel r ++ showPattern p)
119 where
120 braces? = case h of λ where
121 visible → id
122 hidden → braces
123 instance′ → braces ∘′ braces
124
125 showPattern : Pattern → String
126 showPattern (con c []) = showName c
127 showPattern (con c ps) = parens (showName c <+> showPatterns ps)
128 showPattern (dot t) = "." ++ parens (showTerm t)
129 showPattern (var x) = "pat-var" <+> ℕ.show x
130 showPattern (lit l) = showLiteral l
131 showPattern (proj f) = showName f
132 showPattern (absurd _) = "()"
133
134 showClause : Clause → String
135 showClause (clause tel ps t) = "[" <+> showTel tel <+> "]" <+> showPatterns ps <+> "→" <+> showTerm t
136 showClause (absurd-clause tel ps) = "[" <+> showTel tel <+> "]" <+> showPatterns ps
137
138 showClauses : List Clause → String
139 showClauses [] = ""
140 showClauses (c ∷ cs) = showClause c <+> ";" <+> showClauses cs
141
142 showTel : List (String × Arg Type) → String
143 showTel [] = ""
144 showTel ((x , arg i t) ∷ tel) = visibilityParen (visibility i) (x <+> ":" <+> showTerm t) ++ showTel tel
145
146 showDefinition : Definition → String
147 showDefinition (function cs) = "function" <+> braces (showClauses cs)
148 showDefinition (data-type pars cs) =
149 "datatype" <+> ℕ.show pars <+> braces (intersperse ", " (map showName cs))
150 showDefinition (record′ c fs) =
151 "record" <+> showName c <+> braces (intersperse ", " (map (showName ∘′ unArg) fs))
152 showDefinition (constructor′ d q) = "constructor" <+> showName d <+> showQuantity q
153 showDefinition axiom = "axiom"
154 showDefinition primitive′ = "primitive"