Browse thread
[Caml-list] Does this function exist?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | John Prevost <j.prevost@c...> |
| Subject: | Re: [Caml-list] Does this function exist? |
>>>>> "ll" == Lukasz Lew <ll189417@students.mimuw.edu.pl> writes:
ll> OK, so I want just "[3; 3]", (no type information),
Doesn't matter--you still need type information. Otherwise, you'll
get things like:
type foo = A | B | C
print [A; B; C]
outputs: [0; 1; 2]
or
print ["a"; "b"; "c"]
outputs: [<ref>; <ref>; <ref>]
Since the runtime can't tell the difference between integers and
abstract sum types in the first case and can't tell what a pointer is
pointing to in the second case.
Take a look at what you can see in the O'Caml debugger--in some cases
it can tell you the values of things. In many cases, you're in a
polymorphioc function and all it says is "<poly>" for a value, because
it doesn't know how to display the value.
Here's an example of that:
test.ml
------------------------------------------------------------------------
let rec map f = function
| [] -> []
| h::t -> (f h)::(map f t)
type t = A | B | C | D | E
let i1 = [1; 2; 3; 4; 5]
let i2 = [A; B; C; D; E]
let i3 = ["a"; "b"; "c"]
let o1 = map (fun x -> x) i1
let o2 = map (fun x -> x) i2
let o3 = map (fun x -> x) i3
------------------------------------------------------------------------
$ ocamldebug a.out
Objective Caml Debugger version 3.04
(ocd) run
Loading program... done.
Time : 82
Program exit.
(ocd) break Test.map
Breakpoint 1 at 5008 : file Test, line 1 column 13
(ocd) goto 0
Time : 0
Beginning of program.
(ocd) run
Time : 12 - pc : 5040 - module Test
Breakpoint : 1
3 | h::t -> <|b|>(f h)::(map f t)
(ocd) print h
h : 'a = <poly>
(ocd) print t
t : 'a list = [<poly>; <poly>; <poly>; <poly>]
(ocd) bt
#0 Pc : 5040 Test char 50
#1 Pc : 5196 Test char 200
(ocd) up
#1 Pc : 5196 Test char 200
11 let o1 = map (fun x -> x) i1<|a|>
(ocd) print i1
i1 : int list = [1; 2; 3; 4; 5]
Here we see that when we're inside the polymorphic function, the type
information is lost (even though we're in debugging mode.) Only by
going up the stack to a point where the type of the value is known can
we learn anything.
John.
-------------------
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