Project

General

Profile

Download (5.54 KB) Statistics
| Branch: | Tag: | Revision:
1
(* This module takes a main node (reset and step) and a property. It assumes
2
   that the system is properly defined and check that the property is valid. 
3

    
4
   When valid, it returns a set of local invariants. Otherwise, it returns a cex
5
   expressed as a sequence of input values.
6

    
7
*)
8
open Lustre_types
9
open Machine_code_types
10
open Machine_code_common
11
open Zustre_common
12
open Zustre_data
13

    
14
let check machines node =
15

    
16
  let machine = get_machine machines node in
17

    
18
  (* Declaring collecting semantics *)
19
  
20
  let main_output =
21
    rename_machine_list machine.mname.node_id machine.mstep.step_outputs
22
  in
23
  let main_output_dummy =
24
    rename_machine_list ("dummy" ^ machine.mname.node_id) machine.mstep.step_outputs
25
  in
26
  let main_memory_next =
27
    (rename_next_list (* machine.mname.node_id *) (full_memory_vars machines machine)) @
28
      main_output
29
  in
30
  let main_memory_current =
31
    (rename_current_list (* machine.mname.node_id *) (full_memory_vars machines machine)) @
32
      main_output_dummy
33
  in
34

    
35
  (* TODO: push/pop? donner un nom different par instance pour les garder dans le buffer ?
36
     Faut-il declarer les "rel" dans la hashtbl ?
37
  *)
38
  let decl_main = decl_rel "MAIN" main_memory_next in
39

    
40
  
41
  
42
  (* Init case *)
43
  let decl_init = decl_rel "INIT_STATE" [] in
44

    
45
  (* rule INIT_STATE *)
46
  let _ = add_rule [] (Z3.Expr.mk_app
47
			 !ctx
48
			 decl_init
49
			 []
50
  )  in
51

    
52
  (* Re-declaring variables *)
53
  let _ = List.map decl_var main_memory_next in
54

    
55
  let horn_head = 
56
    Z3.Expr.mk_app
57
      !ctx
58
      decl_main
59
      (List.map horn_var_to_expr main_memory_next)
60
  in
61
  (* Special case when the main node is stateless *)
62
  let _ =
63
    if Machine_code_common.is_stateless machine then
64
      begin
65
	
66
	(* Initial set: One Step(m,x)  -- Stateless node  *)	
67
     	(* rule => (INIT_STATE and step(mid, next)) MAIN(next) *)
68

    
69
	(* Note that vars here contains main_memory_next *)
70
	let vars = step_vars_m_x machines machine in
71
	(* Re-declaring variables *)
72
	let _ = List.map decl_var vars in
73
	
74
	let horn_body =
75
	  Z3.Boolean.mk_and !ctx
76
	    [
77
	      Z3.Expr.mk_app !ctx decl_init [];
78
	      Z3.Expr.mk_app !ctx
79
		(get_fdecl (machine_stateless_name node))
80
		(List.map horn_var_to_expr vars) 
81
	    ]
82
	in
83
	add_rule vars (Z3.Boolean.mk_implies !ctx horn_body horn_head)
84
	  
85
	  
86
      end
87
    else
88
      begin
89
	(* Initial set: Reset(c,m) + One Step(m,x) @. *)
90

    
91
	(* Re-declaring variables *)
92
	let vars_reset = reset_vars machines machine in
93
	let vars_step = step_vars_m_x machines machine in
94
	let vars_step_all = step_vars_c_m_x machines machine in
95
	let _ = List.map decl_var (vars_reset @ vars_step @ vars_step_all ) in
96

    
97
	(* rule => (INIT_STATE and reset(mid) and step(mid, next)) MAIN(next) *)
98
	let horn_body =
99
	  Z3.Boolean.mk_and !ctx
100
	    [
101
	      Z3.Expr.mk_app !ctx decl_init [];
102
	      Z3.Expr.mk_app !ctx
103
		(get_fdecl (machine_reset_name node))
104
		(List.map horn_var_to_expr (reset_vars machines machine));
105
	      Z3.Expr.mk_app !ctx
106
		(get_fdecl (machine_step_name node))
107
		(List.map horn_var_to_expr (step_vars_m_x machines machine))
108
	    ]
109
	in
110

    
111
	(* Vars contains all vars: in_out, current, mid, neXt memories *)
112
	let vars = step_vars_c_m_x machines machine in
113
	add_rule vars (Z3.Boolean.mk_implies !ctx horn_body horn_head)
114
	  
115
	  
116
      end
117
  in
118
  
119
  let step_name = 
120
    if Machine_code_common.is_stateless machine then 
121
      machine_stateless_name
122
    else
123
      machine_step_name
124
  in
125
  
126
  (* ; Inductive def@. *)
127

    
128
  List.iter (fun x -> ignore (decl_var x)) main_output_dummy;
129
  
130
  (* (Utils.fprintf_list ~sep:" " (fun fmt v -> fprintf fmt "%a@." pp_decl_var v)) fmt main_output_dummy; *)
131

    
132
  (* fprintf fmt *)
133
  (*   "@[<v 2>(rule (=> @ (and @[<v 0>(MAIN %a)@ (@[<v 0>%a %a@])@]@ )@ (MAIN %a)@]@.))@.@." *)
134
  (*   (Utils.fprintf_list ~sep:" " (pp_horn_var machine)) main_memory_current *)
135
  (*   step_name node *)
136
  (*   (Utils.fprintf_list ~sep:" " (pp_horn_var machine)) (step_vars machines machine) *)
137
  
138
  
139
  let horn_head = 
140
    Z3.Expr.mk_app
141
      !ctx
142
      decl_main
143
      (List.map horn_var_to_expr main_memory_next)
144
  in
145
  let horn_body =
146
    Z3.Boolean.mk_and !ctx
147
      [
148
	Z3.Expr.mk_app !ctx decl_main (List.map horn_var_to_expr main_memory_current);
149
	Z3.Expr.mk_app !ctx (get_fdecl (step_name node)) (List.map horn_var_to_expr (step_vars machines machine))
150
      ]
151
  in
152
  let _ =
153
    add_rule ~dont_touch:[decl_main] []  (Z3.Boolean.mk_implies !ctx horn_body horn_head)
154
      
155
  in
156

    
157

    
158
  (* Property def *)
159
  let decl_err = decl_rel "ERR" [] in
160

    
161
  let prop =
162
    Z3.Boolean.mk_and !ctx (List.map horn_var_to_expr main_output)
163
  in
164
  let not_prop =
165
    Z3.Boolean.mk_not !ctx prop
166
  in
167
  add_rule ~dont_touch:[decl_main;decl_err] main_memory_next (Z3.Boolean.mk_implies !ctx
168
	      (
169
		Z3.Boolean.mk_and !ctx
170
		  [not_prop;
171
		   Z3.Expr.mk_app !ctx decl_main (List.map horn_var_to_expr main_memory_next);
172
		  ]
173
	      )
174
	      (Z3.Expr.mk_app !ctx decl_err []))
175
  ;
176
  
177
      (* fprintf fmt "@[<v 2>(rule (=> @ (and @[<v 0>(not %a)@ (MAIN %a)@])@ ERR))@." *)
178
      (*   (pp_conj (pp_horn_var machine)) main_output *)
179
      (*   (Utils.fprintf_list ~sep:" " (pp_horn_var machine)) main_memory_next *)
180
      (*   ; *)
181
      (*  if !Options.horn_query then fprintf fmt "(query ERR)@." *)
182

    
183
      (* Debug instructions *)
184
  let rules_expr = Z3.Fixedpoint.get_rules !fp in
185
  Format.eprintf "@[<v 2>Registered rules:@ %a@ @]@."
186
    (Utils.fprintf_list ~sep:"@ "
187
       (fun fmt e -> Format.pp_print_string fmt (Z3.Expr.to_string e)) )
188
    rules_expr;
189

    
190
  
191
  let res_status = Z3.Fixedpoint.query_r !fp [decl_err] in
192

    
193
  Format.eprintf "Status: %s@." (Z3.Solver.string_of_status res_status)
194
  
195
(* Local Variables: *)
196
(* compile-command:"make -C ../.." *)
197
(* End: *)
(1-1/5)