{"id":13571709,"url":"https://github.com/zesterer/tao","last_synced_at":"2025-04-12T09:23:12.039Z","repository":{"id":38290416,"uuid":"232619636","full_name":"zesterer/tao","owner":"zesterer","description":"A statically-typed functional language with generics, typeclasses, sum types, pattern-matching, first-class functions, currying, algebraic effects, associated types, good diagnostics, etc.","archived":false,"fork":false,"pushed_at":"2023-09-20T10:01:06.000Z","size":1878,"stargazers_count":1090,"open_issues_count":10,"forks_count":23,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-10-14T19:22:15.518Z","etag":null,"topics":["compiler","functional","programming-language"],"latest_commit_sha":null,"homepage":"https://tao.jsbarretto.com/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zesterer.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-08T17:28:22.000Z","updated_at":"2024-10-11T14:03:30.000Z","dependencies_parsed_at":"2024-10-25T18:43:28.521Z","dependency_job_id":"e7fd9627-8a7b-43ca-bbac-6aa33420883f","html_url":"https://github.com/zesterer/tao","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/zesterer%2Ftao","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Ftao/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Ftao/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Ftao/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zesterer","download_url":"https://codeload.github.com/zesterer/tao/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248544437,"owners_count":21121953,"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":["compiler","functional","programming-language"],"created_at":"2024-08-01T14:01:05.180Z","updated_at":"2025-04-12T09:23:11.991Z","avatar_url":"https://github.com/zesterer.png","language":"Rust","funding_links":[],"categories":["Rust","Uncategorized","Functional"],"sub_categories":["Uncategorized"],"readme":"# Tao\n\n[You can now test Tao in the browser!](https://tao.jsbarretto.com/)\n\nA statically-typed functional language with polymorphism, typeclasses, generalised algebraic effects, sum types,\npattern-matching, first-class functions, currying, good diagnostics, and much more!\n\n\u003ca href = \"https://www.github.com/zesterer/tao\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/zesterer/tao/master/misc/example.png\" alt=\"Demo of Tao's features\"/\u003e\n\u003c/a\u003e\n\nFor more example programs, see...\n\n- [`hello.tao`](https://github.com/zesterer/tao/blob/master/examples/hello.tao): Hello world\n- [`input.tao`](https://github.com/zesterer/tao/blob/master/examples/input.tao): Demonstrates a more complex example of IO effects\n- [`calc.tao`](https://github.com/zesterer/tao/blob/master/examples/calc.tao): A CLI calculator, demonstrating parser combinators\n- [`adventure.tao`](https://github.com/zesterer/tao/blob/master/examples/adventure.tao): A text adventure game\n- [`brainfuck.tao`](https://github.com/zesterer/tao/blob/master/examples/brainfuck.tao): A brainfuck interpreter\n- [`mutate.tao`](https://github.com/zesterer/tao/blob/master/examples/mutate.tao): Mutation expressed as a side effect\n- [`polymorphic_effects.tao`](https://github.com/zesterer/tao/blob/master/examples/polymorphic_effects.tao): Example of a higher-order function that's polymorphic over a side effect\n- [`quickcheck.tao`](https://github.com/zesterer/tao/blob/master/examples/quickcheck.tao): A very poor implementation of [`quickcheck`](https://en.wikipedia.org/wiki/QuickCheck) in Tao\n\n## Goals\n\nRight now, Tao is a hobby project and I have no plans to turn it into a production-worthy language. This may change as\nthe project evolves, but I'd rather spend as much time experimenting with new language features for now. That said, I do\nhave a few goals for the language itself:\n\n- **Totality**\n    - All programs *must* explicitly handle all inputs. There are no mechanisms for panicking, exceptions, etc. The goal\n      is to build a type system that's expressive enough to prove the totality of a wide range of programs.\n    - In time, I'd like to see the language develop support for *termination analysis* techniques like\n      [Walther recursion](https://en.wikipedia.org/wiki/Walther_recursion).\n\n- **Extreme optimisation**\n    - A rather dogged and obnoxious opinion of mine is that the 'optimisation ceiling' for statically-typed, total\n      functional programming languages is significantly higher than traditional imperative languages with comparably\n      weak type systems. I want Tao to be a practical example of this that I can point to rather than deploying nebulous\n      talking points about invariants.\n    - I've deliberately made sure that the core MIR of Tao has a very small surface area, making it amenable to a\n      variety of optimisations and static analyses.\n    - Already the MIR optimiser performs quite a lot of optimisations that radically reduce the number of bytecode\n      instructions emitted. See below for a list of these.\n\n- **Learning**\n    - I have only a high-school knowledge of mathematics. I want to use Tao as a test bench to help me learn more about\n      mathematics, proofs, type systems, logic, and computation.\n    - In addition, I hope that Tao can serve as a useful tool for others looking to get into language design, compiler\n      development, or simply functional programming in general: the codebase is relatively small and pragmatic (at\n      least, given the complexity of some of the language features).\n\n## Features\n\n- [x] Hindley-Milner type inference\n- [x] Useful error messages\n- [x] Algebraic data types\n    - [x] Sum types\n    - [x] Record types\n    - [x] Generic data types\n    - [x] Nominal aliases (i.e: `data Metres = Real`)\n- [x] Type aliases\n- [x] Type polymorphism via generics\n    - [x] Class constraints\n    - [x] Associated type equality constraints\n    - [x] Arbitrary `where` clauses (including associated type equality)\n    - [x] Lazy associated item inference (`Foo.Bar.Baz.Biz` lazily infers the class at each step!)\n    - [x] Type checker is Turing-complete (is this a feature? Probably not...)\n    - [x] Variance is properly tracked through both type and effect parameters\n- [x] Pattern matching\n    - [x] Destructuring and binding\n    - [x] ADT patterns\n    - [x] List patterns (`[a, b, c]`, `[a, b .. c]`, etc.)\n    - [x] Arithmetic patterns (i.e: `n + k`)\n    - [x] Inhabitance checks (i.e: `None` exhaustively covers `Maybe Never`)\n    - [x] Recursive exhaustivity checks\n    - [x] `let` does pattern matching\n- [x] First-class functions\n    - [x] Functions support pattern-matching\n    - [x] Currying\n- [x] Typeclasses\n    - [x] Type parameters\n    - [x] Associated types\n    - [x] Operators are implemented as typeclasses\n- [x] Algebraic effects\n    - [x] Effect objects (independent of functions, unlike some languages)\n    - [x] Basin and propagation syntax (equivalent to Haskell's `do` notation, or Rust's `async`/`try` blocks)\n    - [x] Generic effects\n    - [x] Polymorphic effects (no more `try_x` or `async_x` functions!)\n    - [x] Effect sets (i.e: can express values that have multiple side effects)\n    - [x] Effect aliases\n    - [x] Effect handlers (including stateful handlers, allowing expressing effect-driven IO in terms of monadic IO)\n    - [x] Effects can be parameterised by both types and other effects\n- [x] Built-in lists\n    - [x] Dedicated list construction syntax (`[a, b, c]`, `[a, b .. c, d]`, etc.)\n- [x] Explicit tail call optimisation\n    - [ ] Better syntax/guarantees\n- [x] Optimisation\n    - [x] Monomorphisation of generic code\n    - [x] Inlining\n    - [x] Const folding\n    - [x] Symbolic execution\n    - [x] Branch commutation\n    - [x] Dead code removal\n    - [x] Inhabitance analysis\n    - [x] Exhaustive pattern flattening\n    - [x] Unused function pruning\n    - [x] Unused binding removal\n    - [x] Arithmetic rewriting / simplification\n    - [x] Identity branch removal\n- [x] Bytecode compiler\n- [x] Bytecode virtual machine\n\n## Current working on\n\n- [ ] Pattern exhaustivity checking (sound, but unnecessarily conservative)\n- [ ] Arithmetic patterns (only nat addition is currently implemented)\n- [ ] Typeclasses\n    - [ ] Coherence checker\n    - [ ] Visible member semantics to relax orphan rules\n- [ ] MIR optimiser\n    - [ ] Unboxing\n    - [ ] Automatic repr changes for recursive types\n        - [ ] Transform `data Nat = Succ Nat | Zero` into a runtime integer\n        - [ ] Transform `data List A = Cons (A, List A) | Nil` into a vector\n- [ ] Algebraic effects\n    - [ ] Higher-ranked effects (needed for proper `async` support)\n    - [ ] Arbitrary resuming/suspending of effect objects\n    - [ ] Full monomorphisation of effect objects\n\n## Planned features\n\n- [ ] Better syntax (perhaps indentation-sensitivity for pattern matching?)\n- [ ] Module system (instead of `import` copy/paste)\n- [ ] LLVM/Cranelift backend\n\n## Philosophy\n\n- **Prefer general solutions over special casing**: Flexible and general features should be preferred over specific\n  solutions that might produce rough edges down the line that require even more special-case solutions to solve. It is\n  better to provide a smaller core of general features than to grow the language into an eclectic mess.\n\n- **Correctness over convenience**: If something is wrong or has edge-cases, don't paper over the cracks. Tao tries to\n  force the programmer to write programs that are as well-formed and as bug-free as reasonably possible. Under/overflow\n  *matters*. Unhandled patterns *matter*. Overlapping class impls *matter*.\n\n- **Do the obvious thing**: When there's a choice to be made about behaviour, the thing that's most often correct should\n  be done. All other things should be default-on lints or errors.\n\n- **Similar concepts should have similar syntax**: List/record/data type construction and destruction (i.e: pattern\n  matching) share the same syntax. Function argument patterns and `when` patterns share the same syntax.\n\n- **Local reasoning**: Where possible, the behaviour of a program/function/expression should be obvious with local-only\n  information. No wild overrides or behavioural changes that require looking at imports to understand.\n\n- **Say what you mean**: Syntax *does* matter! Programs are designed to be read, and Tao should encourage the writing of\n  programs that tell a linear story. If you're needing to jump forward and backward to understand a program, that's\n  something that needs fixing, if at all possible.\n\n- **Abstraction should preserve 'core' semantics**: Many languages provide complex macro systems that allow immense\n  towers of meta-programming. Tao is not opposed to meta-programming and abstraction, but aggressively tries to keep\n  such things in terms of the surface syntax, improving legibility and minimising the element of surprise. As a nice\n  addition, rejecting macros makes Tao much friendlier to IDEs and static analysis systems.\n\n## Interesting features\n\nHere follows a selection of features that are either unique to Tao or are uncommon among other languages.\n\n### Generalised algebraic effects\n\nTao has support for 'generalised algebraic effects'. 'Effects' means that Tao can express the side-effects of functions\n(IO, mutation, exceptions, async, etc.) in type signatures. 'Generalised' means that it's possible for you to create\nand use your own effects to express whatever your heart desires. 'Algebraic' means that Tao allows code to be generic\nover an effect (or set of effects). For example, consider the `map` function, used to apply a function to each element\nof a list in turn:\n\n```py\nfn map A, B : (A -\u003e B) -\u003e [A] -\u003e [B]\n    | _, [] =\u003e []\n    \\ f, [x .. xs] =\u003e [f(x) .. map(f, xs)]\n```\n\n`map` can be used like so to, for example, double all elements of a list:\n\n```py\n[1, 2, 3, 4]\n    -\u003e map(fn x =\u003e x * 2)\n\n# Result: [2, 4, 6, 8]\n```\n\nMost languages, such as Rust, have a function like this. Unfortunately, it breaks down quickly when we want to do\nanything even *slightly* different to a 'pure' mapping between elements within the mapping function. For example,\nconsider a program for which the mapping function is fallible, or requires some asynchronous operation to complete. It's\nnecessary to do one of two things:\n\n- Have the function 'silently' exit through stack unwinding, as is the case in C#, C++, etc.\n- Create a copy of the function that can handle failure like `try_map` as in Rust\n\n*It's worth noting that Haskell mostly solves this problem with monads: but they're frequently unwieldy. Effect systems\nand monads have many similarities, but the former works much harder to make them integrate better with regular control\nflow.*\n\nIn Tao, this problem can be solved by making `map` generic over an effect parameter, like so:\n\n```py\nfn map A, B, e : (A -\u003e e ~ B) -\u003e [A] -\u003e e ~ [B]\n    | _, [] =\u003e []\n    \\ f, [x .. xs] =\u003e [f(x)! .. map(f, xs)!]\n```\n\nA few things have changed here.\n\nFirstly, we've introduced an effect parameter, `e`. Secondly, the type signature has changed: the mapper function,\n`A -\u003e B`, now has `e` attached to its return type, resulting in `A -\u003e e ~ B`. This is also present in the final return\ntype of the function, `e ~ [B]`, expressing that the side effects of the function as a whole correspond to those\nperformed by the mapping function.\n\nSecondly, a `!` operator has appeared within the implementation after calling the mapping function. This is the 'effect\npropagation' operator and signals to the compiler that the side effects of `f` should be lifted to the signature of the\nfunction as a whole.\n\nNote that, otherwise, the implementation remains the same: we have not needed to use any complicated machinery to handle\nthe side effect (as might be the case in a Rust-style `try_map`), just a single additional operator.\n\nAs a result of this change, `map` now accepts mapping functions that perform *any* side effect: throwing errors, IO,\nyielding values, mutation, and many more. It also accepts function that perform arbitrary combinations of effects, or\nthose that have no side effects at all (the empty set is still a valid effect set!):\n\n```py\n# Yield each element of the list, resulting in a generator\n[1, 2, 3, 4]\n    -\u003e map(fn x =\u003e yield(x)!)!\n\n# Generate an error if any element of the list is `0`\n[1, 2, 3, 4]\n    -\u003e map(fn x =\u003e if x = 0 then err(\"no zeroes allowed\")! else x)!\n\n# Print each element of the list\n[1, 2, 3, 4]\n    -\u003e map(fn x =\u003e print(x)!)!\n```\n\nThis is the expressive power of algebraic effect systems: we no longer need to worry about\n[function colours](https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/), hidden\npanics/exceptions, or write many versions of a function to handle all kinds of irregular control flow. Because algebraic\neffects generalise so well, it also becomes possible to use them to separate out interfaces from implementations in a\ncomposable way, allowing developers to swap out the implementation of even very core APIs (such as filesystem access) as\nrequired without the complexity and awkwardness of intricate callback systems.\n\nIn Tao, effects are [kinds](https://en.wikipedia.org/wiki/Kind_(type_theory)), just like types, lifetimes, and constants\nin Rust. They're also represented independently of function signatures too, as 'effect objects' (you can think of effect\nobjects as being like `Future`/`Promise`s, but generalised to all side effects). Because of this, it's possible to use\nthem in a vast array of contexts.\n\n### Arithmetic patterns\n\nTao's type system is intended to be completely sound (i.e: impossible to trigger runtime errors beyond 'implementation'\nfactors such as OOM, stack overflow, etc.). For this reason, subtraction of natural numbers yields a signed integer, not\na natural number. However, many algorithms still require that numbers be counted down to zero!\n\nTo solve this problem, Tao has support for performing arithmetic operations within patterns, binding the result. Because\nthe compiler intuitively understands these operations, it's possible to statically determine the soundness of such\noperations and guarantee that no runtime errors or overflows can ever occur. Check out this 100% sound factorial\nprogram!\n\n```py\nfn factorial =\n    | 0 =\u003e 1\n    \\ y ~ x + 1 =\u003e y * factorial(x)\n```\n\n### All functions are lambdas and permit pattern matching\n\nExcluding syntax sugar (like type aliases), Tao has only two high-level constructs: values and types. Every 'function'\nis actually just a value that corresponds to an line lambda, and the inline lambda syntax naturally generalises to\nallow pattern matching. Multiple pattern arguments are permitted, each corresponding to a parameter of the function.\n\n```py\ndef five =\n    let identity = fn x =\u003e x in\n    identity(5)\n```\n\n### Exhaustive pattern matching\n\nTao requires that pattern matching is exhaustive and will produce errors if patterns are not handled.\n\n### Very few delimiters, but whitespace *isn't* semantic\n\nIn Tao, every value is an expression. Even `let`, usually a statement in most languages, is an expression. Tao requires\nno semicolons and no code blocks because of this fact.\n\n### Currying and prefix calling\n\nIn Tao, `arg-\u003ef` is shorthand for `f(arg)` (function application). Additionally, this prefix syntax can be chained,\nresulting in very natural, first-class pipeline syntax.\n\n```py\nmy_list\n    -\u003e filter(fn x =\u003e x % 2 == 0) # Include only even elements\n    -\u003e map(fn x =\u003e x * x)         # Square elements\n    -\u003e sum                        # Sum elements\n```\n\n### Useful, user-friendly error diagnostics\n\nThis one is better demonstrated with an image.\n\n\u003ca href = \"https://www.github.com/zesterer/tao\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/zesterer/tao/master/misc/error.png\" alt=\"Example Tao error\"/\u003e\n\u003c/a\u003e\n\nTao preserves useful information about the input code such as the span of each element, allowing for rich error messages\nthat guide users towards solutions to their programs. Diagnostic rendering itself is done by my crate\n[Ariadne](https://www.github.com/zesterer/ariadne).\n\n### Automatic call graph generation.\n\nTao's compiler can also automatically generate graphviz call graphs of your programs to help you understand them better.\nHere's the expression parser + REPL from `examples/calc.tao`. The call graph will automatically ignore utility functions\n(i.e: functions with a `$[util]` attribute on them), meaning that even very complex programs suddenly become\nunderstandable.\n\n\u003ca href = \"https://raw.githubusercontent.com/zesterer/tao/master/misc/call_graph.svg\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/zesterer/tao/master/misc/call_graph.svg\" alt=\"Call graph of an expression parser in Tao\"/\u003e\n\u003c/a\u003e\n\n## Usage\n\n### Commands\n\nCompile/run a `.tao` file\n\n```\ncargo run -- \u003cFILE\u003e\n```\n\nRun compiler tests\n\n```\ncargo test\n```\n\nCompile/run the standard library\n\n```\ncargo run -- lib/std.tao\n```\n\n### Compiler arguments\n\n- `--opt`: Specify an optimisation mode (`none`, `fast`, `size`)\n\n- `--debug`: Enable debugging output for a compilation stage (`tokens`, `ast`, `hir`, `mir`, `bytecode`)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzesterer%2Ftao","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzesterer%2Ftao","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzesterer%2Ftao/lists"}