|
1 | 1 | -- SPDX-FileCopyrightText: 2025 ECHIDNA Project Team |
2 | | --- SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +-- SPDX-License-Identifier: MIT AND Palimpsest-0.6 |
3 | 3 | -- |
4 | | --- Basic.agda — Simple proofs demonstrating fundamental logical principles. |
5 | | --- Uses agda-stdlib for ℕ and propositional equality. |
| 4 | +-- Basic.agda - Simple proofs demonstrating fundamental logical principles |
| 5 | +-- |
| 6 | +-- This file contains basic proofs for ECHIDNA's Agda backend integration testing. |
| 7 | +-- It demonstrates identity, modus ponens, and transitivity using proof by construction. |
6 | 8 |
|
7 | 9 | module Basic where |
8 | 10 |
|
9 | | -open import Data.Nat using (ℕ; zero; suc) |
10 | | -open import Relation.Binary.PropositionalEquality using (_≡_; refl) |
11 | | - |
12 | | --- ── Identity ────────────────────────────────────────────────────────────── |
| 11 | +-------------------------------------------------------------------------------- |
| 12 | +-- Identity Proof |
| 13 | +-------------------------------------------------------------------------------- |
13 | 14 |
|
14 | | --- The identity function: any proposition implies itself. |
| 15 | +-- The identity function: any proposition implies itself |
| 16 | +-- Type: A → A |
| 17 | +-- This is the simplest proof, returning exactly what we receive |
15 | 18 | id : {A : Set} → A → A |
16 | 19 | id x = x |
17 | 20 |
|
18 | | --- Explicit identity via lambda. |
| 21 | +-- Example: identity for a specific type |
| 22 | +id-nat : (n : ℕ) → ℕ |
| 23 | +id-nat = id |
| 24 | + where |
| 25 | + data ℕ : Set where |
| 26 | + zero : ℕ |
| 27 | + suc : ℕ → ℕ |
| 28 | + |
| 29 | +-- The identity function can also be written explicitly with a lambda |
19 | 30 | id′ : {A : Set} → A → A |
20 | 31 | id′ = λ x → x |
21 | 32 |
|
22 | | --- Identity specialised to ℕ. |
23 | | -id-nat : ℕ → ℕ |
24 | | -id-nat = id |
| 33 | +-------------------------------------------------------------------------------- |
| 34 | +-- Modus Ponens |
| 35 | +-------------------------------------------------------------------------------- |
| 36 | + |
| 37 | +-- Modus ponens: if we have A → B and we have A, then we can derive B |
| 38 | +-- This is function application |
| 39 | +modus-ponens : {A B : Set} → (A → B) → A → B |
| 40 | +modus-ponens f x = f x |
| 41 | + |
| 42 | +-- Alternative notation emphasizing the logical interpretation |
| 43 | +mp : {A B : Set} → (A → B) → A → B |
| 44 | +mp f a = f a |
| 45 | + |
| 46 | +-- Example: applying modus ponens with concrete propositions |
| 47 | +module ModusPonensExample where |
| 48 | + -- Define simple propositions |
| 49 | + data ⊤ : Set where |
| 50 | + tt : ⊤ |
| 51 | + |
| 52 | + data ⊥ : Set where |
25 | 53 |
|
26 | | --- ── Function composition ───────────────────────────────────────────────── |
| 54 | + -- Example: if ⊤ → ⊤ and we have ⊤, we get ⊤ |
| 55 | + example-mp : ⊤ |
| 56 | + example-mp = mp id tt |
27 | 57 |
|
| 58 | +-------------------------------------------------------------------------------- |
| 59 | +-- Transitivity |
| 60 | +-------------------------------------------------------------------------------- |
| 61 | + |
| 62 | +-- Transitivity: if A → B and B → C, then A → C |
| 63 | +-- This is function composition |
| 64 | +trans : {A B C : Set} → (A → B) → (B → C) → (A → C) |
| 65 | +trans f g x = g (f x) |
| 66 | + |
| 67 | +-- Alternative notation using composition operator |
28 | 68 | _∘_ : {A B C : Set} → (B → C) → (A → B) → (A → C) |
29 | 69 | g ∘ f = λ x → g (f x) |
30 | 70 |
|
31 | 71 | infixr 9 _∘_ |
32 | 72 |
|
33 | | --- Composition associativity (definitional — both sides β-reduce to the same λ). |
34 | | -∘-assoc : {A B C D : Set} |
35 | | - → (f : A → B) (g : B → C) (h : C → D) |
36 | | - → (h ∘ (g ∘ f)) ≡ ((h ∘ g) ∘ f) |
37 | | -∘-assoc _ _ _ = refl |
| 73 | +-- Proof that transitivity is associative |
| 74 | +trans-assoc : {A B C D : Set} |
| 75 | + → (f : A → B) → (g : B → C) → (h : C → D) |
| 76 | + → (h ∘ (g ∘ f)) ≡ ((h ∘ g) ∘ f) |
| 77 | +trans-assoc f g h = refl |
| 78 | + where |
| 79 | + -- Propositional equality |
| 80 | + data _≡_ {A : Set} (x : A) : A → Set where |
| 81 | + refl : x ≡ x |
| 82 | + |
| 83 | + infix 4 _≡_ |
| 84 | + |
| 85 | +-- Proof that identity is a left identity for composition |
| 86 | +id-left : {A B : Set} → (f : A → B) → (id ∘ f) ≡ f |
| 87 | +id-left f = refl |
| 88 | + where |
| 89 | + data _≡_ {A : Set} (x : A) : A → Set where |
| 90 | + refl : x ≡ x |
| 91 | + infix 4 _≡_ |
| 92 | + |
| 93 | +-- Proof that identity is a right identity for composition |
| 94 | +id-right : {A B : Set} → (f : A → B) → (f ∘ id) ≡ f |
| 95 | +id-right f = refl |
| 96 | + where |
| 97 | + data _≡_ {A : Set} (x : A) : A → Set where |
| 98 | + refl : x ≡ x |
| 99 | + infix 4 _≡_ |
| 100 | + |
| 101 | +-------------------------------------------------------------------------------- |
| 102 | +-- Additional Basic Combinators |
| 103 | +-------------------------------------------------------------------------------- |
| 104 | + |
| 105 | +-- The K combinator: constant function |
| 106 | +const : {A B : Set} → A → B → A |
| 107 | +const x y = x |
| 108 | + |
| 109 | +-- The S combinator: application combinator |
| 110 | +S : {A B C : Set} → (A → B → C) → (A → B) → A → C |
| 111 | +S f g x = f x (g x) |
| 112 | + |
| 113 | +-- Flip: swaps the order of arguments |
| 114 | +flip : {A B C : Set} → (A → B → C) → (B → A → C) |
| 115 | +flip f y x = f x y |
| 116 | + |
| 117 | +-- Function application operator (useful for reducing parentheses) |
| 118 | +_$_ : {A B : Set} → (A → B) → A → B |
| 119 | +f $ x = f x |
| 120 | + |
| 121 | +infixr 0 _$_ |
| 122 | + |
| 123 | +-------------------------------------------------------------------------------- |
| 124 | +-- Proof that basic combinators satisfy certain properties |
| 125 | +-------------------------------------------------------------------------------- |
| 126 | + |
| 127 | +module CombinatorLaws where |
| 128 | + data _≡_ {A : Set} (x : A) : A → Set where |
| 129 | + refl : x ≡ x |
| 130 | + |
| 131 | + infix 4 _≡_ |
| 132 | + |
| 133 | + -- SKK = I (a famous combinator identity) |
| 134 | + SKK : {A : Set} → (x : A) → S const const x ≡ id x |
| 135 | + SKK x = refl |
| 136 | + |
| 137 | + -- Flip is involutive (applying it twice gets you back where you started) |
| 138 | + flip-involutive : {A B C : Set} → (f : A → B → C) |
| 139 | + → (x : A) → (y : B) |
| 140 | + → flip (flip f) x y ≡ f x y |
| 141 | + flip-involutive f x y = refl |
| 142 | + |
| 143 | +-------------------------------------------------------------------------------- |
| 144 | +-- Curry and Uncurry |
| 145 | +-------------------------------------------------------------------------------- |
| 146 | + |
| 147 | +-- Product type (pairs) |
| 148 | +record _×_ (A B : Set) : Set where |
| 149 | + constructor _,_ |
| 150 | + field |
| 151 | + fst : A |
| 152 | + snd : B |
| 153 | + |
| 154 | +infixr 4 _×_ |
| 155 | +infixr 5 _,_ |
| 156 | + |
| 157 | +-- Curry: convert a function on pairs to a curried function |
| 158 | +curry : {A B C : Set} → (A × B → C) → (A → B → C) |
| 159 | +curry f x y = f (x , y) |
| 160 | + |
| 161 | +-- Uncurry: convert a curried function to a function on pairs |
| 162 | +uncurry : {A B C : Set} → (A → B → C) → (A × B → C) |
| 163 | +uncurry f (x , y) = f x y |
| 164 | + |
| 165 | +-- Proof that curry and uncurry are inverses |
| 166 | +module CurryUncurryLaws where |
| 167 | + data _≡_ {A : Set} (x : A) : A → Set where |
| 168 | + refl : x ≡ x |
| 169 | + |
| 170 | + -- INTENTIONAL AXIOM: Function extensionality (funext) |
| 171 | + -- Justification: funext is consistent with Agda's type theory and is |
| 172 | + -- provable in Cubical Agda (--cubical). In plain Agda it must be |
| 173 | + -- postulated. It is a standard mathematical axiom accepted by all |
| 174 | + -- major proof assistants (Coq's Functional Extensionality, Lean's |
| 175 | + -- funext, HoTT axiom). It does NOT compromise soundness. |
| 176 | + -- See: HoTT Book, Section 2.9; nLab "function extensionality" |
| 177 | + postulate |
| 178 | + funext : {A B : Set} {f g : A → B} |
| 179 | + → ((x : A) → f x ≡ g x) |
| 180 | + → f ≡ g |
| 181 | + |
| 182 | + curry-uncurry : {A B C : Set} → (f : A → B → C) |
| 183 | + → curry (uncurry f) ≡ f |
| 184 | + curry-uncurry f = funext λ x → funext λ y → refl |
| 185 | + |
| 186 | + uncurry-curry : {A B C : Set} → (f : A × B → C) |
| 187 | + → uncurry (curry f) ≡ f |
| 188 | + uncurry-curry f = funext λ { (x , y) → refl } |
0 commit comments