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.7 required=5.0 tests=AWL,SPF_SOFTFAIL 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 51C83BC69 for ; Fri, 23 Nov 2007 11:15:04 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAPI2RkfAXQImh2dsb2JhbACPNgEBAQgKKYEP X-IronPort-AV: E=Sophos;i="4.21,455,1188770400"; d="scan'208";a="4535011" Received: from discorde.inria.fr ([192.93.2.38]) by mail2-smtp-roc.national.inria.fr with ESMTP; 23 Nov 2007 11:14:44 +0100 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id lANAEg3Q003897 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 23 Nov 2007 11:14:44 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao8CALc2RkfBAkMt/2dsb2JhbACRAw X-IronPort-AV: E=Sophos;i="4.21,455,1188770400"; d="scan'208";a="6103482" Received: from vega.fmf.uni-lj.si (HELO postar.fmf.uni-lj.si) ([193.2.67.45]) by mail3-smtp-sop.national.inria.fr with ESMTP; 23 Nov 2007 11:14:26 +0100 Received: from localhost (unknown [192.168.5.1]) by postar.fmf.uni-lj.si (Postfix) with ESMTP id CE2DC1FA026 for ; Fri, 23 Nov 2007 11:14:24 +0100 (CET) X-Virus-Scanned: amavisd-new at spam.fmf.uni-lj.si Received: from postar.fmf.uni-lj.si ([192.168.5.5]) by localhost (spam.fmf.uni-lj.si [192.168.5.1]) (amavisd-new, port 10024) with ESMTP id Fq8W2SrPLRSh for ; Fri, 23 Nov 2007 11:14:22 +0100 (CET) Received: from [193.2.67.88] (unknown [193.2.67.88]) by postar.fmf.uni-lj.si (Postfix) with ESMTP id 429F71FA022 for ; Fri, 23 Nov 2007 11:14:22 +0100 (CET) Message-ID: <4746A860.3000106@fmf.uni-lj.si> Date: Fri, 23 Nov 2007 11:16:00 +0100 From: Andrej Bauer Reply-To: Andrej.Bauer@andrej.com User-Agent: Thunderbird 2.0.0.6 (X11/20071022) MIME-Version: 1.0 To: Caml Subject: Local references in Ocaml vs. state in Haskell Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 4746A812.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; andrej:01 andrej:01 ocaml:01 haskell:01 ocaml:01 haskell:01 lexical:01 computation:01 argument:02 argument:02 evaluates:02 functional:02 namely:02 threaded:03 let:03 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