evincarofautumn / kitten

A statically typed concatenative systems programming language.
http://kittenlang.org/
Other
1.1k stars 42 forks source link

Backslash escaping operator sections #159

Closed sullyj3 closed 7 years ago

sullyj3 commented 8 years ago

Would it be possible to allow something like:

>>> [1,2,3,4,5] \(+ 1) map
[2,3,4,5,6]

This seems much nicer than the current way to do it:

[1,2,3,4,5] {(+ 1)} map

As well as being more consistent.

evincarofautumn commented 8 years ago

Good idea. I think it’s safe to relax \ to work on any term, so \term should always be equivalent to { term }. A bit strangely, it would make \(foo bar baz) perfectly legal syntax for { foo bar baz }, but I think that’s acceptable, because people would prefer the latter anyway.

One minor problem with this IIRC is that Emacs treats \( as matching \), not ), because of some regexp dialects. So we’d have to account for that in kitten-mode. (Editor integrations need a lot of love anyway.)

trans commented 8 years ago

Is the difference b/c it would otherwise have to be written, { 1 (+) }? Also, is \( + 2 + 1 ) valid?

evincarofautumn commented 8 years ago

Operator sections are sugar to avoid some stack operations:

This change would just make e.g. \(+ 1) valid and equivalent to { (+ 1) }, because \foo is already valid and equivalent to { foo }.

(+ 2 + 1) is currently valid, and equivalent to (+ (2 + 1)) or (+ 3), but that’s kind of a bug because it can lead to misleading precedence.

trans commented 8 years ago

I've given it some thought, but I don't see the common pattern.

What's the rule of translation? How does the swap come into it?

evincarofautumn commented 8 years ago

Operator sections are just partially applied operators. For some operator @:

So while (+ 1) is a function that adds 1 to its argument, { (+ 1) } (or, with this proposal, \(+ 1)) is a function that pushes that function to the stack.

evincarofautumn commented 7 years ago

Fixed in 5552773d45238e53ab726a423c28abeb3c395147.