Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 13,450 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
theory SASP_Checker
imports SASP_Semantics
  "HOL-Library.Code_Target_Nat"
begin

section \<open>An Executable Checker for Multi-Valued Planning Problem Solutions\<close>


  subsection \<open>Auxiliary Lemmas\<close>
  lemma map_of_leI:
    assumes "distinct (map fst l)"
    assumes "\<And>k v. (k,v)\<in>set l \<Longrightarrow> m k = Some v"
    shows "map_of l \<subseteq>\<^sub>m m"  
    using assms
    by (metis (no_types, opaque_lifting) domIff map_le_def map_of_SomeD not_Some_eq)  

  lemma [simp]: "fst \<circ> (\<lambda>(a, b, c, d). (f a b c d, g a b c d)) = (\<lambda>(a,b,c,d). f a b c d)"
    by auto
      
  lemma map_mp: "m\<subseteq>\<^sub>mm' \<Longrightarrow> m k = Some v \<Longrightarrow> m' k = Some v"    
    by (auto simp: map_le_def dom_def)
      
      
  lemma map_add_map_of_fold: 
    fixes ps and m :: "'a \<rightharpoonup> 'b"
    assumes "distinct (map fst ps)"
    shows "m ++ map_of ps = fold (\<lambda>(k, v) m. m(k \<mapsto> v)) ps m"
  proof -
    have X1: "fold (\<lambda>(k, v) m. m(k \<mapsto> v)) ps m(a \<mapsto> b) 
            = fold (\<lambda>(k, v) m. m(k \<mapsto> v)) ps (m(a \<mapsto> b))" 
      if "a \<notin> fst ` set ps"
      for a b ps and m :: "'a \<rightharpoonup> 'b"
      using that
      by (induction ps arbitrary: m) (auto simp: fun_upd_twist)
    
    show ?thesis
      using assms
      by (induction ps arbitrary: m) (auto simp: X1)
  qed    

  

  subsection \<open>Well-formedness Check\<close>
  lemmas wf_code_thms = 
      ast_problem.astDom_def ast_problem.astI_def ast_problem.astG_def ast_problem.ast\<delta>_def
      ast_problem.numVars_def ast_problem.numVals_def 
      ast_problem.wf_partial_state_def ast_problem.wf_operator_def ast_problem.well_formed_def
      
      
  declare wf_code_thms[code]
      
  export_code ast_problem.well_formed in SML

    
  subsection \<open>Execution\<close>  
    
  definition match_pre :: "ast_precond \<Rightarrow> state \<Rightarrow> bool" where
    "match_pre \<equiv> \<lambda>(x,v) s. s x = Some v"
    
  definition match_pres :: "ast_precond list \<Rightarrow> state \<Rightarrow> bool" where 
    "match_pres pres s \<equiv> \<forall>pre\<in>set pres. match_pre pre s"
    
  definition match_implicit_pres :: "ast_effect list \<Rightarrow> state \<Rightarrow> bool" where
    "match_implicit_pres effs s \<equiv> \<forall>(_,x,vp,_)\<in>set effs. 
      (case vp of None \<Rightarrow> True | Some v \<Rightarrow> s x = Some v)"
    
  definition enabled_opr' :: "ast_operator \<Rightarrow> state \<Rightarrow> bool" where 
    "enabled_opr' \<equiv> \<lambda>(name,pres,effs,cost) s. match_pres pres s \<and> match_implicit_pres effs s"
      
  definition eff_enabled' :: "state \<Rightarrow> ast_effect \<Rightarrow> bool" where
    "eff_enabled' s \<equiv> \<lambda>(pres,_,_,_). match_pres pres s"
    
  definition "execute_opr' \<equiv> \<lambda>(name,_,effs,_) s. 
    let effs = filter (eff_enabled' s) effs
    in fold (\<lambda>(_,x,_,v) s. s(x\<mapsto>v)) effs s
  "  

  definition lookup_operator' :: "ast_problem \<Rightarrow> name \<rightharpoonup> ast_operator" 
    where "lookup_operator' \<equiv> \<lambda>(D,I,G,\<delta>) name. find (\<lambda>(n,_,_,_). n=name) \<delta>"
    
  definition enabled' :: "ast_problem \<Rightarrow> name \<Rightarrow> state \<Rightarrow> bool" where
    "enabled' problem name s \<equiv> 
      case lookup_operator' problem name of 
        Some \<pi> \<Rightarrow> enabled_opr' \<pi> s
      | None \<Rightarrow> False"

  definition execute' :: "ast_problem \<Rightarrow> name \<Rightarrow> state \<Rightarrow> state" where
    "execute' problem name s \<equiv> 
      case lookup_operator' problem name of 
        Some \<pi> \<Rightarrow> execute_opr' \<pi> s
      | None \<Rightarrow> undefined"
    
    
  context wf_ast_problem begin  
    
    lemma match_pres_correct:
      assumes D: "distinct (map fst pres)"
      assumes "s\<in>valid_states"  
      shows "match_pres pres s \<longleftrightarrow> s\<in>subsuming_states (map_of pres)"
    proof -
      have "match_pres pres s \<longleftrightarrow> map_of pres \<subseteq>\<^sub>m s"
        unfolding match_pres_def match_pre_def
        apply (auto split: prod.splits)
        using map_le_def map_of_SomeD apply fastforce  
        by (metis (full_types) D domIff map_le_def map_of_eq_Some_iff option.simps(3))
    
      with assms show ?thesis          
        unfolding subsuming_states_def 
        by auto
    qed
       
    lemma match_implicit_pres_correct:
      assumes D: "distinct (map (\<lambda>(_, v, _, _). v) effs)"
      assumes "s\<in>valid_states"  
      shows "match_implicit_pres effs s \<longleftrightarrow> s\<in>subsuming_states (map_of (implicit_pres effs))"
    proof -
      from assms show ?thesis
        unfolding subsuming_states_def 
        unfolding match_implicit_pres_def implicit_pres_def
        apply (auto 
            split: prod.splits option.splits 
            simp: distinct_map_filter
            intro!: map_of_leI)
        apply (force simp: distinct_map_filter split: prod.split elim: map_mp)  
        done  
    qed
          
    lemma enabled_opr'_correct:
      assumes V: "s\<in>valid_states"
      assumes "lookup_operator name = Some \<pi>"  
      shows "enabled_opr' \<pi> s \<longleftrightarrow> enabled name s"  
      using lookup_operator_wf[OF assms(2)] assms
      unfolding enabled_opr'_def enabled_def wf_operator_def
      by (auto 
          simp: match_pres_correct[OF _ V] match_implicit_pres_correct[OF _ V]
          simp: wf_partial_state_def
          split: option.split
          )  
       
    lemma eff_enabled'_correct:
      assumes V: "s\<in>valid_states"
      assumes "case eff of (pres,_,_,_) \<Rightarrow> wf_partial_state pres"  
      shows "eff_enabled' s eff \<longleftrightarrow> eff_enabled s eff"  
      using assms  
      unfolding eff_enabled'_def eff_enabled_def wf_partial_state_def
      by (auto simp: match_pres_correct)  
    
    
    lemma execute_opr'_correct:
      assumes V: "s\<in>valid_states"
      assumes LO: "lookup_operator name = Some \<pi>"  
      shows "execute_opr' \<pi> s = execute name s"  
    proof (cases \<pi>)
      case [simp]: (fields name pres effs)
        
      have [simp]: "filter (eff_enabled' s) effs = filter (eff_enabled s) effs"  
        apply (rule filter_cong[OF refl])
        apply (rule eff_enabled'_correct[OF V])
        using lookup_operator_wf[OF LO]  
        unfolding wf_operator_def by auto  
          
      have X1: "distinct (map fst (map (\<lambda>(_, x, _, y). (x, y)) (filter (eff_enabled s) effs)))"
        using lookup_operator_wf[OF LO]
        unfolding wf_operator_def 
        by (auto simp: distinct_map_filter)
        
      term "filter (eff_enabled s) effs"    
          
      have [simp]: 
        "fold (\<lambda>(_, x, _, v) s. s(x \<mapsto> v)) l s =
         fold (\<lambda>(k, v) m. m(k \<mapsto> v)) (map (\<lambda>(_, x, _, y). (x, y)) l) s" 
        for l :: "ast_effect list"
        by (induction l arbitrary: s) auto
          
      show ?thesis 
        unfolding execute_opr'_def execute_def using LO
        by (auto simp: map_add_map_of_fold[OF X1])
    qed
        
      
    lemma lookup_operator'_correct: 
      "lookup_operator' problem name = lookup_operator name"
      unfolding lookup_operator'_def lookup_operator_def
      unfolding ast\<delta>_def  
      by (auto split: prod.split)  
        
    lemma enabled'_correct:
      assumes V: "s\<in>valid_states"
      shows "enabled' problem name s = enabled name s"
      unfolding enabled'_def  
      using enabled_opr'_correct[OF V]
      by (auto split: option.splits simp: enabled_def lookup_operator'_correct)  
        
    lemma execute'_correct:
      assumes V: "s\<in>valid_states"
      assumes "enabled name s"      (* Intentionally put this here, also we could resolve non-enabled case by reflexivity (undefined=undefined) *)
      shows "execute' problem name s = execute name s"  
      unfolding execute'_def  
      using execute_opr'_correct[OF V] \<open>enabled name s\<close>
      by (auto split: option.splits simp: enabled_def lookup_operator'_correct)  
        
        
        
  end    

  context ast_problem 
  begin  
    
    fun simulate_plan :: "plan \<Rightarrow> state \<rightharpoonup> state" where
      "simulate_plan [] s = Some s"
    | "simulate_plan (\<pi>#\<pi>s) s = (
        if enabled \<pi> s then 
          let s' = execute \<pi> s in
          simulate_plan \<pi>s s'
        else
          None
      )"  
    
    lemma simulate_plan_correct: "simulate_plan \<pi>s s = Some s' \<longleftrightarrow> path_to s \<pi>s s'"
      by (induction s \<pi>s s' rule: path_to.induct) auto  
      
    definition check_plan :: "plan \<Rightarrow> bool" where
      "check_plan \<pi>s = (
        case simulate_plan \<pi>s I of 
          None \<Rightarrow> False 
        | Some s' \<Rightarrow> s' \<in> G)"
      
    lemma check_plan_correct: "check_plan \<pi>s \<longleftrightarrow> valid_plan \<pi>s"
      unfolding check_plan_def valid_plan_def 
      by (auto split: option.split simp: simulate_plan_correct[symmetric])
      
  end  
    
  fun simulate_plan' :: "ast_problem \<Rightarrow> plan \<Rightarrow> state \<rightharpoonup> state" where
    "simulate_plan' problem [] s = Some s"
  | "simulate_plan' problem (\<pi>#\<pi>s) s = (
      if enabled' problem \<pi> s then
        let s = execute' problem \<pi> s in
        simulate_plan' problem \<pi>s s
      else
        None
    )"  
    
  text \<open>Avoiding duplicate lookup.\<close>
  (*[code]  *)  
  lemma simulate_plan'_code[code]:
    "simulate_plan' problem [] s = Some s"
    "simulate_plan' problem (\<pi>#\<pi>s) s = (
      case lookup_operator' problem \<pi> of
        None \<Rightarrow> None
      | Some \<pi> \<Rightarrow> 
          if enabled_opr' \<pi> s then 
            simulate_plan' problem \<pi>s (execute_opr' \<pi> s)
          else None
    )"
    by (auto simp: enabled'_def execute'_def split: option.split)
    
    
  definition initial_state' :: "ast_problem \<Rightarrow> state" where
    "initial_state' problem \<equiv> let astI = ast_problem.astI problem in (
       \<lambda>v. if v<length astI then Some (astI!v) else None
     )"
      
  definition check_plan' :: "ast_problem \<Rightarrow> plan \<Rightarrow> bool" where
    "check_plan' problem \<pi>s = (
      case simulate_plan' problem \<pi>s (initial_state' problem) of 
        None \<Rightarrow> False 
      | Some s' \<Rightarrow> match_pres (ast_problem.astG problem) s')"
      
      
  context wf_ast_problem 
  begin  
    
    lemma simulate_plan'_correct:
      assumes "s\<in>valid_states"
      shows "simulate_plan' problem \<pi>s s = simulate_plan \<pi>s s"
      using assms
      apply (induction \<pi>s s rule: simulate_plan.induct)
      apply (auto simp: enabled'_correct execute'_correct execute_preserves_valid)  
      done
        
    lemma simulate_plan'_correct_paper: (* For presentation in paper. 
        Summarizing intermediate refinement step. *)
      assumes "s\<in>valid_states"
      shows "simulate_plan' problem \<pi>s s = Some s'
            \<longleftrightarrow> path_to s \<pi>s s'"
      using simulate_plan'_correct[OF assms] simulate_plan_correct by simp
      
        
    lemma initial_state'_correct: 
      "initial_state' problem = I"
      unfolding initial_state'_def I_def by (auto simp: Let_def)

    lemma check_plan'_correct:
      "check_plan' problem \<pi>s = check_plan \<pi>s"
    proof -
      have D: "distinct (map fst astG)" using wf_goal unfolding wf_partial_state_def by auto 
        
      have S'V: "s'\<in>valid_states" if "simulate_plan \<pi>s I = Some s'" for s'
        using that by (auto simp: simulate_plan_correct path_to_pres_valid[OF I_valid])
          
      show ?thesis
        unfolding check_plan'_def check_plan_def
        by (auto 
            split: option.splits 
            simp: initial_state'_correct simulate_plan'_correct[OF I_valid]
            simp: match_pres_correct[OF D S'V] G_def
            )
    qed  
        
  end

    
  (* Overall checker *)  
    
  definition verify_plan :: "ast_problem \<Rightarrow> plan \<Rightarrow> String.literal + unit" where
    "verify_plan problem \<pi>s = (
      if ast_problem.well_formed problem then
        if check_plan' problem \<pi>s then Inr () else Inl (STR ''Invalid plan'')
      else Inl (STR ''Problem not well formed'')
    )"

  lemma verify_plan_correct:
    "verify_plan problem \<pi>s = Inr () 
    \<longleftrightarrow> ast_problem.well_formed problem \<and> ast_problem.valid_plan problem \<pi>s"
  proof -
    {
      assume "ast_problem.well_formed problem"
      then interpret wf_ast_problem by unfold_locales
          
      from check_plan'_correct check_plan_correct 
      have "check_plan' problem \<pi>s = valid_plan \<pi>s" by simp
    } 
    then show ?thesis  
      unfolding verify_plan_def
      by auto  
  qed

  definition nat_opt_of_integer :: "integer \<Rightarrow> nat option" where
       "nat_opt_of_integer i = (if (i \<ge> 0) then Some (nat_of_integer i) else None)"

  (*Export functions, which includes constructors*)
  export_code verify_plan nat_of_integer integer_of_nat nat_opt_of_integer Inl Inr String.explode String.implode
    in SML
    module_name SASP_Checker_Exported
    file "code/SASP_Checker_Exported.sml"

end