| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Environments (heterogeneous collections) |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --with-K --safe #-} |
| 8 |
|
| 9 |
module Data.Star.Environment {ℓ} (Ty : Set ℓ) where |
| 10 |
|
| 11 |
open import Level using (_⊔_) |
| 12 |
open import Data.Star.List using (List) |
| 13 |
open import Data.Star.Decoration using (All) |
| 14 |
open import Data.Star.Pointer as Pointer using (Any; this; that; result) |
| 15 |
open import Data.Unit.Polymorphic.Base using (⊤) |
| 16 |
open import Function.Base using (const; case_of_) |
| 17 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 18 |
open import Relation.Binary.Construct.Closure.ReflexiveTransitive |
| 19 |
using (_▻_) |
| 20 |
|
| 21 |
-- Contexts, listing the types of all the elements in an environment. |
| 22 |
|
| 23 |
Ctxt : Set ℓ |
| 24 |
Ctxt = List Ty |
| 25 |
|
| 26 |
-- Variables (de Bruijn indices); pointers into environments. |
| 27 |
|
| 28 |
infix 4 _∋_ |
| 29 |
|
| 30 |
_∋_ : Ctxt → Ty → Set ℓ |
| 31 |
Γ ∋ σ = Any (const (⊤ {ℓ})) (σ ≡_) Γ |
| 32 |
|
| 33 |
vz : ∀ {Γ σ} → Γ ▻ σ ∋ σ |
| 34 |
vz = this refl |
| 35 |
|
| 36 |
vs : ∀ {Γ σ τ} → Γ ∋ τ → Γ ▻ σ ∋ τ |
| 37 |
vs = that _ |
| 38 |
|
| 39 |
-- Environments. The T function maps types to element types. |
| 40 |
|
| 41 |
Env : ∀ {e} → (Ty → Set e) → (Ctxt → Set (ℓ ⊔ e)) |
| 42 |
Env T Γ = All T Γ |
| 43 |
|
| 44 |
-- A safe lookup function for environments. |
| 45 |
|
| 46 |
lookup : ∀ {Γ σ} {T : Ty → Set} → Env T Γ → Γ ∋ σ → T σ |
| 47 |
lookup ρ i with result refl x ← Pointer.lookup ρ i = x |