package mopsa

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file optionExt.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(****************************************************************************)
(*                                                                          *)
(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)
(*                                                                          *)
(* Copyright (C) 2017-2019 The MOPSA Project.                               *)
(*                                                                          *)
(* This program is free software: you can redistribute it and/or modify     *)
(* it under the terms of the GNU Lesser General Public License as published *)
(* by the Free Software Foundation, either version 3 of the License, or     *)
(* (at your option) any later version.                                      *)
(*                                                                          *)
(* This program is distributed in the hope that it will be useful,          *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of           *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *)
(* GNU Lesser General Public License for more details.                      *)
(*                                                                          *)
(* You should have received a copy of the GNU Lesser General Public License *)
(* along with this program.  If not, see <http://www.gnu.org/licenses/>.    *)
(*                                                                          *)
(****************************************************************************)

(** Utility functions for ['a option] type. *)

let absorb (f:'a -> 'b option) (a:'a option) : 'b option =
  match a with None -> None | Some x -> f x

let absorb2
    (f:'a  -> 'b -> 'c option)
    (a:'a option)
    (b:'b option) : 'c option
  =
  match a,b with None,_ | _,None -> None | Some x, Some y -> f x y

let neutral2 (f:'a -> 'a -> 'a) (a:'a option) (b:'a option) : 'a option =
  match a,b with None,_ -> b | _,None -> a | Some x, Some y -> Some (f x y)

let merge
    (f1:'b -> 'a)
    (f2:'c -> 'a)
    (f12:'b ->'c -> 'a)
    (none:'a)
    (b1:'b option)
    (b2:'c option) : 'a
  =
  match b1, b2 with
  | None, None -> none
  | Some x1, None -> f1 x1
  | None, Some x2 -> f2 x2
  | Some x1, Some x2 -> f12 x1 x2


let equal (f:'a->'b->bool) (a:'a option) (b:'b option) : bool =
  match a,b with None, None -> true | Some x, Some y -> f x y | _ -> false

let included (f:'a->'b->bool) (a:'a option) (b:'b option) : bool =
  match a,b with None, _ -> true | Some x, Some y -> f x y | _ -> false

let apply (f:'b->'a) (none:'a) (b:'b option) : 'a =
  match b with None -> none | Some x -> f x

let apply2
    (f:'a->'b->'c)
    (none:'c)
    (a:'a option)
    (b:'b option) : 'c
  =
  match a,b with None,_ | _,None -> none | Some x, Some y -> f x y

let default (none:'a) (a:'a option) : 'a =
  apply (fun x -> x) none a

let compare (cmp: 'a -> 'a -> int) (a: 'a option) (b: 'a option) : int =
  if a == b then 0
  else match a, b with
    | None, None -> 0
    | None, Some _ -> -1
    | Some _, None -> 1
    | Some x, Some y -> cmp x y

exception Found_None

let raise_none () = raise Found_None

let catch_none (dfl:'b) (f:'a -> 'b) (a:'a) : 'b =
  try f a with Found_None -> dfl

let none_to_exn (a:'a option) : 'a =
  match a with None -> raise Found_None | Some x -> x

let exn_to_none (f:'a ->'b)  (x:'a) : 'b option =
  try Some (f x) with Found_None -> None

let print ?(none="None") ?(some="") pp fmt x =
  match x with
  | None -> Format.fprintf fmt "%s" none
  | Some a -> Format.fprintf fmt "%s%a" some pp a

let return x = Some x

let bind (f:'a -> 'b option) (x:'a option) : 'b option =
  match x with
  | None -> None
  | Some x -> f x

let lift (f:'a -> 'b) (a:'a option) : 'b option =
  match a with None -> None | Some x -> Some (f x)

let lift2 (f:'a  -> 'b -> 'c) (a:'a option) (b:'b option) : 'c option =
  match a,b with None,_ | _,None -> None | Some x, Some y -> Some (f x y)

let lift_list (l: 'a option list) : 'a list option =
  let rec aux =
    function
    | [] -> Some []
    | None :: _ -> None
    | Some x :: tl ->
      aux tl |>
      lift (fun tl -> x :: tl)
  in
  aux l
OCaml

Innovation. Community. Security.