src/HOL/Tools/Function/measure_functions.ML
author krauss
Sat Jan 02 23:18:58 2010 +0100 (2010-01-02)
changeset 34232 36a2a3029fd3
parent 32603 e08fdd615333
child 37387 3581483cca6c
permissions -rw-r--r--
new year's resolution: reindented code in function package
haftmann@31775
     1
(*  Title:       HOL/Tools/Function/measure_functions.ML
krauss@26875
     2
    Author:      Alexander Krauss, TU Muenchen
krauss@26875
     3
wenzelm@31902
     4
Measure functions, generated heuristically.
krauss@26875
     5
*)
krauss@26875
     6
krauss@26875
     7
signature MEASURE_FUNCTIONS =
krauss@26875
     8
sig
krauss@26875
     9
krauss@26875
    10
  val get_measure_functions : Proof.context -> typ -> term list
krauss@34232
    11
  val setup : theory -> theory
krauss@26875
    12
krauss@26875
    13
end
krauss@26875
    14
krauss@34232
    15
structure MeasureFunctions : MEASURE_FUNCTIONS =
krauss@34232
    16
struct
krauss@26875
    17
krauss@26875
    18
(** User-declared size functions **)
wenzelm@31902
    19
structure Measure_Heuristic_Rules = Named_Thms
wenzelm@31902
    20
(
krauss@34232
    21
  val name = "measure_function"
krauss@34232
    22
  val description =
krauss@34232
    23
    "Rules that guide the heuristic generation of measure functions"
krauss@26875
    24
);
krauss@26875
    25
krauss@34232
    26
fun mk_is_measure t =
krauss@34232
    27
  Const (@{const_name is_measure}, fastype_of t --> HOLogic.boolT) $ t
krauss@26875
    28
krauss@26875
    29
fun find_measures ctxt T =
krauss@34232
    30
  DEPTH_SOLVE (resolve_tac (Measure_Heuristic_Rules.get ctxt) 1)
krauss@34232
    31
    (HOLogic.mk_Trueprop (mk_is_measure (Var (("f",0), T --> HOLogic.natT)))
krauss@34232
    32
     |> cterm_of (ProofContext.theory_of ctxt) |> Goal.init)
krauss@26875
    33
  |> Seq.map (prop_of #> (fn _ $ (_ $ (_ $ f)) => f))
krauss@26875
    34
  |> Seq.list_of
krauss@26875
    35
krauss@26875
    36
krauss@26875
    37
(** Generating Measure Functions **)
krauss@26875
    38
krauss@26875
    39
fun constant_0 T = Abs ("x", T, HOLogic.zero)
krauss@26875
    40
fun constant_1 T = Abs ("x", T, HOLogic.Suc_zero)
krauss@26875
    41
krauss@26875
    42
fun mk_funorder_funs (Type ("+", [fT, sT])) =
krauss@34232
    43
  map (fn m => SumTree.mk_sumcase fT sT HOLogic.natT m (constant_0 sT)) (mk_funorder_funs fT)
krauss@34232
    44
  @ map (fn m => SumTree.mk_sumcase fT sT HOLogic.natT (constant_0 fT) m) (mk_funorder_funs sT)
krauss@26875
    45
  | mk_funorder_funs T = [ constant_1 T ]
krauss@26875
    46
wenzelm@31902
    47
fun mk_ext_base_funs ctxt (Type ("+", [fT, sT])) =
krauss@34232
    48
    map_product (SumTree.mk_sumcase fT sT HOLogic.natT)
krauss@34232
    49
      (mk_ext_base_funs ctxt fT) (mk_ext_base_funs ctxt sT)
krauss@26875
    50
  | mk_ext_base_funs ctxt T = find_measures ctxt T
krauss@26875
    51
krauss@26875
    52
fun mk_all_measure_funs ctxt (T as Type ("+", _)) =
krauss@26875
    53
    mk_ext_base_funs ctxt T @ mk_funorder_funs T
krauss@26875
    54
  | mk_all_measure_funs ctxt T = find_measures ctxt T
krauss@26875
    55
krauss@26875
    56
val get_measure_functions = mk_all_measure_funs
krauss@26875
    57
wenzelm@31902
    58
val setup = Measure_Heuristic_Rules.setup
krauss@26875
    59
krauss@26875
    60
end