caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Symbol type
@ 2010-06-28  2:15 José Romildo Malaquias
  2010-06-28  2:33 ` Michael Ekstrand
  2010-06-28  4:56 ` [Caml-list] " bluestorm
  0 siblings, 2 replies; 4+ messages in thread
From: José Romildo Malaquias @ 2010-06-28  2:15 UTC (permalink / raw)
  To: caml-list

Is there a symbol type in OCaml, with a constant time comparison
function? Something like symbols from Scheme and LISP or atoms from
Prolog. Useful in compiler construction.

Romildo


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Symbol type
  2010-06-28  2:15 Symbol type José Romildo Malaquias
@ 2010-06-28  2:33 ` Michael Ekstrand
  2010-06-28 13:37   ` [Caml-list] " Nicholas Kidd
  2010-06-28  4:56 ` [Caml-list] " bluestorm
  1 sibling, 1 reply; 4+ messages in thread
From: Michael Ekstrand @ 2010-06-28  2:33 UTC (permalink / raw)
  To: caml-list

On 06/27/2010 10:15 PM, José Romildo Malaquias wrote:
> Is there a symbol type in OCaml, with a constant time comparison
> function? Something like symbols from Scheme and LISP or atoms from
> Prolog. Useful in compiler construction.

Not directly.  As I see it, you have two decent options:

- Use/write a symbol table which "interns" symbols to integers.  The
resulting integers can be compared.
- Use/write a symbol table which interns symbols to unique string
instances, so SymTbl.intern "foo" returns the existing string object if
one already exists, and the string object passed in if it's never been
seen before.  The resulting strings can be compared with == rather than
= in constant time.

Either of these options would be fairly similar to how symbols work
under the hood in a Lisp implementation, I believe.

- Michael


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Symbol type
  2010-06-28  2:15 Symbol type José Romildo Malaquias
  2010-06-28  2:33 ` Michael Ekstrand
@ 2010-06-28  4:56 ` bluestorm
  1 sibling, 0 replies; 4+ messages in thread
From: bluestorm @ 2010-06-28  4:56 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 397 bytes --]

If your set of symbol is closed, you can use a variant type (sum type).

type symbols =
| A
| B


If you really need open symbols, you can use [polymorphic variants].

Let tag_a foo = (`A, foo)

 [polymorphic variants]
http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html#toc36


However, you won't have convenience functions such as string_of_symbol; you
would have to define them yourself.

[-- Attachment #2: Type: text/html, Size: 723 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Re: Symbol type
  2010-06-28  2:33 ` Michael Ekstrand
@ 2010-06-28 13:37   ` Nicholas Kidd
  0 siblings, 0 replies; 4+ messages in thread
From: Nicholas Kidd @ 2010-06-28 13:37 UTC (permalink / raw)
  To: Michael Ekstrand; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1118 bytes --]

On Sun, Jun 27, 2010 at 9:33 PM, Michael Ekstrand <michael@elehack.net>wrote:

> On 06/27/2010 10:15 PM, José Romildo Malaquias wrote:
> > Is there a symbol type in OCaml, with a constant time comparison
> > function? Something like symbols from Scheme and LISP or atoms from
> > Prolog. Useful in compiler construction.
>
> Not directly.  As I see it, you have two decent options:
>
> - Use/write a symbol table which "interns" symbols to integers.  The
> resulting integers can be compared.
> - Use/write a symbol table which interns symbols to unique string
> instances, so SymTbl.intern "foo" returns the existing string object if
> one already exists, and the string object passed in if it's never been
> seen before.  The resulting strings can be compared with == rather than
> = in constant time.
>
> Either of these options would be fairly similar to how symbols work
> under the hood in a Lisp implementation, I believe.
>
>

Use the Ocaml hash-consing library.

http://gallium.inria.fr/ml2006/accepted/5.html
http://www.lri.fr/~filliatr/ftp/publis/hash-consing2.pdf

Best,
-Nick

[-- Attachment #2: Type: text/html, Size: 1814 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-06-28 13:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-28  2:15 Symbol type José Romildo Malaquias
2010-06-28  2:33 ` Michael Ekstrand
2010-06-28 13:37   ` [Caml-list] " Nicholas Kidd
2010-06-28  4:56 ` [Caml-list] " bluestorm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).