Source

Opifex / src / Lib / Algebra / Algebra_Monoid.ml

Full commit
  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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
(*
 * Opifex
 *
 * Copyrights(C) 2012 by Pawel Wieczorek <wieczyk at gmail>
 *)

open Batteries
open DataModuleTypes

(*************************************************************************************************
 * The Monoid 
 *
 * Silently assumed to be.. abelian monoid, maybe should I change the name?
 ************************************************************************************************)

module type Monoid = sig

    type t

    val oper     : t -> t -> t

    val neutral  : t

    val equal    : t -> t -> bool

end

(*************************************************************************************************
 * Monoid utilitites
 ************************************************************************************************)

module MonoidUtils (M : Monoid) = struct

    let opers = List.fold_left M.oper M.neutral

    let oper_map f xs = List.map f xs |> opers

    let is_neutral = M.equal M.neutral

    let not_is_neutral x = is_neutral x |> not

    let oper_fold f data xs =
        let f' data elem = opers [data; f data elem] in
        List.fold_left f' data xs 

end

(*************************************************************************************************
 * Tuple2 monoid
 ************************************************************************************************)

module Tuple2Monoid (M1 : Monoid) (M2 : Monoid) = struct

    module Raw = struct

        type t = M1.t * M2.t

        let equal (a1,b1) (a2,b2) =
            M1.equal a1 a2 && M2.equal b1 b2

        let neutral = (M1.neutral, M2.neutral)

        let oper (a1,b1) (a2,b2) = (M1.oper a1 a2, M2.oper b1 b2)

        let combine f g a = (f a, g a)

    end

    include Raw
    include MonoidUtils(Raw)

end

(*************************************************************************************************
 * Tuple3 monoid
 ************************************************************************************************)

module Tuple3Monoid (M1 : Monoid) (M2 : Monoid) (M3 : Monoid) = struct

    module Raw = struct

        type t = M1.t * M2.t * M3.t

        let equal (a1,b1,c1) (a2,b2,c2) =
            M1.equal a1 a2 && M2.equal b1 b2 && M3.equal c1 c2

        let neutral = (M1.neutral, M2.neutral, M3.neutral)

        let oper (a1,b1,c1) (a2,b2,c2) = (M1.oper a1 a2, M2.oper b1 b2, M3.oper c1 c2)

        let combine f g h a = (f a, g a, h a)

    end

    include Raw
    include MonoidUtils(Raw)

end

(*************************************************************************************************
 * The option (lifted) monoid
 ************************************************************************************************)

module OptionMonoid (M : Monoid) = struct

    module Raw = struct

        type t = M.t option

        let oper a b = match (a,b) with
            | None, None -> None
            | Some a as t, None -> t
            | None, (Some a as t) -> t
            | Some a, Some b -> Some (M.oper a b)

        let neutral = None

        let equal a b = match a,b with
            | None, None -> true
            | Some a, Some b -> M.equal a b
            | _ -> false

    end

    include Raw
    include MonoidUtils(Raw)

end


(*************************************************************************************************
 * The one-shot partial monoid
 ************************************************************************************************)

module OneShotPartialMonoid (M : EqType) = struct

    exception Invariant_violation

    module Raw = struct

        type t = M.t option

        let oper a b = match (a,b) with
            | None, None -> None
            | Some a as t, None -> t
            | None, (Some a as t) -> t
            | Some a, Some b -> raise Invariant_violation

        let neutral = None

        let equal a b = match a,b with
            | None, None -> true
            | Some a, Some b -> M.equal a b
            | _ -> false

    end

    include Raw
    include MonoidUtils(Raw)

end

(*************************************************************************************************
 * The Set lattice
 ************************************************************************************************)


module SetMonoid (V : Set.OrderedType) = struct

    module Raw = struct

        module SV = Set.Make(V)

        type t
            = BotSet of SV.t

        let neutral = BotSet SV.empty

        let embed_botset x = BotSet x

        let oper a b = match (a,b) with
            | BotSet a, BotSet b ->
                SV.union a b
                |> embed_botset

        let equal a b = match a,b with
            | BotSet a, BotSet b ->
                SV.equal a b 

        let from_list xs =
            List.fold_right SV.add xs SV.empty 
            |> embed_botset

        let from_singleton = function
            | BotSet m when SV.cardinal m > 1 ->
                failwith "MakeSetMonoid.from_singleton"

            | BotSet m ->
                m
                |> SV.enum
                |> Enum.get

    end

    include Raw
    include MonoidUtils(Raw)

end

(*************************************************************************************************
 * The Map lattice
 ************************************************************************************************)


module MapMonoid (K : Map.OrderedType) (V : Monoid) = struct

    module Raw = struct

        module MK = Map.Make(K)
        module OV = OptionMonoid(V)

        type t
            = Map of V.t MK.t

        let neutral = Map MK.empty

        let embed_map x = Map x

        let oper ma mb = match (ma, mb) with
            | Map ma, Map mb -> 
                MK.merge (fun _ -> OV.oper) ma mb
                |> embed_map

        let singleton k v = 
            MK.singleton k v
            |> embed_map
           
        let equal a b = match a,b with
            | Map a, Map b ->
                MK.equal V.equal a b

    end

    include Raw
    include MonoidUtils(Raw)

end

(*************************************************************************************************
 * Finite Partial Functions
 ************************************************************************************************)


module FunMonoid (K : Map.OrderedType) (V : Monoid) = struct

    module Raw = struct

        module MK = Map.Make(K)

        module OV = OptionMonoid(V)

        module VH = MonoidUtils(V)

        type t
            = BotDescr of V.t MK.t

        let neutral = BotDescr MK.empty

        let embed_botdescr x = BotDescr (MK.filter VH.not_is_neutral x)


        let oper ma mb = match (ma, mb) with
            | BotDescr ma, BotDescr mb -> 
                MK.merge (fun _ -> OV.oper) ma mb
                |> embed_botdescr

        let equal ma mb = match (ma, mb) with
            | BotDescr ma, BotDescr mb ->
                MK.equal V.equal ma mb

        let call f x =
            let find k def m = try MK.find k m with Not_found -> def in
            match f with
                | BotDescr m -> find x V.neutral m

        let update f x y =
            match f with
                | BotDescr m ->
                    MK.add x y m
                    |> embed_botdescr

        let singleton x y =
            update neutral x y

        let from_list xs =
            List.fold_right (uncurry MK.add) xs MK.empty 
            |> embed_botdescr

        let from_arg_list_with_val vl xs =
            xs
            |> List.map (fun a -> (a, vl))
            |> from_list

        let unhask (BotDescr m) = m

    end

    include Raw
    include MonoidUtils(Raw)

end

(*************************************************************************************************
 * Monoid from Lattice
 ************************************************************************************************)

module MonoidFromLattice (L : Algebra_Lattice.Lattice) = struct

    module Raw = struct

        type t = L.t

        let oper = L.join

        let neutral = L.bot

        let equal = L.equal

    end

    include Raw
    include MonoidUtils(Raw)

end


(*************************************************************************************************
 * 
 ************************************************************************************************)

module PreparedMonoids = struct

    (*-------------------------------------------------------------------------------------------
     * IntSum Monoid
     *)

    module RawIntSumMonoid = struct

        type t = int

        let oper a b = a+b

        let neutral = 0

        let equal a b = a = b

    end

    module IntSumMonoid = struct

        include RawIntSumMonoid
        include MonoidUtils(RawIntSumMonoid)

    end

    (*-------------------------------------------------------------------------------------------
     * IntSum Fun Monoid
     *)

    module SumInt_FunMonoid (K : Map.OrderedType ) = struct

        include FunMonoid(K)(IntSumMonoid)

    end


    (*-------------------------------------------------------------------------------------------
     * BoolSum Monoid
     *)

    module RawBoolSumMonoid = struct

        type t = bool

        let oper a b = a || b

        let neutral = false

        let equal a b = a = b

    end

    module BoolSumMonoid = struct

        include RawBoolSumMonoid
        include MonoidUtils(RawBoolSumMonoid)

    end

    (*-------------------------------------------------------------------------------------------
     * BoolSum Fun Monoid
     *)

    module SumBool_FunMonoid (K : Map.OrderedType ) = struct

        include FunMonoid(K)(BoolSumMonoid)

    end



end