Source

Opifex / src / Lib / Algebra / Algebra_Lattice.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
(*
 * Opifex
 *
 * Copyrights(C) 2012 by Pawel Wieczorek <wieczyk at gmail>
 *)

open Batteries

(*************************************************************************************************
 * The Lattice (order structure)
 ************************************************************************************************)

module type Lattice = sig

    type t

    val join : t -> t -> t

    val meet : t -> t -> t

    val top  : t

    val bot  : t

    val equal : t -> t -> bool

end

module DualLattice (L:Lattice) = struct

    type t    = L.t

    let join  = L.meet

    let meet  = L.join

    let top   = L.bot

    let bot   = L.top
   
    let equal = L.equal

end

(*************************************************************************************************
 * Lattice utilities
 ************************************************************************************************)

module LatticeUtils (L:Lattice) = struct

    let joins = List.fold_left L.join L.bot 

    let meets = List.fold_left L.meet L.top 

    let join_map f xs = List.map f xs |> joins

    let meet_map f xs = List.map f xs |> meets

    let is_top = L.equal L.top

    let not_is_top x = is_top x |> not

    let is_bot = L.equal L.bot

    let not_is_bot x = is_bot x |> not

    let join_fold f data xs =
        let f' data elem = joins [data; f data elem] in
        List.fold_left f' data xs 
end

(*************************************************************************************************
 * The option (lifted) lattice
 ************************************************************************************************)

module OptionLattice (L : Lattice) = struct

    module Raw = struct

        type t = L.t option

        let join 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 (L.join a b)

        let meet a b = match (a,b) with
            | None, _ -> None
            | _, None -> None
            | Some a, Some b -> Some (L.meet a b)

        let top = Some L.top

        let bot = None

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

    end

    include Raw
    include LatticeUtils(Raw)

end

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

module SetLattice (V : Interfaces.OrderedType) = struct

    module Raw = struct

        module SV = Set.Make(V)

        type t
            = TopSet of SV.t
            | BotSet of SV.t

        let top = TopSet SV.empty

        let bot = BotSet SV.empty

        let embed_botset x = BotSet x

        let embed_topset x = TopSet x

        let join a b = match (a,b) with
            | TopSet a, TopSet b -> 
                SV.inter a b 
                |> embed_topset

            | BotSet a, BotSet b ->
                SV.union a b
                |> embed_botset

            (*
             * x `in` (TOP \ A) + (BOT + B)
             * iff x `in` TOP /\ x `not-in` A \/ x `in` BOT \/ x `in` B
             * iff x `not-in` A \/ x `in` B
             * iff not (x `not-not-in` A /\ x `not-in` B)
             * iff not (x `in` A /\ x `not-in` B)
             * iff x `not-in` A \ B
             * iff x `in` TOP /\ `not-in` A \ B
             *)
            | TopSet a, BotSet b | BotSet b, TopSet a ->
                SV.diff a b
                |> embed_topset

        let meet a b = match (a,b) with
            | TopSet a, TopSet b -> 
                SV.union a b 
                |> embed_topset

            | BotSet a, BotSet b ->
                SV.inter a b
                |> embed_botset

            (*
             * x `in` (TOP \ A) `inter` (BOT + B)
             * iff x `in` TOP /\ x `not-in` A /\ (x `in` BOT \/ x `in` B)
             * iff x `not-in` A /\ x `in` B
             * iff x `in` B /\ x `not-in` A
             * iff x `in` B \ A
             *)
            | TopSet a, BotSet b | BotSet b, TopSet a ->
                SV.diff b a
                |> embed_botset


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

            | BotSet a, BotSet b ->
                SV.equal a b 

            | _ ->
                false


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

    end

    include Raw
    include LatticeUtils(Raw)

end

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


module MapLattice (K : Map.OrderedType) (V : Lattice) = struct

    module Raw = struct

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

        type t
            = Map of V.t MK.t
            | Top

        let top = Top

        let bot = Map MK.empty

        let embed_map x = Map x

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

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

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

            | Map a, Map b ->
                MK.equal V.equal a b

            | _ ->
                false

    end

    include Raw

    include LatticeUtils(Raw)

end

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


module FunLattice (K : Map.OrderedType) (V : Lattice) = struct

    module Raw = struct

        module MK = Map.Make(K)

        module OV = OptionLattice(V)
        module ODV = OptionLattice(DualLattice(V))

        module VH = LatticeUtils(V)


        (* Function encoding via maps:
         * (BotDescr f)(x) = f(x)    when f(x) is defined
         *                 = _BOT_   otherwise     
         *
         * (TopDescr f)(x) = f(x)    when f(x) is defined
         *                 = _TOP_   otherwise
         *)
        type t
            = BotDescr of V.t MK.t
            | TopDescr of V.t MK.t


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

        let embed_topdescr x = TopDescr (MK.filter VH.not_is_top x)

        (*
         * MK.merge : (key -> 'a option -> 'b option -> 'c option) -> 'a MK.t -> 'b MK.t -> 'c MK.t
         * Instead of writing operators for merging we use computations from the OptionLattice module
         *)

        let join ma mb = match (ma, mb) with
            (* Cases:
             *
             * BotDescr-ma(x) `join` BotDescr-mb(x)
             * Some a `OV.join` Some b
             * = Some (a `V.join` b)
             *
             * BotDescr-ma(x) `join` BotDescr-mb(x)
             * = Some a `OV.join` None
             * = Some a
             * = Some (a `V.join` V.bot)
             *)
            | BotDescr ma, BotDescr mb -> 
                MK.merge (fun _ -> OV.join) ma mb
                |> embed_botdescr

            (* Cases:
             *
             * TopDescr-ma(x) `join` TopDescr-mb(x)
             * = Some a `ODV.meet` Some b
             * = Some (a `DV.meet` b)
             * = Some (a `V.join` b)
             *
             * TopDescr-ma(x) `join` TopDescr-mb(x)
             * = Some a `ODV.meet` None 
             * = None
             *)
            | TopDescr ma, TopDescr mb -> 
                MK.merge (fun _ -> ODV.meet) ma mb
                |> embed_topdescr

            (* Cases:
             *
             * TopDescr-ma(x) `join` BotDescr-mb(x)
             * = Some a `join` Some b
             * = Some (a `join` b)
             *
             * TopDescr-ma(x) `join` BotDescr-mb(x)
             * = None `join` Some b
             * = TOP `join` Some b
             * = TOP
             * = None
             *
             * TopDescr-ma(x) `join` BotDescr-mb(x)
             * = Some a `join` None
             * = Some a `join` BOT
             * = Some a
             *)
            | TopDescr ma, BotDescr mb | BotDescr mb, TopDescr ma ->
                let f a b = match a,b with
                    | None, _        -> None
                    | Some a, None   -> None
                    | Some a, Some b -> Some (V.join a b)
                    in
                MK.merge (fun _ -> f) ma mb
                |> embed_topdescr

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

            | TopDescr ma, TopDescr mb -> 
                MK.merge (fun _ -> ODV.join) ma mb
                |> embed_topdescr

            (* Cases:
             *
             * TopDescr-ma(x) `meet` BotDescr-mb(x)
             * = Some a `meet` Some b
             * = Some (a `meet` b)
             *
             * TopDescr-ma(x) `meet` BotDescr-mb(x)
             * = None `meet` Some b
             * = TOP `meet` Some b
             * = Some b
             *
             * TopDescr-ma(x) `join` BotDescr-mb(x)
             * = Some a `meet` None
             * = Some a `meet` BOT
             * = BOT
             * = None
             *)
            | TopDescr ma, BotDescr mb | BotDescr mb, TopDescr ma ->
                let f a b = match a,b with
                    | _, None        -> None
                    | None, Some b   -> None
                    | Some a, Some b -> Some (V.meet a b)
                    in
                MK.merge (fun _ -> f) ma mb
                |> embed_botdescr

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

        let update f x y =
            match f with
                | TopDescr m ->
                    MK.add x y m
                    |> embed_topdescr

                | BotDescr m ->
                    MK.add x y m
                    |> embed_topdescr

    end

    include Raw

end

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

module PreparedLattice = struct

    module RawPlainIntLattice = struct

        type t = int

        let join a b = max a b

        let meet a b = min a b

        let top = max_int

        let bot = min_int

        let equal a b = a = b

    end

    module PlainIntLattice = struct

        include RawPlainIntLattice
        include LatticeUtils(RawPlainIntLattice)

    end

end