{"id":16301682,"url":"https://github.com/timo/adt","last_synced_at":"2025-04-10T01:18:10.543Z","repository":{"id":7620493,"uuid":"8979548","full_name":"timo/ADT","owner":"timo","description":"Algebraic Data Types for perl6, inspired by Haskell","archived":false,"fork":false,"pushed_at":"2020-01-15T23:03:47.000Z","size":25,"stargazers_count":12,"open_issues_count":1,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T01:18:05.358Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Perl 6","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/timo.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}},"created_at":"2013-03-24T01:26:22.000Z","updated_at":"2020-04-09T11:25:32.000Z","dependencies_parsed_at":"2022-09-14T02:20:15.493Z","dependency_job_id":null,"html_url":"https://github.com/timo/ADT","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/timo%2FADT","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timo%2FADT/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timo%2FADT/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timo%2FADT/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timo","download_url":"https://codeload.github.com/timo/ADT/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137887,"owners_count":21053775,"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-10-10T20:55:20.206Z","updated_at":"2025-04-10T01:18:10.524Z","avatar_url":"https://github.com/timo.png","language":"Perl 6","funding_links":[],"categories":[],"sub_categories":[],"readme":"Algebraic Data Types\n====================\n\nThis module implements algebraic data types inspired by the Haskell syntax.\nSee below for limitations of the current implementation.\n\nHere's a code example for you:\n\n\tuse ADT; \n\t\n    # define the algebraic data type\n    my $adt = q{Tree = Branch Tree left, Tree right | Leaf Str storage};\n    my %res = create_adt($adt);\n    \n    # capture the Tree class\n    my \\Tree = %res\u003cTree\u003e;\n    \n    # create a tree using named parameters\n    my $t =\n        Tree.new-branch(\n            :left(Tree.new-branch(\n                :left(Tree.new-leaf(:storage(1))),\n                :right(Tree.new-leaf(:storage(2))))),\n            :right(Tree.new-leaf(:storage(3))));\n    # pretty-print the tree\n    say $t.gist;\n    \n    # create a tree using positional arguments\n    my $t2 =\n        Tree.new-branch(\n            Tree.new-branch(\n                Tree.new-leaf(1),\n                Tree.new-leaf(2)),\n            Tree.new-leaf(3));\n    say $t2.gist;\n    \n    # capture the subtypes to pattern-match in a given block.\n    my \\Branch = %res\u003cBranch\u003e;\n    my \\Leaf = %res\u003cLeaf\u003e;\n    \n    # map over a whole tree changing all storages.\n    sub treemap($t, *\u0026code) {\n        given $t {\n            when Branch { return Tree.new-branch(treemap($t.left, \u0026code), treemap($t.right, \u0026code)) }\n            when Leaf { return Tree.new-leaf(code($t.storage)) }\n        }\n    }\n    \n    # multiply every leaf node by 10\n    say treemap($t2, * * 10).gist;\n\n\nLimitations\n-----------\n\n### Compile-time availability\n\nSince rakudo doesn't yet allow the EXPORT sub of a package to return a hash of things to export,\nthe ADT cannot be used to define multi subs, because the symbols are not available at compile time.\n\nWhen EXPORT works, code like this will be possible:\n\n    use ADT \"data Tree = Branch Tree left, Tree right | Leaf Str storage\";\n    multi sub treemap(Branch $t, *\u0026c) { ... }\n    multi sub treemap(Leaf $l, *\u0026c) { ... }\n\n### Compile-time checks\n\n\nOne of the neat features of ADT is that the compiler can ensure, that the program is prepared to deal with\nevery defined case (Branch or Leaf, Just or Nothing, ...). A macro can probably be devised to implement\nsuch a compile-time check.\n\n### \"is parsed\" macros\n\nWhen Rakudo gets support for \"is parsed\" macros, definitons of ADT can move from the \"use\" line into the\nmainline of the code, so that they look tidyer and the programmer can put comments nearby etc.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimo%2Fadt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimo%2Fadt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimo%2Fadt/lists"}