summaryrefslogtreecommitdiff
path: root/src/Obs/Term.idr
blob: 9221dbd837f95b1134ab7e66b6accb13b5fc41f5 (plain)
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
||| Abstract syntax for terms.
module Obs.Term

import Data.Fin
import Obs.Sort
import Text.Bounded
import Text.PrettyPrint.Prettyprinter

%default total

-- Definition ------------------------------------------------------------------

public export
data Term : Nat -> Type where
  Var    : WithBounds String -> Fin n -> Term n
  -- Sorts
  Sort   : Bounds -> Sort -> Term n
  -- Dependent Product
  Pi     : Bounds -> String -> Term n -> Term (S n) -> Term n
  Lambda : Bounds -> String -> Term (S n) -> Term n
  App    : Term n -> Term n -> Term n
  -- Dependent Sums
  Sigma  : Bounds -> String -> Term n -> Term (S n) -> Term n
  Pair   : Bounds -> Term n -> Term n -> Term n
  Fst    : Bounds -> Term n -> Term n
  Snd    : Bounds -> Term n -> Term n
  -- True
  Top    : Bounds -> Term n
  Point  : Bounds -> Term n
  -- False
  Bottom : Bounds -> Term n
  Absurd : Bounds -> Term n -> Term n -> Term n

public export
record Definition (n : Nat) where
  constructor MkDefinition
  bounds : Bounds -- of the name
  name   : String
  ty     : Term n
  tm     : Term n

public export
data Block : Nat -> Type where
  Nil  : Block 0
  (:<) : Block n -> Definition n -> Block (S n)

-- Operations ------------------------------------------------------------------

export
fullBounds : Term n -> WithBounds (Term n)
fullBounds tm@(Var var i) = map (\_ => tm) var
fullBounds tm@(Sort x s) = MkBounded tm False x
fullBounds tm@(Pi x var a b) = mergeBounds (mergeBounds (fullBounds a) (fullBounds b)) (MkBounded tm False x)
fullBounds tm@(Lambda x var t) = mergeBounds (fullBounds t) (MkBounded tm False x)
fullBounds tm@(App t u) = map (\_ => tm) (mergeBounds (fullBounds t) (fullBounds u))
fullBounds tm@(Sigma x var a b) = mergeBounds (mergeBounds (fullBounds a) (fullBounds b)) (MkBounded tm False x)
fullBounds tm@(Pair x t u) = mergeBounds (mergeBounds (fullBounds t) (fullBounds u)) (MkBounded tm False x)
fullBounds tm@(Fst x t) = mergeBounds (fullBounds t) (MkBounded tm False x)
fullBounds tm@(Snd x t) = mergeBounds (fullBounds t) (MkBounded tm False x)
fullBounds tm@(Top x) = MkBounded tm False x
fullBounds tm@(Point x) = MkBounded tm False x
fullBounds tm@(Bottom x) = MkBounded tm False x
fullBounds tm@(Absurd x a t) = mergeBounds (mergeBounds (fullBounds a) (fullBounds t)) (MkBounded tm False x)

-- Pretty Print ----------------------------------------------------------------

export
Pretty (Term n) where
  prettyPrec d (Var var _) = pretty var.val
  prettyPrec d (Sort _ s) = prettyPrec d s
  prettyPrec d (Pi _ var a b) =
    parenthesise (d > Open) $
    group $
    parens (pretty var <++> colon <+> softline <+> prettyPrec Open a) <++>
    pretty "->" <+> softline <+>
    prettyPrec Open b
  prettyPrec d (Lambda _ var t) =
    parenthesise (d > Open) $
    group $
    backslash <+> pretty var <++>
    pretty "=>" <+> softline <+>
    prettyPrec Open t
  prettyPrec d (App t u) =
    parenthesise (d >= App) $
    group $
    fillSep [prettyPrec Open t, prettyPrec App u]
  prettyPrec d (Sigma _ var a b) =
    parenthesise (d >= App) $
    group $
    parens (pretty var <++> colon <+> softline <+> prettyPrec Open a) <++>
    pretty "**" <+> softline <+>
    prettyPrec Open b
  prettyPrec d (Pair _ t u) =
    angles $
    group $
    neutral <++> prettyPrec Open t <+> comma <+> softline <+> prettyPrec Open u <++> neutral
  prettyPrec d (Fst _ t) =
    parenthesise (d >= App) $
    group $
    fillSep [pretty "fst", prettyPrec App t]
  prettyPrec d (Snd _ t) =
    parenthesise (d >= App) $
    group $
    fillSep [pretty "snd", prettyPrec App t]
  prettyPrec d (Top _) = pretty "()"
  prettyPrec d (Point _) = pretty "tt"
  prettyPrec d (Bottom _) = pretty "Void"
  prettyPrec d (Absurd _ a t) =
    parenthesise (d > Open) $
    group $
    fillSep [pretty "absurd", prettyPrec App a, prettyPrec App t]

export
Pretty (Definition n) where
  pretty def = group $
    pretty def.name <++> colon <+> softline <+> pretty def.ty <+> hardline <+>
    pretty def.name <++> equals <+> softline <+> pretty def.tm

export
Pretty (Block n) where
  pretty []           = neutral
  pretty ([] :< def)  = pretty def
  pretty (blk :< def) = pretty blk <+> hardline <+> hardline <+> pretty def