caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Sylvain Le Gall <sylvain@le-gall.net>
To: caml-list@inria.fr
Subject: [Caml-list] Re: Types for solved and unsolved conditional values
Date: Fri, 20 May 2011 12:32:27 +0000 (UTC)	[thread overview]
Message-ID: <slrnitcnqq.ks.sylvain@gallu.homelinux.org> (raw)
In-Reply-To: <slrnitcm45.ks.sylvain@gallu.homelinux.org>

On 20-05-2011, Sylvain Le Gall <sylvain@le-gall.net> 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



      reply	other threads:[~2011-05-20 12:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-20 12:03 [Caml-list] " Sylvain Le Gall
2011-05-20 12:32 ` Sylvain Le Gall [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=slrnitcnqq.ks.sylvain@gallu.homelinux.org \
    --to=sylvain@le-gall.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).