package frama-c

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

Source file export_whycore.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
(**************************************************************************)
(*                                                                        *)
(*  This file is part of WP plug-in of Frama-C.                           *)
(*                                                                        *)
(*  Copyright (C) 2007-2024                                               *)
(*    CEA (Commissariat a l'energie atomique et aux energies              *)
(*         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 licenses/LGPLv2.1).            *)
(*                                                                        *)
(**************************************************************************)

(* -------------------------------------------------------------------------- *)
(* --- Common Exportation Engine for Alt-Ergo and Why3                    --- *)
(* -------------------------------------------------------------------------- *)

open Logic
open Format
open Plib
open Engine
open Export

module Make(T : Term) =
struct

  open T
  module T = T
  module E = Export.Make(T)
  module Env = E.Env

  type trigger = (T.var,Fun.t) ftrigger
  type typedef = (tau,Field.t,Fun.t) ftypedef

  let rec full_trigger = function
    | TgAny -> false
    | TgVar _ -> true
    | TgGet(a,k) -> full_trigger a && full_trigger k
    | TgSet(a,k,v) -> full_trigger a && full_trigger k && full_trigger v
    | TgFun(_,xs) | TgProp(_,xs) -> List.for_all full_trigger xs

  let rec full_triggers = function
    | [] -> []
    | ts :: tgs ->
      match List.filter full_trigger ts with
      | [] -> full_triggers tgs
      | ts -> ts :: full_triggers tgs

  class virtual engine =
    object(self)

      inherit E.engine

      (* -------------------------------------------------------------------------- *)
      (* --- Types                                                              --- *)
      (* -------------------------------------------------------------------------- *)

      method t_int = "int"
      method t_real = "real"
      method t_bool = "bool"
      method t_prop = "prop"

      method pp_tvar fmt k =
        if 1 <= k && k <= 26
        then fprintf fmt "'%c" (char_of_int (int_of_char 'a' + k - 1))
        else fprintf fmt "'_%d" k

      (* -------------------------------------------------------------------------- *)
      (* --- Scope                                                              --- *)
      (* -------------------------------------------------------------------------- *)

      method op_scope _ = None

      (* -------------------------------------------------------------------------- *)
      (* --- Arrays                                                             --- *)
      (* -------------------------------------------------------------------------- *)

      method pp_array_get fmt a k =
        fprintf fmt "@[<hov 2>%a[%a]@]" self#pp_atom a self#pp_flow k

      method pp_array_set fmt a k v =
        fprintf fmt "@[<hov 2>%a[%a@ <- %a]@]"
          self#pp_atom a self#pp_atom k self#pp_flow v

      (* -------------------------------------------------------------------------- *)
      (* --- Records                                                            --- *)
      (* -------------------------------------------------------------------------- *)

      method virtual op_record : string * string

      method pp_get_field fmt r f =
        fprintf fmt "%a.%s" self#pp_atom r (self#field f)

      method pp_def_fields fmt fvs =
        let base,fvs = match T.record_with fvs with
          | None -> None,fvs | Some(r,fvs) -> Some r,fvs in
        begin
          let (left,right) = self#op_record in
          fprintf fmt "@[<hov 2>%s" left ;
          Plib.iteri
            (fun i (f,v) ->
               ( match i , base with
                 | (Isingle | Ifirst) , Some r ->
                   fprintf fmt "@ %a with" self#pp_flow r
                 | _ -> () ) ;
               ( match i with
                 | Ifirst | Imiddle ->
                   fprintf fmt "@ @[<hov 2>%s = %a ;@]"
                     (self#field f) self#pp_flow v
                 | Isingle | Ilast ->
                   fprintf fmt "@ @[<hov 2>%s = %a@]"
                     (self#field f) self#pp_flow v )
            ) fvs ;
          fprintf fmt "@ %s@]" right ;
        end

      (* -------------------------------------------------------------------------- *)
      (* --- Higher Order                                                       --- *)
      (* -------------------------------------------------------------------------- *)

      method pp_apply (_:cmode) (_:term) (_:formatter) (_:term list) =
        failwith "Qed.Export.Why: higher-order application"

      (* -------------------------------------------------------------------------- *)
      (* --- Higher Order                                                       --- *)
      (* -------------------------------------------------------------------------- *)

      method pp_param fmt ((x,t) : string * tau) =
        fprintf fmt "%a:%a" self#pp_var x self#pp_tau t

      method pp_lambda (_:formatter) (_: (string * tau) list) =
        failwith "Qed.Export.Why : lambda abstraction"

      (* -------------------------------------------------------------------------- *)
      (* --- Declarations                                                       --- *)
      (* -------------------------------------------------------------------------- *)

      method virtual pp_declare_adt : formatter -> ADT.t -> int -> unit
      method virtual pp_declare_def : formatter -> ADT.t -> int -> tau -> unit
      method virtual pp_declare_sum : formatter -> ADT.t -> int -> (Fun.t * tau list) list -> unit

      method declare_type fmt adt n = function
        | Tabs ->
          self#pp_declare_adt fmt adt n ;
          pp_print_newline fmt ()
        | Tdef def ->
          self#pp_declare_def fmt adt n def ;
          pp_print_newline fmt ()
        | Tsum cases ->
          self#pp_declare_sum fmt adt n cases ;
          pp_print_newline fmt ()
        | Trec fts ->
          begin
            Format.fprintf fmt "@[<hv 0>@[<hv 2>" ;
            self#pp_declare_adt fmt adt n ;
            let left,right = self#op_record in
            fprintf fmt " = %s" left ;
            Plib.iteri
              (fun index (f,t) ->
                 match index with
                 | Isingle | Ilast ->
                   fprintf fmt "@ @[<hov 2>%s : %a@]" (self#field f) self#pp_tau t
                 | Imiddle | Ifirst ->
                   fprintf fmt "@ @[<hov 2>%s : %a@] ;" (self#field f) self#pp_tau t
              ) fts ;
            fprintf fmt "@] %s@]@\n" right ;
          end

      method pp_declare_symbol t fmt f =
        let name = link_name (self#link f) in
        match t with
        | Cprop -> fprintf fmt "predicate %s" name
        | Cterm -> fprintf fmt "function %s" name

      method virtual pp_trigger : trigger printer
      method virtual pp_intros : tau -> string list printer (* forall with no separator *)

      method declare_prop ~kind fmt lemma xs tgs (p : term) =
        self#global
          begin fun () ->
            fprintf fmt "@[<hv 2>%s %s:" kind lemma ;
            let groups = List.fold_left
                (fun groups x ->
                   let a = self#bind x in
                   let t = T.tau_of_var x in
                   let xs = try E.TauMap.find t groups with Not_found -> [] in
                   E.TauMap.add t (a::xs) groups
                ) E.TauMap.empty xs in
            let order = E.TauMap.fold
                (fun t xs order -> (t,List.sort String.compare xs)::order)
                groups [] in
            let tgs = full_triggers tgs in
            Plib.iteri
              (fun index (t,xs) ->
                 let do_triggers = match index with
                   | Ifirst | Imiddle -> false
                   | Isingle | Ilast -> tgs<>[] in
                 if do_triggers then
                   begin
                     let pp_or = Plib.pp_listcompact ~sep:"|" in
                     let pp_and = Plib.pp_listcompact ~sep:"," in
                     let pp_triggers = pp_or (pp_and self#pp_trigger) in
                     fprintf fmt "@ @[<hov 2>%a@]" (self#pp_intros t) xs ;
                     fprintf fmt "@ @[<hov 2>[%a].@]" pp_triggers tgs ;
                   end
                 else
                   fprintf fmt "@ @[<hov 2>%a.@]" (self#pp_intros t) xs
              ) order ;
            fprintf fmt "@ @[<hov 2>%a@]@]@\n" self#pp_prop p
          end

      method declare_axiom = self#declare_prop ~kind:"axiom"

    end

end
OCaml

Innovation. Community. Security.