caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re:  Constructor/reference bug?
@ 1999-08-12 12:05 Damien Doligez
  0 siblings, 0 replies; 6+ messages in thread
From: Damien Doligez @ 1999-08-12 12:05 UTC (permalink / raw)
  To: caml-list, skaller

>From: John Skaller <skaller@maxtal.com.au>

>let g x = 
>let lineno = ref (1,"") in
>let rec f x' = match x' with
>| NEWLINE p :: t -> lineno := p; f t
>| COLON :: t     -> CTRL !lineno :: f t
>| h :: t -> h :: f t
>| [] -> []
>in f x;;
>
>The code doesn't work as I expected: every
>CTRL value refers to the same lineno, the last one.
>If I change the COLON line to read:
>
>| COLON :: t -> let x = !lineno in CTRL x :: f t
>
>the code works as expected -- the CTRL refers to the
>value of lineno at the time the last NEWLINE was processed.
>
>What's happening? Are coproduct constructors lazy?


You have to be careful because f is a function with side effects.

In the line

    | COLON :: t     -> CTRL !lineno :: f t

the subexpression "!lineno" is not guaranteed to be evaluated before
"f t".  The current implementation happens to evaluate "f t" first.
Using "let x = !lineno in ..." is the right way to write your
function.

-- Damien




^ permalink raw reply	[flat|nested] 6+ messages in thread
* Constructor/reference bug?
@ 1999-08-11  8:05 John Skaller
  1999-08-12 10:51 ` Nicolas Ollinger
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: John Skaller @ 1999-08-11  8:05 UTC (permalink / raw)
  To: caml-list

I have some code like:

let g x = 
let lineno = ref (1,"") in
let rec f x' = match x' with
| NEWLINE p :: t -> lineno := p; f t
| COLON :: t     -> CTRL !lineno :: f t
| h :: t -> h :: f t
| [] -> []
in f x;;

The code doesn't work as I expected: every
CTRL value refers to the same lineno, the last one.
If I change the COLON line to read:

| COLON :: t -> let x = !lineno in CTRL x :: f t

the code works as expected -- the CTRL refers to the
value of lineno at the time the last NEWLINE was processed.

What's happening? Are coproduct constructors lazy?

[I tested some simpler samples by hand and my tests
worked as expected, so I'm confused]


-------------------------------------------------------
John Skaller    email: skaller@maxtal.com.au
		http://www.maxtal.com.au/~skaller
		phone: 61-2-96600850
		snail: 10/1 Toxteth Rd, Glebe NSW 2037, Australia





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

end of thread, other threads:[~1999-08-12 14:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-08-12 12:05 Constructor/reference bug? Damien Doligez
  -- strict thread matches above, loose matches on Subject: below --
1999-08-11  8:05 John Skaller
1999-08-12 10:51 ` Nicolas Ollinger
1999-08-12 11:22 ` Jean-Christophe Filliatre
1999-08-12 11:25 ` Andreas Rossberg
1999-08-12 12:16 ` Markus Mottl

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).