Article View: comp.lang.functional
Article #493Re: A question about types in ML
From: Ascander Suarez
Date: Mon, 12 Nov 1990 17:55
Date: Mon, 12 Nov 1990 17:55
54 lines
2213 bytes
2213 bytes
In article <4906@rex.cs.tulane.edu>, fs@caesar.cs.tulane.edu (Frank
Silbermann) writes:
|> ...
|> One might seek to create a union type which
|> contains within it _all_ possible types
|> (i.e. as the solution to a recursive type equation).
|> Unfortunately this violates the requirement
|> that all type signatures be finite constructions
|> from the primitive types.
|>
Although not in the standard, some versions of ML features the dynamic
type. This type can be used to implement heterogeneous lists of
values whose types are encapsulated as dynamics.
A dynamic value can be seen as a pair (value, type of the value);
ex (in caml V2-6):
#let x = dynamic 3 and f = dynamic (fun x -> x);;
Value x = (dynamic (3 : num)) : dyn
Value f = (dynamic (<fun> : ('a -> 'a))) : dyn
#let list = [f;x];;
Value list = [(dynamic (<fun> : ('a -> 'a))); (dynamic (3 : num))] :
dyn list
#let classifyDyn = function
# dynamic (b:bool) -> "bool"
# | dynamic (n:num) -> "num"
# | dynamic (l:'a list) -> "list"
# | dynamic (f:'a ->'a) -> "id"
# | _ -> "??";;
Value classifyDyn = <fun> : (dyn -> string)
#map classifyDyn list;;
["id"; "num"] : string list
Of course there is one restriction related to the use of the dynamic
constructor: It can't be used in local expressions to encapsulate
a value whose type depends on the environment:
Ex:
#let g x = dynamic x;;
line 1: cannot generalize type 'a of variable x
for dynamic expression: dynamic x
1 error in typechecking
Typecheck Failed
- Ascander Suarez
suarez@decprl.dec.com
Technical Details
Message-ID:
<1990Nov12.175545.28356@prl.dec.com>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!decwrl!bacchus.pa.dec.com!decprl!decprl!suarezReferences:
<paula.658247771@shum> <fraser.658398987@melomys> <4906@rex.cs.tulane.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!decwrl!bacchus.pa.dec.com!decprl!decprl!suarez From: suarez@prl.dec.com (Ascander Suarez) Newsgroups: comp.lang.functional Subject: Re: A question about types in ML Keywords: dynamic types, heterogeneus list Message-ID: <1990Nov12.175545.28356@prl.dec.com> Date: 12 Nov 90 17:55:45 GMT References: <paula.658247771@shum> <fraser.658398987@melomys> <4906@rex.cs.tulane.edu> Sender: news@prl.dec.com (USENET News System) Reply-To: suarez@decprl.dec.com (Ascander Suarez) Distribution: comp Organization: Digital Equipment Corporation - Paris Research Laboratory Lines: 54