Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / option.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8structure Option: OPTION =
9struct
10
11type 'a t = 'a option
12
13open Pervasive.Option
14
15fun fold (opt, b, f) =
16 case opt of
17 NONE => b
18 | SOME x => f (x, b)
19
20fun exists (z, f) =
21 case z of
22 NONE => false
23 | SOME x => f x
24
25fun forall (z, f) =
26 case z of
27 NONE => true
28 | SOME x => f x
29
30fun foreach (opt, f) =
31 case opt of
32 NONE => ()
33 | SOME x => f x
34
35val app = foreach
36
37fun map (opt, f) =
38 case opt of
39 NONE => NONE
40 | SOME x => SOME (f x)
41
42fun equals (o1, o2, eq) =
43 case (o1, o2) of
44 (NONE, NONE) => true
45 | (SOME x, SOME y) => eq (x, y)
46 | _ => false
47
48fun isNone opt =
49 case opt of
50 NONE => true
51 | SOME _ => false
52
53fun toString xToString opt =
54 case opt of
55 NONE => "None"
56 | SOME x => concat ["Some ", xToString x]
57
58fun layout layoutX opt =
59 let
60 open Layout
61 in
62 case opt of
63 NONE => str "None"
64 | SOME x => seq [str "Some ", layoutX x]
65 end
66
67end