From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q45Dph8h002307 for ; Sat, 5 May 2012 15:51:43 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuEBAMYvpU/RVaG2kWdsb2JhbABFoHeRdQgiAQEBAQkLCxsEI4IMAQEBAwESAiwBGxILAQMBCwYFCw0NISIBEQEFAQoSBhMSCQeHXQEDBgULnBcJA4wkgnOESAoZJwMKFUKIdgEFC4p0hiAElX6BEY1RPYQO X-IronPort-AV: E=Sophos;i="4.75,536,1330902000"; d="scan'208";a="156881217" Received: from mail-gg0-f182.google.com ([209.85.161.182]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 05 May 2012 15:51:38 +0200 Received: by ggnk4 with SMTP id k4so950060ggn.27 for ; Sat, 05 May 2012 06:51:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=VicB0vpKfEJ0AqcFx/TZHg7ro1iNlsrkTjpfWabHjTo=; b=r1q17P8HSCz+U7U8IUszbE12qaHJ9hNfLpDQzik2q/9gE5pGXMsPwu7H49ioFsYJue QY7N7BAGsWEJOJ6cavVtDkY1jrNQMMrFqLmhkYYSr0SuX08nV0cGBsSODyyHu0Uk8VIy /IWtyX/1nrb2htHpDKtfEL6YmiZtre3cuBACwHIrx+cZaE8i/qFMzfGRkMfnzTYQaAO2 3RBZwRoHfTaB+1kpaElpjSl9N6oSh1xb5iGWGOtxEcL6tILAgNmJpEtTWCQ0lSPQWB/f tuVZ8O5BcdzxbVnz2NEEBpkvGEMRukSZpbtxw7KHGAoy7c80pv3SUluktuRQX846E3ZR /mQg== Received: by 10.50.135.4 with SMTP id po4mr5034614igb.60.1336225896876; Sat, 05 May 2012 06:51:36 -0700 (PDT) MIME-Version: 1.0 Received: by 10.50.140.100 with HTTP; Sat, 5 May 2012 06:50:56 -0700 (PDT) In-Reply-To: <87r4uykb09.fsf@frosties.localnet> References: <87r4uykb09.fsf@frosties.localnet> From: Gabriel Scherer Date: Sat, 5 May 2012 15:50:56 +0200 Message-ID: To: Goswin von Brederlow Cc: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id q45Dph8h002307 Subject: Re: [Caml-list] A shallow option type > Is it possible to somehow declare the constraint that 'a in 'a shallow > must not be itself a 'b shallow? If I understand correctly, this is not possible directly, and while you could do that with a sufficiently clever layer of phantom types, I'm not sure it is worth the additional complexity. In particular, you would not be able to statically move from ('a shallow) to ('a) by the imperative action of updating a reference (one would need a type system with strong update, where state change may incur type change, to do that). I think that, given your constraints, the simplest solution is probably to just use 'a, while informally specifying that it is not always safe to use, and providing from the C side a null value of type 'a, so that you can dynamically test on the OCaml side that your variable indeed is initialized. (I suppose you need to expose yet-unitialized values to the OCaml side, otherwise you wouldn't need to reason about nullability at all.) On Sat, May 5, 2012 at 3:33 PM, Goswin von Brederlow wrote: > Hi, > > I wish there was an option type that would work without extra > indirection (or more importantly without extra allocation of an ocaml > value when setting it to something). > > Why? I'm interfacing with C in a multithreaded way. The data is > allocated on the C side so it won't be moved around by the GC. The ocaml > side will modify data and the C side will utilize it. Now if ocaml > changes a multable 'a option then it will allocate a block for "Some x" > and the GC will move that block around during compaction. Which means > the 'a option can't be safely used without holding the runtime system > lock. Which then means the threads can't run in parallel. > > What I want is a > >    type 'a shallow = NULL | 'a  (constraint 'a != 'b shallow) > > I have some ideas on how to implement this in a module as abstract type > providing get/set/clear functions, which basically means I map None to a > C NULL pointer and Some x to plain x. I know x can never be the NULL > pointer, except when someone creates a 'a shallow shallow and sets Some > None. That would turn into simply None. > > Is it possible to somehow declare the constraint that 'a in 'a shallow must > not be itself a 'b shallow? > > MfG >        Goswin > > -- > Caml-list mailing list.  Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs >