package electrod

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

Source file Msg.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
(*******************************************************************************
 * electrod - a model finder for relational first-order linear temporal logic
 * 
 * Copyright (C) 2016-2020 ONERA
 * Authors: Julien Brunel (ONERA), David Chemouil (ONERA)
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * SPDX-License-Identifier: MPL-2.0
 * License-Filename: LICENSE.md
 ******************************************************************************)

(** Contains all error messages and error handling stuff. *)

open Containers
open Fmtc

(** An abbreviation. *)
module Location = Location

(** {1 Setting up the logging machinery} *)

module LoggingSetup = struct
  (* Logging "source" for the electrod {b library}. *)
  let electrod_src =
    Logs.Src.create "electrod.onera.fr" ~doc:"logs electrod events"


  (** Creates the logging "source" for the electrod {b library} (not the 
      whole application). Defines functions: debug, info, err, warn 
      {[  let univ_duplicate_atoms args =
            info @@ fun m -> args @@ fun written actual ->
            m "univ contains duplicate atoms: %a\n\
               ignoring them and continuing with this: %a"
              (Atom.pp_list) written
              (Atom.pp_list) actual]}
  *)

  module M = (val Logs.src_log electrod_src : Logs.LOG)

  include M
end

let debug = LoggingSetup.debug

let info = LoggingSetup.info

let warn = LoggingSetup.warn

let err m = LoggingSetup.kmsg (fun () -> raise Exit) Logs.Error m

let style =
  let open! Logs in
  let open Logs_fmt in
  function
  | App ->
      app_style
  | Error ->
      err_style
  | Warning ->
      warn_style
  | Info ->
      info_style
  | Debug ->
      debug_style


(** {1 Module to get source code fragments and show them (in error messages for
    instance), possibly with colored output} *)
module Extract = struct
  let lines file loc =
    match file with
    | None ->
        []
    | Some f ->
        IO.(
          with_in f
          @@ fun ic ->
          IO.read_lines_gen ic
          |> Gen.drop (Int.max 0 Location.(begl loc - 1))
          |> Gen.take (Int.max 1 Location.(1 + endl loc - begl loc))
          |> Gen.to_list)


  (* splits the string into 2 parts, the first containing [num] characters *)
  let split_string s (num : int) =
    let lg = String.length s in
    (* debug (fun m -> m "%s (%d) / %d" s lg nb); *)
    assert (num >= 0 && num <= lg);
    (* debug (fun m -> m "sub %d %d" 0 nb); *)
    let first = String.sub s 0 num in
    (* debug (fun m -> m "sub %d %d" nb (lg-nb)); *)
    let last = String.sub s num (lg - num) in
    (* debug (fun m -> m "%s°%s" first last); *)
    (first, last)


  let pp =
    pair ~sep:nop string
    @@ pair
         ~sep:nop
         ( styled `Bold
         @@ styled `Magenta
         @@ list ~sep:Format.pp_force_newline string )
         string


  let extract file loc =
    let lines = lines file loc in
    let innocent_first_last_idx = Int.max 0 (Location.begc loc) in
    let suspect_last_last_idx = Int.max 0 (Location.endc loc) in
    match lines with
    | [] ->
        ("", ([], ""))
    | [ line ] ->
        (* trick : cut first the last part *)
        let first_part, innocent_last =
          split_string line suspect_last_last_idx
        in
        let innocent_first, suspect =
          split_string first_part innocent_first_last_idx
        in
        (innocent_first, ([ suspect ], innocent_last))
    | first :: others ->
        let innocent_first, suspect_first =
          split_string first innocent_first_last_idx
        in
        let suspect_middle, last =
          List.take_drop (Int.max 0 Location.(endl loc - begl loc - 1)) others
        in
        let last = List.hd last in
        let suspect_last, innocent_last =
          split_string last suspect_last_last_idx
        in
        let suspect = (suspect_first :: suspect_middle) @ [ suspect_last ] in
        (innocent_first, (suspect, innocent_last))

  (* pair of pairs! *)
end

(** {1 Messages (errors, warnings...)}  *)

(** [code num] takes a message number and prints is as an error code (4
    (zero-padded) digits). DO NOT EVER --EVER!-- CHANGE THE NUMBER OF A MESSAGE
    (AT WORST, DELETE THE MESSAGE AND LET THE NUMBER BE LOST). ONLY ADD MESSAGES
    AT THE {b END} OF THE FOLLOWING MESSAGING MODULES. *)
let code num = Printf.sprintf "%03d" num

(** {2 Errors that stop the program} *)

module Fatal = struct
  let lexical args =
    err
    @@ fun m ->
    args
    @@ fun infile lexbuf msg ->
    let loc =
      Location.from_positions
        (Lexing.lexeme_start_p lexbuf)
        (Lexing.lexeme_end_p lexbuf)
    in
    m
      ~header:(code 1)
      "%a%a: lexical error: %s"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      msg


  let syntax args =
    err
    @@ fun m ->
    args
    @@ fun file lexbuf ->
    let loc =
      Location.from_positions
        (Lexing.lexeme_start_p lexbuf)
        (Lexing.lexeme_end_p lexbuf)
    in
    m
      ~header:(code 2)
      "%s:%a: syntax error %a%a"
      file
      Location.pp
      loc
      (* (print_extract ~color:error_color) (file, loc); *)
      string
      (Lexing.lexeme lexbuf)
      (hardline **< Extract.pp)
      (Extract.extract (Some file) loc)


  let wrong_suffix args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let name, loc = (Raw_ident.basename id, Raw_ident.location id) in
    m
      ~header:(code 3)
      "%a%a: syntax error: %S is not a valid suffix for indexed identifiers%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      name
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let different_prefixes args =
    err
    @@ fun m ->
    args
    @@ fun infile first last ->
    let first_loc = Raw_ident.location first in
    let last_loc = Raw_ident.location last in
    m
      ~header:(code 4)
      "%a%S (%a) and %S (%a) have different_prefixes%a"
      (option @@ (sp **> colon **> string))
      infile
      (Raw_ident.basename first)
      Location.pp
      first_loc
      (Raw_ident.basename last)
      Location.pp
      last_loc
      (hardline **< Extract.pp)
      (Extract.extract infile (Location.span (first_loc, last_loc)))


  let not_an_interval args =
    err
    @@ fun m ->
    args
    @@ fun infile first last ->
    let first_loc = Raw_ident.location first in
    let last_loc = Raw_ident.location last in
    m
      ~header:(code 5)
      "%a%S (%a) and %S (%a) do not define a valid (increasing) range of \
       atoms%a"
      (option @@ (sp **> colon **> string))
      infile
      (Raw_ident.basename first)
      Location.pp
      first_loc
      (Raw_ident.basename last)
      Location.pp
      last_loc
      (hardline **< Extract.pp)
      (Extract.extract infile (Location.span (first_loc, last_loc)))


  let rel_name_already_used args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 6)
      "%a%a: identifier %S already used%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let undeclared_id args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 7)
      "%a%a: identifier %S unknown%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let should_denote_a_constant_set args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 8)
      "%a%a: %S does not denote a constant set%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let incompatible_arities args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 9)
      "%a%a: inconsistent arities used in some tuples for %S%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let undeclared_atoms args =
    err
    @@ fun m ->
    args
    @@ fun infile loc absent ->
    m
      ~header:(code 10)
      "%a%a: atom(s) not declared in 'univ': %a%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (vbox2 @@ list ~sep:sp Atom.pp)
      absent
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let inf_not_in_sup args =
    err
    @@ fun m ->
    args
    @@ fun infile id pp_bound inf sup ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 11)
      "%a%a: lower bound of %S is not included in upper bound@\n\
       %a@\n\
       lower bound = %a@\n\
       upper bound = %a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      Extract.pp
      (Extract.extract infile loc)
      (box2 @@ pp_bound)
      inf
      (box2 @@ pp_bound)
      sup


  let inexact_ref_used_in_exact_scope args =
    err
    @@ fun m ->
    args
    @@ fun infile id ref_id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 12)
      "%a%a: %S is declared as exact but refers to relation %S which has \
       inexact bounds%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      (Raw_ident.basename ref_id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let arity_error args =
    err
    @@ fun m ->
    args
    @@ fun infile exp msg ->
    let loc = exp.Gen_goal.exp_loc in
    m
      ~header:(code 13)
      "%a%a: %s%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      msg
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let init_and_fby_incompatible_arities args =
    err
    @@ fun m ->
    args
    @@ fun infile id init fby ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 14)
      "%a%a: inconsistent arities used in the 'init' (%d) and 'then' (%d) \
       parts for %S%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      init
      fby
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let cannot_decide_arity args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 15)
      "%a%a: the arity of %S cannot be inferred, specify it explicitly%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let specified_computed_arities_discrepancy args =
    err
    @@ fun m ->
    args
    @@ fun infile id specified_arity computed_arity ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 16)
      "%a%a: discrepancy between the specified (%d) and inferred (%d) arities \
       for %S%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      specified_arity
      computed_arity
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let instance_is_var args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 17)
      "%a%a: %S refers to a variable relation, its value cannot be fixed in an \
       instance%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let instance_not_in_scope args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 18)
      "%a%a: the value given for %S in the instance does not comply with the \
       declared scope%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let instance_already_declared args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 19)
      "%a%a: instance %S already declared%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let syntax_error_paragraphs args =
    err
    @@ fun m ->
    args
    @@ fun infile msg ->
    m ~header:(code 20) "%a%s" (option @@ (sp **> colon **> string)) infile msg


  let symmetry_wrongly_defined args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 21)
      "%a%a: left-hand-side and right-hand-side of symmetry do\n\
      \       not have the same length%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (hardline **< Extract.pp)
      (Extract.extract infile loc)


  let solver_failed args =
    err
    @@ fun m ->
    args
    @@ fun solver scr smv errcode erroutput ->
    m
      ~header:(code 22)
      "%s@ failed@ with@ error@ code@ %d@ and@ error@ output:@\n\
       %s@\n\
       Script@ and@ model@ files@ kept@ at@ %S@ and@ %S,@ remember@ to@ \
       remove@ them."
      solver
      errcode
      erroutput
      scr
      smv


  let solver_bug args =
    err
    @@ fun m ->
    args
    @@ fun solver msg ->
    m ~header:(code 23) "unexpected situation in %s: %s" solver msg


  let no_multiplicity_allowed_here args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 24)
      "%a%a: %S: only one toplevel, 'lone' or 'one', multiplicity is allowed \n\
      \       for relations"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)


  let multiplicity_only_in_a_sup args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 25)
      "%a%a: %S: a multiplicity is only allowed in the upper bound of an \
       inexact scope"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)


  let inf_must_be_empty args =
    err
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 26)
      "%a%a: the lower bound of %S must be empty as it is enumerable"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
end

(** {2 Warnings (the program does not fail)} *)

module Warn = struct
  let univ_duplicate_atoms args =
    warn
    @@ fun m ->
    args
    @@ fun __infile written actual ->
    let fmt = Fmtc.box2 @@ Atom.pp_list in
    m
      ~header:(code 1)
      "univ contains duplicate atoms...:@ %a@;\
       ...ignoring them and continuing with:@ %a"
      fmt
      written
      fmt
      actual


  let empty_scope_declared args =
    warn
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 2)
      "%a%a: %s is always empty@\n%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      Extract.pp
      (Extract.extract infile loc)


  let duplicate_elements args =
    warn
    @@ fun m ->
    args
    @@ fun infile id bound_kind pp_bound bound ->
    let loc = Raw_ident.location id in
    let pp_inf_sup (which : [ `Inf | `Sup | `Exact ]) =
      match which with `Exact -> "" | `Inf -> " lower" | `Sup -> " upper"
    in
    m
      ~header:(code 3)
      "%a%a: the%s bound of %s contains duplicate elements...:@ %a@\n\
       ...ignoring them and continuing with:@ %a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (pp_inf_sup bound_kind)
      (Raw_ident.basename id)
      Extract.pp
      (Extract.extract infile loc)
      (box2 @@ pp_bound)
      bound


  let disj_with_only_one_variable args =
    warn
    @@ fun m ->
    args
    @@ fun infile id ->
    let loc = Raw_ident.location id in
    m
      ~header:(code 4)
      "%a%a: keyword \"disj\" ignored as %S is a single variable@\n%a"
      (option @@ (colon **> string))
      infile
      Location.pp
      loc
      (Raw_ident.basename id)
      Extract.pp
      (Extract.extract infile loc)


  let met_spurious_variable args =
    warn
    @@ fun m ->
    args
    @@ fun var ->
    m ~header:(code 5) "Spurious variable(s) in the SMV trace: %S" var
end
OCaml

Innovation. Community. Security.