
Rust, Macros, λ-calculus/Church numerals, oh my - nixpulvis
https://github.com/nixpulvis/lalrpop-lambda/blob/master/src/encode/numerals.rs
======
nixpulvis
If you want to play with this, you can run `cargo run` from this:
[https://github.com/nixpulvis/lambash/](https://github.com/nixpulvis/lambash/)
repository.

It currently does both with and without η-reduction, and generally should work
similarly to
[http://lambda.jimpryor.net/code/lambda_evaluator/](http://lambda.jimpryor.net/code/lambda_evaluator/)
a very cool online JS lambda evaluator.

------
tempodox
Another source code that demonstrates how things that are easy to write, read
and understand in FP languages are a nightmare in Rust. Maybe making
functional code indecipherable is not the best example of what Rust may
actually be good for.

~~~
nixpulvis
I'm actually finding the opposite. This is mapping very nicely to Rust. But of
course not as nicely as it would if I could encode things as lambda terms in
the language itself. I'm currently working on an impl of Fn for the
Expression. Maybe it'll work out.

This code isn't really meant to be an easily digestible read. I don't make
much effort to explain the church numerals for example, I just show that it
works with a few tests.

I personally find a lot of good FP principles come into play in Rust quite
frequently.

------
buster
My first thought is, that code should be plain ascii and preferably english.
But ultimately it's fine and nice that the language supports Unicode. It
should be a matter of code guidelines and of the particular project setting
the preferred way beforehand.

It should not be a decision by people implementing the language but not all
projects using that language.

------
boazbarak
I wrote an implementation of the λ calculus in python as an appendix for my
book
[https://nbviewer.jupyter.org/github/boazbk/nandnotebooks/blo...](https://nbviewer.jupyter.org/github/boazbk/nandnotebooks/blob/master/lambda.ipynb)

------
hansdieter1337
looking forward to the first homograph attack in rust

~~~
db48x
Hmm. What would you attack? That is, if you were writing a crate that I was
thinking of using in my program, how would homographs allow you to compromise
my code?

~~~
wyldfire
The best attack is of an alias of a popular crate. Duplicate the
functionality/behavior/version number but include a subtle backdoor.

~~~
db48x
How would people end up using your evil crate though? I guess you could rely
on having people copy and paste code from your documentation or tutorials, but
if they just type the name then they get the original.

------
ajross
While it's definitely cute and amusing, this is exactly why languages that
specify their source code in non-ascii encodings are a bad idea. Fully 92.6%
of working programmers have no idea how to produce a λ or γ on their input
device of choice. Maybe a third of those could eventually find it with a
character picker tool.

Java started this (in the unicode era anyway -- yeah yeah, APL). The community
quite sanely rejected it as a matter of style. I have no idea why it hasn't
been killed dead in new designs.

~~~
llamathrowaway
Maybe it’s time for a plugin that does this (or maybe it already exists?):

You press the backslash key, enter the name of the symbol, then press enter,
and the symbol will show up in your buffer.

Something like:

\lambda -> λ

\Lambda -> Λ

\\\ -> \

et cetra.

~~~
frosted-flakes
A dedicated global compose key solves this problem very elegantly. Hitting the
◆ compose key (right alt for me) followed by a series of intuitive characters
inserts the corresponding character.

For example:

◆ - - - produces an em dash (—)

◆ - - . produces an en dash (–)

◆ ' e produces é

◆ | c produces the cent symbol (¢)

Usually, you can just guess the combination and be right 3/4 times. Otherwise,
it's fairly easy to look it up, or create it if it doesn't exist yet.

Some distros of Linux have this built-in, but I use WinCompose[1] on Windows.

[1][https://github.com/samhocevar/wincompose](https://github.com/samhocevar/wincompose)

~~~
btown
macOS has something very similar with the option key, but the set of
characters doesn’t include all Greek letters which is incredibly annoying:
[https://sites.psu.edu/symbolcodes/mac/codemac/](https://sites.psu.edu/symbolcodes/mac/codemac/)

~~~
frosted-flakes
Except most of the key combinations are not intuitive. I could never guess
them like I could with a compose key.

------
nixpulvis
Amazing, 46 comments, all about my choosing to use the _correct_ symbol for
the macro. It's not like I choose some super misleading Unicode characters,
like for example, how macOS smart quotes might change "foo" into “foo”.

Again, technically correct... maybe programming languages should match these
kinds of quotes too.

But this is all more or less beside the point. I can type `λ!{x.x}` easily,
since I have a Greek keyboard on my system (and a λ on my wrist, and a cat
named π).

It's even more of a small deal as I've included the equivalent `abs!{x.e}` and
`app!(e1,e2)` macros. In fact `λ!` and `γ!` are just macro "aliases".

Also, given the prominence of α-renaming, β-reduction, and η-reduction, I'm
very glad I can use these symbols in Rust.

~~~
rbonvall
I agree, it's nice to use the same symbols you'd use on paper. I did something
similar in Scala [http://mez.cl/en/lambda-dsl/](http://mez.cl/en/lambda-dsl/)

~~~
nixpulvis
Very cool! I think you've inspired me to implement something like `Expression
<< Expression` much like your `$`.

I'm currently trying to determine the best way to implement `From<Expression>
for u64` (or maybe `Option<u64>`...), so I can convert both to _and_ from
`u64` types as church encoded numerals. Eventually the goal is to `impl
Into/From<Expression>` for all the types one might use, giving a horribly
inefficient runtime for Rust ;)

