From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=HTML_MESSAGE, UNPARSEABLE_RELAY autolearn=disabled version=3.1.3 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 0C517BC6C for ; Fri, 23 Nov 2007 16:50:25 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAHeFRkfAXQImh2dsb2JhbACCPTaMQQEBAQgKKVA/ X-IronPort-AV: E=Sophos;i="4.21,458,1188770400"; d="scan'208";a="4546970" Received: from discorde.inria.fr ([192.93.2.38]) by mail2-smtp-roc.national.inria.fr with ESMTP; 23 Nov 2007 16:50:24 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id lANFoMD0019193 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 23 Nov 2007 16:50:24 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAALKFRkegU01qnmdsb2JhbACCPTaMQQEBAQEHBAYRGFA/ X-IronPort-AV: E=Sophos;i="4.21,458,1188770400"; d="scan'208";a="4835038" Received: from nyginimrp10.us.db.com ([160.83.77.106]) by mail1-smtp-roc.national.inria.fr with ESMTP; 23 Nov 2007 16:50:21 +0100 Received: from sdbo1006.db.com by nyginimrp10.us.db.com id lANFoEFO010333; Fri, 23 Nov 2007 10:50:15 -0500 In-Reply-To: <4746A860.3000106@fmf.uni-lj.si> To: Andrej.Bauer@andrej.com Cc: Caml , caml-list-bounces@yquem.inria.fr Subject: Re: [Caml-list] Local references in Ocaml vs. state in Haskell MIME-Version: 1.0 X-Mailer: Lotus Notes Release 6.5.5 CCH1 March 07, 2006 From: Jeff Polakow Message-ID: Date: Fri, 23 Nov 2007 10:50:07 -0500 X-MIMETrack: Serialize by Router on sdbo1006/DBNA/DeuBaInt/DeuBa(Release 6.5.5FP1|April 11, 2006) at 11/23/2007 10:50:15 AM, Serialize complete at 11/23/2007 10:50:15 AM Content-Type: multipart/alternative; boundary="=_alternative 0056FD238525739C_=" X-Miltered: at discorde with ID 4746F6BE.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 haskell:01 polakow:01 polakow:01 ocaml:01 haskell:01 andrej:01 beginner's:01 bug:01 andrej:01 beginner's:01 bug:01 sans-serif:98 sans-serif:98 lexical:01 This is a multipart message in MIME format. --=_alternative 0056FD238525739C_= Content-Type: text/plain; charset="US-ASCII" 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. --=_alternative 0056FD238525739C_= Content-Type: text/html; charset="US-ASCII"
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.
--=_alternative 0056FD238525739C_=--