{"id":19681954,"url":"https://github.com/higherorderco/hvm-core-serialization","last_synced_at":"2025-04-29T05:30:25.517Z","repository":{"id":208244369,"uuid":"721140476","full_name":"HigherOrderCO/hvm-core-serialization","owner":"HigherOrderCO","description":null,"archived":false,"fork":false,"pushed_at":"2024-06-05T13:23:10.000Z","size":39,"stargazers_count":3,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-05T13:11:17.741Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","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/HigherOrderCO.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}},"created_at":"2023-11-20T12:46:05.000Z","updated_at":"2025-04-04T03:09:38.000Z","dependencies_parsed_at":"2024-01-13T01:07:00.607Z","dependency_job_id":"7e4b674d-4614-450e-9d39-14ec3e0df634","html_url":"https://github.com/HigherOrderCO/hvm-core-serialization","commit_stats":null,"previous_names":["higherorderco/hvm-core-serialization"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HigherOrderCO%2Fhvm-core-serialization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HigherOrderCO%2Fhvm-core-serialization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HigherOrderCO%2Fhvm-core-serialization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HigherOrderCO%2Fhvm-core-serialization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HigherOrderCO","download_url":"https://codeload.github.com/HigherOrderCO/hvm-core-serialization/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251444026,"owners_count":21590401,"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":[],"created_at":"2024-11-11T18:09:16.204Z","updated_at":"2025-04-29T05:30:23.187Z","avatar_url":"https://github.com/HigherOrderCO.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Encoding\n\nThis crate provides a set of functions to encode and decode an interaction net and its related structures.\n\n## Net\n\nA net consists of a root tree, a list of trees pointing to each other(redexes), and a wiring.\n\nSo we basically encode:\n- The root tree using the `Tree` encoding\n- How many redexes there are, using `VarLenNumber` encoding\n- The redexes(which are `Vec\u003c(Tree, Tree)\u003e`) using the `Tree` encoding, and reading 2 times the number of redexes\n- The wiring using the `Wiring` encoding\n\n## Scalars\n\n- `HVMRef`, uses fixed 60-bits.\n- `VarLenNumber`: uses [elias-gamma encoding](https://en.wikipedia.org/wiki/Elias_gamma_coding).\n- `Tag` uses 3-bits for the variant and:\n  - for `NUM`, has a `VarLenNumber`\n  - for `REF`, has a `HVMRef`\n  - for `TUP`/`DUP`, has a `VarLenNumber` for the label\n  - for `OP2` and `OP1`, has a fixed 1-bit to differentiate between `OP1` and `OP2`, and 4-bits for the operator.\n  - for the rest, has no data\n\n## Tree\n\n\u003e Note: The tree encoding by itself does NOT handle the encoding of variables, they are handled by the `Net` encoding.\n\nWe encode the tree by following a pre-order traversal, and writing the tag of each node\n\nSo for example, the tree `((@foo *) [* #123])`:\n- By doing a pre-order traversal, we get the structure:\n  1. `((@foo *) [* #123])`, has children, visit left, write `CON` (3-bits)\n  2. `(@foo *)`, has children, visit left, write `CON` (3-bits)\n  3. `@foo`, has no children, write `REF(@foo)`, backtrack to sibling (3-bits + 60-bits)\n  4. `*`, has no children, write `ERA`, backtrack to sibling (3-bits)\n  5. `[* #123]`, has children, visit left, write `DUP(0)` (3-bits + 1-bit)\n  6. `*`, has no children, write `ERA`, backtrack to sibling (3-bits)\n  7. `#123`, has no children, write `NUM(123)` (3-bits + 15-bits)\n  8. Done, with a total of 65 bits\n\n## Wiring\n\nFor encoding the whole net, we need to encode the wiring between all of the `VAR` nodes(free ports).\n\n\u003e Remember that any `VAR` node can connect with any other `VAR` node in the net.\n\nSuppose we have 6 ports, and we want to encode the following wiring:\n\n```text\n1 2 3 4 5 6\n-----------\na b b c a c\n```\n\n- the 1st port connects with the 5th\n- the 2nd port connects with the 3rd\n- the 4th port connects with the 6th\n\nWe can encode this in the following way:\n\n```text\nstarting with an empty wiring:\n---wiring--   | written bits\n_ _ _ _ _ _   | []\n\nwe begin with the first empty port, which is \"a\":\na _ _ _ _ _   | []\n\nwe know it is connected with the 5th port which looking at the\navailable empty ports is the 4th port out of 5 possible available ports\nso we know that we only have to encode 5 numbers, which can fit in 3 bits,\nso we write 4 in 3 bits:\na _ _ _ a _   | [0b100]\n\nwe continue with the first empty port, which is \"b\":\na b _ _ a _   | [0b100]\n\nagain, we know that it connects to the \"global\" 3rd port,\nbut looking at the available empty ports, we see that it is the 1st available port\nout of 3 possible available ports, so we write 0 in 2 bits:\na b b _ a _   | [0b100, 0b00]\n\nwe are now in the last connection, because it is the last one,\nwe know that it can only connect to the remaining one, so we don't\nneed to write any more bits to recover the original wiring:\na b b c a c   | [0b100, 0b00]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhigherorderco%2Fhvm-core-serialization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhigherorderco%2Fhvm-core-serialization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhigherorderco%2Fhvm-core-serialization/lists"}