| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Helpers intended to ease the development of "tactics" which use |
| 5 |
-- proof by reflection |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
open import Data.Fin.Base using (Fin) |
| 11 |
open import Data.Nat.Base using (ℕ) |
| 12 |
open import Data.Vec.Base as Vec using (Vec; allFin) |
| 13 |
open import Function.Base using (id) |
| 14 |
open import Function.Bundles using (module Equivalence) |
| 15 |
open import Level using (Level) |
| 16 |
open import Relation.Binary.Bundles using (Setoid) |
| 17 |
|
| 18 |
-- Think of the parameters as follows: |
| 19 |
-- |
| 20 |
-- * Expr: A representation of code. |
| 21 |
-- * var: The Expr type should support a notion of variables. |
| 22 |
-- * ⟦_⟧: Computes the semantics of an expression. Takes an |
| 23 |
-- environment mapping variables to something. |
| 24 |
-- * ⟦_⇓⟧: Computes the semantics of the normal form of the |
| 25 |
-- expression. |
| 26 |
-- * correct: Normalisation preserves the semantics. |
| 27 |
-- |
| 28 |
-- Given these parameters two "tactics" are returned, prove and solve. |
| 29 |
-- |
| 30 |
-- For an example of the use of this module, see Algebra.RingSolver. |
| 31 |
|
| 32 |
module Relation.Binary.Reflection |
| 33 |
{e a s} |
| 34 |
{Expr : ℕ → Set e} {A : Set a} |
| 35 |
(Sem : Setoid a s) (open Setoid Sem using (Carrier; _≈_)) |
| 36 |
(var : ∀ {n} → Fin n → Expr n) |
| 37 |
(⟦_⟧ ⟦_⇓⟧ : ∀ {n} → Expr n → Vec A n → Carrier) |
| 38 |
(correct : ∀ {n} (e : Expr n) ρ → ⟦ e ⇓⟧ ρ ≈ ⟦ e ⟧ ρ) |
| 39 |
where |
| 40 |
|
| 41 |
open import Data.Vec.N-ary |
| 42 |
open import Data.Product.Base using (_×_; _,_; proj₁; proj₂) |
| 43 |
import Relation.Binary.PropositionalEquality.Core as ≡ |
| 44 |
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning |
| 45 |
|
| 46 |
open ≈-Reasoning Sem |
| 47 |
|
| 48 |
-- If two normalised expressions are semantically equal, then their |
| 49 |
-- non-normalised forms are also equal. |
| 50 |
|
| 51 |
prove : ∀ {n} (ρ : Vec A n) e₁ e₂ → |
| 52 |
⟦ e₁ ⇓⟧ ρ ≈ ⟦ e₂ ⇓⟧ ρ → |
| 53 |
⟦ e₁ ⟧ ρ ≈ ⟦ e₂ ⟧ ρ |
| 54 |
prove ρ e₁ e₂ hyp = begin |
| 55 |
⟦ e₁ ⟧ ρ ≈⟨ correct e₁ ρ ⟨ |
| 56 |
⟦ e₁ ⇓⟧ ρ ≈⟨ hyp ⟩ |
| 57 |
⟦ e₂ ⇓⟧ ρ ≈⟨ correct e₂ ρ ⟩ |
| 58 |
⟦ e₂ ⟧ ρ ∎ |
| 59 |
|
| 60 |
-- Applies the function to all possible "variables". |
| 61 |
|
| 62 |
close : ∀ {A : Set e} n → N-ary n (Expr n) A → A |
| 63 |
close n f = f $ⁿ Vec.map var (allFin n) |
| 64 |
|
| 65 |
-- A variant of prove which should in many cases be easier to use, |
| 66 |
-- because variables and environments are handled in a less explicit |
| 67 |
-- way. |
| 68 |
-- |
| 69 |
-- If the type signature of solve is a bit daunting, then it may be |
| 70 |
-- helpful to instantiate n with a small natural number and normalise |
| 71 |
-- the remainder of the type. |
| 72 |
|
| 73 |
solve : ∀ n (f : N-ary n (Expr n) (Expr n × Expr n)) → |
| 74 |
Eqʰ n _≈_ (curryⁿ ⟦ proj₁ (close n f) ⇓⟧) (curryⁿ ⟦ proj₂ (close n f) ⇓⟧) → |
| 75 |
Eq n _≈_ (curryⁿ ⟦ proj₁ (close n f) ⟧) (curryⁿ ⟦ proj₂ (close n f) ⟧) |
| 76 |
solve n f hyp = |
| 77 |
curryⁿ-cong _≈_ ⟦ proj₁ (close n f) ⟧ ⟦ proj₂ (close n f) ⟧ |
| 78 |
(λ ρ → prove ρ (proj₁ (close n f)) (proj₂ (close n f)) |
| 79 |
(curryⁿ-cong⁻¹ _≈_ |
| 80 |
⟦ proj₁ (close n f) ⇓⟧ ⟦ proj₂ (close n f) ⇓⟧ |
| 81 |
(Eqʰ-to-Eq n _≈_ hyp) ρ)) |
| 82 |
|
| 83 |
-- A variant of solve which does not require that the normal form |
| 84 |
-- equality is proved for an arbitrary environment. |
| 85 |
|
| 86 |
solve₁ : ∀ n (f : N-ary n (Expr n) (Expr n × Expr n)) → |
| 87 |
∀ⁿ n (curryⁿ λ ρ → |
| 88 |
⟦ proj₁ (close n f) ⇓⟧ ρ ≈ ⟦ proj₂ (close n f) ⇓⟧ ρ → |
| 89 |
⟦ proj₁ (close n f) ⟧ ρ ≈ ⟦ proj₂ (close n f) ⟧ ρ) |
| 90 |
solve₁ n f = |
| 91 |
Equivalence.from (uncurry-∀ⁿ n) λ ρ → |
| 92 |
≡.subst id (≡.sym (left-inverse (λ _ → _ ≈ _ → _ ≈ _) ρ)) |
| 93 |
(prove ρ (proj₁ (close n f)) (proj₂ (close n f))) |
| 94 |
|
| 95 |
-- A variant of _,_ which is intended to make uses of solve and solve₁ |
| 96 |
-- look a bit nicer. |
| 97 |
|
| 98 |
infix 4 _⊜_ |
| 99 |
|
| 100 |
_⊜_ : ∀ {n} → Expr n → Expr n → Expr n × Expr n |
| 101 |
_⊜_ = _,_ |