1signature PmatchHeuristics = 2sig 3 4 type term = Term.term 5 type thm = Thm.thm 6 type thry = {Thy : string, Tyop : string} -> 7 {case_const : term, constructors : term list} option 8 type pmatch_heuristic = {skip_rows : bool, (* skip splitting for redundant rows? *) 9 collapse_cases : bool, (* collapse cases that lead to the same result ? *) 10 (* given a list of rows of patterns, which column to split on? *) 11 col_fun : thry -> term list list -> int } 12 13 (* some predefined heuristics *) 14 val pheu_classic : pmatch_heuristic (* HOL 4's old heuristic *) 15 val pheu_first_row : pmatch_heuristic 16 val pheu_constr_prefix : pmatch_heuristic 17 val pheu_qba : pmatch_heuristic (* the recommended one *) 18 val pheu_cqba : pmatch_heuristic 19 val pheu_first_col : pmatch_heuristic 20 val pheu_last_col : pmatch_heuristic 21 22 (* A manual heuristic. For each decision, it prints the columns and 23 takes the column-number from the provided list of explicit 24 choices. If the list is too short, the first column is 25 chosen. One should run this heuristic first with an empty list 26 as argument. Then look at the choices and add 0s for all the 27 initial choices you liked to be 0 an then a different choice you 28 prefer. Rerun again (because the following choices will change) 29 and iterate. This provided very fine control, but is tedious. *) 30 val pheu_manual : int list -> pmatch_heuristic 31 32 (* A heuristic based on column ranks. Given a pattern match matrix like 33 34 p_11 ... p_1n 35 ... 36 p_m1 --- p_mn 37 38 and a list of ranking functions prheuL = [r_1, ... r_j]. The 39 heuristic pheu_rank applies all ranking functions to all columns. 40 Let's denote the result of "r_i current_thyr [p_k1, ... pkm]" with 41 c_ik. It then picks column i such that [c_1i, ... c_ji] is maximal 42 accroding to the lexicographic ordering of integers. 43 *) 44 val pheu_rank : (thry -> term list -> int) list -> pmatch_heuristic 45 46 (* some ranking functions *) 47 val prheu_first_row : thry -> term list -> int 48 val prheu_constr_prefix : thry -> term list -> int 49 val prheu_small_branching_factor : thry -> term list -> int 50 val prheu_arity : thry -> term list -> int 51 52 (* A comparison for the results of heuristic application 53 (list of pattern lists, resulting decision tree) *) 54 type pmatch_heuristic_res_compare = (term list list * term) Lib.cmp 55 val pmatch_heuristic_cases_cmp : pmatch_heuristic_res_compare (* few cases are good *) 56 val pmatch_heuristic_size_cmp : pmatch_heuristic_res_compare (* small terms are good *) 57 58 (* Using such comparisons, we can supply multiple heuristics and choose the best results. 59 For technical reasons, this function might be stateful and therefore get's unit arguments. 60 61 The usage of a heu_fun by the Pmatch library is as follows. Heu_fun initialises the functions and returns a 62 compare function and a function heu_fun' which provides heuristics. As long as 63 heu_fun' () provides fresh heuristics these are tried. Then the best result of all these 64 heuristics according to the compare function is choosen. *) 65 type pmatch_heuristic_fun = unit -> pmatch_heuristic_res_compare * (unit -> pmatch_heuristic option) 66 67 val default_heuristic_fun : pmatch_heuristic_fun 68 val classic_heuristic_fun : pmatch_heuristic_fun 69 70 (* An exhaustive heuristic_fun. It tries all possibilities and very quickly blows up! 71 Only usable for very small examples! *) 72 val exhaustive_heuristic_fun : pmatch_heuristic_res_compare -> pmatch_heuristic_fun 73 74 75 (* custom pmatch_heuristic_fun can be easiest constructed by an explicit list of heuristics and 76 a compare function *) 77 val pmatch_heuristic_list : pmatch_heuristic_res_compare -> pmatch_heuristic list -> pmatch_heuristic_fun 78 79 (* A list of useful heuristics to be used with pmatch_heuristic_list *) 80 val default_heuristic_list : pmatch_heuristic list 81 82 83 (* The pmatch_heuristic_fun to be used by default and various functions to set it *) 84 val pmatch_heuristic : pmatch_heuristic_fun ref 85 86 val set_heuristic : pmatch_heuristic -> unit 87 val set_heuristic_fun : pmatch_heuristic_fun -> unit 88 val set_heuristic_list_size : pmatch_heuristic list -> unit 89 val set_heuristic_list_cases : pmatch_heuristic list -> unit 90 val set_default_heuristic : unit -> unit 91 val set_classic_heuristic : unit -> unit 92 93 val with_classic_heuristic : ('a -> 'b) -> ('a -> 'b) 94 val with_heuristic : pmatch_heuristic -> ('a -> 'b) -> ('a -> 'b) 95 val with_manual_heuristic : int list -> ('a -> 'b) -> ('a -> 'b) 96 val is_classic : unit -> bool 97end 98