Rust, Macros, λ-calculus/Church numerals, oh my
github.com
github.com
It currently does both with and without η-reduction, and generally should work similarly to http://lambda.jimpryor.net/code/lambda_evaluator/ a very cool online JS lambda evaluator.
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.
It should not be a decision by people implementing the language but not all projects using that language.
[1]: https://github.com/rust-lang/rfcs/blob/master/text/2457-non-...
Also, homographs currently produce a warning...
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.
https://github.com/nixpulvis/lalrpop-lambda/blob/master/src/...
I think it’s important to allow Unicode for developers to be more inclusive of other languages and cultures. This is at the forefront of the community of Rust.
As for keywords, yes, I’m with you there.
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.
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.
Some? Nearly all of them do — usually it just needs to be activated in the desktop environment.
I used a … and an — in this post using the compose key without even thinking twice.
> Usually, you can just guess the combination and be right 3/4 times.
¾ even. Just guessing that it was <comp> 3 4.
Ctrl-K l* produces λ
Ctrl-K L* produces Λ
Unsurprisingly, Emacs also supports this when programming in Agda (\lambda) i'm not sure if it's a builtin feature or part of the Agda plugin.
In Agda using Unicode identifiers is widely accepted, even in the standard library: https://github.com/agda/agda-stdlib/blob/master/src/Relation...
Seems like your problem isn't that the source has a specified encoding, but that the syntax allows for non-ascii characters in identifiers, which is actually an unstable feature of Rust, only available with nightly compiler builds. See the feature flag: https://github.com/nixpulvis/lalrpop-lambda/blob/master/src/...
Specifying ASCII for the source code, as your post suggests is better, would leave everyone stuck using \uxxxx escapes for non-ASCII data in string literals. That's unnecessary when you instead specify UTF-8.
Programming has mostly looked the same since we moved away from punchcards, and it seems worthwhile exploring different approaches (eg visual programming) and augmentations to the existing system. Now that we have UTF-8, it's trivial to represent all sorts of characters and use them for coding, and it's only natural that some people try.
Now, while I personally haven't seen a convincing application of using non-ascii beyond comments, that doesn't mean they don't exist. And if people come up with such a way, and a large number of people decide it's a good idea, then tooling in whatever form will follow and make using this new style of coding easy.
I feel like arguing that this should never be done because people don't know where to find the symbols is a bit like arguing we shouldn't build electric vehicles because we don't have a charger network on highways yet..
I've only been pasting in a few in READMEs and standard constants for brevity.
Like anything else, it can certainly be misused. That doesn't mean it's never worth using though.
Unless you speak Greek, I can't think of any other reasons for Greek letters to wander in to code.
Notation matters. What if I told you that to add two numbers you had to write it out as plus(number, number)? Clearly you'd riot. The reason people complain about short IDs is because they're not used to it, but there are a lot of domains (math, science) where using traditional notation enhances understanding. Hell, even in programming if someone wrote sequenceIndex instead of i when iterating over the indices of an array I'd think they were trying to troll.
These sorts of conventions enhance readability by providing extra information. You know instantly that `y` must be data, while `theta` is a parameter.
I don't think r² as a variable name is a good idea. It may look natural for some (including myself), but for others it implies a function while not being one.
r² = r^2;
A = πr²> ∑-+-distribute : ∑ (A + B) C ≃ (∑ A (C ∘ left)) + (∑ B (C ∘ right))
This is quite readable and would be understandable to someone who did not know Agda or the library in particular, but who understood the subject material. If the characters were replaced by words the overhead would be big.
The Plan9 operating system invented the compose key, which makes writing unicode characters easy enough for anyone to use them. Unfortunately, it requires a bit of setup to make the compose key work in X on Linux or *BSD (I have described th process here[1]). Agda and Coq therefore use special input modes for Emacs and other editors.
I also use Unicode in my Latex documents to make them more readable. [2]
[0]: https://agda.readthedocs.io/en/latest/getting-started/quick-...
[1]: https://hakon.gylterud.net/tutorials/unicode.html
[2]: https://hakon.gylterud.net/tutorials/latex-maths-unicode.htm...
Does macOS have something similar to this?
The allowed set of characters would have to be vetted very carefully before I'd consider non-ASCII.
Edit: A small taste of what we can expect: https://i.imgur.com/k8S00sM.jpg
Unicode maintains a list of these characters (homographs), and Rust rejects them / warns on them.
Also, bugs caused by accidental homographs in identifier names are caught by any reasonable type system, just like any typos. As for your edit, you can't stop clever people being clever without code reviews anyway, no matter what the language.
Programming languages and standard libraries are written in English, programming terminology is invented in English, the international community adopted english as a defacto standard, almost all third party APIs expose English interfaces.
Every time I've seen a project written in my native language it's been ridiculously hard to follow because the terminology is translated in various ways (there's no official or even widely used terms because hardly anyone uses native language in SW development and CS outside of academia) and it's a mish mash between languages since everything not written in the project (along with language keywords) is written in English.
I don't usually take a hardline stances on things but this is one thing where it would be 100% a dealbreaker for working on a project.
And outside the western hemisphere where off course if the code base is unlikely cross, say, outside the Chinese borders, where Mandarin would be more appropriate.
Then there are non-industrial standard codebases, say a hobby project. I’m sure that somewhere in Guinea-Bissau there is a 17 year old learning Python at this moment, really happy that they can write Portuguese special characters as variable names in python 3 (if only the documentation had been translated to Portuguese).
Just because English is the appropriate language in by far the majority of cases, does’t mean we should deny the possibility of not using it.
I can imagine a simple remedy for this. Have the IDE suggest symbol replacements whenever the use types the name of the symbol. 'lambda' causes λ to be suggested.
Clearly full Unicode-enabled source code is the future, just look at how amazing it can be: http://baldi.me/blog/emoji-in-sql
> I have no idea why it hasn't been killed dead in new designs.
Well there is the use case that someone can't or doesn't want to learn/use english and their native language instead. These people definitely should be able to do that. For me personally, I've arrived at the conclusion that I personally do not want to code in projects in my native tongue. Simply because then a) it's only visible to a limited community and b) I constantly have to translate english concepts/ideas into the native tongue or risk having a half-english/half-german mixup. Other people might arrive at other conclusions, that's their thing and therefore they should be able to code in their native tongue and script.
This "native speakers" question is different from the question of using λ instead of lambda in an otherwise english codebase, or using Chebyshev's cyrillic name vs an ASCII transliteration. And here my opinion is different: some people may benefit from being able to code in their native tongue, but nobody has any benefits from λ or suddenly having a set of cyrillic characters as function name.
If you check the lib.rs of the project, you can see that it opts-in to nightly features. Naming anything λ is not possible in stable Rust right now. This will change though whith a newly merged RFC [1]. Fortunately, great care had been put on preventing homoglyph and mixed script attacks. My biggest issue with the change is that it's not opt-in but some future compiler release will accept non-ascii idents by default. It's easy to see why they arrived at this outcome: the entire thread is full with political ideology. According to the people in the thread, it might make someone feel excluded if they had to put #![allow(non_ascii_idents)] to lib.rs or an analogon to Cargo.toml... Seriously...
The keywords etc. are all in english anyway, an additional #![allow(non_ascii_idents)] can't be such a big issue can it.
I hope, like in java, non-ascii idents in mostly english codebases will get rejected by the Rust community as bad coding style. Fortunately there will be an option at least to forbid non-ascii identifiers without needing additional tooling and I guess I'll enable it in all of my own codebases.
I generally agree we should all be so fortunate as to be able to read the code put in front of us.
And you clearly know how to type a λ, you did it 6 times in your post.
I copy-pasted it. If there is no source to copy-paste from I'd have to use google. Don't want to be forced to do either in my code.
Also most programmers wouldn't care to implement toy projects on the lambda calculus as part of their job. This is like complaining that most doctors couldn't program their own MRI machine.
I've seen plenty of code bases which are not in English, since the developers did not have English as their first language. This is usually a well-contemplated decision - for instance, the Ruby language source code is specifically written so that you don't require knowledge of Japanese to understand the core libraries or the C reference implementation.
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.
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 ;)