{"id":15707300,"url":"https://github.com/siraben/hasktran","last_synced_at":"2025-05-12T19:44:49.884Z","repository":{"id":111747700,"uuid":"234847490","full_name":"siraben/hasktran","owner":"siraben","description":"A compiler for FRACTRAN written in Haskell.","archived":false,"fork":false,"pushed_at":"2022-04-04T07:31:25.000Z","size":17,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-01T02:49:47.431Z","etag":null,"topics":["assembler","compiler","fractran","haskell","monad-transformers","tagless-final"],"latest_commit_sha":null,"homepage":null,"language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/siraben.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}},"created_at":"2020-01-19T05:48:24.000Z","updated_at":"2024-11-03T10:17:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"cc2b53e8-2529-4b6b-a81b-65a1a50b3450","html_url":"https://github.com/siraben/hasktran","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siraben%2Fhasktran","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siraben%2Fhasktran/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siraben%2Fhasktran/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siraben%2Fhasktran/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/siraben","download_url":"https://codeload.github.com/siraben/hasktran/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253809806,"owners_count":21967804,"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":["assembler","compiler","fractran","haskell","monad-transformers","tagless-final"],"created_at":"2024-10-03T20:39:35.698Z","updated_at":"2025-05-12T19:44:49.860Z","avatar_url":"https://github.com/siraben.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hasktran - A compiler for FRACTRAN written in Haskell.\n\nSee my\n[writeup](https://siraben.dev/2020/02/26/translating_cl.html) of\nthis compiler.\n\nThis was inspired by [Malisper's Common Lisp assembler for\nFRACTRAN](https://malisper.me/building-fizzbuzz-fractran-bottom/), but\nof course, in a purely functional manner.\n\nFRACTRAN programs have the type `[repr [Rational]]`, where `repr` is\nan instance of the `FracComp` typeclass, from which the special forms\n(`subi`, `while`, `goto` etc.) can be derived, making the language\nTuring complete.\n\nHere is an example FRACTRAN program written in Haskell, rather, a\nfunction that takes an `Integer` and _produces_ a FRACTRAN program,\nnot unlike Lisp macros.\n\n## Features\n- Embedding of DSL into Haskell using tagless final style\n- Support for high-level control flow constructs\n- Peephole optimization of compiled programs\n```haskell\nsumTo :: FracComp repr =\u003e Integer -\u003e [repr [Rational]]\nsumTo n = [addi \"c\" 0, addi \"n\" n, while (jge \"n\" 0) [adds \"c\" \"n\", subi \"n\" 1]]\n```\n\nThe following show the invocation of the assembler and naïve\ninterpreter and peephole optimizer.\n```haskell\nλ\u003e runAssembler (sumTo 10)                      -- Program length: 31\nRight [847425747 % 2,13 % 3,19 % 13,11 % 3,11 % 29,31 % 11,41 % 31,23\n% 11,23 % 47,2279 % 23,59 % 301,59 % 41,67 % 413,329 % 67,61 % 59,73 %\n61,83 % 73,71 % 61,71 % 97,445 % 71,707 % 89,103 % 5353,103 % 83,109 %\n5459,5141 % 109,107 % 103,113 % 749,113 % 19,131 % 113,29 % 131,127 %\n113]\nλ\u003e peepholeOptimize \u003c$\u003e runAssembler (sumTo 10) -- Program length: 25\nRight [847425747 % 2,19 % 3,11 % 3,11 % 29,41 % 11,23 % 11,2279 %\n47,59 % 301,59 % 41,67 % 413,329 % 67,61 % 59,83 % 61,71 % 61,445 %\n97,707 % 89,103 % 5353,103 % 83,109 % 5459,5141 % 109,107 % 103,113 %\n749,113 % 19,29 % 113,127 % 113]\nλ\u003e runAsm (sumTo 10)\n[(Prime 97,55),(Prime 107,1)]\n```\n\nThus showing the sum of the numbers from 1 to 10 inclusive is 55.\n\nA pretty printer for FRACTRAN programs was also added, and thanks to\nthe tagless final style, we are able to print high level constructs\nlike while loops by writing the corresponding printer.\n```haskell\nλ\u003e :t pretty\npretty :: Traversable t =\u003e t (S a) -\u003e Doc\nλ\u003e pretty (sumTo 10)\nc += 0\nn += 10\nwhile n \u003e= 0 {\n  c += n\n  n -= 1\n}\n```\n## Future plans\n- [x] Pretty printer\n- [x] Peephole optimization\n- [ ] Partial evaluator\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiraben%2Fhasktran","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsiraben%2Fhasktran","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiraben%2Fhasktran/lists"}