Skip to content

Commit 193d257

Browse files
further progress progress for switching nums to bs
1 parent 26e5d35 commit 193d257

File tree

1 file changed

+150
-95
lines changed

1 file changed

+150
-95
lines changed

hol/table_var_to_arithScript.sml

Lines changed: 150 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,15 @@ val _ = Hol_datatype `
4444
`;
4545

4646
val _ = Hol_datatype `
47-
airth_key = key_val of arith_lv | key_const of num
47+
airth_key = key_val of arith_lv | key_const of bitv
4848
`;
4949

5050

5151
Type intvl_row = “:airth_key # interval # num # 'a action_expr”;
52-
5352
Type intvl_table = “:('a intvl_row) list”;
5453
Type intvl_table_list = “:('a intvl_table ) list”;
5554

5655

57-
5856
val _ = Hol_datatype `
5957
pd_type =
6058
type_length of num
@@ -98,10 +96,10 @@ Definition max_from_type_def:
9896
End
9997

10098

101-
Definition resolve_pd_max_def:
102-
resolve_pd_max pd_type lval =
99+
Definition resolve_pd_min_max_def:
100+
resolve_pd_min_max pd_type lval =
103101
case resolve_lval_type pd_type lval of
104-
| SOME (type_length n) => SOME (max_from_type n)
102+
| SOME (type_length n) => SOME ((n2v 0, n), ( n2v (max_from_type n), n) )
105103
| _ => NONE
106104
End
107105

@@ -295,6 +293,13 @@ End
295293

296294

297295

296+
297+
298+
(*==================================*)
299+
(* line coversion definitions *)
300+
(*==================================*)
301+
302+
298303
(*MAP (\x. process_guard_to_arith m_e min max g curr_int) guards_list*)
299304
(* think about splitting the procedure *)
300305
Definition process_guards_rec_def:
@@ -304,20 +309,30 @@ Definition process_guards_rec_def:
304309
End
305310

306311

312+
313+
Definition convert_line_with_key_def:
314+
(convert_line_with_key key_type m_e min max ([], s, res) = NONE ) ∧
315+
(convert_line_with_key key_type m_e min max (var_guards, s, res) =
316+
SOME (key_type, process_guards_rec m_e min max var_guards (Single min max) , s, res))
317+
End
307318

308-
Definition get_lval_of_guard_in_me_def:
309-
get_lval_of_guard_in_me m_e var_g =
310-
case var_g of
311-
| Var x => (case ALOOKUP m_e x of
312-
| SOME a => get_lval a
313-
| NONE => NONE)
314-
| Not (Var x) => (case ALOOKUP m_e x of
315-
| SOME a => get_lval a
316-
| NONE => NONE)
317-
| _ => NONE
319+
320+
Definition convert_lines_map_with_key_def:
321+
convert_lines_map_with_key key_type m_e min max lines =
322+
MAP (\line. convert_line_with_key key_type m_e min max line) lines
318323
End
319324

320325

326+
327+
328+
329+
(*==================================*)
330+
(* WFness conditions for var tbl *)
331+
(*==================================*)
332+
333+
334+
335+
321336
Definition all_vars_defined_abstract_def:
322337
(all_vars_defined_abstract m_e [] = T) ∧
323338
(all_vars_defined_abstract m_e (True::rest) = all_vars_defined_abstract m_e rest) ∧
@@ -332,75 +347,92 @@ End
332347

333348

334349

350+
351+
352+
353+
Definition get_lval_of_guard_in_me_def:
354+
get_lval_of_guard_in_me m_e var_g =
355+
case var_g of
356+
| Var x => (case ALOOKUP m_e x of
357+
| SOME a => get_lval a
358+
| NONE => NONE)
359+
| Not (Var x) => (case ALOOKUP m_e x of
360+
| SOME a => get_lval a
361+
| NONE => NONE)
362+
| _ => NONE
363+
End
364+
365+
335366
Definition get_guard_lvals_def:
336367
get_guard_lvals m_e guards =
337-
FILTER IS_SOME (MAP (get_lval_of_guard_in_me m_e) guards)
368+
FILTER (λ x . IS_SOME x ) (MAP (get_lval_of_guard_in_me m_e) guards)
338369
End
339370

340-
(* Helper function to check if all lvals are the same *)
341-
Definition all_lvals_same_def:
342-
(all_lvals_same [] = T) ∧
343-
(all_lvals_same [x] = T) ∧
344-
(all_lvals_same (SOME x :: rest) =
345-
EVERY (λlval_opt. lval_opt = SOME x) rest) ∧
346-
(all_lvals_same (NONE :: rest) = F)
347-
End
348371

349-
(* Helper function to get the common lval if all are the same *)
350-
Definition get_common_lval_def:
351-
(get_common_lval [] = NONE) ∧
352-
(get_common_lval (SOME x :: rest) =
353-
if all_lvals_same (SOME x :: rest) then SOME x else NONE) ∧
354-
(get_common_lval (NONE :: rest) = NONE)
372+
Definition ALL_SAME_def:
373+
(ALL_SAME [] = T) ∧
374+
(ALL_SAME [x] = T) ∧
375+
(ALL_SAME (x::y::rest) = ((x = y) ∧ ALL_SAME (y::rest)))
355376
End
356377

357-
(*
358-
359-
(* Add this new function to analyze the entire table first *)
360-
Definition analyze_table_type_def:
361-
(analyze_table_type m_e pd_type [] = SOME (T, key_const 1, 1)) ∧
362-
(analyze_table_type m_e pd_type lines =
363-
let all_guards = FLAT (MAP FST lines) in
364-
if ¬(all_vars_defined_abstract m_e all_guards) then
365-
NONE
366-
else
367-
let lvals = get_guard_lvals m_e all_guards in
368-
case lvals of
369-
| [] => SOME (T, key_const 1, 1) (* All guards are boolean across entire table *)
370-
| l => case get_common_lval lvals of
371-
| NONE => NONE
372-
| SOME lv => case resolve_pd_max pd_type lv of
373-
| SOME max => SOME (T, key_val lv, max) (* All non-boolean guards use same LVal *)
374-
| NONE => NONE) (* Different LVals detected *)
375-
End
376378

379+
Definition one_unique_lval_in_guards_def:
380+
one_unique_lval_in_guards m_e all_guards =
381+
let lvals = get_guard_lvals m_e all_guards in
382+
case lvals of
383+
| [] => NONE (* No lvals found *)
384+
| h::t => if ALL_SAME (h::t)
385+
then h (* Returns SOME lv if all same *)
386+
else NONE
387+
End
377388

389+
390+
Definition valid_line_def:
391+
valid_line (guards, s, res) = (guards ≠ [])
392+
End
378393

379394

380-
Definition process_guards_rec_def:
381-
(process_guards_rec m_e min max [] init_int = init_int) ∧
382-
(process_guards_rec m_e min max (g::gs) init_int =
383-
process_guards_rec m_e min max gs (process_guard_to_arith m_e min max g init_int))
395+
Definition valid_table_def:
396+
valid_table table =
397+
((table ≠ []) ∧ EVERY valid_line table)
384398
End
385399

386400

387-
388-
Definition convert_line_with_key_def:
389-
(convert_line_with_key key_type m_e min max ([], s, res) = NONE ) ∧
390-
(convert_line_with_key key_type m_e min max (var_guards, s, res) =
391-
SOME (key_type, process_guards_rec m_e min max var_guards (Single min max) , s, res))
401+
Definition valid_tables_def:
402+
valid_tables tables = EVERY valid_table tables
392403
End
393404

405+
394406

395-
Definition convert_lines_map_with_key_def:
396-
convert_lines_map_with_key key_type m_e min max lines =
397-
MAP (\line. convert_line_with_key key_type m_e min max line) lines
407+
(* Add this new function to analyze the entire table first *)
408+
Definition analyze_table_type_def:
409+
(analyze_table_type m_e pd_type [] = NONE) ∧
410+
(analyze_table_type m_e pd_type table =
411+
let all_guards = FLAT (MAP FST table) in
412+
case all_vars_defined_abstract m_e all_guards of
413+
| T => ( case EVERY (λx. x = (False:atom_var)) all_guards of
414+
| T => NONE
415+
| F => (case EVERY (λx. x = True ∨ x = False ) all_guards of
416+
| T => SOME (T, key_const (n2v 1, 1), (n2v 0,1), (n2v 1,1))
417+
| F => ( case one_unique_lval_in_guards m_e all_guards of
418+
(* All non-boolean guards use same lval *)
419+
| SOME lv => (
420+
case resolve_pd_min_max pd_type lv of
421+
| SOME (min,max) => SOME (T, key_val lv, min, max)
422+
| NONE => NONE
423+
)
424+
| NONE => NONE
425+
)
426+
)
427+
)
428+
| F => NONE
429+
)
398430
End
399431

400432

401-
402-
Definition convert_single_table_def:
403-
(convert_single_table [] m_e pd_type = SOME []) ∧
433+
434+
Definition convert_single_table_def:
435+
(convert_single_table [] m_e pd_type = NONE) ∧
404436
(convert_single_table lines m_e pd_type =
405437
case analyze_table_type m_e pd_type lines of
406438
| SOME (T, key_type, min, max) => (* Convert all lines with the same key_type and max *)
@@ -416,21 +448,8 @@ End
416448

417449

418450

419-
Definition valid_line_def:
420-
valid_line (guards, s, res) = (guards ≠ [])
421-
End
422451

423452

424-
Definition valid_table_def:
425-
valid_table table =
426-
((table ≠ []) ∧ EVERY valid_line table)
427-
End
428-
429-
430-
Definition valid_tables_def:
431-
valid_tables tables = EVERY valid_table tables
432-
End
433-
434453

435454
Definition convert_tables_def:
436455
(convert_tables [] _ _ = SOME []) ∧
@@ -450,13 +469,10 @@ End
450469

451470

452471
(*
453-
val policy1_var = “([[([(Var "x" :atom_var); (Var "y" :atom_var)],(0 :num),
472+
val policy1_var = “[[([(Var "x" :atom_var); (Var "y" :atom_var)],(0 :num),
454473
(state (3 :num) :(string # num list) action_expr));
455474
([(Var "x" :atom_var); Not (Var "y" :atom_var)],(0 :num),
456475
(state (4 :num) :(string # num list) action_expr));
457-
([False],(3 :num),action ("fwd",[(1 :num)]));
458-
([True; False],(3 :num),action ("fwd",[(1 :num)]));
459-
([True],(3 :num),action ("fwd",[(1 :num)]));
460476
([Not (Var "x" :atom_var)],(0 :num),
461477
(state (4 :num) :(string # num list) action_expr))];
462478
[([(Var "z" :atom_var)],(4 :num),
@@ -466,34 +482,40 @@ val policy1_var = “([[([(Var "x" :atom_var); (Var "y" :atom_var)],(0 :num),
466482
([True],(3 :num),(state (3 :num) :(string # num list) action_expr))];
467483
[([True],(3 :num),action ("fwd",[(1 :num)]));
468484
([True],(7 :num),action ("fwd",[(2 :num)]));
469-
([True],(8 :num),action ("drop",([] :num list)))]])”;
485+
([True],(8 :num),action ("drop",([] :num list)))]]”;
470486
471487
472488
val test_pd_nested = ``[
473489
("h", type_record [
474490
("len", type_length 5);
475491
("flags", type_length 5);
476-
("ttl", type_length 5)
492+
("ttl", type_length 5)
477493
])
478-
]``;
494+
]``;
479495
480496
481-
482497
val test_lval1 = ``lv_acc (lv_x "h") "ttl"``;
483498
val test_lval2 = ``lv_acc (lv_x "h") "flags"``;
499+
val test_lval3 = ``lv_x "z"``;
484500
485-
val test_atom1 = ``arithm_gt ^test_lval1 0``;
486-
val test_atom2 = ``arithm_lt ^test_lval1 10``;
487-
val test_atom3 = ``arithm_lt ^test_lval2 3``;
488501
489-
490-
val test_m_e = ``[("x", ^test_atom1); ("y", ^test_atom2); ("z", ^test_atom3) ]``;
502+
val test_atom1 = ``arithm_gt ^test_lval1 (n2v 0,5)``; (* h.ttl > 0 *)
503+
val test_atom2 = ``arithm_lt ^test_lval1 (n2v 10,5)``; (* h.ttl < 10 *)
504+
val test_atom3 = ``arithm_lt ^test_lval2 (n2v 3,5)``; (* h.flags < 3 *)
505+
506+
507+
val test_m_e = ``[
508+
("x", ^test_atom1);
509+
("y", ^test_atom2);
510+
("z", ^test_atom3)
511+
]``;
491512
492-
EVAL ``convert_tables ^policy1_var ^test_m_e ^test_pd_nested``;
513+
EVAL ``convert_tables (^policy1_var) ^test_m_e ^test_pd_nested``;
514+
493515
*)
494516

495517

496-
518+
(*
497519
498520
499521
(* SEMANTICS *)
@@ -555,7 +577,7 @@ Definition sem_intvl_tables_def:
555577
match_intvl_tbll intvl_tbll packet_input st_in
556578
End
557579
558-
580+
(* IMPORTANT well formdness every var in m_e is indeed defined in pd*)
559581
560582
561583
@@ -615,6 +637,39 @@ QED
615637
616638
617639
640+
641+
642+
643+
Theorem append_defined_implies_first_defined:
644+
∀m_e l l'. all_vars_defined_abstract m_e (l ++ l') ⇒
645+
(all_vars_defined_abstract m_e l' ∧ all_vars_defined_abstract m_e l)
646+
Proof
647+
Induct_on `l` >> simp[all_vars_defined_abstract_def] >>
648+
Cases >> simp[all_vars_defined_abstract_def] >>
649+
rpt strip_tac >> res_tac >>
650+
rpt (BasicProvers.FULL_CASE_TAC >> gvs[]) >>
651+
res_tac
652+
QED
653+
654+
655+
656+
657+
Theorem all_vars_defined_abstract_on_individual:
658+
∀ m_e l.
659+
(all_vars_defined_abstract m_e) (FLAT l) ⇒
660+
(EVERY (\x. all_vars_defined_abstract m_e x) l)
661+
Proof
662+
Induct_on `l` >> simp[all_vars_defined_abstract_def] >>
663+
Cases >> simp[all_vars_defined_abstract_def] >>
664+
rpt strip_tac >> res_tac >>
665+
`h'::(t ++ FLAT l) = [h'] ++ t ++ FLAT l` by simp[] >>
666+
`(h':: t) = [h'] ++ t ` by simp[] >>
667+
metis_tac[append_defined_implies_first_defined]
668+
QED
669+
670+
671+
672+
618673
Definition norm_match_tbl_def:
619674
(norm_match_tbl [] m_v st_in = NONE) ∧
620675
(norm_match_tbl (h::t) m_v st_in =
@@ -838,7 +893,7 @@ QED
838893
839894
840895
841-
896+
val lemma = ETA_CONV ``\x. all_vars_defined_abstract m_e x``;
842897
843898
844899

0 commit comments

Comments
 (0)