package travesty

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

Source file bi_mappable_intf.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(* This file is part of 'travesty'.

   Copyright (c) 2018, 2019 by Matt Windsor

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the
   "Software"), to deal in the Software without restriction, including
   without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense, and/or sell copies of the Software, and to permit
   persons to whom the Software is furnished to do so, subject to the
   following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
   NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
   USE OR OTHER DEALINGS IN THE SOFTWARE. *)

(** The main groups of signatures provided by this module are:

    {ul
     {- {{!basic} Basic{i n}}: minimal definition of bi-mappable modules;}
     {- {{!s} S{i n}}: full bi-mappable containers, produced by applying
        functors to the above.}}

    We also define other signatures, mostly for internal book-keeping. They
    may be useful elsewhere, however. *)

open Base

(** {3:basic Basic signatures} *)

(** {4:generic The generic signature}

    As with {{!Traversable} Traversable}, we define the basic signature of
    bi-mappable structures in an arity-generic way, then specialise it for
    the various arities. *)

(** [Basic_generic] describes bi-mapping on any arity of type.

    - For arity-0 types, use {{!S0} S0}: [('l, 'r) t] becomes [t], ['l left]
      becomes [left], and ['r right] becomes [right].
    - For arity-1 types with a fixed right type, use {{!S1_left} S1_left}:
      [('l, 'r) t] becomes ['l t], ['l left] becomes ['l], and ['r right]
      becomes [right].
    - For arity-1 types with a fixed left type, use {{!S1_right} S1_right}:
      [('l, 'r) t] becomes ['l t], ['l left] becomes [left], and ['r right]
      becomes ['r].
    - For arity-2 types, use {{!S2} S2}: [('l, 'r) t] becomes [('l, 'r) t],
      ['l left] becomes ['l], and ['r right] becomes ['r]. *)
module type Basic_generic = sig
  include Types_intf.Bi_generic

  val bi_map :
       ('l1, 'r1) t
    -> left:('l1 left -> 'l2 left)
    -> right:('r1 right -> 'r2 right)
    -> ('l2, 'r2) t
  (** [bi_map c ~left ~right] maps [left] over every ['l1 left], and [right]
      over every ['r1 right], in [c]. *)
end

(** {4:sigs Arity-specific basic signatures}

    The basic signatures are {{!Basic0} Basic0}, which defines mapping
    across an arity-0 type [t] (with a fixed, associated element type
    [elt]); {{!Basic1_left} Basic1_left} and {{!Basic1_right} Basic1_right},
    which fix the right and left element type respectively (leaving the
    named type floating); and {{!Basic2} Basic2}, which defines mapping
    across an arity-2 type [('l, 'r) t] with left element type ['l] and
    right element type ['r]. *)

(** [Basic0] is the basic signature of an arity-0 bi-mappable type.

    Functions mapped over arity-0 types must preserve both element types. *)
module type Basic0 = sig
  include Types_intf.Bi0

  include
    Basic_generic
    with type ('l, 'r) t := t
     and type 'l left := left
     and type 'r right := right
end

(** [Basic1_left] is the basic signature of an arity-1 bi-mappable type with
    a floating left type and fixed right type.

    Functions mapped over arity-1 types may change the left element type,
    but not the right. *)
module type Basic1_left = sig
  include Types_intf.Bi_left

  include
    Basic_generic
    with type ('l, 'r) t := 'l t
     and type 'l left := 'l
     and type 'r right := right
end

(** [Basic1_right] is the signature of an arity-1 bi-mappable type with a
    floating right type and fixed left type.

    Functions mapped over arity-1 types may change the right element type,
    but not the left. *)
module type Basic1_right = sig
  include Types_intf.Bi_right

  include
    Basic_generic
    with type ('l, 'r) t := 'r t
     and type 'l left := left
     and type 'r right := 'r
end

(** [Basic2] is the signature of an arity-2 bi-mappable type with floating
    left and right types. *)
module type Basic2 = sig
  (** Type of containers. *)
  include T2

  include
    Basic_generic
    with type ('l, 'r) t := ('l, 'r) t
     and type 'l left := 'l
     and type 'r right := 'r
end

(** {3:s Signatures for bi-mappable types}

    The signatures below include various functions we can derive from
    bi-mappable types. *)

(** [Generic] is a generic interface for bi-mappable types, used to build
    [S0] (arity-0) and [S1] (arity-1). *)
module type Generic = sig
  include Basic_generic

  val map_left : ('l1, 'r) t -> f:('l1 left -> 'l2 left) -> ('l2, 'r) t
  (** [map_left c ~f] maps [f] over the left type of [c] only. *)

  val map_right : ('l, 'r1) t -> f:('r1 right -> 'r2 right) -> ('l, 'r2) t
  (** [map_right c ~f] maps [f] over the right type of [c] only. *)
end

(** [S0] is the full signature of an arity-0 bi-mappable type.

    Functions mapped over arity-0 types must preserve both element types. *)
module type S0 = sig
  include Types_intf.Bi0

  include
    Generic
    with type ('l, 'r) t := t
     and type 'l left := left
     and type 'r right := right
end

(** [S1_left] is the full signature of an arity-1 bi-mappable type with a
    floating left type and fixed right type.

    Functions mapped over arity-1 types may change the left element type,
    but not the right. *)
module type S1_left = sig
  include Types_intf.Bi_left

  include
    Generic
    with type ('l, 'r) t := 'l t
     and type 'l left := 'l
     and type 'r right := right
end

(** [S1_right] is the full signature of an arity-1 bi-mappable type with a
    floating right type and fixed left type.

    Functions mapped over arity-1 types may change the right element type,
    but not the left. *)
module type S1_right = sig
  include Types_intf.Bi_right

  include
    Generic
    with type ('l, 'r) t := 'r t
     and type 'l left := left
     and type 'r right := 'r
end

(** [S2] is the full signature of an arity-2 bi-mappable type with floating
    left and right types. *)
module type S2 = sig
  (** Type of containers. *)
  include T2

  include
    Generic
    with type ('l, 'r) t := ('l, 'r) t
     and type 'l left := 'l
     and type 'r right := 'r
end