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