|
1 | 1 | -- SPDX-FileCopyrightText: 2025 ECHIDNA Project Team |
2 | | --- SPDX-License-Identifier: MIT AND Palimpsest-0.6 |
| 2 | +-- SPDX-License-Identifier: PMPL-1.0-or-later |
3 | 3 | -- |
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. |
| 4 | +-- Basic.agda — Simple proofs demonstrating fundamental logical principles. |
| 5 | +-- Uses agda-stdlib for ℕ and propositional equality. |
8 | 6 |
|
9 | 7 | module Basic where |
10 | 8 |
|
11 | | --------------------------------------------------------------------------------- |
12 | | --- Identity Proof |
13 | | --------------------------------------------------------------------------------- |
| 9 | +open import Data.Nat using (ℕ; zero; suc) |
| 10 | +open import Relation.Binary.PropositionalEquality using (_≡_; refl) |
| 11 | + |
| 12 | +-- ── Identity ────────────────────────────────────────────────────────────── |
14 | 13 |
|
15 | | --- The identity function: any proposition implies itself |
16 | | --- Type: A → A |
17 | | --- This is the simplest proof, returning exactly what we receive |
| 14 | +-- The identity function: any proposition implies itself. |
18 | 15 | id : {A : Set} → A → A |
19 | 16 | id x = x |
20 | 17 |
|
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 |
| 18 | +-- Explicit identity via lambda. |
30 | 19 | id′ : {A : Set} → A → A |
31 | 20 | id′ = λ x → x |
32 | 21 |
|
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 |
53 | | - |
54 | | - -- Example: if ⊤ → ⊤ and we have ⊤, we get ⊤ |
55 | | - example-mp : ⊤ |
56 | | - example-mp = mp id tt |
57 | | - |
58 | | --------------------------------------------------------------------------------- |
59 | | --- Transitivity |
60 | | --------------------------------------------------------------------------------- |
| 22 | +-- Identity specialised to ℕ. |
| 23 | +id-nat : ℕ → ℕ |
| 24 | +id-nat = id |
61 | 25 |
|
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) |
| 26 | +-- ── Function composition ───────────────────────────────────────────────── |
66 | 27 |
|
67 | | --- Alternative notation using composition operator |
68 | 28 | _∘_ : {A B C : Set} → (B → C) → (A → B) → (A → C) |
69 | 29 | g ∘ f = λ x → g (f x) |
70 | 30 |
|
71 | 31 | infixr 9 _∘_ |
72 | 32 |
|
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 | | - postulate |
171 | | - funext : {A B : Set} {f g : A → B} |
172 | | - → ((x : A) → f x ≡ g x) |
173 | | - → f ≡ g |
174 | | - |
175 | | - curry-uncurry : {A B C : Set} → (f : A → B → C) |
176 | | - → curry (uncurry f) ≡ f |
177 | | - curry-uncurry f = funext λ x → funext λ y → refl |
178 | | - |
179 | | - uncurry-curry : {A B C : Set} → (f : A × B → C) |
180 | | - → uncurry (curry f) ≡ f |
181 | | - uncurry-curry f = funext λ { (x , y) → refl } |
| 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 |
0 commit comments