~ubuntu-branches/ubuntu/precise/agda-stdlib/precise

« back to all changes in this revision

Viewing changes to src/Data/Context.agda

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2011-02-25 22:28:40 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110225222840-jt16gl302kca7h0g
Tags: 0.5-1~ubuntu1

* Upload to Ubuntu from Debian's VCS due to delays in unstable caused by
  the GHC 7 transition (LP: #725364)
* [ba20206] Make package architecture independent (Closes: #573807, #598708,
  #599958)
* [0fb0600] Standards-Version → 3.9.1, no changes required
* [d3f13b8] Update required Agda version to 2.2.8
* [cc1f5c8] Imported Upstream version 0.4
* [2c82171] Add watch file
* [9251e0b] Imported Upstream version 0.5
* [0518fa6] No longer need procps | hurd BD as we no longer have the ticker
* [daf2445] Don't use upstream's make install — handled ourselves by
  dh_install
* [1b86533] Update watchfile to point at new location
* [637f47d] Remove debian/gbp.conf as we are no longer building for exp
* [cc88671] Require Agda 2.2.10
* [e99dab5] Set maximum stack size to 1G to prevent overflows in the build
* [251cd1d] Run the test suite manually
* [a7db697] Set the variable in the emacs loading script properly

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
------------------------------------------------------------------------
 
2
-- Contexts, variables, substitutions, etc.
 
3
------------------------------------------------------------------------
 
4
 
 
5
{-# OPTIONS --universe-polymorphism #-}
 
6
 
 
7
-- Based on Conor McBride's "Outrageous but Meaningful Coincidences:
 
8
-- Dependent type-safe syntax and evaluation".
 
9
 
 
10
-- The contexts and variables are parametrised by a universe.
 
11
 
 
12
open import Universe
 
13
 
 
14
module Data.Context {u e} (Uni : Universe u e) where
 
15
 
 
16
open Universe.Universe Uni
 
17
 
 
18
open import Data.Product as Prod
 
19
open import Data.Unit
 
20
open import Function
 
21
open import Level
 
22
 
 
23
------------------------------------------------------------------------
 
24
-- Contexts and "types"
 
25
 
 
26
mutual
 
27
 
 
28
  -- Contexts.
 
29
 
 
30
  infixl 5 _▻_
 
31
 
 
32
  data Ctxt : Set (u ⊔ e) where
 
33
    ε   : Ctxt
 
34
    _▻_ : (Γ : Ctxt) (σ : Type Γ) → Ctxt
 
35
 
 
36
  -- Semantic types: maps from environments to universe codes.
 
37
 
 
38
  Type : Ctxt → Set (u ⊔ e)
 
39
  Type Γ = Env Γ → U
 
40
 
 
41
  -- Interpretation of contexts: environments.
 
42
 
 
43
  Env : Ctxt → Set e
 
44
  Env ε       = Lift ⊤
 
45
  Env (Γ ▻ σ) = Σ (Env Γ) λ γ → El (σ γ)
 
46
 
 
47
-- Type signature for variables, terms, etc.
 
48
 
 
49
Term-like : (ℓ : Level) → Set _
 
50
Term-like ℓ = (Γ : Ctxt) → Type Γ → Set ℓ
 
51
 
 
52
-- Semantic values: maps from environments to universe values.
 
53
 
 
54
Value : Term-like _
 
55
Value Γ σ = (γ : Env Γ) → El (σ γ)
 
56
 
 
57
------------------------------------------------------------------------
 
58
-- "Substitutions"
 
59
 
 
60
-- Semantic substitutions or context morphisms: maps from environments
 
61
-- to environments.
 
62
 
 
63
infixr 4 _⇨_
 
64
 
 
65
_⇨_ : Ctxt → Ctxt → Set _
 
66
Γ ⇨ Δ = Env Δ → Env Γ
 
67
 
 
68
-- Maps between types.
 
69
 
 
70
infixr 4 _⇨₁_
 
71
 
 
72
_⇨₁_ : ∀ {Γ} → Type Γ → Type Γ → Set _
 
73
σ ⇨₁ τ = ∀ {γ} → El (τ γ) → El (σ γ)
 
74
 
 
75
-- Application of substitutions to types.
 
76
--
 
77
-- Note that this operation is composition of functions. I have chosen
 
78
-- not to give a separate name to the identity substitution, which is
 
79
-- simply the identity function, nor to reverse composition of
 
80
-- substitutions, which is composition of functions.
 
81
 
 
82
infixl 8 _/_
 
83
 
 
84
_/_ : ∀ {Γ Δ} → Type Γ → Γ ⇨ Δ → Type Δ
 
85
σ / ρ = σ ∘ ρ
 
86
 
 
87
-- Application of substitutions to values.
 
88
 
 
89
infixl 8 _/v_
 
90
 
 
91
_/v_ : ∀ {Γ Δ σ} → Value Γ σ → (ρ : Γ ⇨ Δ) → Value Δ (σ / ρ)
 
92
v /v ρ = v ∘ ρ
 
93
 
 
94
-- Weakening.
 
95
 
 
96
wk : ∀ {Γ σ} → Γ ⇨ Γ ▻ σ
 
97
wk = proj₁
 
98
 
 
99
-- Extends a substitution with another value.
 
100
 
 
101
infixl 5 _►_
 
102
 
 
103
_►_ : ∀ {Γ Δ σ} (ρ : Γ ⇨ Δ) → Value Δ (σ / ρ) → Γ ▻ σ ⇨ Δ
 
104
_►_ = <_,_>
 
105
 
 
106
-- A substitution which only replaces the first "variable".
 
107
 
 
108
sub : ∀ {Γ σ} → Value Γ σ → Γ ▻ σ ⇨ Γ
 
109
sub v = id ► v
 
110
 
 
111
-- One can construct a substitution between two non-empty contexts by
 
112
-- supplying two functions, one which handles the last element and one
 
113
-- which handles the rest.
 
114
 
 
115
infixl 10 _↑_
 
116
 
 
117
_↑_ : ∀ {Γ Δ σ τ} (ρ : Γ ⇨ Δ) → σ / ρ ⇨₁ τ → Γ ▻ σ ⇨ Δ ▻ τ
 
118
_↑_ = Prod.map
 
119
 
 
120
-- Lifting.
 
121
 
 
122
infix 10 _↑
 
123
 
 
124
_↑ : ∀ {Γ Δ σ} (ρ : Γ ⇨ Δ) → Γ ▻ σ ⇨ Δ ▻ σ / ρ
 
125
ρ ↑ = ρ ↑ id
 
126
 
 
127
------------------------------------------------------------------------
 
128
-- Variables
 
129
 
 
130
-- Variables (de Bruijn indices).
 
131
 
 
132
infix 4 _∋_
 
133
 
 
134
data _∋_ : Term-like (u ⊔ e) where
 
135
  zero : ∀ {Γ σ}               → Γ ▻ σ ∋ σ / wk
 
136
  suc  : ∀ {Γ σ τ} (x : Γ ∋ σ) → Γ ▻ τ ∋ σ / wk
 
137
 
 
138
-- Interpretation of variables: a lookup function.
 
139
 
 
140
lookup : ∀ {Γ σ} → Γ ∋ σ → Value Γ σ
 
141
lookup zero    (γ , v) = v
 
142
lookup (suc x) (γ , v) = lookup x γ
 
143
 
 
144
-- Application of substitutions to variables.
 
145
 
 
146
infixl 8 _/∋_
 
147
 
 
148
_/∋_ : ∀ {Γ Δ σ} → Γ ∋ σ → (ρ : Γ ⇨ Δ) → Value Δ (σ / ρ)
 
149
x /∋ ρ = lookup x /v ρ
 
150
 
 
151
------------------------------------------------------------------------
 
152
-- Context extensions
 
153
 
 
154
mutual
 
155
 
 
156
  -- Context extensions.
 
157
 
 
158
  infixl 5 _▻_
 
159
 
 
160
  data Ctxt⁺ (Γ : Ctxt) : Set (u ⊔ e) where
 
161
    ε   : Ctxt⁺ Γ
 
162
    _▻_ : (Γ⁺ : Ctxt⁺ Γ) (σ : Type (Γ ++ Γ⁺)) → Ctxt⁺ Γ
 
163
 
 
164
  -- Appends a context extension to a context.
 
165
 
 
166
  infixl 5 _++_
 
167
 
 
168
  _++_ : (Γ : Ctxt) → Ctxt⁺ Γ → Ctxt
 
169
  Γ ++ ε        = Γ
 
170
  Γ ++ (Γ⁺ ▻ σ) = (Γ ++ Γ⁺) ▻ σ
 
171
 
 
172
mutual
 
173
 
 
174
  -- Application of substitutions to context extensions.
 
175
 
 
176
  infixl 8 _/⁺_
 
177
 
 
178
  _/⁺_ : ∀ {Γ Δ} → Ctxt⁺ Γ → Γ ⇨ Δ → Ctxt⁺ Δ
 
179
  ε        /⁺ ρ = ε
 
180
  (Γ⁺ ▻ σ) /⁺ ρ = Γ⁺ /⁺ ρ ▻ σ / ρ ↑⋆ Γ⁺
 
181
 
 
182
  -- N-ary lifting of substitutions.
 
183
 
 
184
  infixl 10 _↑⋆_
 
185
 
 
186
  _↑⋆_ : ∀ {Γ Δ} (ρ : Γ ⇨ Δ) → ∀ Γ⁺ → Γ ++ Γ⁺ ⇨ Δ ++ Γ⁺ /⁺ ρ
 
187
  ρ ↑⋆ ε        = ρ
 
188
  ρ ↑⋆ (Γ⁺ ▻ σ) = (ρ ↑⋆ Γ⁺) ↑