From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 060E77EE99 for ; Mon, 20 Jan 2014 22:31:40 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of siraaj@khandkar.net) identity=pra; client-ip=63.251.153.119; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="siraaj@khandkar.net"; x-sender="siraaj@khandkar.net"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of siraaj@khandkar.net) identity=mailfrom; client-ip=63.251.153.119; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="siraaj@khandkar.net"; x-sender="siraaj@khandkar.net"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@newguinea.khandkar.net) identity=helo; client-ip=63.251.153.119; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="siraaj@khandkar.net"; x-sender="postmaster@newguinea.khandkar.net"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqAEAF6U3VI/+5l3/2dsb2JhbABZv2+BK3SCJQEBAQR4ARALGAkWDwkDAgECAUUGDQEFAgEBF4dqxAIXjn8HhDgBA4lHinWVfoNL X-IPAS-Result: AqAEAF6U3VI/+5l3/2dsb2JhbABZv2+BK3SCJQEBAQR4ARALGAkWDwkDAgECAUUGDQEFAgEBF4dqxAIXjn8HhDgBA4lHinWVfoNL X-IronPort-AV: E=Sophos;i="4.95,692,1384297200"; d="scan'208";a="54082680" Received: from khandkar.net (HELO newguinea.khandkar.net) ([63.251.153.119]) by mail2-smtp-roc.national.inria.fr with ESMTP; 20 Jan 2014 22:31:38 +0100 Received: from r3-t2.local (pool-71-183-245-233.nycmny.fios.verizon.net [71.183.245.233]) by newguinea.khandkar.net (Postfix) with ESMTPA id ECE5913E48; Mon, 20 Jan 2014 16:31:33 -0500 (EST) Message-ID: <52DD95B6.7020001@khandkar.net> Date: Mon, 20 Jan 2014 16:31:34 -0500 From: Siraaj Khandkar User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Yotam Barnoy CC: Ocaml Mailing List References: <52DD904B.1000008@khandkar.net> In-Reply-To: X-Enigmail-Version: 1.6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] Purity in ocaml You can certainly program without exceptions, for sure. I'm just saying that there's isn't a way for static analysis to guarantee purity without ability of tracking their usage. Or are you suggesting something to the effect of flagging any function, and its callers, that uses the keyword `raise`? On 1/20/14 4:16 PM, Yotam Barnoy wrote: > I don't think exceptions are a deal-breaker. First, it's not hard to have > the same pure structures without exceptions. Second, it wouldn't be too > difficult to allow for a wrapper function that translates exceptions to > return values. I do that already on most data structures. This layer could > be annotated with 'unsafe', which is pretty much needed anyway for calling > external C functions or for printing within pure functions, or it could > perhaps be given a specialized 'exception' annotation signifying that the > only role of this function is to translate exceptions. So pure code could > call both 'unsafe' and 'exception' functions. Exception functions could be > checked more rigorously to make sure all they do is translate exceptions > into values. > > Other than that, catching of exceptions would generally not be allowed in > pure/st functions. > > -Yotam > > > On Mon, Jan 20, 2014 at 4:08 PM, Siraaj Khandkar wrote: > >> On 1/20/14 3:45 PM, Yotam Barnoy wrote: >>> I wanted to gauge the interest of people on the list in adding purity >>> annotations to ocaml. Purity is one of those things that could really >> help >>> with reducing memory allocations through deforestation and decreasing the >>> running time of programs written in the functional paradigm, and it could >>> be very useful for parallelism as well. The basic scheme I have in mind >> is >>> this: >>> >>> - Functions that do not access mutable structures would be marked pure. >>> - Functions that access only local mutable structures would be marked as >> st >>> (a la state monad) >>> - Functions that access global mutable data would be unmarked (as they >> are >>> now). >>> - Pure functions can call st functions/code so long as all of the state >>> referred to by the st code is contained within said pure functions. >>> - Functions that call higher order functions, but do not modify mutable >>> state would be marked hpure (half-pure). These functions would be pure so >>> long as the functions they call remain pure. This allows List.map, >>> List.fold etc to work for both pure and impure code. >>> - The same thing exists for st code: hst represents functions that take >>> higher order functions but only performs local state mutation. >>> - In order to take advantage of this mechanism, there's no need to >> annotate >>> functions. The type inference algorithm will figure out the strictest >> type >>> that can be applied to a function and will save the annotation to an >>> external, saved annotation file. This means that non-annotated code can >>> take advantage of purity without doing any extra work, and the programmer >>> never has to think about purity. >>> - Having the purity annotations as an option is useful to force certain >>> parts of the code, such as monads, to be pure. >>> - An edge case: local state can be made to refer to global state by some >>> external function call. Therefore, local state is considered 'polluted' >>> (and global) if it is passed to an impure function. >>> - Exceptions: not sure how to handle them yet. The easiest solution is to >>> forbid them in st/pure code. Another easy alternative is to only allow >>> catching them in impure code, as haskell does. >>> >>> Thoughts? >> >> Exceptions was the first thought that came to mind when I began reading >> this - I think the ability to track unhandled exceptions, which I think >> OcamlPro is working on, is a pre-req for any purity analysis to be >> meaningful, since so many, otherwise pure, structures raise exceptions :/