r/programming • u/Sandro_Lovnicki • Aug 25 '19
pLam: A mighty tool for learning and exploring pure λ-calculus
17
u/takanuva Aug 25 '19
I'll be teaching the lambda calculus to a class in two weeks. This should be a useful tool! Thank you.
2
u/Sandro_Lovnicki Aug 25 '19
Sounds amazing, I am very glad to hear that. Good luck!
Feel free to contact me if you need any additional information that you cannot find in project’s README10
u/takanuva Aug 25 '19
Which evaluation order is the interpreter using? It would be nice if you could provide this as an option, so people could see the call-by-value vs. call-by-name semantics.
4
u/Sandro_Lovnicki Aug 25 '19
It uses call by name.
I agree, it would be great to have this option. I’m definitely planning to work on that for the upcoming releases.
13
u/Sandro_Lovnicki Aug 25 '19
24
u/minibuster Aug 25 '19
Maybe others are smarter than me, but I think a few concrete examples would go a long way to make the article more approachable. It's clear you spent a lot of time putting it together, so I thought it might be unfortunate if some people get lost where a few extra sentences would have helped guide them through.
For example, for sentences like
An application is two λ-expressions side-by-side, written as M N , meaning that M is applied to N.
and
One can also write λ-abstraction in the curried form: \xy. x or \x y. x.
I found the high level hard to get my head around, like alphabet salad.
It is good that you link to the Wikipedia article, which starts early on with a motivation section that's pretty clear and straightforward. Still, if you wanted your article to stand alone without necessitating a jump into Wikipedia, I think a few concrete examples could really help new users who are interested but also intimidated by the topic.
Just an opinion. Please feel free to ignore it :)
15
u/Sandro_Lovnicki Aug 25 '19
Good advice.
I rushed through all the Lambda Calculus stuff because I focused this article more on pLam and assumed readers will know Lambda Calculus well. But then again, if this was my assumption, why write anything about LC.I will edit the article with a few more sentences. Thanks!
8
u/minibuster Aug 25 '19
Thanks for being so receptive!
I re-read the article, and another thought is it could also be enough to change this line
You can continue reading more about λ-calculus at Wikipedia if interested.
into something like
I've kept this introduction to Lambda Calculus short, because I'm assuming most readers are already familiar with it, and I want to focus this article more on pLam. If you want a deeper dive into lambda calculus, including concrete motivating examples, you can continue reading more about it at Wikipedia.
Up to you whichever direction you take it! Thanks for taking the time to write pLam and sharing it with the community.
4
u/Dobias Aug 26 '19
From the article:
For example,
λx.λy.xwould be written\x. \y. xin pLam. One can also write λ-abstraction in the curried form:\xy. xor\x y. x.But from Wikipedia:
[...], currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument.
So, isn't it the other way around?
\x. \y. xis the curried form, while\x y. xis the non-curried (or uncurried) form?2
u/Sandro_Lovnicki Aug 26 '19
You are absolutely right, I will fix that. Thanks for finding that error!
2
24
u/lowercaseTI Aug 25 '19
I wish I knew what any if this meant. I'd be so cool and happy doing lambda calculus 😭
30
u/Sandro_Lovnicki Aug 25 '19 edited Aug 25 '19
Yeah, lambda calculus is very confusing at first. I still struggle making complex expressions.
Maybe you can get a basic understanding of what this all is by starting at article I posted
2
4
u/Dougw6 Aug 25 '19
This looks really cool. I've always been fascinated with lambda calculus. I'll have to check this out.
3
u/Sandro_Lovnicki Aug 25 '19
Tnx. I created pLam for the same reason, so I could explore and learn lambda calculus in depth. It ended up being a part of my master thesis. Now I wish it helps many others in learning, understanding and enjoying
7
3
u/joshisgross Aug 26 '19
This is really cool and quite well-done for a first Haskell project! I've looked at a lot of toy lambda-calculus applications over the years and this has a super friendly UX and useful output, compared to everything else I've seen. Thanks for doing this and publishing it!
1
2
2
u/Dobias Aug 26 '19
I tried getting this thing running on my local machine, but I failed. So I tried using Docker. This is the Dockerfile I just created:
FROM ubuntu:18.04
RUN apt-get update -y
RUN apt-get install -y git
RUN apt-get install -y wget
RUN wget -qO- https://get.haskellstack.org/ | sh
WORKDIR /plam
RUN git clone https://github.com/sandrolovnicki/pLam.git
WORKDIR /plam/pLam
RUN stack setup
RUN apt-get install -y libncurses5-dev libncursesw5-dev
RUN ls -1
RUN stack buildBut when building:
docker build -t plam .And then running it, it fails with:
$ docker run -it --rm plam
_
| |
____| | ___ __ __
| _ \ |__| _ \| \/ |
| _/____|_______/_| v2.0.0
|_| pure plam: <stdout>: commitBuffer: invalid argument (invalid character)Any ideas?
1
u/Sandro_Lovnicki Aug 26 '19 edited Aug 26 '19
I’ve seen this with one other user. It has something to do with greek letter lambda. You see how it fails with (invalid character) right at the moment it has to print lambda as a part of pLam header.
I don’t know how to solve this, never could recreate it. Btw, my machine is not from Greece if that crosses anyone’s mind as a solution.
If you manage to solve it, please let me know so I can add it to README and acknowledge you if you allow
I also use Ubuntu 18.04
1
u/Dobias Aug 27 '19
I don’t know how to solve this, never could recreate it
You could try to use my `Dockerfile` to recreate it. :)
1
u/Sandro_Lovnicki Aug 27 '19
Can you try changing the 'λ' character with '\x03bb' in app/Main.hs line 23? Then build it again and run.
If it prints the pLam header correctly, I will replace all λ symbols in source code with \x03bb, release v2.0.1 and those problems should be fixed.
1
u/Dobias Aug 27 '19
Simply replacing
λwithlall overMain.hsFROM ubuntu:18.04 RUN apt-get update -y RUN apt-get install -y git RUN apt-get install -y wget RUN wget -qO- https://get.haskellstack.org/ | sh WORKDIR /plam RUN git clone https://github.com/sandrolovnicki/pLam.git WORKDIR /plam/pLam RUN stack setup RUN apt-get install -y libncurses5-dev libncursesw5-dev RUN ls -1 RUN sed -i "s/λ/l/g" app/Main.hs RUN stack build CMD stack exec plamseems to help. :D
$ docker run -it --rm plam _ | | ____| | ___ __ __ | _ \ |__| _ \| \/ | | _/____|_______/_| v2.0.0 |_| pure l-calculus interpreter ================================= pLam>For testing with
\x03bbyou can just try with the aboveDockerfile(docker build -t plam .anddocker run -it --rm plam) on your own. I had weird compilation errors when I tried, but probably just because I did not escape the backslash properly in thesedcommand or something similar.1
u/Sandro_Lovnicki Aug 27 '19
What is your encoding in Terminal? Under Terminal -> Set Character Encoding. Mine is Unicode - UTF-8. I think this is the reason why I cannot experience this bug with λ
1
u/Dobias Aug 27 '19
I'm using Mate Terminal
1.18.2, and the encoding is already set toUnicode (UTF-8).1
u/Sandro_Lovnicki Aug 27 '19
Very weird then. I will try to get to the bottom of this problem, but for now you can use it like you did; by replacing all 'λ' with 'l'. There are also some 'λ' in src/Syntax.hs and src/Helper.hs. There are two more greek letters to be raplced; 'α' and 'β' in app/Helper.hs. You can then replace them with 'a' and 'b'
17
2
u/beyondtwosouls0 Aug 25 '19 edited Aug 25 '19
Upon googling „plam“, the first hit ended up on urban dictionary. You may reconsider the naming
2
u/Sandro_Lovnicki Aug 25 '19
Yeah, sick. I did that search couple of months ago out of curiosity... Imagine my surprise. I’m hoping that most people don’t know this word. For now at least
1
u/Skaarj Aug 26 '19
From reading the Readme I'm surprised about the restrictions on names. Variables need to be a single letter? Is this neccessary for lambda calculus? I was under the impression one can determine by context if a token is bound or free variable or something else.
3
u/Sandro_Lovnicki Aug 26 '19 edited Aug 26 '19
It is not necessary, but is a practice in lambda calculus for variables to be lower-case characters.
pLam also has "environment variables" which are names for predefined lambda expressions and they are any string that is not a lambda variable (lower-case character). I wanted to make a clear distinction between those.
It would be pretty hard to decide what is a lambda variable and what is an environment variable with no naming restrictions. For example, (\x. x T y) is a valid expression in pLam (and we want it that way). In this case, pLam wouldn't consider y a lambda variable because it is not bound and would think it is an environment variable, but could't find it in environment. On the other hand, pLam also needs to know that T really is an environment variable (and not maybe lambda variable like y) that must be replaced with (\x y. x) at appropriate time.
An option would be to just leave the expression as is if it is not found in environment and not bound. But I think it is nicer as it is now.
3
2
2
u/burnie93 Aug 26 '19
This is cool! I was trying to learn it with pen and paper two years ago (mainly handling operations with SKI). I'm curious about this.
0
60
u/Xayan Aug 25 '19
Wow, look at those procedurally generated Elvish runes, really cool.