package frama-c-lannotate

  1. Overview
  2. Docs

Source file simplify.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
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
(**************************************************************************)
(*                                                                        *)
(*  This file is part of the Frama-C's Lannotate plug-in.                 *)
(*                                                                        *)
(*  Copyright (C) 2012-2022                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  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, version 2.1.                                              *)
(*                                                                        *)
(*  It 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.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file LICENSE)                       *)
(*                                                                        *)
(**************************************************************************)

open Cil_types

let debug_level = 3

type formula = [
  | `TAnd of formula * formula
  | `TOr of formula * formula
  | `TNot of formula
  | `TAtom of int
  | `TTrue
  | `TFalse
]

type formula2 = [
  | `TAnd of formula2 * formula2
  | `TOr of formula2 * formula2
  | `TAtomPos of int
  | `TAtomNeg of int
  | `TTrue
  | `TFalse
]

let rec pp_formula f x =
  match x with
  | `TAnd (a, b) ->
    Format.fprintf f "( %a /\\ %a )" pp_formula a pp_formula b
  | `TOr (a, b) ->
    Format.fprintf f "( %a \\/ %a )" pp_formula a pp_formula b
  | `TAtomPos i
  | `TAtom i ->
    Format.fprintf f "#%d" i
  | `TAtomNeg i ->
    Format.fprintf f "(~ #%d)" i
  | `TNot a ->
    Format.fprintf f "(~ %a)" pp_formula a
  | `TTrue ->
    Format.fprintf f "True"
  | `TFalse ->
    Format.fprintf f "False"

let rec ntimes n e =
  if n > 0 then e :: (ntimes (n-1) e)
  else []

let dnf_true n =
  [ ntimes n `Dontcare ]

let dnf_false (_n : int) =
  []

let dnf_or (n : int) a b =
  assert (List.for_all (fun minterm -> n = List.length minterm) a);
  assert (List.for_all (fun minterm -> n = List.length minterm) b);
  List.append a b

let dnf_and_extbools a b =
  match a, b with
  | `False, `True | `True, `False -> None
  | `True, (`Dontcare | `True) | `Dontcare, `True -> Some `True
  | `False, (`Dontcare| `False) | `Dontcare, `False -> Some `False
  | `Dontcare, `Dontcare -> Some `Dontcare

let dnf_and_minterms n aminterm bminterm =
  assert (n = List.length aminterm);
  assert (n = List.length bminterm);
  let f acc a b =
    match acc with
    | None -> None
    | Some acc ->
      match dnf_and_extbools a b with
      | Some res -> Some (res :: acc)
      | _ -> None
  in
  match List.fold_left2 f (Some []) aminterm bminterm with
  | Some res -> Some (List.rev res)
  | None -> None;;

let dnf_and_dnf_minterm (n: int) a bminterm =
  let f acc aminterm =
    match dnf_and_minterms n aminterm bminterm with
    | Some res -> res :: acc
    | None -> acc
  in
  let res = List.rev (List.fold_left f [] a) in
  Options.debug ~level:10 "@[%a@] . @[%a@] = @[%a@]"
    Bes.pp_dnf_expression a Bes.pp_dnf_minterm bminterm Bes.pp_dnf_expression res;
  res

let dnf_and (n: int) a b =
  let f acc minterm =
    dnf_or n (dnf_and_dnf_minterm n a minterm) acc
  in
  List.fold_left f [] b

let dnf_var (n: int) v value : [> `True |`False |`Dontcare] list list =
  assert (v >= 0 && v < n);
  [ (ntimes v `Dontcare) @ [ value ] @ (ntimes (n-v-1) `Dontcare) ]

let rec propagate_nots_aux ~neg (t : formula) : formula2 =
  match t, neg with
  | `TAnd (a, b), false ->
    `TAnd (propagate_nots_aux ~neg a, propagate_nots_aux ~neg b)
  | `TAnd (a, b), true ->
    `TOr (propagate_nots_aux ~neg a, propagate_nots_aux ~neg b)
  | `TOr (a, b), false ->
    `TOr (propagate_nots_aux ~neg a, propagate_nots_aux ~neg b)
  | `TOr (a, b), true ->
    `TAnd (propagate_nots_aux ~neg a, propagate_nots_aux ~neg b)
  | `TNot a, neg ->
    propagate_nots_aux ~neg:(not neg) a
  | `TAtom a, false ->
    `TAtomPos a
  | `TAtom a, true ->
    `TAtomNeg a
  | `TTrue, false -> `TTrue
  | `TTrue, true -> `TFalse
  | `TFalse, false -> `TFalse
  | `TFalse, true -> `TTrue

let propagate_nots (formula : formula) : formula2 =
  let res = propagate_nots_aux ~neg:false formula in
  Options.debug ~level:debug_level "remove negations: @[%a@] -> @[%a@]"
    pp_formula formula pp_formula res;
  res

let rec to_dnf_aux n t =
  match t with
  | `TAnd (a, b) ->
    let a = to_dnf_aux n a in
    let b = to_dnf_aux n b in
    let res = dnf_and n a b in
    Options.debug ~level:(debug_level+1) "@[%a@] . @[%a@] = @[%a@]"
      Bes.pp_dnf_expression a Bes.pp_dnf_expression b Bes.pp_dnf_expression res;
    res
  | `TOr (a, b) ->
    let a = to_dnf_aux n a in
    let b = to_dnf_aux n b in
    let res = dnf_or n a b in
    Options.debug ~level:(debug_level+1) "@[%a@] + @[%a@] = @[%a@]"
      Bes.pp_dnf_expression a Bes.pp_dnf_expression b Bes.pp_dnf_expression res;
    res
  | `TAtomPos id -> dnf_var n id `True
  | `TAtomNeg id -> dnf_var n id `False
  | `TTrue -> dnf_true n
  | `TFalse -> dnf_false n

let to_dnf n (t : formula) =
  let res = to_dnf_aux n (propagate_nots t) in
  Options.debug ~level:debug_level "convert to dnf: @[%a@] -> @[%a@]"
    pp_formula t Bes.pp_dnf_expression res;
  res

let convert_back_minterm minterm : formula =
  let f ((i,acc) : int * formula list) atom =
    let acc = match atom with
      | `Dontcare -> acc
      | `True -> (`TAtom i) :: acc
      | `False -> (`TNot (`TAtom i)) :: acc
    in
    (i+1, acc)
  in
  let _, rev = List.fold_left f (0, []) minterm in
  match rev with
  | [] -> `TTrue
  | last :: rest ->
    List.fold_left (fun acc e -> `TAnd (e, acc)) last rest;;

let convert_back_dnf dnf : formula =
  match List.rev dnf with
  | [] -> `TFalse
  | head :: tail ->
    let head' = convert_back_minterm head in
    let f (acc : formula) e : formula =
      `TOr (convert_back_minterm e, acc)
    in
    List.fold_left f head' tail

let simplify n formula =
  let dnf = to_dnf n formula in
  let optdnf, _exact =  Bes.auto_optimize dnf in
  convert_back_dnf optdnf

module type BOOLEAN_CONVERTIBLE = sig
  type t
  type info
  val convert : ?info:info -> t -> int*info*formula
  val convert_back : info:info -> formula -> t
end


module Make (C : BOOLEAN_CONVERTIBLE) = struct

  let simplify e =
    let n, info, t1 = C.convert e in
    let optt1 = simplify n t1 in
    C.convert_back ~info optt1

end

module Exp = Make (struct

    type t = Cil_types.exp
    module ExpH = Cil_datatype.ExpStructEq.Hashtbl
    type info = int ExpH.t * t array
    open Ast_const

    type atom_map = int ExpH.t;;

    let rec fstpass (h : atom_map) n l e : int * exp list * formula =
      match e.enode with
      | BinOp (LAnd, a, b, _) ->
        let (na, la, a') = fstpass h n l a in
        let (nb, lb, b') = fstpass h na la b in
        (nb, lb, `TAnd (a', b'))
      | BinOp (LOr, a, b, _) ->
        let (na, la, a') = fstpass h n l a in
        let (nb, lb, b') = fstpass h na la b in
        (nb, lb, `TOr (a', b'))
      | UnOp (LNot, e, _) ->
        let (n', l', e') = fstpass h n l e in
        (n', l', `TNot e')
      | _ ->
        if ExpH.mem h e then
          (n, l, `TAtom (ExpH.find h e))
        else
          (ExpH.add h e n; (n+1, e :: l, `TAtom n))

    let convert ?info e =
      let h, n, l = match info with
        | Some (h, arr) -> (h, Array.length arr, List.rev (Array.to_list arr))
        | None -> ExpH.create 12, 0, []
      in
      let n, l, tmp = fstpass h n l e in
      (n, (h, Array.of_list (List.rev l)), tmp)

    let rec convert_back arr (phi:formula) : t =
      match phi with
      | `TAnd (a, b) ->
        Exp_builder.binop LAnd (convert_back arr a) (convert_back arr b)
      | `TOr (a, b) ->
        Exp_builder.binop LOr (convert_back arr a) (convert_back arr b)
      | `TNot a ->
        Exp_builder.lnot (convert_back arr a)
      | `TAtom i ->
        arr.(i)
      | `TTrue ->
        Exp_builder.one ()
      | `TFalse ->
        Exp_builder.zero ()

    let convert_back ~info:(_, arr) phi =
      convert_back arr phi
  end)

let simplify_exp = Exp.simplify
OCaml

Innovation. Community. Security.