{"id":19825882,"url":"https://github.com/octachron/rational_in_types","last_synced_at":"2026-03-27T04:56:15.362Z","repository":{"id":77493461,"uuid":"60729981","full_name":"Octachron/rational_in_types","owner":"Octachron","description":"Type-level rational puzzles in OCaml","archived":false,"fork":false,"pushed_at":"2016-06-08T21:18:11.000Z","size":23,"stargazers_count":24,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-01T14:41:54.542Z","etag":null,"topics":["ocaml","phantom-types","polymorphic-variants"],"latest_commit_sha":null,"homepage":null,"language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Octachron.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2016-06-08T20:51:58.000Z","updated_at":"2024-11-25T17:29:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"9af2c0df-4857-4b22-8361-20d05fa50e69","html_url":"https://github.com/Octachron/rational_in_types","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Octachron/rational_in_types","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Octachron%2Frational_in_types","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Octachron%2Frational_in_types/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Octachron%2Frational_in_types/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Octachron%2Frational_in_types/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Octachron","download_url":"https://codeload.github.com/Octachron/rational_in_types/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Octachron%2Frational_in_types/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263061404,"owners_count":23407606,"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":["ocaml","phantom-types","polymorphic-variants"],"created_at":"2024-11-12T11:08:57.898Z","updated_at":"2026-03-27T04:56:15.316Z","avatar_url":"https://github.com/Octachron.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"“ And with strange aeons, even types may multiply ”\n\nThis library is essentially a brain teaser, where types are twisted\nand tortured for the sake of type-level arithmetic. Neither usability\nnor sanity should be expected here.\n\n\nUsing and abusing polymorphic variants, this library implements all ring\noperations over rational with fixed precision numerator and denominator at\nthe type level. Only 3 bits fixed-precision integers are implemented, it is\nnot clear how much further the OCaml compiler can be tortured by adding\nmore bits.\n\n\nThe integer representation used is quite simple:\nThe bit `0` and `1` are represented by\n```OCaml\ntype 'a z = [ `_0 of 'a ]\ntype 'a o = [ `_1 of 'a ]\n```\nAn integer `n` with three bits `n_0,n_1,n_2` is mapped to the object type\n```\n\u003cb0: _ n_0; b_1: _ n_1; b_2: _ n_2; overflow: _ z\u003e\n```\nIt is then possible to use polymorphic variant to implement any boolean\nfunctions. A potential useful intermediary for humans is to implements\nlogical gates.\n\nFor instance, flipping a bit `'a` can be accomplished by the function\n\n```OCaml\ntype ('a,'flip) flip = Flip\n  constraint\n    'a = [\u003c `_0 of 'flip | `_1 of 'flip]\n  constraint\n    'table =\n    [\u003c\n      | `_0 of [ `_1 of 'f]\n      | `_1 of [ `_0 of 'f]\n    ]\n  constraint\n    'a = 'table\n```\nNote that the type of `'a` is consumed by the function flip and the result is\nstored inside the type `'flip`. This implies that integer types can be used only\nonce in any type level computation. Fortunately, it is also possible to implements\na cloning function as\n\n```OCaml\ntype ('a,'clone1,'clone2) cloner = Cloner\n  constraint\n    'a = [\u003c `_0 of ( ('clone1 * 'clone2) as 't)  | `_1 of 't]\n  constraint\n    'table = [\u003c `_0 of 'b z * ' c z\n             | `_1 of 'b o * 'c o\n             ]\n  constraint\n    'a = 'table\n```\n\nAn adder gate goes one step further with three input and two outputs\n```OCaml\ntype ('a,'b,'c, 'r, 'c_out) adder = Adder\n  (** Constraint on the inputs *)\n  constraint\n    'a = [\u003c `_0 of 'b | `_1 of 'b ]\n  constraint\n    'b = [\u003c `_0 of 'c | `_1 of 'c ]\n  constraint\n    'c = [\u003c `_0 of ( 'r*'c_out as 't) | `_1 of 't ]\n  (** Logic table of the adder gate *)\n  constraint\n    'table =\n    [\u003c `_0 of\n         [\u003c `_0 of\n              [\u003c `_0 of ('r2 z as 'r2_0) * ( 'c2 z as 'c2_0)\n              | `_1 of ( 'r2 o as 'r2_1) *  'c2_0 ]\n         | `_1 of\n              [\u003c `_0 of 'r2_1 *  'c2_0\n              | `_1 of 'r2_0 *  ('c2 o as 'c2_1) ]\n         ]\n    | `_1 of\n         [\u003c `_0 of\n              [\u003c `_0 of 'r2_1 * 'c2_0\n              | `_1 of 'r2_0 * 'c2_1 ]\n         | `_1 of\n              [\u003c `_0 of 'r2_0 * 'c2_1\n              | `_1 of 'r2_1 * 'c2_1 ]\n         ]\n    ]\n   constraint\n     'table = 'a\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foctachron%2Frational_in_types","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foctachron%2Frational_in_types","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foctachron%2Frational_in_types/lists"}