caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oleg Trott <oleg_trott@columbia.edu>
To: Michael Vanier <mvanier@cs.caltech.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] extensible records again
Date: Sun, 21 Mar 2004 11:10:32 -0500	[thread overview]
Message-ID: <405DBE78.5020609@columbia.edu> (raw)
In-Reply-To: <20040321084008.429279BBA2@orchestra.cs.caltech.edu>

Michael Vanier wrote:

>>Date: Sun, 21 Mar 2004 03:08:23 -0500
>>From: Oleg Trott <oleg_trott@columbia.edu>
>>
>>
>>    
>>
>>Note that in a language like Scheme, users can not add new types other 
>>than by combining the primitives. So, I suppose you aren't really 
>>talking about language users, but those who extend the language.
>>    
>>
>
>Exactly.  The analogy I give is with a language like python, where you can
>add new types at the C level.  Of course, this can be done in e.g. PLT
>scheme or guile or perl or ruby as well.
>
>  
>
>>The obvious thing to do is to use polymorphic variants. E.g.
>>
>>(* "core language" *)
>>
>>let plus a b =
>>   match (a, b) with
>>   |  (`Int x), (`Int y) -> `Int (x + y)
>>    (*  ... *)
>>   |  _ -> failwith "runtime type error: argument is not a number"
>>
>>    
>>
>>>
>>>      
>>>
>>(* language exension example, adding "files" *)
>>
>>let open = function `File f -> open_file f | _ -> failwith "runtime type 
>>error: argument is not a file"
>>
>>
>>HTH
>>Oleg
>>
>>    
>>
>
>I don't know if I can do this.  
>
If you mean that your program won't type-check, then yes, it will, e.g.

let x = `Int 3
let y = `File f (* f is something that open_file accepts *)
let z = plus x y
let _ = open y
let _ = open (plus x z)

All of this will type check, and will give run-time "type" errors 
instead (as a dynamically typed language should)

>I have a top-level "data" type which all
>data objects must be instances of.  So in the case above, "open" would be a
>function which took an argument of type "data".  The question is then: how
>do I specify the "data" type without explicitly making it polymorphic?
>  
>
You simply don't need to declare "data" (which is trying to emulate 
Lisp's type T). In fact, in your case, I believe it's more convenient 
not to create an artificial distinction between the "core types" and 
"other types": suppose someone extended your language with "files", and 
you later decide to add "file" to your "core types". You'll have to 
redefine "data" and refactor a lot of your base code to do it, unless 
you just use polymorphic variants, as suggested (in which case there is 
no special distinction between "core types" and "other types").

HTH
Oleg

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-03-21 16:11 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-21  6:21 Michael Vanier
2004-03-21  8:08 ` Oleg Trott
2004-03-21  8:40   ` Michael Vanier
2004-03-21 16:10     ` Oleg Trott [this message]
2004-03-21 17:06       ` skaller
2004-03-21 17:36         ` Oleg Trott
2004-03-22  3:19           ` skaller
2004-03-22  7:49           ` Ville-Pertti Keinonen
2004-03-22  9:32             ` Oleg Trott
2004-03-22 10:25               ` Ville-Pertti Keinonen
2004-03-21 22:35         ` Michael Vanier
2004-03-22  3:39           ` skaller
2004-03-21 22:34       ` Michael Vanier
2004-03-22  3:31         ` skaller
2004-03-22  5:54           ` Michael Vanier
2004-03-23 19:14             ` skaller
2004-03-24  1:41               ` Jacques Garrigue
2004-03-24  8:44                 ` Julien Signoles
2004-03-24 10:04                   ` Jacques Garrigue
2004-03-21  8:53 ` Martin Jambon
2004-03-21  9:22   ` Michael Vanier
2004-03-21 17:00 ` skaller
2004-03-22  8:13 ` Achim Blumensath
2004-03-23  2:14   ` Michael Vanier
2004-03-23  7:25     ` Achim Blumensath
2004-03-31 10:05 ` Marcin 'Qrczak' Kowalczyk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=405DBE78.5020609@columbia.edu \
    --to=oleg_trott@columbia.edu \
    --cc=caml-list@inria.fr \
    --cc=mvanier@cs.caltech.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).