1 ;;;; Copyright (C) 2024 Vouivre Digital Corporation
3 ;;;; This file is part of Vouivre.
5 ;;;; Vouivre is free software: you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU General Public
7 ;;;; License as published by the Free Software Foundation, either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; Vouivre is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public
16 ;;;; License along with Vouivre. If not, see <https://www.gnu.org/licenses/>.
18 (define-module (vouivre type rules)
19 #:use-module (vouivre type system))
23 (define-rule formation-1
25 (Γ (: x A) ⊢ (type (B x))))
27 (define-rule formation-2
31 (define-rule formation-3
35 (define-rule formation-4
39 (define-rule formation-5
43 (define-rule formation-6
47 (define-rule type-reflexive
51 (define-rule type-symmetric
55 (define-rule type-transitive
60 (define-rule element-reflexive
64 (define-rule element-symmetric
68 (define-rule element-transitive
73 (define-rule variable-conversion
78 (define-rule substitution
79 (Γ (/ Δ a x) ⊢ (/ J a x))
83 (define-rule substitution-type-congruence
84 (Γ (/ Δ a x) ⊢ (≐ (/ B a x) (/ B a' x)))
86 (Γ (: x A) Δ ⊢ (type B)))
88 (define-rule substitution-element-congruence
89 (Γ (/ Δ a x) ⊢ (≐ (/ b a x) (/ b a' x) (/ B a x)))
91 (Γ (: x A) Δ ⊢ (: b B)))
93 (define-rule weakening
98 (define-rule generic-element
102 ;;;; Structural Derivations
104 (define-derivation change-of-variables ((Γ (: x' A) Δ ⊢ J)
107 (substitution #:start 1 a x' x x A A)
114 (define-derivation interchange ((Γ (: y B) (: x A) Δ ⊢ J)
117 (Γ (: x A) (: y B) Δ ⊢ J))
118 (substitution #:start 2 a y x y' A B)
119 (weakening #:start 1)
127 (substitution #:start 2 a y' x y A B)
132 (weakening #:start 1 #:end 2)
138 (define-derivation element-conversion ((Γ ⊢ (: a A'))
141 (substitution a a x a' A A)
143 (variable-conversion A A')
150 (define-derivation congruence ((Γ ⊢ (≐ a b A'))
153 (substitution-element-congruence a a a' b x x A A)
155 (variable-conversion A A')
162 ;;;; Dependent Function Type
165 (Γ ⊢ (type (Π (: x A) (B x))))
166 (Γ (: x A) ⊢ (type (B x))))
169 (Γ ⊢ (≐ (Π (: x A) (B x))
170 (Π (: x A') (B' x))))
172 (Γ (: x A) ⊢ (≐ (B x) (B' x))))
177 (Γ (: x A) ⊢ (: (b x) (B x))))
183 (Γ (: x A) ⊢ (≐ (b x)
187 (define-rule evaluation
188 (Γ (: x A) ⊢ (: (f x) (B x)))
189 (Γ ⊢ (: f (Π (: x A) (B x)))))
191 (define-rule evaluation-eq
192 (Γ (: x A) ⊢ (≐ (f x) (f' x) (B x)))
193 (Γ ⊢ (≐ f f' (Π (: x A) (B x)))))
196 (Γ (: x A) ⊢ (≐ ((λ y (b y)) x)
199 (Γ (: x A) ⊢ (: (b x) (B x))))
205 (Γ ⊢ (: f (Π (: x A) (B x)))))
207 ;;;; Ordinary Function Type
209 (define-derivation → ((Γ ⊢ (:= (→ A B) (type (Π (: x A) B))))
217 (define-derivation →-eq ((Γ ⊢ (≐ (→ A B) (→ A' B')))
220 (type-transitive B (Π (: x A) B))
226 (type-transitive B (Π (: x A') B'))
240 (define-derivation →-λ ((Γ ⊢ (: (λ x (b x)) (→ A B)))
241 (Γ (: x A) ⊢ (: (b x) B))
244 (element-conversion A (Π (: x A) B))
252 (define-derivation →-λ-eq ((Γ ⊢ (≐ (λ x (b x))
255 (Γ (: x A) ⊢ (≐ (b x) (b' x) B))
258 (congruence A (Π (: x A) B))
266 (define-derivation →-evaluation ((Γ (: x A) ⊢ (: (f x) B))
271 (element-conversion A (→ A B))
277 (define-derivation →-evaluation-eq ((Γ (: x A) ⊢ (≐ (f x) (f' x) B))
278 (Γ ⊢ (≐ f f' (→ A B)))
281 (evaluation-eq f f f' f')
282 (congruence A (→ A B))
288 (define-derivation →-β ((Γ (: x A) ⊢ (≐ ((λ x (b x)) x)
291 (Γ (: x A) ⊢ (: (b x) B)))
295 (define-derivation →-η ((Γ ⊢ (≐ (λ x (f x)) f (→ A B)))
299 (congruence A (Π (: x A) B))
301 (element-conversion A (→ A B))
313 (define-rule =-formation
314 (Γ (: x A) ⊢ (type (= a x)))
317 (define-rule =-introduction
318 (Γ ⊢ (: refl (= a a)))
321 (define-rule =-elimination
328 (Γ (: x A) (: p (= a x)) ⊢ (type (P x p))))
330 (define-rule =-computation
331 (Γ (: u (P a refl)) ⊢ (≐ (((ind-eq u) a) refl)
335 (Γ (: x A) (: p (= a x)) ⊢ (type (P x p))))
337 ;;;; Dependent Pair Type
339 (define-rule Σ-formation
340 (Γ ⊢ (type (Σ (: x A) (B x))))
341 (Γ (: x A) ⊢ (type (B x))))
344 (Γ ⊢ (≐ (Σ (: x A) (B x))
345 (Σ (: x A') (B' x))))
347 (Γ (: x A) ⊢ (≐ (B x) (B' x))))
349 (define-rule Σ-introduction
350 (Γ ⊢ (: pair (Π (: x A)
354 (Γ (: x A) ⊢ (type (B x))))
356 (define-rule Σ-elimination
357 (Γ ⊢ (: ind-Σ (→ (Π (: x A)
363 (Γ (: x A) ⊢ (type (B x)))
364 (Γ (: z (Σ (: x A) (B x))) ⊢ (type (P z))))
366 (define-rule Σ-computation
367 (Γ (: x A) (: y (B x)) (: g (Π (: x A)
370 ⊢ (≐ ((ind-Σ g) ((pair x) y))
373 (Γ (: z (Σ (: x A) (B x))) ⊢ (type (P z))))
377 (define-rule ℕ-formation
380 (define-rule ℕ-introduction-1
383 (define-rule ℕ-introduction-2
384 (⊢ (: succ (→ ℕ ℕ))))
386 (define-rule ℕ-elimination
387 (Γ ⊢ (: (ind-ℕ p0 ps)
390 (Γ (: n ℕ) ⊢ (type (P n)))
392 (Γ ⊢ (: ps (Π (: n ℕ)
396 (define-rule ℕ-computation-1
397 (Γ ⊢ (≐ ((ind-ℕ p0 ps) 0)
400 (Γ (: n ℕ) ⊢ (type (P n)))
402 (Γ ⊢ (: ps (Π (: n ℕ)
406 (define-rule ℕ-computation-2
407 (Γ (: n ℕ) ⊢ (≐ ((ind-ℕ p0 ps) (succ n))
408 (ps n ((ind-ℕ p0 ps) n))
410 (Γ (: n ℕ) ⊢ (type (P n)))
412 (Γ ⊢ (: ps (Π (: n ℕ)
418 (define-rule bool-formation
421 (define-rule bool-introduction-1
424 (define-rule bool-introduction-2
427 (define-rule bool-elimination
428 (Γ ⊢ (: (ind-Bool p0 p1)
431 (Γ (: x Bool) ⊢ (type (P x)))
432 (Γ ⊢ (: p0 (P false)))
433 (Γ ⊢ (: p1 (P true))))
435 (define-rule bool-computation-1
436 (Γ ⊢ (≐ ((ind-Bool p0 p1) false)
439 (Γ (: x Bool) ⊢ (type (P x)))
440 (Γ ⊢ (: p0 (P false)))
441 (Γ ⊢ (: p1 (P true))))
443 (define-rule bool-computation-2
444 (Γ ⊢ (≐ ((ind-Bool p0 p1) true)
447 (Γ (: x Bool) ⊢ (type (P x)))
448 (Γ ⊢ (: p0 (P false)))
449 (Γ ⊢ (: p1 (P true))))
453 (define-rule list-formation
454 (Γ ⊢ (type (List A)))
457 (define-rule list-introduction-1
458 (Γ ⊢ (: nil (List A)))
461 (define-rule list-introduction-2
462 (Γ ⊢ (: cons (→ A (→ (List A)
466 (define-rule list-elimination
467 (Γ ⊢ (: (ind-List p0 ps)
471 (Γ (: xs (List A)) ⊢ (type (P xs)))
473 (Γ ⊢ (: ps (Π (: x A)
476 (P ((cons x) xs))))))))
478 (define-rule list-computation-1
479 (Γ ⊢ (≐ ((ind-List p0 ps) nil)
483 (Γ (: xs (List A)) ⊢ (type (P xs)))
485 (Γ ⊢ (: ps (Π (: x A)
488 (P ((cons x) xs))))))))
490 (define-rule list-computation-2
491 (Γ (: x A) (: xs (List A)) ⊢ (≐ ((ind-List p0 ps) ((cons x) xs))
492 (((ps x) xs) ((ind-List p0 ps) xs))
495 (Γ (: xs (List A)) ⊢ (type (P xs)))
497 (Γ ⊢ (: ps (Π (: x A)
500 (P ((cons x) xs))))))))
504 (define-rule vector-formation
505 (Γ ⊢ (type (Vector A n)))
509 (define-rule vector-introduction-1
510 (Γ ⊢ (: #() (Vector A 0)))
513 (define-rule vector-introduction-2
514 (Γ ⊢ (: :: (Π (: n ℕ)
516 (Vector A (succ n)))))))
519 (define-rule vector-elimination
520 (Γ ⊢ (: (ind-Vector p0 ps)
522 (Π (: xs (Vector A n))
525 (Γ (: n ℕ) (: xs (Vector A n)) ⊢ (type (P xs)))
527 (Γ ⊢ (: ps (Π (: n ℕ)
529 (Π (: xs (Vector A n))
531 (P (((:: n) x) xs)))))))))
533 (define-rule vector-computation-1
534 (Γ ⊢ (≐ ((ind-Vector p0 ps) #())
538 (Γ (: n ℕ) (: xs (Vector A n)) ⊢ (type (P xs)))
540 (Γ ⊢ (: ps (Π (: n ℕ)
542 (Π (: xs (Vector A n))
544 (P (((:: n) x) xs)))))))))
546 (define-rule vector-computation-2
547 (Γ (: x A) (: n ℕ) (: xs (Vector A n)) ⊢ (≐ (((ind-Vector p0 ps) n) (((:: n) x) xs))
549 (((ind-Vector p0 ps) n) xs))
550 (P (((:: n) x) xs))))
552 (Γ (: n ℕ) (: xs (Vector A n)) ⊢ (type (P xs)))
554 (Γ ⊢ (: ps (Π (: n ℕ)
556 (Π (: xs (Vector A n))
558 (P (((:: n) x) xs)))))))))
560 ;;;; More Derivations
562 (define-derivation identity ((Γ ⊢ (: (λ x x) (→ A A)))
570 (define-derivation if ((Γ ⊢ (: (λ z (λ x (λ y ((ind-Bool y x) z))))
575 (Γ (: z Bool) ⊢ (type (P z))))
579 (interchange #:end 2)
581 (substitution A Bool a true x z)
582 (bool-introduction-2)
584 (interchange #:start 1)
586 (substitution A Bool a true x z)
587 (bool-introduction-2)
591 (substitution A Bool a true x z)
592 (bool-introduction-2)
594 (substitution A Bool a false x z)
595 (bool-introduction-1)
597 (evaluation #:start 2 f (ind-Bool y x))
598 (bool-elimination #:start 2 (P false) (P false) (P true) (P true))
600 (substitution A Bool a true x z)
601 (bool-introduction-2)
604 (substitution A Bool a false x z)
605 (bool-introduction-1)
609 (substitution A Bool a true x z)
610 (bool-introduction-2)
613 (substitution A Bool a false x z)
614 (bool-introduction-1)
616 (weakening #:start 1)
618 (substitution A Bool a true x z)
619 (bool-introduction-2)
621 (substitution A Bool a false x z)
622 (bool-introduction-1)
625 (substitution A Bool a true x z)
626 (bool-introduction-2)
631 (substitution A Bool a true x z)
632 (bool-introduction-2)
634 (substitution A Bool a false x z)
635 (bool-introduction-1)
637 (weakening #:start 1)
640 (substitution #:start 1 A Bool a true x z)
641 (bool-introduction-2 #:start 1)
646 (substitution A Bool a true x z)
647 (bool-introduction-2)
652 (substitution A Bool a false x z)
653 (bool-introduction-1)
657 (define-derivation not ((Γ ⊢ (: (ind-Bool true false)
659 (element-conversion A (Π (: x Bool) Bool))
660 (bool-elimination (P false) Bool (P true) Bool)
664 (bool-introduction-2)
665 (bool-introduction-1)
671 (define-derivation constant ((Γ (: y B) ⊢ (: (λ x y) (→ A B)))
675 (weakening #:start 1)
688 (define-derivation =-inverse ((Γ ⊢ (: (λ x (ind-eq refl))
696 (substitution #:start 1 a refl x u A (= x x))
697 (=-introduction #:start 1 A A)
700 (→-evaluation #:start 1 f ind-eq)
701 (element-conversion #:start 1 A (→ (= x x) (Π (: y A) (Π (: p (= x y)) (= y x)))))
702 (=-elimination #:start 1)
705 (weakening #:start 2)
706 (=-formation #:start 1)
712 (=-formation #:start 1)
716 (type-reflexive #:start 1)
724 (type-symmetric #:start 2)
725 (definition #:start 2)
726 (=-formation #:start 1)
732 (=-formation #:start 1)
739 (=-formation #:start 1)
745 (=-formation #:start 1)
749 (define-derivation add (((: m ℕ) ⊢ (: (ind-ℕ m ((λ n succ) m)) (→ ℕ ℕ))))
750 (element-conversion #:start 1 A (Π (: x ℕ) ℕ))
751 (ℕ-elimination #:start 1 (P 0) ℕ (P n) ℕ (P (succ n)) ℕ)
752 (weakening #:start 1)
761 (element-conversion #:start 1 A (→ ℕ ℕ))
762 (→-evaluation f (λ n succ))
775 (definition #:start 1)
789 (define-derivation add-2-length ((Γ ⊢ (:= add-2-length
797 (weakening #:start 1)
806 (define-derivation length ((Γ ⊢ (: (λ xs ((ind-List 0 add-2-length) xs))
810 (evaluation f (ind-List 0 add-2-length))
811 (list-elimination (P nil) ℕ x x (P ((cons x) xs)) ℕ)