[racket] polymorphic functions

306 views
Skip to first unread message

Răzvan Rotaru

unread,
Oct 11, 2012, 9:20:09 AM10/11/12
to us...@racket-lang.org
Hi,

I realized recently that one thing I miss in Racket are polymorphic core functions (car, cdr, map, etc.). So I was wondering what it would take to make them polymorphic. What is the best way to implement a polymorphic function in Racket? Any ideas, suggestions and critics are mighty welcome. Thanks.

Razvan

Matthias Felleisen

unread,
Oct 11, 2012, 9:38:59 AM10/11/12
to Răzvan Rotaru, us...@racket-lang.org

What do you mean by polymorphic?

parametric polymorphism: this is a type-level concept with no true material counterpart on the dynamic level. Since Racket is untyped, you can't ask for this. Naturally Typed Racket comes with first-class polymorphic functions.

ad hoc polymorphism, aka generics (the better word to avoid confusion): if you want (length '(1 2 4)) == (length "123") == (length (vector 1 2 3)), then the historical response is that such functions violates the carefully chosen middl ground of Scheme, which wants the readers (of code) to understand from the function length, string-length, vector-length that the program is currently dealing with lists, strings, or vectors.

Having said that, we recognize the need to support generic interfaces to new data structures. While this is still an experiment, you may wish to look at

http://pre.racket-lang.org/docs/html/reference/struct-generics.html?q=generic

Keep watching for more ideas in this direction but don't hold your breath -- Matthias
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users

Greg Hendershott

unread,
Oct 11, 2012, 4:34:09 PM10/11/12
to Răzvan Rotaru, us...@racket-lang.org
I'm also not sure exactly what you mean by "polymorphic". Matthias
talked about generics.

When you mention "car, cdr, map, etc.", I wonder if you might want to
know about sequences, iterations, and comprehensions.

(define a-list (list 1 2 3))
(define a-vector (vector 1 2 3))

The following will work if `seq' is either one:

(for ([x seq])
... do something with x ...)

[Although if you know it will always be a specific kind of sequence,
you can use a "hint" to make it faster:

(for ([x (in-list a-list)]) ...
(for ([x (in-vector a-vector)]) ...]

There are many more kinds of sequences besides lists and vectors, and
you can define your own.

More info:
Sequences: http://pre.racket-lang.org/docs/html/reference/sequences.html
Iterations and comprehensions:
http://pre.racket-lang.org/docs/html/guide/for.html


P.S. IIUC, sequences recently were reimplemented using generics. So I
suppose I'm talking about a specific facet of what Matthias was
already talking about.

P.P.S. My background in C++ made me think I would miss "polymorphism"
in the sense of class inheritance. It turned out I didn't miss it at
all. Although if I did miss it, Racket does have classes and objects.

Răzvan Rotaru

unread,
Oct 13, 2012, 4:18:27 PM10/13/12
to Greg Hendershott, us...@racket-lang.org
Hi,


> ad hoc polymorphism, aka generics (the better word to avoid confusion): if you want (length '(1 2 4)) == (length "123") == (length (vector 1 2 3)),
> then the historical response is that such functions violates the carefully chosen middl ground of Scheme, which wants the readers (of code) to
> understand from the function length, string-length, vector-length that the program is currently dealing with lists, strings, or vectors.

Thanks for the quick answer to Matthias. I read it shortly after it was sent, but I did not have the time to reply. What I mean by polymorphism is what Matthias described as generics. So I am speaking about length applied to anything that is a collection of things.

Somehow I fail to see the more pros than cons for this "middle ground choice", but I don't intend to criticize it. I want explore whether I can change the implementation of the core functions to make them generic. Say I want to create a new language "generic scheme". :)

@Greg, I know sequences and the functions around it, and they are indeed part of what I'm looking for. Essentially, I am speaking about replacing the core scheme functions, which work with lists, with generic versions that work with sequences. Before plunging into it, I would like to know other oppinions (whether it's possible and/or what are the implications, drawbacks).

Regarding the generic interfaces that are currently experimental, I can imagine this is a much awaited feature in racket. I wonder whether performance will be an aspect that will be considered or not. As far as I know this kind of functionality is usually implemented at a low level (in the VM). I will keep an eye on the progress. :)




Asumu Takikawa

unread,
Oct 15, 2012, 1:59:51 AM10/15/12
to Răzvan Rotaru, us...@racket-lang.org
On 2012-10-13 23:18:27 +0300, Răzvan Rotaru wrote:
> @Greg, I know sequences and the functions around it, and they are indeed
> part of what I'm looking for. Essentially, I am speaking about replacing
> the core scheme functions, which work with lists, with generic versions
> that work with sequences. Before plunging into it, I would like to know
> other oppinions (whether it's possible and/or what are the implications,
> drawbacks).

Several of us have thought about this, especially in relation to the new
generics feature that is in Racket 5.3. In particular, Vincent has an
experiment with "buildable sequences" on github that implements nice
generic operations like map/g:
https://github.com/stamourv/buildable-sequences

Potentially, the map/g from this example could replace the list map as
the standard mapping function. Whether Racket will actually do that is a
design question that we don't yet have a consensus on.

Cheers,
Asumu

Reply all
Reply to author
Forward
0 new messages