model_parser.mli 11.5 KB
Newer Older
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
4
(*  Copyright 2010-2018   --   Inria - CNRS - Paris-Sud University  *)
5 6 7 8 9 10 11
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)

12
(*
13
***************************************************************
14 15 16
**  Counter-example model values
****************************************************************
*)
17 18 19 20 21 22 23 24

type float_type =
  | Plus_infinity
  | Minus_infinity
  | Plus_zero
  | Minus_zero
  | Not_a_number
  | Float_value of string * string * string
25
  | Float_hexa of string * float
26

27
val interp_float: ?interp:bool -> string -> string -> string -> float_type
28

29 30
type model_value =
 | Integer of string
31
 | Decimal of (string * string)
32
 | Fraction of (string * string)
33
 | Float of float_type
34
 | Boolean of bool
35
 | Array of model_array
36
 | Record of model_record
37
 | Proj of model_proj
38
 | Bitvector of string
39
 | Apply of string * model_value list
40
 | Unparsed of string
41
and  arr_index = {
42
  arr_index_key : string;
43 44 45 46 47 48
  arr_index_value : model_value;
}
and model_array = {
  arr_others  : model_value;
  arr_indices : arr_index list;
}
49 50
and model_proj = (proj_name * model_value)
and proj_name = string
51 52
and model_record = (field_name * model_value) list
and field_name = string
53

54 55
val array_create_constant :
  value : model_value ->
56 57 58
  model_array
(** Creates constant array with all indices equal to the parameter value. *)

59 60
val array_add_element :
  array : model_array ->
61
  index : string      ->
62
  value : model_value ->
63
  model_array
64
(** Adds an element to the array.
65
    @param array : the array to that the element will be added
66

67 68 69 70 71 72 73 74
    @param index : the index on which the element will be added.
    Note that the index must be of value model_value.Integer

    @param value : the value of the element to be added
*)

val print_model_value : Format.formatter -> model_value -> unit

75 76

(*
77
***************************************************************
78 79 80
**  Model elements
***************************************************************
*)
81

82
type model_element_kind =
83 84 85
| Result
  (* Result of a function call (if the counter-example is for postcondition)  *)
| Old
86 87 88 89
  (* Old value of function argument (if the counter-example is for postcondition) *)
| Error_message
  (* The model element represents error message, not source-code element.
     The error message is saved in the name of the model element.*)
90 91
| Other

92 93 94 95 96 97
(** Information about the name of the model element *)
type model_element_name = {
  men_name   : string;
    (** The name of the source-code element.  *)
  men_kind   : model_element_kind;
    (** The kind of model element. *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
98
  men_attrs : Ident.Sattr.t;
99
}
100

101 102
(** Counter-example model elements. Each element represents
    a counter-example for a single source-code element.*)
103
type model_element = {
104
  me_name       : model_element_name;
105
    (** Information about the name of the model element  *)
106
  me_value      : model_value;
107
    (** Counter-example value for the element. *)
108
  me_location   : Loc.position option;
109
    (** Source-code location of the element. *)
110
  me_term       : Term.term option;
111
    (** Why term corresponding to the element.  *)
112 113
}

114
val create_model_element :
115 116
  name      : string ->
  value     : model_value ->
117
  attrs     : Ident.Sattr.t ->
118
  ?location : Loc.position ->
119 120
  ?term     : Term.term ->
  unit ->
121
  model_element
122
(** Creates a counter-example model element.
123 124 125 126
    @param name : the name of the source-code element

    @param value  : counter-example value for the element

127 128
    @param location : source-code location of the element

129
    @param term : why term corresponding to the element *)
130

131
(*
132
***************************************************************
133 134
**  Model definitions
***************************************************************
135 136
*)
type model
137

138 139
val is_model_empty : model -> bool
val default_model : model
140 141

(*
142
***************************************************************
143 144 145
**  Quering the model
***************************************************************
*)
146

147
val print_model :
148
  ?me_name_trans:(model_element_name -> string) ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
149
  print_attrs:bool ->
150
  Format.formatter ->
151
  model ->
152 153 154 155
  unit
(** Prints the counter-example model

    @param me_name_trans the transformation of the model elements
156 157
      names. The input is information about model element name. The
      output is the name of the model element that should be displayed.
158
    @param model the counter-example model to print
Guillaume Melquiond's avatar
Guillaume Melquiond committed
159 160
    @param print_attrs: when set to true, the name is printed together with the
    attrs associated to the specific ident.
161 162
*)

163 164 165 166
val print_model_human :
  ?me_name_trans:(model_element_name -> string) ->
  Format.formatter ->
  model ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
167
  print_attrs:bool ->
168 169 170 171 172 173
  unit
(** Same as print_model but is intended to be human readable.

*)


174
val model_to_string :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
175
  print_attrs:bool ->
176
  ?me_name_trans:(model_element_name -> string) ->
177 178 179
  model ->
  string
(** See print_model  *)
180

181
(* TODO probably deprecated.
182
val print_model_vc_term :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
183
  print_attrs:bool ->
184
  ?me_name_trans: (model_element_name -> string) ->
185 186 187 188 189 190 191 192 193 194 195 196
  ?sep: string ->
  Format.formatter ->
  model ->
  unit
(** Prints counter-example model elements related to term that
    triggers VC.

    @param sep separator of counter-example model elements
    @param me_name_trans see print_model
    @model the counter-example model.
*)

197
val model_vc_term_to_string :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
198
  print_attrs:bool ->
199
  ?me_name_trans: (model_element_name -> string) ->
200 201 202 203 204 205 206
  ?sep: string ->
  model ->
  string
(** Gets string with counter-example model elements related to term that
    triggers VC.
    See print_model_vc_term
*)
207
*)
208

209
val print_model_json :
210
  ?me_name_trans:(model_element_name -> string) ->
211
  ?vc_line_trans:(int -> string) ->
212
  Format.formatter ->
213
  model ->
214 215
  unit
(** Prints counter-example model to json format.
216

217
    @param me_name_trans see print_model
218 219 220 221 222 223 224 225 226 227
    @param vc_line_trans the transformation from the line number corresponding
      to the term that triggers VC before splitting VC to the name of JSON field
      storing counterexample information related to this term. By default, this
      information is stored in JSON field corresponding to this line, i.e.,
      the transformation is string_of_int.
      Note that the exact line of the construct that triggers VC may not be
      known. This can happen if the term that triggers VC spans multiple lines
      and it is splitted.
      This transformation can be used to store the counterexample information
      related to this term in dedicated JSON field.
228
    @param model the counter-example model to print.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
229
    @param print_attrs if set to true, add attrs associated to the name id to
230
      the counterexample output
231

232 233 234 235 236 237 238 239 240 241 242 243
    The format is the following:
    - counterexample is JSON object with fields indexed by names of files
      storing values of counterexample_file
    - counterexample_file is JSON object with fields indexed by line numbers
      storing values of counterexample_line
    - counterexample_line is JSON array (ordered list) with elements
      corresponding to counterexample_element
    - counterexample_element is JSON object with following fields
      - "name": name of counterexample element
      - "value": value of counterexample element
      - "kind": kind of counterexample element:
        - "result": Result of a function call (if the counter-example is for postcondition)
244
        - "old": Old value of function argument (if the counter-example is for postcondition)
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        - "error_message": The model element represents error message, not source-code element.
            The error message is saved in the name of the model element
        - "other"

    Example:
    {
      "records.adb": {
          "84": [
            {
              "name": "A.A",
              "value": "255",
              "kind": "other"
            },
            {
              "name": "B.B",
              "value": "0",
              "kind": "other"
            }
          ]
      }
    }
266
*)
267

268
val model_to_string_json :
269
  ?me_name_trans:(model_element_name -> string) ->
270
  ?vc_line_trans:(int -> string) ->
271 272 273
  model ->
  string
(** See print_model_json *)
274

275
val interleave_with_source :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
276
  print_attrs:bool ->
277 278
  ?start_comment:string ->
  ?end_comment:string ->
279
  ?me_name_trans:(model_element_name -> string) ->
280
  model ->
281
  rel_filename:string ->
282
  source_code:string ->
283 284
  locations:(Loc.position * 'a) list ->
  string * (Loc.position * 'a) list
285 286 287 288 289 290 291 292 293 294
(** Given a source code and a counter-example model interleaves
    the source code with information in about the counter-example.
    That is, for each location in counter-example trace creates
    a comment in the output source code with information about
    values of counter-example model elements.

    @param start_comment the string that starts a comment
    @param end_comment the string that ends a comment
    @param me_name_trans see print_model
    @param model counter-example model
295
    @param rel_filename the file name of the source relative to the session
296
    @param source_code the input source code
297
    @param locations the source locations that are found in the code
298 299

    @return the source code with added comments with information
300 301 302
    about counter-example model. The second part of the pair are
    locations modified so that it takes into account that counterexamples
    were added.
303
*)
304 305

(*
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
***************************************************************
**  Filtering the model
***************************************************************
*)
val model_for_positions_and_decls : model ->
  positions: Loc.position list -> model
(** Given a model and a list of source-code positions returns model
    that contains only elements from the input model that are on these
    positions plus for every file in the model, elements that are
    in the positions of function declarations. Elements with other
    positions are filtered out.

    Assumes that for each file the element on the first line of the model
    has position of function declaration.

    Only filename and line number is used to identify positions.
*)

(*
***************************************************************
326 327 328 329
** Registering model parser
***************************************************************
*)
type model_parser =  string -> Printer.printer_mapping -> model
330 331
(** Parses the input string into model elements, estabilishes
    a mapping between these elements and mapping from printer
332 333
    and builds model data structure.
*)
334

335
type raw_model_parser =
336
  Wstdlib.Sstr.t -> ((string * string) list) Wstdlib.Mstr.t ->
337
    string list -> Ident.Sattr.t Wstdlib.Mstr.t -> string -> model_element list
338 339 340 341 342 343 344 345 346
(** Parses the input string into model elements.
    [raw_model_parser: proj->record_map->noarg_cons->s->mel]
    [proj]: is the list of projections
    [record_map]: is a map associating the name of printed projections to the
      fields (couple of printed field and model_trace name).
    [noarg_cons]: List of constructors with no arguments (collected to avoid
      confusion between variable and constructors)
    [s]: model
    [mel]: collected model
347
 *)
348

349
val register_model_parser : desc:Pp.formatted -> string -> raw_model_parser -> unit
350 351 352 353

val lookup_model_parser : string -> model_parser

val list_model_parsers : unit -> (string * Pp.formatted) list