{"id":22411607,"url":"https://github.com/5HT/per2","last_synced_at":"2025-07-31T22:30:46.158Z","repository":{"id":228651024,"uuid":"774594927","full_name":"5HT/per2","owner":"5HT","description":"🧊 Автоматизована система доведення теорем на основі W-індукції","archived":false,"fork":false,"pushed_at":"2025-05-31T04:09:58.000Z","size":807,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-30T01:03:33.289Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://per.groupoid.space/","language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/5HT.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-03-19T20:31:56.000Z","updated_at":"2025-06-09T15:08:29.000Z","dependencies_parsed_at":"2024-03-19T20:55:42.254Z","dependency_job_id":"4b9b0580-9a54-43e8-8c5d-2c4f8d59c4f1","html_url":"https://github.com/5HT/per2","commit_stats":{"total_commits":9,"total_committers":1,"mean_commits":9.0,"dds":0.0,"last_synced_commit":"6d2291ca98b745442a27eace6a59c69feaf85ac4"},"previous_names":["groupoid/per","groupoid/per.ex","groupoid/per2"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/5HT/per2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5HT%2Fper2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5HT%2Fper2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5HT%2Fper2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5HT%2Fper2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/5HT","download_url":"https://codeload.github.com/5HT/per2/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/5HT%2Fper2/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268127131,"owners_count":24200311,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-31T02:00:08.723Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-05T13:14:30.461Z","updated_at":"2025-07-31T22:30:46.122Z","avatar_url":"https://github.com/5HT.png","language":"OCaml","readme":"Per Martin-Löf: Intuitionistic Type Theory\n==========================================\n\n\u003cimg src=\"https://per.groupoid.space/img/per.jpg\" height=400\u003e\n\n## Abstract\n\n**Per** is MLTT-73 tactical theorem prover implemented in OCaml, constitutes a minimal core for a dependently-typed lambda calculus,\nconstrained to exclude pattern matching, let-bindings, implicit arguments, modules, namespaces, and function extensionality.\nIt encompasses universes, dependent products `Pi`, dependent pairs `Sigma`, and identity types `Id`.\nRefinements ensure totality for user-defined lambda terms via a positive occurrence check. \nIts mathematical properties, focusing on correctness, soundness, totality, canonicity, decidability and related\nattributes relevant to formal mathematics are being analyzed.\n\n## Introduction\n\nThe type checker operates over a term syntax comprising:\n\n* `Universe i`: Type universes with level `i ∈ ℕ`.\n* `Pi (x, A, B)`: Dependent function, where `A : Universe i` and `B : Universe j` under `x : A`.\n  `Lam (x, A, t)`: Lambda abstraction with totality enforced.\n  `App (f, a)`: Function application.\n* `Sigma (x, A, B)`: Dependent pair types.\n  `Pair (a, b)`, `Fst p`, `Snd p` construction and projections.\n* `Id (A, a, b)`: Identity type, with `Refl` a and `J` eliminator.\n\nThe typing judgment `Γ ⊢ t : T` is defined via `infer` and `check` functions,\nwith definitional equality `Γ ⊢ t = t'` implemented via `equal`.\n\n## Syntax\n\n```OCaml\ntype term =\n  | Var of name | Universe of level\n  | Pi of name * term * term | Lam of name * term * term | App of term * term\n  | Sigma of name * term * term | Pair of term * term | Fst of term | Snd of term\n  | Id of term * term * term | Refl of term | J of term * term * term * term * term * term  (* J A a b C d p *)\n```\n\n## Semantics\n\n### Syntactic Equality `equal`\n\nStructural equality of terms under an environment and context.\n\nThe function implements judgmental equality with substitution to handle bound variables,\navoiding explicit α-conversion by assuming fresh names (a simplification\nover full de Bruijn indices [3]). The recursive descent ensures congruence,\nbut lacks normalization, making it weaker than CIC’s definitional equality,\nwhich includes β-reduction.\n\n* **Terminal Cases**: Variables `Var x` are equal if names match; universes `Universe i` if levels are identical.\n* **Resursive Cases**: `App (f, arg)` requires equality of function and argument.\n`Pi (x, a, b)` compares domains and codomains, adjusting for variable renaming via substitution.\n`Inductive d` checks name, level, and parameters.\n`Constr` and `Elim` compare indices, definitions, and arguments/cases.\n* Default: Returns false for mismatched constructors.\n\n**Theorem**. Equality is reflexive, symmetric, and transitive modulo\nα-equivalence (cf. [1], Section 2). For `Pi (x, a, b)` and `Pi (y, a', b')`,\nequality holds if `a = a'` and `b[x := Var x] = b'[y := Var x]`,\nensuring capture-avoiding substitution preserves meaning.\n\n### Context Variables Lookup `lookup_var`\n\nRetrieve a variable’s type from the context.\nContext are the objects in the Substitutions categories.\n\n* Searches `ctx` for `(x, ty)` using `List.assoc`.\n* Returns `Some ty` if found, `None` otherwise.\n\n**Theorem**: Context lookup is well-defined under uniqueness\nof names (cf. [1], Section 3). If `ctx = Γ, x : A, Δ`,\nthen `lookup_var ctx x = Some A`.\n\n### Substitution Calculus `subst`\n\nSubstitute term `s` for variable `x` in term `t`.\nSubstitutions are morphisms in Substitution categorties.\n\nThe capture-avoiding check `if x = y` prevents variable capture\nbut assumes distinct bound names, a simplification over full\nrenaming or de Bruijn indices. For Elim, substituting in the\nmotive and cases ensures recursive definitions remain sound,\naligning with CIC’s eliminator semantics.\n\n* `Var`: Replaces `x` with `s`, leaves others unchanged.\n* `Pi/Lam`: Skips substitution if bound variable shadows `x`, else recurses on domain and body.\n* `App/Constr/Elim`: Recurses on subterms.\n\n**Theorem**. Substitution preserves typing (cf. [13], Lemma 2.1).\nIf `Γ ⊢ t : T` and `Γ ⊢ s : A`, then `Γ ⊢ t[x := s] : T[x := s]`\nunder suitable conditions on x.\n\n### Infer Equality Induction `infer_J`\n\nEnsuring `J (ty, a, b, c, d, p)` has type `c a b p by` validating the motive,\nbase case, and path against CIC’s equality elimination rule.\n\nThe `infer_J` function implements the dependent elimination rule for identity\ntypes in the Calculus of Inductive Constructions (CIC), enabling proofs and\ncomputations over equality (e.g., `symmetry : Π a b : ty, Π p : Id (ty, a, b), Id(ty, b, a)`).\nIt type-checks the term `J (ty, a, b, c, d, p)` by ensuring \n`ty : Universe 0` is the underlying type, `a : ty` and `b : ty` are endpoints,\n`c : Π (x:ty), Π (y:ty), Π (p: Id(ty, x, y)), Type0` is a motive over all paths,\n`d : Π (x:ty), c x x (Refl x)` handles the reflexive case,\nand `p : Id(ty, a, b)` is the path being eliminated.\nThe function constructs fresh variables to define the motive\nand base case types, checks each component, and returns `c a b p` (normalized),\nreflecting the result of applying the motive to the specific path. \n\n**Theorem**. For an environment `env` and context `ctx`, given a type `A : Type_i`,\nterms `a : A`, `b : A`, a motive `C : Π (x:A), Π (y:A), Π(p:Id(A, x, y)),Type_j`,\na base case `d : Π(x:A), C x x (Refl x)`, and a path `p : Id(A, a, b)`, the\nterm `J (A, a, b, C, d, p)` is well-typed with type `C a b p`. (Reference:\nCIC [1], Section 4.5; Identity Type Elimination Rule).\n\n### Type Inference `infer`\n\nInfer the type of term `t` in context `ctx` and environment `env`.\n\nFor `Pi` and `Lam`, universe levels ensure consistency\n(e.g., `Type i : Type (i + 1)`), while `Elim` handles induction,\ncritical for dependent elimination. Note that lambda agrument should be typed\nfor easier type synthesis [13].\n\n### Check Universes `check_universe`\n\nEnsure `t` is a universe, returning its level.\nInfers type of `t`, expects `Universe i`.\n\nThis auxiliary enforces universe hierarchy, preventing\nparadoxes (e.g., Type : Type). It relies on infer,\nassuming its correctness, and throws errors for\nnon-universe types, aligning with ITT’s stratification.\n\n**Theorem**: Universe checking is decidable (cf. [13]).\nIf `ctx ⊢ t : Universe i`, then `check_universe env ctx t = i`.\n\n### Check `check`\n\nCheck that `t` has type `ty`.\n\n* `Lam`: Ensures the domain is a type, extends the context, and checks the body against the codomain.\n* Default: Infers t’s type, normalizes ty, and checks equality.\n\nThe function leverages bidirectional typing: specific cases (e.g., `Lam`)\ncheck directly, while the default case synthesizes via infer and compares\nwith a normalized ty, ensuring definitional equality (β-reduction).\nCompleteness hinges on normalize terminating (ITT’s strong normalization)\nand equal capturing judgmental equality.\n\n**Theorem**. Type checking is complete (cf. [1], Normalization).\nIf `ctx ⊢ t : T` in the type theory, then `check env ctx t T` succeeds,\nassuming normalization and sound inference.\n\n### One-step β-reductor `reduce`\n\nPerform one-step β-reduction or inductive elimination.\n\nThe function implements a one-step reduction strategy combining ITT’s β-reduction \nwith CIC’s ι-reduction for inductives. The `App (Lam, arg)` case directly applies\nsubstitution, while `Elim (Constr)` uses `apply_case` to handle induction,\nensuring recursive calls preserve typing via the motive p. The `Pi` case,\nthough unconventional, supports type-level computation, consistent with CIC’s flexibility. \n\n* `App (Lam, arg)`: Substitutes arg into the lambda body (β-reduction).\n* `App (Pi, arg)`: Substitutes arg into the codomain (type-level β-reduction).\n* `App (f, arg)`: Reduces f, then arg if f is unchanged.\n* Default: Returns unchanged.\n\n**Theorem**. Reduction preserves typing (cf. [8], Normalization Lemma, Subject Reduction).\nIf `ctx ⊢ t : T` and `t → t'` via β-reduction or inductive elimination, then `ctx ⊢ t' : T`.\n\n### Normalization `normalize`\n\nThis function fully reduces a term t to its normal form by iteratively\napplying one-step reductions via reduce until no further changes occur,\nensuring termination for well-typed terms.\n\nThis function implements strong normalization, a cornerstone of MLTT [9]\nand CIC [1], where all reduction sequences terminate. The fixpoint\niteration relies on reduce’s one-step reductions (β for lambdas, ι\nfor inductives), with equal acting as the termination oracle.\nFor `plus 2 2`, it steps to `succ succ succ succ zero`, terminating at a constructor form.\n\n**Theorem**. Normalization terminates (cf. [1]. Strong Normalization via CIC).\nEvery well-typed term in the system has a ormal form under β- and ι-reductions.\n\n## Conclusion\n\nPer’s elegance rests on firm theoretical ground. Here, we reflect on key meta-theorems for Classical MLTT with General Inductive Types, drawing from CIC’s lineage:\n\n* **Soundness and Completeness**: Per’s type checker is sound—every term it accepts has a type under MLTT’s rules [Paulin-Mohring, 1996].\n  This ensures that every term accepted by Per is typable in the underlying theory.\n  Relative to the bidirectional type checking algorithm, context is appropriately managed [Harper \u0026 Licata, 2007].\n  The interplay of inference and checking modes guarantees this property.\n* **Canonicity, Normalization, and Totality**: Canonicity guarantees that every closed term of type `Nat` normalizes\n  to `zero` or `succ n` [Martin-Löf, 1984]. Per’s normalize achieves strong normalization—every term reduces to a\n  unique normal form—thanks to CIC’s strict positivity [Coquand \u0026 Paulin-Mohring, 1990]. Totality follows: all\n  well-typed functions terminate, as seen in list_length reducing to `succ (succ zero)`.\n* **Consistency and Decidability**: Consistency ensures no proof of ⊥ exists, upheld by normalization and the\n  absence of paradoxes like Girard’s [Girard, 1972]. Type checking is decidable in Per, as our algorithm\n  terminates for well-formed inputs, leveraging CIC’s decidable equality [Asperti et al., 2009].\n* **Conservativity and Initiality**: Per is conservative over simpler systems like System F, adding dependent\n  types without altering propositional truths [Pfenning \u0026 Paulin-Mohring, 1989]. Inductive types like Nat satisfy\n  initiality—every algebra morphism from Nat to another structure is uniquely defined—ensuring categorical universality [Dybjer, 1997].\n\n### Soundness\n\n* Definition: Type preservation and logical consistency hold.\n* Formal Statement: 1) If `Γ ⊢ t : T` and `infer t = t'`, then `Γ ⊢ t' : T`;\n  2) No `t` exists such that `Γ ⊢ t : Id (Universe 0, Universe 0, Universe 1)`.\n* Proof: Preservation via terminating reduce; consistency via positivity and intensionality.\n* Status: Sound, inforced by rejecting non-total lambdas.\n\n### Completeness\n\n* Definition: The type checker captures all well-typed terms of MLTT within its bidirectional framework.\n* Formal Statement: If `Γ ⊢ 𝑡 : T`, then `infer Δ Γ 𝑡 = T` or `check Δ Γ 𝑡 T` holds under suitable `Δ`.\n* Status: Complete relative to the implemented algorithm.\n\n### Canonicity\n\n* Definition: Reduction reaches a normal form; equality is decidable.\n* Formal Statement: `equal Δ Γ t t'` terminates, reflecting normalize’s partial eta and beta reductions in `normnalize`.\n* Status: Satisfied within the scope of implemented reductions.\n\n### Totality\n\n* Definition: All well-typed constructs terminate under reduction.\n* Formal Statement: 1) For `Inductive d : Universe i`, each `Constr (j, d, args)` is total;\n  2) For `t : T` with `Ind` or `J`, `reduce t` terminates;\n  3) For `Lam (x, A, t) : Pi (x, A, B)`, `reduce (App (Lam (x, A, t), a))` terminates for all `a : A`;\n  4) `normalize Δ Γ t` terminates.\n\n### Consistency\n\nThe system is logically consistent, meaning no term `t` exists such that `Γ ⊢ t : ⊥`.\nThis is upheld by normalization and the absence of paradoxes such as Girard's [Girard, 1972].\n\n### Decidability\n\n* Definition: Type checking and equality are computable.\n* Formal Statement: `infer` and `check` terminate with a type or `TypeError`.\n* Status: Decidable, enhanced by termination checks on lambda expressions.\n\n## Artefact\n\n```\nhttps://per.groupoid.space/\n\n  🧊 MLTT Theorem Prover version 0.5 (c) 2025 Groupoїd Infinity\n\nFor help type `help`.\n\nStarting proof for: Π(n : Nat).Nat\nGoal 1:\nContext: []\n⊢ Π(n : Nat).Nat\n\n1 goals remaining\n\u003e\n```\n\n## MLTT\n\n[9]. Martin-Löf, P. Intuitionistic Type Theory. 1980.\u003cbr\u003e\n[10]. Thierry Coquand. An Algorithm for Type-Checking Dependent Types. 1996. \u003cbr\u003e\n\n## PTS\n\n[11]. N. G. de Bruijn. Lambda Calculus Notation with Nameless Dummies. 1972. \u003cbr\u003e\n[12]. J.-Y. Girard. Interprétation fonctionnelle et élimination des coupures. 1972. \u003cbr\u003e\n[13]. Thierry Coquand, Gerard Huet. \u003ca href=\"https://core.ac.uk/download/pdf/82038778.pdf\"\u003eThe Calculus of Constructions\u003c/a\u003e. 1988.\u003cbr\u003e\n\n## Author\n\nNamdak Tonpa\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5HT%2Fper2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F5HT%2Fper2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F5HT%2Fper2/lists"}