caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Local references in Ocaml vs. state in Haskell
@ 2007-11-23 10:16 Andrej Bauer
  2007-11-23 15:50 ` [Caml-list] " Jeff Polakow
  0 siblings, 1 reply; 2+ messages in thread
From: Andrej Bauer @ 2007-11-23 10:16 UTC (permalink / raw)
  To: Caml

I have some programs written in ocaml that use references. I wanted to 
translate them to Haskell, but since I am not an active Haskell user, I 
got stuck with a very simple problem, namely, that the state monad in 
Haskell provides _global_ references while ocaml has _local_ references.

Suppose in ocaml I have this:

(* Does a functional [f] look at its argument [a]? *)
let touched f a =
   let flag = ref false in
   let _ = f (fun n -> flag := true; a n) in
     !flag

"touched f a" evaluates "f a" and records the fact that f actually 
evaluated a at some argument. Note that flag is a local reference, so f 
does not have access to it.

Using a global flag does not work:

let flag = ref false

let touched' f a =
   flag := false ;
   let _ = f (fun n -> flag := true; a n) in
     !flag

Now we can write an f which touches its argument but fools touched' by 
reversing the value of flag.

let f a =
   let y = a 42 in
     touched := false ; y

If I try to use the State monad I will get a global reference threaded 
thrugh the computation, and f will have access to it. I want a local 
reference that is inaccessible outside its lexical scope. I am probably 
missing something obvious about Haskell.

I am hoping that even though this is a question about Haskell, you won't 
mind, as it gives everyone on this list an opportunity to show how ocaml 
is superior to Haskell. Just kidding :-)

Andrej


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

* Re: [Caml-list] Local references in Ocaml vs. state in Haskell
  2007-11-23 10:16 Local references in Ocaml vs. state in Haskell Andrej Bauer
@ 2007-11-23 15:50 ` Jeff Polakow
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Polakow @ 2007-11-23 15:50 UTC (permalink / raw)
  To: Andrej.Bauer; +Cc: Caml, caml-list-bounces

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

Hello,

  The ocaml code can be directly translatedto Haskell using the ST monad 
and STRef.

-Jeff


caml-list-bounces@yquem.inria.fr wrote on 11/23/2007 05:16:00 AM:

> I have some programs written in ocaml that use references. I wanted to 
> translate them to Haskell, but since I am not an active Haskell user, I 
> got stuck with a very simple problem, namely, that the state monad in 
> Haskell provides _global_ references while ocaml has _local_ references.
> 
> Suppose in ocaml I have this:
> 
> (* Does a functional [f] look at its argument [a]? *)
> let touched f a =
>    let flag = ref false in
>    let _ = f (fun n -> flag := true; a n) in
>      !flag
> 
> "touched f a" evaluates "f a" and records the fact that f actually 
> evaluated a at some argument. Note that flag is a local reference, so f 
> does not have access to it.
> 
> Using a global flag does not work:
> 
> let flag = ref false
> 
> let touched' f a =
>    flag := false ;
>    let _ = f (fun n -> flag := true; a n) in
>      !flag
> 
> Now we can write an f which touches its argument but fools touched' by 
> reversing the value of flag.
> 
> let f a =
>    let y = a 42 in
>      touched := false ; y
> 
> If I try to use the State monad I will get a global reference threaded 
> thrugh the computation, and f will have access to it. I want a local 
> reference that is inaccessible outside its lexical scope. I am probably 
> missing something obvious about Haskell.
> 
> I am hoping that even though this is a question about Haskell, you won't 

> mind, as it gives everyone on this list an opportunity to show how ocaml 

> is superior to Haskell. Just kidding :-)
> 
> Andrej
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


---

This e-mail may contain confidential and/or privileged information. If you 
are not the intended recipient (or have received this e-mail in error) 
please notify the sender immediately and destroy this e-mail. Any 
unauthorized copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.

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

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

end of thread, other threads:[~2007-11-23 15:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-23 10:16 Local references in Ocaml vs. state in Haskell Andrej Bauer
2007-11-23 15:50 ` [Caml-list] " Jeff Polakow

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