type-unlambda/README.md

72 lines
2.1 KiB
Markdown
Raw Normal View History

2021-06-06 19:41:45 +00:00
# type-unlambda
2020-11-06 09:07:33 +00:00
2021-06-03 12:27:57 +00:00
[![MIT License][]](LICENSE)
2020-11-06 09:07:33 +00:00
2021-06-03 12:27:57 +00:00
[Unlambda][] interpreter implemented in TypeScript's type system.
2020-11-06 09:07:33 +00:00
## Getting Started
Installation:
```sh
2020-11-23 03:52:07 +00:00
npm install --save-dev typescript
2020-11-06 09:07:33 +00:00
npm install --save-dev @esolangs/type-unlambda
```
Example usage:
2020-11-06 09:07:33 +00:00
```typescript
import Unlambda from '@esolangs/type-unlambda';
type Code = '``@c`d``s`|k`@c';
type Input = 'Hello!';
type Output = Unlambda<Code, Input>; // Output == '!olleH'
2020-11-06 09:07:33 +00:00
```
2021-06-06 17:46:53 +00:00
Screenshots (from Visual Studio Code):
2020-11-06 09:07:33 +00:00
2021-06-06 17:46:53 +00:00
![Example](assets/example.png)
2020-11-06 09:07:33 +00:00
## Notes
2021-06-03 13:27:35 +00:00
You're likely to get the following error when trying to run a program with
type-unlambda:
2020-11-06 09:07:33 +00:00
> Type instantiation is excessively deep and possibly infinite.ts(2589).
2021-06-03 13:27:35 +00:00
To write loops in TypeScript's type system, we have to use recursions,
like we do in other purely functional programming languages. Meanwhile, we use
[CPS][] to implement continuations, which also introduces heavy recursion.
2021-06-06 19:41:45 +00:00
However, TypeScript's type system is not meant for general purpose programming.
There is no [TCE][Tail Call], and recursion has its limits.
2020-11-06 09:07:33 +00:00
2021-06-03 13:27:35 +00:00
In [src/compiler/checker.ts][TSC checker], there is a hard-coded limit for
type instantiation:
2020-11-06 09:07:33 +00:00
```typescript
if (instantiationDepth === 50 || instantiationCount >= 5000000) {
// ...
return errorType;
}
```
2021-06-03 13:27:35 +00:00
You may expect that there is an option somewhere that this limit can be
configured, like `-ftemplate-depth=n` in gcc/clang. Unfortunately, there isn't,
[and it's likely to stay that way][PR 29602].
2020-11-06 09:07:33 +00:00
2021-06-03 13:27:35 +00:00
To workaround this limitation, we modify the code of `tsserver` or `tsc`
in `node_modules`, until the error no longer applies. Changing
`instantiationDepth` to `1000` is sufficient to run the example above.
2021-06-03 12:27:57 +00:00
<!-- Reference Links -->
[MIT License]: https://img.shields.io/badge/license-MIT-blue.svg
[Unlambda]: http://www.madore.org/~david/programs/unlambda/
[CPS]: https://en.wikipedia.org/wiki/Continuation-passing_style/
2021-06-06 19:41:45 +00:00
[Tail Call]: https://en.wikipedia.org/wiki/Tail_call/
2021-06-03 13:27:35 +00:00
[TSC checker]:
https://github.com/microsoft/TypeScript/blob/v4.1.2/src/compiler/checker.ts
2021-06-03 12:27:57 +00:00
[PR 29602]: https://github.com/microsoft/TypeScript/pull/29602