1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
||| Abstract syntax for terms.
module Obs.Term
import Data.Fin
import Obs.Universe
import Text.Bounded
import Text.PrettyPrint.Prettyprinter
%default total
-- Definition ------------------------------------------------------------------
public export
data Term : Nat -> Type where
Var : String -> Fin n -> Term n
-- Universes
Universe : Universe -> Term n
-- Dependent Product
Pi : WithBounds String -> WithBounds (Term n) -> WithBounds (Term (S n)) -> Term n
Lambda : WithBounds String -> WithBounds (Term (S n)) -> Term n
App : WithBounds (Term n) -> WithBounds (Term n) -> Term n
-- Dependent Sums
Sigma : WithBounds String -> WithBounds (Term n) -> WithBounds (Term (S n)) -> Term n
Pair : WithBounds (Term n) -> WithBounds (Term n) -> Term n
Fst : WithBounds (Term n) -> Term n
Snd : WithBounds (Term n) -> Term n
-- Booleans
Bool : Term n
True : Term n
False : Term n
If : WithBounds String ->
WithBounds (Term (S n)) ->
WithBounds (Term n) ->
WithBounds (Term n) ->
WithBounds (Term n) ->
Term n
-- True
Top : Term n
Point : Term n
-- False
Bottom : Term n
Absurd : WithBounds (Term n) -> Term n
-- Equality
Equal : WithBounds (Term n) -> WithBounds (Term n) -> Term n
Refl : WithBounds (Term n) -> Term n
Transp : WithBounds (Term n) ->
WithBounds (Term n) ->
WithBounds (Term n) ->
WithBounds (Term n) ->
WithBounds (Term n) ->
Term n
Cast : WithBounds (Term n) ->
WithBounds (Term n) ->
WithBounds (Term n) ->
WithBounds (Term n) ->
Term n
CastId : WithBounds (Term n) -> Term n
public export
record Definition (n : Nat) where
constructor MkDefinition
name : WithBounds String
ty : WithBounds (Term n)
tm : WithBounds (Term n)
public export
data Block : Nat -> Type where
Nil : Block 0
(:<) : Block n -> Definition n -> Block (S n)
-- Pretty Print ----------------------------------------------------------------
prettyPrec : Prec -> Term n -> Doc ann
prettyPrecBounds : Prec -> WithBounds (Term n) -> Doc ann
prettyPrec d (Var var _) = pretty var
prettyPrec d (Universe s) = prettyPrec d s
prettyPrec d (Pi var a b) =
parenthesise (d > Open) $
group $
parens (pretty var.val <++> colon <+> softline <+> align (prettyPrecBounds Open a)) <++>
pretty "->" <+> line <+>
align (prettyPrecBounds Open b)
prettyPrec d (Lambda var t) =
parenthesise (d > Open) $
group $
backslash <+> pretty var.val <++>
pretty "=>" <+> line <+>
align (prettyPrecBounds Open t)
prettyPrec d (App t u) =
parenthesise (d >= App) $
group $
vsep [prettyPrecBounds Open t, prettyPrecBounds App u]
prettyPrec d (Sigma var a b) =
parenthesise (d >= App) $
group $
parens (pretty var.val <++> colon <+> softline <+> align (prettyPrecBounds Open a)) <++>
pretty "**" <+> line <+>
align (prettyPrecBounds Open b)
prettyPrec d (Pair t u) =
angles $
group $
neutral <++> prettyPrecBounds Open t <+> comma <+> line <+> prettyPrecBounds Open u <++> neutral
prettyPrec d (Fst t) =
parenthesise (d >= App) $
group $
vsep [pretty "fst", prettyPrecBounds App t]
prettyPrec d (Snd t) =
parenthesise (d >= App) $
group $
vsep [pretty "snd", prettyPrecBounds App t]
prettyPrec d Bool = pretty "Bool"
prettyPrec d True = pretty "True"
prettyPrec d False = pretty "False"
prettyPrec d (If var a t f g) =
parenthesise (d >= App) $
group $
vsep $
[ pretty "if"
, parens $
group $
backslash <+> pretty var.val <++>
pretty "=>" <+> line <+>
align (prettyPrecBounds Open a)
, prettyPrecBounds App t
, prettyPrecBounds App f
, prettyPrecBounds App g
]
prettyPrec d Top = pretty "()"
prettyPrec d Point = pretty "tt"
prettyPrec d Bottom = pretty "Void"
prettyPrec d (Absurd t) =
parenthesise (d > Open) $
group $
vsep [pretty "absurd", prettyPrecBounds App t]
prettyPrec d (Equal t u) =
parenthesise (d >= Equal) $
group $
prettyPrecBounds Equal t <++> pretty "~" <+> line <+> prettyPrecBounds Equal u
prettyPrec d (Refl t) =
parenthesise (d >= App) $
group $
vsep [pretty "refl", prettyPrecBounds App t]
prettyPrec d (Transp a t u t' e) =
parenthesise (d >= App) $
group $
vsep $
[ pretty "transp"
, prettyPrecBounds App a
, prettyPrecBounds App t
, prettyPrecBounds App u
, prettyPrecBounds App t'
, prettyPrecBounds App e
]
prettyPrec d (Cast a b e t) =
parenthesise (d >= App) $
group $
vsep $
[ pretty "cast"
, prettyPrecBounds App a
, prettyPrecBounds App b
, prettyPrecBounds App e
, prettyPrecBounds App t
]
prettyPrec d (CastId t) =
parenthesise (d >= App) $
group $
vsep [pretty "castRefl", prettyPrecBounds App t]
prettyPrecBounds d (MkBounded v _ _) = prettyPrec d v
export
Pretty (Term n) where
prettyPrec = Term.prettyPrec
export
Pretty (Definition n) where
pretty def = group $
pretty def.name.val <++> colon <+> softline <+> pretty def.ty.val <+> hardline <+>
pretty def.name.val <++> equals <+> softline <+> pretty def.tm.val
export
Pretty (Block n) where
pretty [] = neutral
pretty ([] :< def) = pretty def
pretty (blk :< def) = pretty blk <+> hardline <+> hardline <+> pretty def
|