From mboxrd@z Thu Jan 1 00:00:00 1970 X-Sympa-To: caml-list@inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p4KCWjSe004976 for ; Fri, 20 May 2011 14:32:46 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlAFAJxe1k1QW+UMgWdsb2JhbACYHo1+FAEBFiYlxA2GGQSNYoIvjnU X-IronPort-AV: E=Sophos;i="4.65,242,1304287200"; d="scan'208";a="83591170" Received: from lo.gmane.org ([80.91.229.12]) by mail3-smtp-sop.national.inria.fr with ESMTP; 20 May 2011 14:32:40 +0200 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QNOsg-00048G-G1 for caml-list@inria.fr; Fri, 20 May 2011 14:32:38 +0200 Received: from ks368928.kimsufi.com ([94.23.39.26]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 20 May 2011 14:32:38 +0200 Received: from sylvain by ks368928.kimsufi.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 20 May 2011 14:32:38 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Sylvain Le Gall Date: Fri, 20 May 2011 12:32:27 +0000 (UTC) Message-ID: References: X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ks368928.kimsufi.com User-Agent: slrn/pre1.0.0-18 (Linux) X-Validation-by: sylvain@le-gall.net Subject: [Caml-list] Re: Types for solved and unsolved conditional values On 20-05-2011, Sylvain Le Gall wrote: > Hello all, > > I would like to express a conditional value that can be either solved or > unsolved and be able to quickly match the result, in the case of a > solved value. The value returned should be 'a. > > Some code: > > type ('a, 'b) conditional > > type 'a unsolved = ('a, (expr, 'a) list) conditional > type 'a solved = ('a, [`Always 'a | `NotSolved]) conditional > > (* Unsolved conditional *) > let myval = > [EFlag "foo", "foo"; > EBool true, "nofoo"] > > (* Solved conditional *) > let myval' = > `Always "foo" > Another solution, using phantom type: cond.mli: type ('a, 'b) conditional val get: ('a, 'b) conditional -> 'a val solve: ('a, 'b) conditional -> ('a, [`Solved]) conditional val get_solved: ('a, [`Solved]) conditional -> [`Always 'a | `Sometimes (expr * 'a) list] cond.ml: type ('a, 'b) conditional = (expr * 'a) list let get cond = ... let solve cond = try [EBool true, get cond] with _ -> cond let get_solved = function | [EBool true, vl] -> `Always vl | lst -> `Sometimes lst Cheers, Sylvain Le Gall -- My company: http://www.ocamlcore.com Linkedin: http://fr.linkedin.com/in/sylvainlegall Start an OCaml project here: http://forge.ocamlcore.org OCaml blogs: http://planet.ocamlcore.org