{"id":28316727,"url":"https://github.com/groupoid/henk","last_synced_at":"2025-06-24T06:30:44.494Z","repository":{"id":57686675,"uuid":"11726487","full_name":"groupoid/henk","owner":"groupoid","description":"🧊 Чиста система з всесвітами","archived":false,"fork":false,"pushed_at":"2025-06-02T02:09:48.000Z","size":11223,"stargazers_count":147,"open_issues_count":0,"forks_count":16,"subscribers_count":20,"default_branch":"main","last_synced_at":"2025-06-12T04:29:18.402Z","etag":null,"topics":["erlang","lambda-cube","pts","pure","termination"],"latest_commit_sha":null,"homepage":"https://henk.groupoid.space","language":"Erlang","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/groupoid.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":"2013-07-28T22:02:26.000Z","updated_at":"2025-06-02T02:09:51.000Z","dependencies_parsed_at":"2023-12-16T07:03:16.633Z","dependency_job_id":"49b4f8a9-c9df-44b9-8c46-96f5523be519","html_url":"https://github.com/groupoid/henk","commit_stats":{"total_commits":48,"total_committers":1,"mean_commits":48.0,"dds":0.0,"last_synced_commit":"9ff10391f35b2659bc3ded845a3a03359d0a1768"},"previous_names":["groupoid/henk.ex"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/groupoid/henk","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/groupoid%2Fhenk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/groupoid%2Fhenk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/groupoid%2Fhenk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/groupoid%2Fhenk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/groupoid","download_url":"https://codeload.github.com/groupoid/henk/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/groupoid%2Fhenk/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261045864,"owners_count":23102126,"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","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":["erlang","lambda-cube","pts","pure","termination"],"created_at":"2025-05-25T03:05:55.309Z","updated_at":"2025-06-24T06:30:44.466Z","avatar_url":"https://github.com/groupoid.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"Henk Barendregt: Pure Type System\n---------------------------------\n\nThis photo provided exclusively for **Henk** type checker.\n\n\u003cimg src=\"https://henk.groupoid.space/img/Henk%20Barendregt.jpg\" height=400\u003e\n\n# Abstract\n\n**Henk** languages described first by Erik Meijer and Simon Peyton Jones in 1997.\nLater on in 2015 a new implementation of the ideas in Haskell appeared, called Morte.\nIt used the Böhm-Berarducci encoding of recursive data types into non-recursive terms.\nMorte has constants, variables, and kinds, is based only on **П**, **λ** and **apply** constructions,\none axiom and four deduction rules. The Henk language resembles Henk design and Morte implementation.\nThis language is indended to be small, concise, easily provable, clean and\nbe able to produce verifiable programs that can be distributed over the networks and compiled at target with safe linkage.\n\n```\n$ mix deps.get\n$ iex -S mix\n```\n\n## Syntax\n\nThe Henk Syntax is the following:\n\n```\n   \u003c\u003e ::= #option\n\n    I ::= #identifier\n\n    U ::= * \u003c #number \u003e\n\n    O ::= U | I | ( O ) | O O\n            | λ ( I : O ) → O\n            | ∀ ( I : O ) → O\n```\n\nHenk is an implementation of PTS with an Infinite Number of Universes, the pure lambda calculus with dependent types.\nIt can be compiled (code extraction) to bytecode of Erlang virtual machines BEAM and LING.\n\n## Semantics\n\n### Hierarchy\n\nThe hierarchy function computes the universe level of a type constructor (e.g., a function or product type)\nby delegating to dep with a configurable mode. In the code, `hierarchy Arg Out -\u003e dep Arg Out (env om hierarchy impredicative)`\nuses an environment variable to choose between impredicative (default) and predicative settings,\nallowing flexibility in the type system’s universe structure. Formally, Barendregt describes the hierarchy in PTS via the rules R,\nwhere the resulting sort s3 of a product `Πx:A.B` depends on the sorts of A and B: in impredicative CoC,\nwhile in predicative systems, it may lift to a higher universe. He writes, \"The choice of hierarchy rule\ndetermines the expressiveness and consistency of the system\" (Barendregt, Lambda Calculus: Its Syntax and\nSemantics, 1992, p. 567), which the code reflects by parameterizing this choice. He writes, \"The choice of\nhierarchy rule determines the expressiveness and consistency of the system\" (Barendregt, Lambda Calculus:\nIts Syntax and Semantics, 1992, p. 567), which the code reflects by parameterizing this choice.\n\n### Star\n\nThe star function extracts or validates the numeric level of a universe sort, central to the infinite hierarchy of types.\nIn the code, `star (star,N) -\u003e N` returns the integer `N` from a universe term `star N` (e.g., \\*0, \\*1,\nwhile `star S -\u003e error (\"universe\",\\*,S)` errors out for invalid inputs. Formally, Barendregt defines sorts in PTS as a set \nS, such as `{∗,□}` in CoC, or an infinite sequence `\\*0 : \\*1 : \\*2 : ...` in systems with universes, where each \n`**n : **(n+1)`. He states, \"Sorts form the backbone of the type hierarchy, with each level governing the types\nbelow it\" (Barendregt, Introduction to Generalized Type Systems, 1991, p. 6), and the code’s star function directly\nsupports this by managing universe indices.\n\n### Equality\n\nThe `eq` function tests convertibility between two terms, a key aspect of type checking in PTS.\nIt checks dependent products for equality by comparing domains and codomains with substitution,\nwhile `eq (app (F1,A)) (app (F2,A2)) -\u003e eq(F1, F2), eq(A1, A2)` handles applications.\nFormally, Barendregt defines equality as beta-convertibility: \n`M =_β N` if `M` and `N` reduce to the same normal form. He writes, \"Equality in typed\nlambda calculi relies on convertibility, ensuring types align under reduction\" (Barendregt,\nIntroduction to Generalized Type Systems, 1991, p. 17), which eq implements via structural\nand substitution-based comparison.\n\n### Shift\n\nThe shift function adjusts the indices of free variables in a term to account for changes in the binding context,\nsuch as when a term is moved under a new binder (e.g., a lambda or universal quantifier). In the code,\n`shift (var (N,I)) N P when I \u003e= P -\u003e var (N,I+1)` increments the index `I` of a variable named `N` by `1`\nif I \u003e= P (the cutoff point), while `shift (∀,(N,0)) (I,O) N P` recursively shifts indices\nin the input `I` and output `O` of a dependent function type, adjusting `O` under an additional\nbinder `P+1`. Formally, per Barendregt, shifting (often denoted as an \"up\" operation) is defined as:\nfor a term t, the shifted term `t↑_c^d` increases all free variable indices `i≥c` by `d`, where `c`\nis the cutoff and `d` is the shift amount (typically 1). Barendregt states, \"The operation `t↑_c^d`\nis used to avoid capture of variables when substituting under binders\" (Barendregt, Lambda Calculus: Its Syntax and Semantics, 1992, p. 49),\nensuring that free variables retain their intended references during term manipulation.\n\n### Substitution\n\nThe subst function performs substitution, replacing a variable in a term with another term,\nwhile respecting the binding structure to avoid variable capture. In the code,\n`subst (var (N,L)) N V L -\u003e V` replaces a variable with index L matching the\ncurrent level with the value `V`, while `subst ((λ,(N,0)),(I,O)) N V L  -\u003e ((λ,(N,0)),(subst I N V L, subst O N shift(V,N,0) L+1))`\nsubstitutes in a lambda term, shifting `V` to adjust for the new binder in `O`.\nFormally, Barendregt defines substitution as: for a term `t`, variable `x`, and term `u`,\nthe substitution `t[x:=u]` replaces all free occurrences of `x` in `t` with `u`,\nwith indices adjusted to prevent capture by binders. He writes, \"Substitution `t[x:=u]`\nmust ensure that free variables in `u` are not accidentally bound by binders in \n`t`, necessitating a careful adjustment of indices\" (Barendregt, Introduction to\nGeneralized Type Systems, 1991, p. 15), which aligns with the code’s use of shift\nwithin subst to maintain correctness in dependent type systems like the Calculus of Constructions.\n\n### Normalization\n\nThe norm function computes the normal form of a term, performing beta reduction and structural\nnormalization. In the code, `norm (app (F,A)) -\u003e case norm(F) of ((λ,(N,0)),(O,O)) -\u003e norm(subst(O,N,A))`\nreduces applications by substituting into lambdas, while `norm (→,(I,O)) -\u003e ((∀,(_,0)),(norm(I),norm(O)))`\nrewrites function types as dependent products. Formally, Barendregt defines normalization as reducing a\nterm to its normal form via beta reduction, where `(λx.M)N→M[x:=N]`, ensuring strong normalization\nin systems like CoC. He states, \"Normalization guarantees that every typable term has a unique\nnormal form, critical for consistency\" (Barendregt, Lambda Calculus: Its Syntax and Semantics, 1992, p. 62),\nwhich norm achieves through recursive reduction.\n\n### Type Inference\n\nThe type function infers or checks the type of a term in a given context, enforcing the PTS typing rules.\nIn the code, `type (star N) _ -\u003e star (N+1)` assigns each universe its successor, `type ((∀,(N,0)),(I,O)) D -\u003e star hierarchy(star(type(I,D)), star(type(O,[{N,norm(I)}|D])))`\ntypes dependent products, and `type (app (F,A)) D` handles applications by matching function\ntypes and substituting. Formally, Barendregt defines typing in PTS via judgments `Γ ⊢ M : A`,\ngoverned by rules like `Γ⊢Πx:A.B:s3 if Γ⊢A:s1 /\\ Γ,x:A⊢B:s2` for products. He notes, \"The typing\nrelation ensures that every term inhabits a sort or type, preserving the hierarchy\" (Barendregt,\nLambda Calculus: Its Syntax and Semantics, 1992, p. 568), which type upholds by recursively computing types within the context.\n\n## Artefact\n\nIn repository `henk` you may find the following parts of core:\n\n* [Parser](https://github.com/groupoid/henk/blob/main/src/elixir/syntax/morte/om_parse.erl)\n* [Type Checker](https://github.com/groupoid/henk/blob/main/src/elixir/typechecker/om_type.erl)\n* [Eraser](https://github.com/groupoid/henk/blob/main/src/elixir/extractor/om_erase.erl)\n* [Code Extractor](https://github.com/groupoid/henk/blob/main/src/elixir/extractor/om_extract.erl)\n\nHenk ships with different \"modes\" (spaces of types with own encodings), or \"preludes\", which\nyou may find in `lib` directory. They are selectable with `om:mode(\"normal\")`.\n\n#### [Henk Library](https://github.com/groupoid/henk/tree/main/src/elixir/priv/Morte)\n\n```sh\nhenk.groupoid.space/src/elixir/priv/Morte\n  ├── Bool\n  ├── Cmd\n  ├── Eq\n  ├── Equ\n  ├── Frege\n  ├── IO\n  ├── IOI\n  ├── Lazy\n  ├── Leibnitz\n  ├── List\n  ├── Maybe\n  ├── Mon\n  ├── Monad\n  ├── Monoid\n  ├── Morte\n  ├── Nat\n  ├── Path\n  ├── Prod\n  ├── Prop\n  ├── Sigma\n  ├── Simple\n  ├── String\n  ├── Unit\n  └── Vector\n```\n\nThis is a minimal practical prelude similar to Morte's base library of Gabriella Gonzalez.\nIt contains common inductive constructions encoded using plain Church (or Böhm-Berarducci if you wish) encoding,\nand two basic (co)monadic effect systems: IO (free monad, for finite I/O) and IOI (free comonad,\nfor infinitary I/O, long-term processes). The generated code is being sewed with\nErlang effects that are passed as parameters to pure functions.\n\nNote: all these folders (modules) are encoded in plain CoC in Henk repository to demonstrate\nyou the basic principles how things work. Later all these should be written in Per\nlanguages and translated to Henk automatically (if possible). You may think of Henk as the low-level\ntyped assembler of type theory.\n\nPTS\n---\n\n* \u003ca href=\"https://pure.tue.nl/ws/portalfiles/portal/2039924/256169.pdf\"\u003eAUTOMATH, a language for mathematics.\u003c/a\u003e [Nicolaas Govert de Bruijn]\n* \u003ca href=\"https://home.ttic.edu/~dreyer/course/papers/barendregt.pdf\"\u003eLambda Calculi with Types\u003c/a\u003e [Henk Barendregt]\n* \u003ca href=\"https://core.ac.uk/download/pdf/82038778.pdf\"\u003eThe Calculus of Constructions\u003c/a\u003e [Thierry Coquand, Gerard Huet]\n* \u003ca href=\"https://www.cambridge.org/core/services/aop-cambridge-core/content/view/869991BA6A99180BF96A616894C6D710/S0956796800020025a.pdf/introduction-to-generalized-type-systems.pdf\"\u003eIntroduction to Generalized Type Systems\u003c/a\u003e [Henk Barendregt]\n* \u003ca href=\"https://www.cse.chalmers.se/~coquand/v1.pdf\"\u003eSome remarks about Dependent Type Theory\u003c/a\u003e [Thierry Coquand]\n* \u003ca href=\"https://www.microsoft.com/en-us/research/wp-content/uploads/1997/01/henk.pdf\"\u003eHenk: a typed intermediate language\u003c/a\u003e [Erik Meijer, Simon Peyton Jones]\n* \u003ca href=\"https://www.haskellforall.com/2014/09/morte-intermediate-language-for-super.html\"\u003eMorte: an intermediate language for super-optimizing functional programs \u003c/a\u003e [Gabriella Gonzalez]\n* \u003ca href=\"https://henk.groupoid.space/doc/henk.pdf\"\u003eHenk: Pure Type System for Erlang\u003c/a\u003e [Maksym Sokhatskyi]\n* \u003ca href=\"https://henk.groupoid.space/doc/pts_ua.pdf\"\u003eСистема доведення теорем з однiєю аксiомою\u003c/a\u003e [Maksym Sokhatskyi]\n\nCredits\n-------\n\n* \u003ca itemprop=\"sameAs\" content=\"https://orcid.org/0000-0001-7127-8796\" href=\"https://orcid.org/0000-0001-7127-8796\" target=\"orcid.widget\" rel=\"me noopener noreferrer\" style=\"vertical-align:top;white-space: nowrap;\"\u003eMaksym Sokhatskyi \u003cimg src=\"https://orcid.org/sites/default/files/images/orcid_16x16.png\"\u003e 🇺🇦\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgroupoid%2Fhenk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgroupoid%2Fhenk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgroupoid%2Fhenk/lists"}