Debian Codegraph @

theories/Ltac2/Option.v

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Some of the below functions are inspired by ocaml-extlib *)
12
13 Require Import Ltac2.Init.
14 Require Import Ltac2.Control.
15
16 Ltac2 may (f : 'a -> unit) (ov : 'a option) :=
17 match ov with
18 | Some v => f v
19 | None => ()
20 end.
21
22 Ltac2 map (f : 'a -> 'b) (ov : 'a option) :=
23 match ov with
24 | Some v => Some (f v)
25 | None => None
26 end.
27
28 Ltac2 filter (f : 'a -> bool) (o : 'a option) :=
29 match o with
30 | Some x => if f x then Some x else None
31 | None => None
32 end.
33
34 Ltac2 iter (f : 'a -> unit) (ov : 'a option) :=
35 match ov with
36 | Some v => f v
37 | None => ()
38 end.
39
40 Ltac2 default (def : 'a) (ov : 'a option) :=
41 match ov with
42 | Some v => v
43 | None => def
44 end.
45
46 Ltac2 map_default (f : 'a -> 'b) (def : 'b) (ov : 'a option) :=
47 match ov with
48 | Some v => f v
49 | None => def
50 end.
51
52 Ltac2 get (ov : 'a option) :=
53 match ov with
54 | Some v => v
55 | None => Control.throw No_value
56 end.
57
58 Ltac2 get_bt (ov : 'a option) :=
59 match ov with
60 | Some v => v
61 | None => Control.zero No_value
62 end.
63
64 Ltac2 bind (x : 'a option) (f : 'a -> 'b option) :=
65 match x with
66 | Some x => f x
67 | None => None
68 end.
69
70 Ltac2 join (x : 'a option option) :=
71 match x with
72 | Some x => x
73 | None => None
74 end.
75
76 Ltac2 ret (x : 'a) := Some x.
77
78 Ltac2 lift (f : 'a -> 'b) (x : 'a option) := map f x.
79
80 Ltac2 equal (eq : 'a -> 'b -> bool) (a : 'a option) (b : 'b option) : bool
81 := match a with
82 | None => match b with
83 | None => true
84 | _ => false
85 end
86 | Some a => match b with
87 | Some b => eq a b
88 | _ => false
89 end
90 end.
91
92 Ltac2 compare (cmp : 'a -> 'b -> int) (a : 'a option) (b : 'b option) :=
93 match a, b with
94 | Some a, Some b => cmp a b
95 | None, None => 0
96 | None, Some _ => -1
97 | Some _, None => 1
98 end.
99
100
101 Ltac2 is_some (a : 'a option) :=
102 match a with
103 | Some _ => true
104 | None => false
105 end.
106
107 Ltac2 is_none (a : 'a option) :=
108 match a with
109 | Some _ => false
110 | None => true
111 end.