Library defined literals

View: New views
13 Messages — Rating Filter:   Alert me  

Library defined literals

by Walter Smith :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

I understand that the XML support in Scala is for the most part just libraries... all but one exception: XML literals have to be supported directly by the compiler.

Has there been some thoughts about letting libraries define literals, somewhat similar to implicit conversions? Not only XML literals, but numbers, strings, etc. just as well?

For JDK 7 they discuss allowing underscores within numeric literals to group them, and binary number literals, and list, set, and map literals. Scala can do the latter already... and not as literals but as library defined methods! If it was possible to define your own literals, DSLs could get even more natural: Writing 25º18'25" would be very regular for nautical applications. Not to mention a lot of mathematical, physical, or chemical symbols that exist in the Unicode character set.

I'm quite sure anyone involved with writing compilers will start to shiver at the mere thought, but maybe it could turn out just as elegant and never-to-have-been-imagined-before like implicit conversions do. I think it would make the language even more regular... but maybe I'm wrong.

Just 2 ct.
Kind regards
Walter

Re: Library defined literals

by Arrgh :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Walter Smith wrote:
> I understand that the XML support in Scala is for the most part just
> libraries... all but one exception: XML literals have to be supported
> directly by the compiler.
>
> Has there been some thoughts about letting libraries define literals,
> somewhat similar to implicit conversions? Not only XML literals, but
> numbers, strings, etc. just as well?
>  
I'm not aware of much serious discussion--let alone work--on the idea
since this thread way back in Dec. 2007:  
http://www.nabble.com/-scala--JavaPolis-Interview-tc14437381.html#a14447135

> For JDK 7 they discuss allowing underscores within numeric literals to group
> them, and binary number literals, and list, set, and map literals. Scala can
> do the latter already... and not as literals but as library defined methods!
> If it was possible to define your own literals, DSLs could get even more
> natural: Writing 25º18'25" would be very regular for nautical applications.
> Not to mention a lot of mathematical, physical, or chemical symbols that
> exist in the Unicode character set.
>  
A lot of Unicode characters are already perfectly valid in Scala
identifiers, but you'll run into problems pretty quickly when you try to
use a syntax that's idiomatic in its own problem domain but not very
similar to Scala's syntax, as flexible as that can be.
> I'm quite sure anyone involved with writing compilers will start to shiver
> at the mere thought, but maybe it could turn out just as elegant and
> never-to-have-been-imagined-before like implicit conversions do. I think it
> would make the language even more regular... but maybe I'm wrong.
>  
Doing the simplest thing that could possibly work (e.g. think up and
implement an escape syntax, build a framework that allows
Tuple2[Pattern, Function1[String, DSLNode]] or somesuch to be registered
with the compiler) is probably not a huge undertaking (he said glibly)
but there would no doubt be some very tricky bits, e.g. embedded Scala
expressions <like>{this}</like>.

-0xe1a

Re: Library defined literals

by Grey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Not intended to hijack the thread, but for me the below mentioned Unicode capability has led to a number of interesting ends.  All kinds of simple 1 or 2 char symbolic methods (various arrow and math symbols) for example can be defined.  Greek letter markers, such as marking all side-effecting method names to end in a capital Delta, referential methods with Chi.  Its all too easy to take it too an extreme, and its way too much fun.

If you haven't tried to use the full pallet of unicode alphabets and symbols in your code yet, give it a whirl.  For fun.  

Interesting if some of Scala's reserved symbols were legally aliasable with their equivalent unicode character.

On Tue, Jun 9, 2009 at 3:53 PM, Alex Cruise <alex@...> wrote:
    ...
A lot of Unicode characters are already perfectly valid in Scala identifiers, but you'll run into problems pretty quickly when you try to use a syntax that's idiomatic in its own problem domain but not very similar to Scala's syntax, as flexible as that can be.
  ..

Re: Library defined literals

by Arrgh :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ray Racine wrote:
> Interesting if some of Scala's reserved symbols were legally aliasable
> with their equivalent unicode character.
Yes, that would be interesting indeed. :)

def f(n: ⇒ Int) = for (x ← 1 to n) yield Map(x → ("*" * x))

-0xe1a

Re: Library defined literals

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jun 09, 2009 at 02:59:02PM -0700, Alex Cruise wrote:
> Yes, that would be interesting indeed. :)
>
> def f(n: ⇒ Int) = for (x ← 1 to n) yield Map(x → ("*" * x))

Stranger yet would be if the language included some out of the box:

scala> for (x \u2190 1 to 10) yield x
res0: scala.collection.generic.VectorView[Int,Vector[_]] = RangeM(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> (x: Int) \u21D2 x + 1
res1: (Int) => Int = <function>

--
Paul Phillips      | All men are frauds.  The only difference between
Apatheist          | them is that some admit it.  I myself deny it.
Empiricist         |     -- H. L. Mencken
i pull his palp!   |----------* http://www.improving.org/paulp/ *----------

Re: Library defined literals

by Arrgh :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Paul Phillips wrote:

> On Tue, Jun 09, 2009 at 02:59:02PM -0700, Alex Cruise wrote:
>  
>> Yes, that would be interesting indeed. :)
>>
>> def f(n: ⇒ Int) = for (x ← 1 to n) yield Map(x → ("*" * x))
>>    
> Stranger yet would be if the language included some out of the box:
>
> scala> for (x \u2190 1 to 10) yield x
> res0: scala.collection.generic.VectorView[Int,Vector[_]] = RangeM(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>
> scala> (x: Int) \u21D2 x + 1
> res1: (Int) => Int = <function>
>  
I even debated posting the output but then decided it'd be fun for
people to try it out themselves. :/

-0xe1a



Re: Library defined literals

by Grey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

For what its worth you can do this with emacs using compose region.  You tell emacs to display a substitute for a string in a buffer.

For example, lets say you want to display ≤ where ever the scala source code contains '<' followed by '=', ie. <=.

(defconst scala-less-equal-char (decode-char 'ucs #x2264))

(defconst scala-key-words
'("\\(<=\\)"
1
(progn (compose-region (match-beginning 1)
(match-end 1)
scala-less-equal-char)
nil)))

(font-lock-add-keywords nil scala-key-words)

One can do a number of interesting things.  All 'var' keywords are in red, scala-dangerous-face. 

⊤⊥ for Any and Unit.
¬ for 'not'
∧ ∨ for &&, ||

Underneath the source file doesn't change an iota, its only in how it is displayed in the emacs editor, so the file compiles clean.

Ray

P.S. Unable to currently test the above, but its the right idea.  I don't have access to my system at the moment.

On Tue, Jun 9, 2009 at 5:59 PM, Alex Cruise <alex@...> wrote:
Ray Racine wrote:
Interesting if some of Scala's reserved symbols were legally aliasable with their equivalent unicode character.
Yes, that would be interesting indeed. :)

def f(n: ⇒ Int) = for (x ← 1 to n) yield Map(x → ("*" * x))

-0xe1a


Re: Library defined literals

by Ricky Clarkson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you use this to do more advanced things like make new F<String,
Integer>() { public Integer f(String s) { return s.length(); } } look
like s => s.length() ?

2009/6/10 Ray Racine <ray.racine@...>:

> For what its worth you can do this with emacs using compose region.  You
> tell emacs to display a substitute for a string in a buffer.
>
> For example, lets say you want to display ≤ where ever the scala source code
> contains '<' followed by '=', ie. <=.
>
> (defconst scala-less-equal-char (decode-char 'ucs #x2264))
>
> (defconst scala-key-words
>   '("\\(<=\\)"
>      1
>
>      (progn (compose-region (match-beginning 1)
>        (match-end 1)
>        scala-less-equal-char)
>    nil)))
>
> (font-lock-add-keywords nil scala-key-words)
>
> One can do a number of interesting things.  All 'var' keywords are in red,
> scala-dangerous-face.
>
> ⊤⊥ for Any and Unit.
> ¬ for 'not'
> ∧ ∨ for &&, ||
>
> Underneath the source file doesn't change an iota, its only in how it is
> displayed in the emacs editor, so the file compiles clean.
>
> Ray
>
> P.S. Unable to currently test the above, but its the right idea.  I don't
> have access to my system at the moment.
>
> On Tue, Jun 9, 2009 at 5:59 PM, Alex Cruise <alex@...> wrote:
>>
>> Ray Racine wrote:
>>>
>>> Interesting if some of Scala's reserved symbols were legally aliasable
>>> with their equivalent unicode character.
>>
>> Yes, that would be interesting indeed. :)
>>
>> def f(n: ⇒ Int) = for (x ← 1 to n) yield Map(x → ("*" * x))
>>
>> -0xe1a
>
>

Re: Library defined literals

by Grey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You can match a desired region by a regexp, and since a given region is substituted by compose-region as described below you can do darn near whatever you want.  For example, emacs allows for inlined base-64 encoded images, you could have it display Miss June if you'd like and it would still compile.

(defun compose-region (start end &optional components modification-func)
  "Compose characters in the current region.

Characters are composed relatively, i.e. composed by overstricking or
stacking depending on ascent, descent and other properties.

When called from a program, expects these four arguments.

First two arguments START and END are positions (integers or markers)
specifying the region.

Optional 3rd argument COMPONENTS, if non-nil, is a character, a string
or a vector or list of integers and rules.

If it is a character, it is an alternate character to display instead
of the text in the region.

If it is a string, the elements are alternate characters.  In
this case, TAB element has a special meaning.  If the first
characer is TAB, the glyphs are displayed with left padding space
so that no pixel overlaps with the previous column.  If the last
character is TAB, the glyphs are displayed with rigth padding
space so that no pixel overlaps with the following column.

If it is a vector or list, it is a sequence of alternate characters and
composition rules, where (2N)th elements are characters and (2N+1)th
elements are composition rules to specify how to compose (2N+2)th
elements with previously composed N glyphs.

A composition rule is a cons of global and new glyph reference point
symbols.  See the documentation of `reference-point-alist' for more
detail.

Optional 4th argument MODIFICATION-FUNC is a function to call to
adjust the composition when it gets invalid because of a change of
text in the composition."
  (interactive "r")

On Wed, Jun 10, 2009 at 11:45 AM, Ricky Clarkson <ricky.clarkson@...> wrote:
Can you use this to do more advanced things like make new F<String,
Integer>() { public Integer f(String s) { return s.length(); } } look
like s => s.length() ?

2009/6/10 Ray Racine <ray.racine@...>:
> For what its worth you can do this with emacs using compose region.  You
> tell emacs to display a substitute for a string in a buffer.
>
> For example, lets say you want to display ≤ where ever the scala source code
> contains '<' followed by '=', ie. <=.
>
> (defconst scala-less-equal-char (decode-char 'ucs #x2264))
>
> (defconst scala-key-words
>   '("\\(<=\\)"
>      1
>
>      (progn (compose-region (match-beginning 1)
>        (match-end 1)
>        scala-less-equal-char)
>    nil)))
>
> (font-lock-add-keywords nil scala-key-words)
>
> One can do a number of interesting things.  All 'var' keywords are in red,
> scala-dangerous-face.
>
> ⊤⊥ for Any and Unit.
> ¬ for 'not'
> ∧ ∨ for &&, ||
>
> Underneath the source file doesn't change an iota, its only in how it is
> displayed in the emacs editor, so the file compiles clean.
>
> Ray
>
> P.S. Unable to currently test the above, but its the right idea.  I don't
> have access to my system at the moment.
>
> On Tue, Jun 9, 2009 at 5:59 PM, Alex Cruise <alex@...> wrote:
>>
>> Ray Racine wrote:
>>>
>>> Interesting if some of Scala's reserved symbols were legally aliasable
>>> with their equivalent unicode character.
>>
>> Yes, that would be interesting indeed. :)
>>
>> def f(n: ⇒ Int) = for (x ← 1 to n) yield Map(x → ("*" * x))
>>
>> -0xe1a
>
>


Re: Library defined literals

by Walter Smith :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Grey wrote:
Not intended to hijack the thread, but for me the below mentioned Unicode
capability has led to a number of interesting ends.  All kinds of simple 1
or 2 char symbolic methods (various arrow and math symbols) for example can
be defined.
Well, you did, but maybe not as much as you have thought ;-)

The thing that I had been thinking about before I came up with the library defined literals was using the superscript and subscript numbers (\u2080 etc.) like they are often used in science, but I hadn't found any regular way for Scala to allow their use for libraries. Maybe we all just think too much in programming languages already, so we can't see the simplicity mathematical expressions could offer to a programming language. Maybe we are too much settled on using plain text editors, too.

Walter

Re: Library defined literals

by Arthur Peters :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As I read this thread I keep thinking, "That would be great, .... if I could type that character on my keyboard".

How do you guys go about typing the special characters?

-Arthur


On Wed, Jun 10, 2009 at 4:25 PM, Walter Smith <walter.smith@...> wrote:


Grey wrote:
>
> Not intended to hijack the thread, but for me the below mentioned Unicode
> capability has led to a number of interesting ends.  All kinds of simple 1
> or 2 char symbolic methods (various arrow and math symbols) for example
> can
> be defined.

Well, you did, but maybe not as much as you have thought ;-)

The thing that I had been thinking about before I came up with the library
defined literals was using the superscript and subscript numbers (\u2080
etc.) like they are often used in science, but I hadn't found any regular
way for Scala to allow their use for libraries. Maybe we all just think too
much in programming languages already, so we can't see the simplicity
mathematical expressions could offer to a programming language. Maybe we are
too much settled on using plain text editors, too.

Walter
--
View this message in context: http://www.nabble.com/Library-defined-literals-tp23949477p23970321.html
Sent from the Scala - Debate mailing list archive at Nabble.com.



Re: Library defined literals

by Viktor Klang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



10 jun 2009 kl. 22.32 Arthur Peters <arthur.peters@...> skrev:

As I read this thread I keep thinking, "That would be great, .... if I could type that character on my keyboard".

How do you guys go about typing the special characters?

A Scala keymap?  Key combos? (=> becomes the arrow, ^2 becomes superscript 2 etc) Would be a nice experiment.

Viktor



-Arthur


On Wed, Jun 10, 2009 at 4:25 PM, Walter Smith <walter.smith@...> wrote:


Grey wrote:
>
> Not intended to hijack the thread, but for me the below mentioned Unicode
> capability has led to a number of interesting ends.  All kinds of simple 1
> or 2 char symbolic methods (various arrow and math symbols) for example
> can
> be defined.

Well, you did, but maybe not as much as you have thought ;-)

The thing that I had been thinking about before I came up with the library
defined literals was using the superscript and subscript numbers (\u2080
etc.) like they are often used in science, but I hadn't found any regular
way for Scala to allow their use for libraries. Maybe we all just think too
much in programming languages already, so we can't see the simplicity
mathematical expressions could offer to a programming language. Maybe we are
too much settled on using plain text editors, too.

Walter
--
View this message in context: http://www.nabble.com/Library-defined-literals-tp23949477p23970321.html
Sent from the Scala - Debate mailing list archive at Nabble.com.



Re: Library defined literals

by Derek Chen-Becker-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Viktor Klang wrote:
>> How do you guys go about typing the special characters?
>
> A Scala keymap?  Key combos? (=> becomes the arrow, ^2 becomes
> superscript 2 etc) Would be a nice experiment.
>

I already posted instructions for setting up a custom Scala XKB mapping
for the following (compose key is right alt on my keyboard):

compose key + = : ⇒
compose key + < : ←
compose key + > : →

With XKB you can make just about any combo of compose key + something
translate to any unicode character.

Derek