* [Caml-list] Types for solved and unsolved conditional values
@ 2011-05-20 12:03 Sylvain Le Gall
2011-05-20 12:32 ` [Caml-list] " Sylvain Le Gall
0 siblings, 1 reply; 2+ messages in thread
From: Sylvain Le Gall @ 2011-05-20 12:03 UTC (permalink / raw)
To: caml-list
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"
A basic solution would be to make types independent, this way:
type ('a, 'b) conditional = 'a, (expr * 'b) list
type 'a unsolved = ('a, unit) conditional
type 'a solved = ('a, [`Always of int | `NotSolved]) conditional
let myval =
(),
[EFlag "foo", "foo";
EBool true, "nofoo"]
let myval' =
`Always 0,
[EFlag "foo", "foo";
EBool true, "nofoo"]
But I don't feel it is the right solution. Is there any way to do it
better ?
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Caml-list] Re: Types for solved and unsolved conditional values
2011-05-20 12:03 [Caml-list] Types for solved and unsolved conditional values Sylvain Le Gall
@ 2011-05-20 12:32 ` Sylvain Le Gall
0 siblings, 0 replies; 2+ messages in thread
From: Sylvain Le Gall @ 2011-05-20 12:32 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-05-20 12:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-20 12:03 [Caml-list] Types for solved and unsolved conditional values Sylvain Le Gall
2011-05-20 12:32 ` [Caml-list] " Sylvain Le Gall
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox