src/HOL/Tools/function_package/lexicographic_order.ML
author krauss
Tue Nov 07 22:06:32 2006 +0100 (2006-11-07)
changeset 21237 b803f9870e97
parent 21201 803bc7672d17
child 21319 cf814e36f788
permissions -rw-r--r--
untabified
bulwahn@21131
     1
(*  Title:       HOL/Tools/function_package/lexicographic_order.ML
krauss@21201
     2
    ID:          $Id$
bulwahn@21131
     3
    Author:      Lukas Bulwahn, TU Muenchen
bulwahn@21131
     4
bulwahn@21131
     5
Method for termination proofs with lexicographic orderings.
bulwahn@21131
     6
*)
bulwahn@21131
     7
bulwahn@21131
     8
signature LEXICOGRAPHIC_ORDER =
bulwahn@21131
     9
sig
krauss@21237
    10
  val lexicographic_order : Method.method
krauss@21237
    11
  val setup: theory -> theory
bulwahn@21131
    12
end
bulwahn@21131
    13
bulwahn@21131
    14
structure LexicographicOrder : LEXICOGRAPHIC_ORDER =
bulwahn@21131
    15
struct
bulwahn@21131
    16
bulwahn@21131
    17
(* Theory dependencies *)
bulwahn@21131
    18
val measures = "List.measures"
bulwahn@21131
    19
val wf_measures = thm "wf_measures"
bulwahn@21131
    20
val measures_less = thm "measures_less"
bulwahn@21131
    21
val measures_lesseq = thm "measures_lesseq"
krauss@21237
    22
                      
bulwahn@21131
    23
fun del_index (n, []) = []
bulwahn@21131
    24
  | del_index (n, x :: xs) =
krauss@21237
    25
    if n>0 then x :: del_index (n - 1, xs) else xs 
bulwahn@21131
    26
bulwahn@21131
    27
fun transpose ([]::_) = []
bulwahn@21131
    28
  | transpose xss = map hd xss :: transpose (map tl xss)
bulwahn@21131
    29
bulwahn@21131
    30
fun mk_sum_case (f1, f2) =
bulwahn@21131
    31
    case (fastype_of f1, fastype_of f2) of
krauss@21237
    32
      (Type("fun", [A, B]), Type("fun", [C, D])) =>
krauss@21237
    33
      if (B = D) then
krauss@21237
    34
        Const("Datatype.sum.sum_case", (A --> B) --> (C --> D) --> Type("+", [A,C]) --> B) $ f1 $ f2
krauss@21237
    35
      else raise TERM ("mk_sum_case: range type mismatch", [f1, f2]) 
krauss@21237
    36
    | _ => raise TERM ("mk_sum_case", [f1, f2])
krauss@21237
    37
                 
bulwahn@21131
    38
fun dest_wf (Const ("Wellfounded_Recursion.wf", _) $ t) = t
bulwahn@21131
    39
  | dest_wf t = raise TERM ("dest_wf", [t])
krauss@21237
    40
                      
bulwahn@21131
    41
datatype cell = Less of thm | LessEq of thm | None of thm | False of thm;
krauss@21237
    42
         
bulwahn@21131
    43
fun is_Less cell = case cell of (Less _) => true | _ => false  
krauss@21237
    44
                                                        
bulwahn@21131
    45
fun is_LessEq cell = case cell of (LessEq _) => true | _ => false
krauss@21237
    46
                                                            
bulwahn@21131
    47
fun thm_of_cell cell =
bulwahn@21131
    48
    case cell of 
krauss@21237
    49
      Less thm => thm
krauss@21237
    50
    | LessEq thm => thm
krauss@21237
    51
    | False thm => thm
krauss@21237
    52
    | None thm => thm
krauss@21237
    53
                  
bulwahn@21131
    54
fun mk_base_fun_bodys (t : term) (tt : typ) =
bulwahn@21131
    55
    case tt of
krauss@21237
    56
      Type("*", [ft, st]) => (mk_base_fun_bodys (Const("fst", tt --> ft) $ t) ft) @ (mk_base_fun_bodys (Const("snd", tt --> st) $ t) st)      
krauss@21237
    57
    | _ => [(t, tt)]
krauss@21237
    58
           
bulwahn@21131
    59
fun mk_base_fun_header fulltyp (t, typ) =
bulwahn@21131
    60
    if typ = HOLogic.natT then
krauss@21237
    61
      Abs ("x", fulltyp, t)
bulwahn@21131
    62
    else Abs ("x", fulltyp, Const("Nat.size", typ --> HOLogic.natT) $ t)
krauss@21237
    63
         
bulwahn@21131
    64
fun mk_base_funs (tt: typ) = 
bulwahn@21131
    65
    mk_base_fun_bodys (Bound 0) tt |>
krauss@21237
    66
                      map (mk_base_fun_header tt)
krauss@21237
    67
    
bulwahn@21131
    68
fun mk_ext_base_funs (tt : typ) =
bulwahn@21131
    69
    case tt of
krauss@21237
    70
      Type("+", [ft, st]) =>
krauss@21237
    71
      product (mk_ext_base_funs ft) (mk_ext_base_funs st)
krauss@21237
    72
              |> map mk_sum_case
krauss@21237
    73
    | _ => mk_base_funs tt
krauss@21237
    74
           
bulwahn@21131
    75
fun dest_term (t : term) =
bulwahn@21131
    76
    let
krauss@21237
    77
      val (vars, prop) = (FundefLib.dest_all_all t)
krauss@21237
    78
      val prems = Logic.strip_imp_prems prop
krauss@21237
    79
      val (tuple, rel) = Logic.strip_imp_concl prop
krauss@21237
    80
                         |> HOLogic.dest_Trueprop 
krauss@21237
    81
                         |> HOLogic.dest_mem
krauss@21237
    82
      val (lhs, rhs) = HOLogic.dest_prod tuple
bulwahn@21131
    83
    in
krauss@21237
    84
      (vars, prems, lhs, rhs, rel)
bulwahn@21131
    85
    end
krauss@21237
    86
    
bulwahn@21131
    87
fun mk_goal (vars, prems, lhs, rhs) rel =
bulwahn@21131
    88
    let 
krauss@21237
    89
      val concl = HOLogic.mk_binrel rel (lhs, rhs) |> HOLogic.mk_Trueprop
krauss@21237
    90
    in  
krauss@21237
    91
      Logic.list_implies (prems, concl) |>
krauss@21237
    92
                         fold_rev FundefLib.mk_forall vars
bulwahn@21131
    93
    end
krauss@21237
    94
    
bulwahn@21131
    95
fun prove (thy: theory) (t: term) =
bulwahn@21131
    96
    cterm_of thy t |> Goal.init 
bulwahn@21131
    97
    |> SINGLE (CLASIMPSET auto_tac) |> the
krauss@21237
    98
    
bulwahn@21131
    99
fun mk_cell (thy : theory) (vars, prems) (lhs, rhs) = 
krauss@21237
   100
    let 
krauss@21237
   101
      val goals = mk_goal (vars, prems, lhs, rhs) 
krauss@21237
   102
      val less_thm = goals "Orderings.less" |> prove thy
bulwahn@21131
   103
    in
krauss@21237
   104
      if Thm.no_prems less_thm then
krauss@21237
   105
        Less (Goal.finish less_thm)
krauss@21237
   106
      else
krauss@21237
   107
        let
krauss@21237
   108
          val lesseq_thm = goals "Orderings.less_eq" |> prove thy
krauss@21237
   109
        in
krauss@21237
   110
          if Thm.no_prems lesseq_thm then
krauss@21237
   111
            LessEq (Goal.finish lesseq_thm)
krauss@21237
   112
          else 
krauss@21237
   113
            if prems_of lesseq_thm = [HOLogic.Trueprop $ HOLogic.false_const] then False lesseq_thm
krauss@21237
   114
            else None lesseq_thm
krauss@21237
   115
        end
bulwahn@21131
   116
    end
krauss@21237
   117
    
bulwahn@21131
   118
fun mk_row (thy: theory) base_funs (t : term) =
bulwahn@21131
   119
    let
krauss@21237
   120
      val (vars, prems, lhs, rhs, _) = dest_term t
krauss@21237
   121
      val lhs_list = map (fn x => x $ lhs) base_funs
krauss@21237
   122
      val rhs_list = map (fn x => x $ rhs) base_funs
bulwahn@21131
   123
    in
krauss@21237
   124
      map (mk_cell thy (vars, prems)) (lhs_list ~~ rhs_list)
bulwahn@21131
   125
    end
krauss@21237
   126
      
bulwahn@21131
   127
(* simple depth-first search algorithm for the table *)
bulwahn@21131
   128
fun search_table table =
bulwahn@21131
   129
    case table of
krauss@21237
   130
      [] => SOME []
krauss@21237
   131
    | _ =>
krauss@21237
   132
      let
krauss@21237
   133
        val check_col = forall (fn c => is_Less c orelse is_LessEq c)
krauss@21237
   134
        val col = find_index (check_col) (transpose table)
krauss@21237
   135
      in case col of
krauss@21237
   136
           ~1 => NONE 
krauss@21237
   137
         | _ =>
krauss@21237
   138
           let
krauss@21237
   139
             val order_opt = table |> filter_out (fn x => is_Less (nth x col)) |> map (curry del_index col) |> search_table
krauss@21237
   140
             val transform_order = (fn col => map (fn x => if x>=col then x+1 else x))
krauss@21237
   141
           in case order_opt of
krauss@21237
   142
                NONE => NONE
krauss@21237
   143
              | SOME order =>SOME (col::(transform_order col order))
krauss@21237
   144
           end
krauss@21237
   145
      end
krauss@21237
   146
      
bulwahn@21131
   147
fun prove_row row (st : thm) =
bulwahn@21131
   148
    case row of
krauss@21237
   149
      [] => sys_error "INTERNAL ERROR IN lexicographic order termination tactic - fun prove_row (row is empty)" 
krauss@21237
   150
    | cell::tail =>
krauss@21237
   151
      case cell of
krauss@21237
   152
        Less less_thm =>
krauss@21237
   153
        let
krauss@21237
   154
          val next_thm = st |> SINGLE (rtac measures_less 1) |> the
krauss@21237
   155
        in
krauss@21237
   156
          implies_elim next_thm less_thm
krauss@21237
   157
        end
krauss@21237
   158
      | LessEq lesseq_thm =>
krauss@21237
   159
        let
krauss@21237
   160
          val next_thm = st |> SINGLE (rtac measures_lesseq 1) |> the
krauss@21237
   161
        in
krauss@21237
   162
          implies_elim next_thm lesseq_thm 
krauss@21237
   163
          |> prove_row tail
krauss@21237
   164
        end
krauss@21237
   165
      | _ => sys_error "INTERNAL ERROR IN lexicographic order termination tactic - fun prove_row (Only expecting Less or LessEq)"
krauss@21237
   166
             
bulwahn@21131
   167
fun pr_unprovable_subgoals table =
bulwahn@21131
   168
    filter (fn x => not (is_Less x) andalso not (is_LessEq x)) (flat table)
krauss@21237
   169
    |> map ((fn th => Pretty.string_of (Pretty.chunks (Display.pretty_goals (Thm.nprems_of th) th))) o thm_of_cell)
krauss@21237
   170
    
bulwahn@21131
   171
fun pr_goal thy t i = 
bulwahn@21131
   172
    let
krauss@21237
   173
      val (_, prems, lhs, rhs, _) = dest_term t 
krauss@21237
   174
      val prterm = string_of_cterm o (cterm_of thy)
bulwahn@21131
   175
    in
krauss@21237
   176
      (* also show prems? *)
bulwahn@21131
   177
        i ^ ") " ^ (prterm lhs) ^ " '<' " ^ (prterm rhs) 
bulwahn@21131
   178
    end
krauss@21237
   179
    
bulwahn@21131
   180
fun pr_fun thy t i =
bulwahn@21131
   181
    (string_of_int i) ^ ") " ^ (string_of_cterm (cterm_of thy t))
krauss@21237
   182
    
bulwahn@21131
   183
fun pr_cell cell = case cell of Less _ => " <  " 
krauss@21237
   184
                              | LessEq _ => " <= " 
krauss@21237
   185
                              | None _ => " N  "
krauss@21237
   186
                              | False _ => " F  "
krauss@21237
   187
                                             
bulwahn@21131
   188
(* fun pr_err: prints the table if tactic failed *)
bulwahn@21131
   189
fun pr_err table thy tl base_funs =  
bulwahn@21131
   190
    let 
krauss@21237
   191
      val gc = map (fn i => chr (i + 96)) (1 upto (length table))
krauss@21237
   192
      val mc = 1 upto (length base_funs)
krauss@21237
   193
      val tstr = ("   " ^ (concat (map (fn i => " " ^ (string_of_int i) ^ "  ") mc))) ::
krauss@21237
   194
                 (map2 (fn r => fn i => i ^ ": " ^ (concat (map pr_cell r))) table gc)
krauss@21237
   195
      val gstr = ("Goals:"::(map2 (pr_goal thy) tl gc))
krauss@21237
   196
      val mstr = ("Measures:"::(map2 (pr_fun thy) base_funs mc))   
krauss@21237
   197
      val ustr = ("Unfinished subgoals:"::(pr_unprovable_subgoals table))
bulwahn@21131
   198
    in
krauss@21237
   199
      tstr @ gstr @ mstr @ ustr
bulwahn@21131
   200
    end
krauss@21237
   201
      
bulwahn@21131
   202
(* the main function: create table, search table, create relation,
bulwahn@21131
   203
   and prove the subgoals *)
bulwahn@21131
   204
fun lexicographic_order_tac (st: thm) = 
bulwahn@21131
   205
    let
krauss@21237
   206
      val thy = theory_of_thm st
krauss@21237
   207
      val termination_thm = ProofContext.get_thm (Variable.thm_context st) (Name "termination")
krauss@21237
   208
      val next_st = SINGLE (rtac termination_thm 1) st |> the
krauss@21237
   209
      val premlist = prems_of next_st
bulwahn@21131
   210
    in
krauss@21237
   211
      case premlist of 
bulwahn@21131
   212
            [] => error "invalid number of subgoals for this tactic - expecting at least 1 subgoal" 
bulwahn@21131
   213
          | (wf::tl) => let
krauss@21237
   214
    val (var, prop) = FundefLib.dest_all wf
krauss@21237
   215
    val rel = HOLogic.dest_Trueprop prop |> dest_wf |> head_of
krauss@21237
   216
    val crel = cterm_of thy rel
krauss@21237
   217
    val base_funs = mk_ext_base_funs (fastype_of var)
krauss@21237
   218
    val _ = writeln "Creating table"
krauss@21237
   219
    val table = map (mk_row thy base_funs) tl
krauss@21237
   220
    val _ = writeln "Searching for lexicographic order"
krauss@21237
   221
    val possible_order = search_table table
krauss@21237
   222
      in
krauss@21237
   223
    case possible_order of 
krauss@21237
   224
        NONE => error (cat_lines ("Could not prove it by lexicographic order:"::(pr_err table thy tl base_funs)))
krauss@21237
   225
      | SOME order  => let
krauss@21237
   226
      val clean_table = map (fn x => map (nth x) order) table
krauss@21237
   227
      val funs = map (nth base_funs) order
krauss@21237
   228
      val list = HOLogic.mk_list (fn x => x) (fastype_of var --> HOLogic.natT) funs
krauss@21237
   229
      val relterm = Abs ("x", fastype_of var, Const(measures, (fastype_of list) --> (range_type (fastype_of rel))) $ list)
krauss@21237
   230
      val crelterm = cterm_of thy relterm
krauss@21237
   231
      val _ = writeln ("Instantiating R with " ^ (string_of_cterm crelterm))
krauss@21237
   232
      val _ = writeln "Proving subgoals"
krauss@21237
   233
        in
krauss@21237
   234
      next_st |> cterm_instantiate [(crel, crelterm)]
krauss@21237
   235
        |> SINGLE (rtac wf_measures 1) |> the
krauss@21237
   236
        |> fold prove_row clean_table
krauss@21237
   237
        |> Seq.single
bulwahn@21131
   238
                    end
bulwahn@21131
   239
            end
bulwahn@21131
   240
    end
bulwahn@21131
   241
krauss@21201
   242
val lexicographic_order = Method.SIMPLE_METHOD lexicographic_order_tac
krauss@21201
   243
krauss@21201
   244
val setup = Method.add_methods [("lexicographic_order", Method.no_args lexicographic_order, "termination prover for lexicographic orderings")]
bulwahn@21131
   245
bulwahn@21131
   246
end