https://github.com/zk-phi/lambda_to_lazyk
A Lambda Calculus to Lazy K translator
https://github.com/zk-phi/lambda_to_lazyk
programming-language
Last synced: 5 months ago
JSON representation
A Lambda Calculus to Lazy K translator
- Host: GitHub
- URL: https://github.com/zk-phi/lambda_to_lazyk
- Owner: zk-phi
- Created: 2015-09-14T19:03:24.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-02-27T08:33:03.000Z (almost 10 years ago)
- Last Synced: 2024-12-30T21:15:44.943Z (about 1 year ago)
- Topics: programming-language
- Homepage:
- Size: 6.84 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.org
Awesome Lists containing this project
README
* a Lambda Calculus to Lazy K translator
** Usage
Run the translator with
: swipl lambda_to_lazyk.swi
and give a lambda expresssion to the ~compile~ pred.
: ?- compile(x -> y -> y @ x).
: S(K(S(I)))(K)
** Syntax
- fundamental syntax
- ~->~ :: abstraction (right associative)
- ~@~ :: application (left associative)
- syntax sugars
- lists :: ~[x, y, ...]~ = ~cons @ x @ (cons @ y @ ... nil)~
- numbers :: ~4~ = ~succ @ (succ @ (succ @ (succ @ zero)))~
- strings :: ~"abc"~ = ~[97, 98, 99]~
** Built-in macros
- data representation
- boolean :: ~true/0~, ~false/0~
- number :: ~zero/0~, ~succ/1~
- list :: ~nil/0~, ~cons/2~
- predicates
- unary :: ~zerop/1~, ~nullp/1~
- binary :: ~ge/2~, ~gt/2~, ~eq/2~, ~neq/2~, ~le/2~, ~lt/2~
- operations
- arithmetic :: ~add/2~, ~sub/2~, ~mult/2~, ~exp/2~, ~mod/2~, ~div/2~
- boolean :: ~not/1~, ~and/2~, ~or/2~, ~xor/2~
- list :: ~car/1~, ~cdr/1~, ~nthcdr/2~, ~nth/2~, ~map/2~, ~append/2~, ~reverse/1~
- others
- control :: ~if/3~,
- higher order :: ~fix/1~, ~compose/2~
- utils :: ~int_to_str/1~
** User-defined macros
Define macros with ~define~ pred
: ?- define(twice, f -> x -> f @ (f @ x)).
: true.
so that you can use the definition in the expressions.
: ?- compile(twice @ (x -> x) @ (x -> y -> y @ x)).
: S(S(K(S))(K))(I)(I)(S(K(S(I)))(K))
** Examples
*** Hello World
: ?- compile(x -> reverse @ "!dlroW ,olleH").
*** Project Eulerr #001
: ?- define(thirty_seven, succ @ (exp @ (mult @ 2 @ 3) @ 2)). % equals to 37 but optimized in code size
: ?- define(limit, mult @ thirty_seven @ ((x -> x @ x) @ 3)). % equals to 999
: ?- define(solve_1, x -> if @ (zerop @ (mult @ (mod @ x @ 3) @ (mod @ x @ 5))) @ x @ 0).
: ?- define(solve, fix @ (f -> x -> if @ (zerop @ x) @ 0 @ (add @ (solve_1 @ x) @ (f @ (pred @ x))))).
: ?- compile(x -> int_to_str @ (solve @ limit)).