pLam (pure Lambda calculus) is a tool to explore, define and evaluate various 位-expressions. Code written in pLam can be executed interactively within pLam's shell or stored in a file with .plam
extension and run anytime.
Inside import/
directory, many useful 位-expressions are already implemented to be used as libraries.
Table of contents
Install a Package
Arch Linux
pLam's AUR package is at https://aur.archlinux.org/packages/plam thanks to @Xmgplays.
Git Clone URL: https://aur.archlinux.org/plam.git
Debian
(coming soon...)
Build from source
Prerequisites
This project builds using Haskell tool stack documented at https://docs.haskellstack.org/en/stable/README/.
On most Unix systems, you can get stack by typing:
curl -sSL https://get.haskellstack.org/ | sh
or:
wget -qO- https://get.haskellstack.org/ | sh
On Windows, you can download 64-bit installer given at https://docs.haskellstack.org/en/stable/README/.
First time setup
- clone project repository
git clone https://github.com/sandrolovnicki/pLam.git
- go to project directory
cd pLam
- setup stack on isolated location
stack setup
Building
- use stack to build project
stack build
note: if build was not successful, it may be due to:
- curses library
- the solution is to install it (on Ubuntu:
sudo apt-get install libncurses5-dev libncursesw5-dev
)
- the solution is to install it (on Ubuntu:
Running (locally)
5.a) use stack to run project executable from project's directory
stack exec plam
Running (globally (Unix systems))
5.b) use make_global.sh
script to create a global command 'plam' that can be used to start pLam from anywhere in your system. The script will also change your import path in src/Config.hs so you need to build the project again.
sudo ./make_global.sh
stack build
Now, (and anytime in the future!), you can start pLam from anywhere in your system by just typing
plam
Syntax and semantics
位-expressions
Variable
位-variable is required to be lowercase and a single letter. For example, x
is a good 位-variable for pLam and X
, var
,... are not. There are also environment variables (names for defined 位-expressions) which are every string that is not parsed as 位-variable, 位-abstraction or 位-application.
Abstraction
位-abstraction is written the same as in the language of pure (untyped) 位-calculus, except that pLam treats a symbol \
as 位
and it is required to write a space after .
. For example, 位x.位y.x
would be written \x. \y. x
in pLam. One can also write 位-abstraction in the "uncurried" form: \xy. x
or \x y. x
.
Application
位-application is written like 2 位-expressions separated by a space, for example (\x. x) (\xy.x)
or (\x. x) MyExpression
or myexp1 myexp2
. Brackets (
and )
are used as usual and are not required to be written for application association; the default association is to the left, so M N P
is parsed as (M N) P
and one only needs to specify with brackets if the intended expression should be M (N P)
.
Commands
A block of code in pLam is a line, and possible lines (commands) are the following:
Define
- syntax:
<string> = <位-expression>
- semantics: let the
<string>
be a name for<位-expression>
. - examples:
T = \x y. x
,myexpression = T (T (\x. x) T) T
- restriction:
<string>
needs to be of length>1 or starting with uppercase letter
Evaluate
- syntax:
?<evop> ?<evop> <位-expression>
where<evop>
s are evaluation options;:d
and/or:cbv
- semantics: reduce the
<位-expression>
to 尾-normal form. If:d
is chosen as option, all the reduction steps will be shown. If:cbv
is chosen as option, reductions will be performed in a call-by-value manner, first reducing the argument before substituting it for bound variable. That is, call-by-name is the default reduction option if:cbv
is not chosen. - example:
\x y. x
,:d T (T (\x. x) T) T
,:d :cbv T (T (\x. x) T) T
,:cbv and (or T F) T
- example 2:
F omega T
will reduce toT
, but:cbv F omega T
will run forever - restriction: none
Import
- syntax:
:import <string>
- semantics: put all the expressions defined in the file
import/<string>.plam
into the list of environment variables. - example:
:import std
- restriction:
<string>.plam
has to be insideimport/
directory within the pLam project directory
Export
- syntax
:export <string>
- semantics: put all the expressions in the list of environment variables into the file
import/<string>.plam
- example:
:export test
- restriction:
<string>.plam
cannot already exist
Comment
- syntax:
--<string>
- semantics: a comment line
- example:
-- this is a comment
- restriction: none
Run
- syntax:
:run <string>
- semantics: runs a
.plam
file with relative path<string>.plam
- example:
:run <relative-path-to-plam>/examples/2.5.2
- restrictions:
~
for home does not work
- syntax:
:print <string>
- semantics: prints
<string>
to a new line. It mostly makes sense to use it in .plam programs to be executed, not in interactive mode where a comment should do the job better. - example:
:print this is a message
- restrictions: none
Syntactic Sugars
pLam is equipped with some (optional) shortcuts to work with often used expressions.
Church numerals
Church numerals can be typed as 0
, 1
, 2
,... and pLam parses those integers as 位fx. x
, 位fx. f x
, 位fx. f (f x)
, ...
Binary numerals
Similar to handling Church numerals, pLam also handles binary numerals from binary.plam
library. You can type them as 0b
, 1b
, 2b
, ... which is them parsed as 位p. p (位xy. y) (位exy.x)
, 位p. p (位xy. x) (位exy.x)
, 位p. p (位xy. y) (位p. p (位xy. x) (位exy.x))
, ...
Note that binary numerals are nothing standard, but something I implemented, so I suppose the only documentation for them is here.
Lists
List encoding is pretty standard; empty = T
, append = 位htfl. l h t
, and you can use syntact sugar which parses [1,2]
into 位fl. l 1 (位fl. l 2 empty)
, [T,\x.x]
into 位fl. l T (位fl. l (位x.x) empty)
and so on...
Examples
NOTE: Output might be slightly different due to constant fixes and changes. Fully updated examples will be put each time they diverge too far from current.
All the examples can be found in examples/
directory.
Fun with booleans
pLam> :import booleans
pLam>
pLam> and (or F (not F)) (xor T F)
|> reductions count : 18
|> uncurried 尾-normal form : (位xy. x)
|> curried (partial) 伪-equivalent : T
Fun with arithmetic
pLam> :import std
pLam>
pLam> mul (add 2 (S 2)) (sub (exp 2 3) (P 8))
|> reductions count : 762
|> uncurried 尾-normal form : (位fx. f (f (f (f (f x)))))
|> curried (partial) 伪-equivalent : 5
Factorial
"standard" way
pLam> :import std
pLam>
pLam> fFact = \f. \x. (isZ x) 1 (mul x (f (P x)))
pLam> Fact = Y fFact
pLam>
pLam> Fact 3
|> reductions count : 646
|> uncurried 尾-normal form : (位fx. f (f (f (f (f (f x))))))
|> curried (partial) 伪-equivalent : 6
primitive recursive way
pLam> :import std
pLam> :import comp
pLam>
pLam> fact = PR0 1 (C22 mul (C2 S I12) I22)
pLam> fact 3
|> reductions count : 898
|> uncurried 尾-normal form : (位fx. f (f (f (f (f (f x))))))
|> curried (partial) 伪-equivalent : 6
Binary numerals
pLam> :import binary
pLam>
pLam> 0b
|> reductions count : 2
|> uncurried 尾-normal form : (位p.((p (位xy. y)) (位exy.x)))
|> curried (partial) 伪-equivalent : 0b
pLam>
pLam> 2048b
|> reductions count : 24
|> uncurried 尾-normal form : (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. y)) (位p.((p (位xy. x)) (位exy.x)))))))))))))))))))))))))
|> curried (partial) 伪-equivalent : (位p. ((p F) 1024b))
pLam>
pLam> addB 7b (subBs 2b 3b)
|> reductions count : 9458
|> uncurried 尾-normal form : (位p.((p (位xy. x)) (位p.((p (位xy. x)) (位p.((p (位xy. x)) (位exy.x)))))))
|> curried (partial) 伪-equivalent : 7b
Lists
pLam> :import list
pLam>
pLam> list = Merge [3,1] [2]
pLam> rlist = Reverse list
pLam>
pLam> Get 0 rlist
|> reductions count : 243
|> uncurried 尾-normal form : (位fx. f (f x))
|> curried (partial) 伪-equivalent : 2
pLam> Get 0 list
|> reductions count : 50
|> uncurried 尾-normal form : (位fx. f (f (f x)))
|> curried (partial) 伪-equivalent : 3
pLam>
pLam> QSort list
|> reductions count : 459
|> uncurried 尾-normal form : (位fl. (l (位fx. f x)) (位fl. (l (位fx. f (f x))) (位fl. (l (位fx. f (f (f x)))) (位fl. f))))
|> curried (partial) 伪-equivalent : (位f. (位l. ((l 1) (位f. (位l. ((l 2) (位f. (位l. ((l 3) empty)))))))))
Redex coloring
Running the existing program:
pLam> :run examples/2.5.2
=================================
< zero
=================================
|> reductions count : 114
|> uncurried 尾-normal form : (位fx. f (f x))
|> curried (partial) 伪-equivalent : 2
Without entering pLam's shell:
plam ~/Projects/pLam/examples/2.5.2.plam
=================================
< zero
=================================
|> reductions count : 114
|> uncurried 尾-normal form : (位fx. f (f x))
|> curried (partial) 伪-equivalent : 2
Done.
Disclaimer for Haskell experts
I am not a Haskell expert. In fact, this is my first and only Haskell project. It is inevitable that existing code could be written better and I wish to do it in the upcoming future.
The goal of the project was to create an environment for easy building of new expressions from previously defined ones, so that one could explore 位-calculus. It was a helper tool so I could define and test a new numeral system in 位-calculus, for my master thesis. Now, when this all went well, the time is coming for me to get back to Haskell code.
Contributing
If you would like to see some improvements or new features, you can open an issue. I will sort issues into milestones and you will know pretty quickly when to expect it to be done. If you can implement your ideas yourself, great! Pull requests are welcome.