@@ -398,7 +398,7 @@ fun bdd_to_tables_iterative bdd_term groupings_term =
398398
399399
400400
401- (* MTBDD to Rules - simple outout *)
401+ (* MTBDD to Rules: simple outout *)
402402fun mtbdd_to_rules1 bdd_term =
403403let
404404 open pairSyntax listSyntax stringSyntax numSyntax;
461461 val new_visited = node :: visited
462462 in
463463 if ntype = " termn" then
464- [(path, node_data)] (* Don't reverse - keep root-to-leaf order *)
464+ [(path, node_data)] (* Don't reverse: keep root-to-leaf order *)
465465 else if ntype = " non_termn" then
466466 let
467467 val var_name = get_var_name node_data
470470 SOME (_, l, r) => (l, r)
471471 | NONE => raise Fail (" Children not found: " ^ Int.toString node)
472472
473- (* TRUE first, then FALSE - this gives natural ordering *)
473+ (* TRUE first, then FALSE: this gives natural ordering *)
474474 val true_paths = dfs left ((var_name, true )::path) new_visited
475475 val false_paths = dfs right ((var_name, false )::path) new_visited
476476 in
483483 dfs start [] []
484484 end
485485
486- (* Build predicate - path is already root-to-leaf *)
486+ (* Build predicate: path is already root-to-leaf *)
487487 fun build_predicate path =
488488 let
489489 fun mk_var v = ``(Var ^(stringSyntax.fromMLstring v)) : pred``
548548end
549549
550550
551-
552551(* MTBDD to Rules with or combinations grouped by action *)
553552fun mtbdd_to_rules2 bdd_term =
554553let
608607 SOME (_, data) => data
609608 | NONE => raise Fail (" find_node_data: node " ^ Int.toString node_id ^ " not found" )
610609
611- (* Collect paths *)
610+ (* Collect paths:keep them in DFS order *)
612611 fun collect_paths start =
613612 let
614613 fun dfs node path visited =
655654 end
656655
657656 val root = num_of_term root_term
658- val paths = collect_paths root
657+ val paths = collect_paths root (* In DFS order: leftmost first, rightmost last *)
659658
660- (* Group by action *)
659+ (* Group by action:but we need to preserve the order of actions as they first appear *)
660+ val action_order_list = ref ([] : term list) (* Order of first appearance *)
661661 val action_map = ref ([] : (term * term list) list)
662662
663663 fun add_to_map (path, node_data) =
666666 val action = get_action node_data
667667
668668 fun find_and_update [] =
669- (action_map := (action, [minterm]) :: (!action_map); ())
669+ (action_map := (action, [minterm]) :: (!action_map);
670+ action_order_list := action :: (!action_order_list); ())
670671 | find_and_update ((a, preds)::rest) =
671672 if aconv a action then
672673 (action_map := (a, minterm::preds) :: rest; ())
678679
679680 val _ = List.app add_to_map paths
680681
681- (* Combine with OR - remove complex simplification *)
682+ (* Combine with OR *)
682683 fun combine_group (action, preds) =
683684 let
684685 (* Remove duplicates *)
@@ -709,36 +710,27 @@ let
709710 (combined, action)
710711 end
711712
712- val rules = map combine_group (!action_map)
713-
714- (* Separate and add default drop *)
715- val (allow, drop) =
716- List.partition (fn (_, action) =>
717- let
718- val (const, args) = strip_comb action
719- in
720- if #Name (dest_thy_const const) = " action" then
721- case args of
722- [pair, _] =>
723- let val (cmd, _) = dest_pair pair
724- in fromHOLstring cmd <> " drop" end
725- | _ => false
726- else false
727- end ) rules
728-
729- val final_rules =
730- if null drop then
731- allow @ [(``(True : pred)``, ``action (" drop" ,[])``)]
732- else
733- allow @ drop
713+ (* Create rules in the order actions first appear in the path traversal *)
714+ val rules =
715+ let
716+ fun process_order [] acc = rev acc
717+ | process_order (act::rest) acc =
718+ case List.find (fn (a, _) => aconv a act) (!action_map) of
719+ SOME (_, preds) =>
720+ process_order rest (combine_group (act, preds) :: acc)
721+ | NONE => process_order rest acc
722+ in
723+ process_order (rev (!action_order_list)) [] (* Reverse because we added in reverse order *)
724+ end
734725
726+ (* Now rules are in the order actions first appear in DFS traversal *)
735727 (* Build result *)
736728 fun build_list [] = ``[] : action_policy_type``
737729 | build_list ((pred, act)::rest) =
738730 ``(^(pred), ^(act)) :: ^(build_list rest)``
739731
740732in
741- build_list final_rules
733+ build_list rules
742734end
743735
744736
0 commit comments