src/HOL/Data_Structures/Define_Time_Function.ML
author wenzelm
Fri, 18 Oct 2024 14:37:09 +0200
changeset 81184 f270cd6ee185
parent 81147 503e5280ba72
child 81255 47530e9a7c33
permissions -rw-r--r--
clarified syntax declarations: keep things together;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     1
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     2
signature TIMING_FUNCTIONS =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     3
sig
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
     4
type 'a wctxt = {
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
     5
  ctxt: local_theory,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
     6
  origins: term list,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
     7
  f: term -> 'a
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
     8
}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     9
type 'a converter = {
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    10
  constc : 'a wctxt -> term -> 'a,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    11
  funcc : 'a wctxt -> term -> term list -> 'a,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    12
  ifc : 'a wctxt -> typ -> term -> term -> term -> 'a,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    13
  casec : 'a wctxt -> term -> term list -> 'a,
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    14
  letc : 'a wctxt -> typ -> term -> (string * typ) list -> term -> 'a
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    15
}
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    16
val walk : local_theory -> term list -> 'a converter -> term -> 'a
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    17
val Iconst : term wctxt -> term -> term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    18
val Ifunc : term wctxt -> term -> term list -> term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    19
val Iif : term wctxt -> typ -> term -> term -> term -> term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    20
val Icase : term wctxt -> term -> term list -> term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    21
val Ilet : term wctxt -> typ -> term -> (string * typ) list -> term -> term
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    22
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    23
type pfunc = { names : string list, terms : term list, typs : typ list }
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    24
val fun_pretty':  Proof.context -> pfunc -> Pretty.T
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    25
val fun_pretty:  Proof.context -> Function.info -> Pretty.T
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    26
val print_timing':  Proof.context -> pfunc -> pfunc -> unit
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    27
val print_timing:  Proof.context -> Function.info -> Function.info -> unit
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    28
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    29
val reg_and_proove_time_func: local_theory -> term list -> term list
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    30
      -> bool -> bool -> Function.info * local_theory
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    31
val reg_time_func: local_theory -> term list -> term list
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    32
      -> bool -> bool -> Function.info * local_theory
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    33
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    34
val time_dom_tac: Proof.context -> thm -> thm list -> int -> tactic
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    35
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    36
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    37
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    38
structure Timing_Functions : TIMING_FUNCTIONS =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    39
struct
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    40
(* Configure config variable to adjust the prefix *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    41
val bprefix = Attrib.setup_config_string @{binding "time_prefix"} (K "T_")
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    42
val bprefix_snd = Attrib.setup_config_string @{binding "time_prefix_snd"} (K "T2_")
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    43
(* Configure config variable to adjust the suffix *)
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    44
val bsuffix = Attrib.setup_config_string @{binding "time_suffix"} (K "")
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    45
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    46
(* some default values to build terms easier *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    47
val zero = Const (@{const_name "Groups.zero"}, HOLogic.natT)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    48
val one = Const (@{const_name "Groups.one"}, HOLogic.natT)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    49
(* Extracts terms from function info *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    50
fun terms_of_info (info: Function.info) =
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    51
  map Thm.prop_of (case #simps info of SOME s => s
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    52
                                     | NONE => error "No terms of function found in info")
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    53
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    54
type pfunc = {
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    55
  names : string list,
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    56
  terms : term list,
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    57
  typs : typ list
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    58
}
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    59
fun info_pfunc (info: Function.info): pfunc =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    60
let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    61
  val {defname, fs, ...} = info;
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    62
  val T = case hd fs of (Const (_,T)) => T
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    63
                      | (Free (_,T)) => T
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
    64
                      | _ => error "Internal error: Invalid info to print"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    65
in
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    66
  { names=[Binding.name_of defname], terms=terms_of_info info, typs=[T] }
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    67
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    68
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    69
(* Auxiliary functions for printing functions *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    70
fun fun_pretty' ctxt (pfunc: pfunc) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    71
let
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    72
  val {names, terms, typs} = pfunc;
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    73
  val header_beg = Pretty.str "fun ";
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    74
  fun prepHeadCont (nm,T) = [Pretty.str (nm ^ " :: "), (Pretty.quote (Syntax.pretty_typ ctxt T))]
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    75
  val header_content =
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    76
     List.concat (prepHeadCont (hd names,hd typs) :: map ((fn l => Pretty.str "\nand " :: l) o prepHeadCont) (ListPair.zip (tl names, tl typs)));
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    77
  val header_end = Pretty.str " where\n  ";
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    78
  val header = [header_beg] @ header_content @ [header_end];
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    79
  fun separate sep prts =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    80
    flat (Library.separate [Pretty.str sep] (map single prts));
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    81
  val ptrms = (separate "\n| " (map (Syntax.pretty_term ctxt) terms));
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    82
in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    83
  Pretty.text_fold (header @ ptrms)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    84
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    85
fun fun_pretty ctxt = fun_pretty' ctxt o info_pfunc
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    86
fun print_timing' ctxt (opfunc: pfunc) (tpfunc: pfunc) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    87
let
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    88
  val {names, ...} = opfunc;
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    89
  val poriginal = Pretty.item [Pretty.str "Original function:\n", fun_pretty' ctxt opfunc]
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    90
  val ptiming = Pretty.item [Pretty.str ("Running time function:\n"), fun_pretty' ctxt tpfunc]
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    91
in
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    92
  Pretty.writeln (Pretty.text_fold [
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    93
      Pretty.str ("Converting " ^ (hd names) ^ (String.concat (map (fn nm => ", " ^ nm) (tl names))) ^ "\n"),
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    94
      poriginal, Pretty.str "\n", ptiming])
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    95
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    96
fun print_timing ctxt (oinfo: Function.info) (tinfo: Function.info) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    97
  print_timing' ctxt (info_pfunc oinfo) (info_pfunc tinfo)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    98
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
    99
fun print_lemma ctxt defs (T_terms: term list) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   100
let
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   101
  val names =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   102
    defs
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   103
    |> map snd
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   104
    |> map (fn s => "_" ^ s)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   105
    |> List.foldr (op ^) ""
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   106
  val begin = "lemma T" ^ names ^ "_simps [simp,code]:\n"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   107
  fun convLine T_term =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   108
    "  \"" ^ Syntax.string_of_term ctxt T_term ^ "\"\n"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   109
  val lines = map convLine T_terms
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   110
  fun convDefs def = " " ^ (fst def)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   111
  val proof = "  by (simp_all add:" :: (map convDefs defs) @ [")"]
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   112
  val _ = Pretty.writeln (Pretty.str "Characteristic recursion equations can be derived:")
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   113
in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   114
  (begin :: lines @ proof)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   115
  |> String.concat
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   116
  (* |> Active.sendback_markup_properties [Markup.padding_fun] *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   117
  |> Pretty.str
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   118
  |> Pretty.writeln
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   119
end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   120
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   121
fun contains l e = exists (fn e' => e' = e) l
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   122
fun contains' comp l e = exists (comp e) l
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   123
(* Split name by . *)
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   124
val split_name = String.fields (fn s => s = #".")
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   125
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   126
(* returns true if it's an if term *)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   127
fun is_if (Const (@{const_name "HOL.If"},_)) = true
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   128
  | is_if _ = false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   129
(* returns true if it's a case term *)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   130
fun is_case (Const (n,_)) = n |> split_name |> List.last |> String.isPrefix "case_"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   131
  | is_case _ = false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   132
(* returns true if it's a let term *)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   133
fun is_let (Const (@{const_name "HOL.Let"},_)) = true
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   134
  | is_let _ = false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   135
(* change type of original function to new type (_ \<Rightarrow> ... \<Rightarrow> _ to _ \<Rightarrow> ... \<Rightarrow> nat)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   136
    and replace all function arguments f with (t*T_f) if used *)
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   137
fun change_typ' used (Type ("fun", [T1, T2])) = 
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   138
      Type ("fun", [check_for_fun' (used 0) T1, change_typ' (fn i => used (i+1)) T2])
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   139
  | change_typ' _ _ = HOLogic.natT
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   140
and check_for_fun' true (f as Type ("fun", [_,_])) = HOLogic.mk_prodT (f, change_typ' (K false) f)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   141
  | check_for_fun' false (f as Type ("fun", [_,_])) = change_typ' (K true) f
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   142
  | check_for_fun' _ t = t
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   143
val change_typ = change_typ' (K true)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   144
(* Convert string name of function to its timing equivalent *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   145
fun fun_name_to_time' ctxt s second name =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   146
let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   147
  val prefix = Config.get ctxt (if second then bprefix_snd else bprefix)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   148
  val suffix = (if s then Config.get ctxt bsuffix else "")
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   149
  fun replace_last_name [n] = [prefix ^ n ^ suffix]
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   150
    | replace_last_name (n::ns) = n :: (replace_last_name ns)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   151
    | replace_last_name _ = error "Internal error: Invalid function name to convert"
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   152
  val parts = split_name name
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   153
in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   154
  String.concatWith "." (replace_last_name parts)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   155
end
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   156
fun fun_name_to_time ctxt s name = fun_name_to_time' ctxt s false name
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   157
(* Count number of arguments of a function *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   158
fun count_args (Type (n, [_,res])) = (if n = "fun" then 1 + count_args res else 0)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   159
  | count_args _ = 0
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   160
(* Check if number of arguments matches function *)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   161
fun check_args s (t, args) =
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   162
    (if length args = count_args (type_of t) then ()
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   163
     else error ("Partial applications/Lambdas not allowed (" ^ s ^ ")"))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   164
(* Removes Abs *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   165
fun rem_abs f (Abs (_,_,t)) = rem_abs f t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   166
  | rem_abs f t = f t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   167
(* Map right side of equation *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   168
fun map_r f (pT $ (eq $ l $ r)) = (pT $ (eq $ l $ f r))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   169
  | map_r _ _ = error "Internal error: No right side of equation found"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   170
(* Get left side of equation *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   171
fun get_l (_ $ (_ $ l $ _)) = l
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   172
  | get_l _ = error "Internal error: No left side of equation found"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   173
(* Get right side of equation *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   174
fun get_r (_ $ (_ $ _ $ r)) = r
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   175
  | get_r _ = error "Internal error: No right side of equation found"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   176
(* Return name of Const *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   177
fun Const_name (Const (nm,_)) = SOME nm
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   178
  | Const_name _ = NONE
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   179
fun is_Used (Type ("Product_Type.prod", _)) = true
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   180
  | is_Used _ = false
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   181
(* Custom compare function for types ignoring variable names *)
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   182
fun typ_comp (Type (A,a)) (Type (B,b)) = (A = B) andalso List.foldl (fn ((c,i),s) => typ_comp c i andalso s) true (ListPair.zip (a, b))
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   183
  | typ_comp (Type _) _ = false
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   184
  | typ_comp _ (Type _) = false
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   185
  | typ_comp _ _ = true
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   186
fun const_comp (Const (nm,T)) (Const (nm',T')) = nm = nm' andalso typ_comp T T'
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   187
  | const_comp _ _ = false
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   188
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   189
fun time_term ctxt s (Const (nm,T)) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   190
let
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   191
  val T_nm = fun_name_to_time ctxt s nm
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   192
  val T_T = change_typ T
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   193
in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   194
(SOME (Syntax.check_term ctxt (Const (T_nm,T_T))))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   195
  handle (ERROR _) =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   196
    case Syntax.read_term ctxt (Long_Name.base_name T_nm)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   197
      of (Const (T_nm,T_T)) =>
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   198
        let
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   199
          fun col_Used i (Type ("fun", [Type ("fun", _), Ts])) (Type ("fun", [T', Ts'])) =
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   200
            (if is_Used T' then [i] else []) @ col_Used (i+1) Ts Ts'
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   201
            | col_Used i (Type ("fun", [_, Ts])) (Type ("fun", [_, Ts'])) = col_Used (i+1) Ts Ts'
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   202
            | col_Used _ _ _ = []
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   203
        in
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   204
          SOME (Const (T_nm,change_typ' (contains (col_Used 0 T T_T)) T))
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   205
        end
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   206
       | _ => error ("Timing function of " ^ nm ^ " is not defined")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   207
end
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   208
  | time_term _ _ _ = error "Internal error: No valid function given"
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   209
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   210
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   211
type 'a wctxt = {
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   212
  ctxt: local_theory,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   213
  origins: term list,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   214
  f: term -> 'a
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   215
}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   216
type 'a converter = {
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   217
  constc : 'a wctxt -> term -> 'a,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   218
  funcc : 'a wctxt -> term -> term list -> 'a,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   219
  ifc : 'a wctxt -> typ -> term -> term -> term -> 'a,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   220
  casec : 'a wctxt -> term -> term list -> 'a,
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   221
  letc : 'a wctxt -> typ -> term -> (string * typ) list -> term -> 'a
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   222
}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   223
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   224
(* Walks over term and calls given converter *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   225
(* get rid and use Term.strip_abs.eta especially for lambdas *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   226
fun build_abs t ((nm,T)::abs) = build_abs (Abs (nm,T,t)) abs
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   227
  | build_abs t [] = t
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   228
fun walk ctxt (origin: term list) (conv as {ifc, casec, funcc, letc, ...} : 'a converter) (t as _ $ _) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   229
  let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   230
    val (f, args) = strip_comb t
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   231
    val this = (walk ctxt origin conv)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   232
    val _ = (case f of Abs _ => error "Lambdas not supported" | _ => ())
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   233
    val wctxt = {ctxt = ctxt, origins = origin, f = this}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   234
  in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   235
    (if is_if f then
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   236
      (case f of (Const (_,T)) =>
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   237
        (case args of [cond, t, f] => ifc wctxt T cond t f
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   238
                   | _ => error "Partial applications not supported (if)")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   239
               | _ => error "Internal error: invalid if term")
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   240
      else if is_case f then casec wctxt f args
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   241
      else if is_let f then
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   242
      (case f of (Const (_,lT)) =>
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   243
         (case args of [exp, t] =>
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   244
            let val (abs,t) = strip_abs t in letc wctxt lT exp abs t end
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   245
                     | _ => error "Partial applications not allowed (let)")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   246
               | _ => error "Internal error: invalid let term")
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   247
      else funcc wctxt f args)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   248
  end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   249
  | walk ctxt origin (conv as {constc, ...}) c = 
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   250
      constc {ctxt = ctxt, origins = origin, f = walk ctxt origin conv} c
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   251
fun Ifunc (wctxt: term wctxt) t args = list_comb (#f wctxt t,map (#f wctxt) args)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   252
val Iconst = K I
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   253
fun Iif (wctxt: term wctxt) T cond tt tf =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   254
  Const (@{const_name "HOL.If"}, T) $ (#f wctxt cond) $ (#f wctxt tt) $ (#f wctxt tf)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   255
fun Icase (wctxt: term wctxt) t cs = list_comb (#f wctxt t,map (#f wctxt) cs)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   256
fun Ilet (wctxt: term wctxt) lT exp abs t =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   257
  Const (@{const_name "HOL.Let"},lT) $ (#f wctxt exp) $ build_abs (#f wctxt t) abs
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   258
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   259
(* 1. Fix all terms *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   260
(* Exchange Var in types and terms to Free *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   261
fun freeTerms (Var(ixn,T)) = Free (fst ixn, T)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   262
  | freeTerms t = t
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   263
fun freeTypes (TVar ((t, _), T)) = TFree (t, T)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   264
  | freeTypes t = t
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   265
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   266
fun noFun (Type ("fun", _)) = error "Functions in datatypes are not allowed in case constructions"
80643
11b8f2e4c3d2 branches of case expressions may need to be eta-expanded
nipkow
parents: 80636
diff changeset
   267
  | noFun T = T
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   268
fun casecBuildBounds n t = if n > 0 then casecBuildBounds (n-1) (t $ (Bound (n-1))) else t
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   269
fun casecAbs wctxt n (Type ("fun",[T,Tr])) (Abs (v,Ta,t)) = (map_atyps noFun T; Abs (v,Ta,casecAbs wctxt n Tr t))
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   270
  | casecAbs wctxt n (Type ("fun",[T,Tr])) t =
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   271
    (map_atyps noFun T; Abs ("uu",T,casecAbs wctxt (n + 1) Tr t))
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   272
  | casecAbs wctxt n _ t = (#f wctxt) (casecBuildBounds n (Term.incr_bv n 0 t))
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   273
fun fixCasecCases _ _ [t] = [t]
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   274
  | fixCasecCases wctxt (Type (_,[T,Tr])) (t::ts) = casecAbs wctxt 0 T t :: fixCasecCases wctxt Tr ts
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   275
  | fixCasecCases _ _ _ = error "Internal error: invalid case types/terms"
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   276
fun fixCasec wctxt (t as Const (_,T)) args =
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   277
      (check_args "cases" (t,args); list_comb (t,fixCasecCases wctxt T args))
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   278
  | fixCasec _ _ _ = error "Internal error: invalid case term"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   279
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   280
fun shortFunc fixedNum (Const (nm,T)) = 
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   281
    Const (nm,T |> strip_type |>> drop fixedNum |> (op --->))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   282
  | shortFunc _ _ = error "Internal error: Invalid term"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   283
fun shortApp fixedNum (c, args) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   284
  (shortFunc fixedNum c, drop fixedNum args)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   285
fun shortOriginFunc (term: term list) fixedNum (f as (c as Const (_,_), _))  =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   286
  if contains' const_comp term c then shortApp fixedNum f else f
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   287
  | shortOriginFunc _ _ t = t
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   288
fun fixTerms ctxt (term: term list) (fixedNum: int) (t as pT $ (eq $ l $ r)) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   289
  let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   290
    val _ = check_args "args" (strip_comb (get_l t))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   291
    val l' = shortApp fixedNum (strip_comb l) |> list_comb
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   292
    val shortOriginFunc' = shortOriginFunc (term |> map (fst o strip_comb)) fixedNum
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   293
    val r' = walk ctxt term {
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   294
          funcc = (fn wctxt => fn t => fn args =>
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   295
              (check_args "func" (t,args); (t, map (#f wctxt) args) |> shortOriginFunc' |> list_comb)),
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   296
          constc = (fn _ => fn c => (case c of Abs _ => error "Lambdas not supported" | _ => c)),
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   297
          ifc = Iif,
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   298
          casec = fixCasec,
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   299
          letc = (fn wctxt => fn expT => fn exp => fn abs => fn t =>
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   300
              let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   301
                val f' = if length abs = 0 then
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   302
                (case (#f wctxt) (t$exp) of t$_ => t | _ => error "Internal error: case could not be fixed (let)")
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   303
                else (#f wctxt) t
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   304
              in (Const (@{const_name "HOL.Let"},expT) $ ((#f wctxt) exp) $ build_abs f' abs) end)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   305
      } r
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   306
  in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   307
    pT $ (eq $ l' $ r')
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   308
  end
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   309
  | fixTerms _ _ _ _ = error "Internal error: invalid term"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   310
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   311
(* 2. Check for properties about the function *)
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   312
(* 2.1 Check if function is recursive *)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   313
fun or f (a,b) = f a orelse b
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   314
fun find_rec ctxt term = (walk ctxt term {
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   315
          funcc = (fn wctxt => fn t => fn args =>
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   316
            List.exists (fn term => (Const_name t) = (Const_name term)) term
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   317
             orelse List.foldr (or (#f wctxt)) false args),
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   318
          constc = (K o K) false,
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   319
          ifc = (fn wctxt => fn _ => fn cond => fn tt => fn tf =>
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   320
            (#f wctxt) cond orelse (#f wctxt) tt orelse (#f wctxt) tf),
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   321
          casec = (fn wctxt => fn t => fn cs =>
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   322
            (#f wctxt) t orelse List.foldr (or (rem_abs (#f wctxt))) false cs),
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   323
          letc = (fn wctxt => fn _ => fn exp => fn _ => fn t =>
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   324
            (#f wctxt) exp orelse (#f wctxt) t)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   325
      }) o get_r
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   326
fun is_rec ctxt (term: term list) = List.foldr (or (find_rec ctxt term)) false
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   327
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   328
(* 3. Convert equations *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   329
(* Some Helper *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   330
val plusTyp = @{typ "nat => nat => nat"}
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   331
fun plus (SOME a) (SOME b) = SOME (Const (@{const_name "Groups.plus"}, plusTyp) $ a $ b)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   332
  | plus (SOME a) NONE = SOME a
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   333
  | plus NONE (SOME b) = SOME b
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   334
  | plus NONE NONE = NONE
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   335
fun opt_term NONE = HOLogic.zero
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   336
  | opt_term (SOME t) = t
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   337
fun use_origin (Free (nm, T as Type ("fun",_))) = HOLogic.mk_fst (Free (nm,HOLogic.mk_prodT (T, change_typ T)))
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   338
  | use_origin t = t
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   339
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   340
(* Conversion of function term *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   341
fun fun_to_time' ctxt (origin: term list) second (func as Const (nm,T)) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   342
let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   343
  val origin' = map (fst o strip_comb) origin
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   344
in
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   345
  if contains' const_comp origin' func then SOME (Free (func |> Term.term_name |> fun_name_to_time' ctxt true second, change_typ T)) else
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   346
  if Zero_Funcs.is_zero (Proof_Context.theory_of ctxt) (nm,T) then NONE else
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   347
    time_term ctxt false func
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   348
end
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   349
  | fun_to_time' _ _ _ (Free (nm,T)) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   350
      SOME (HOLogic.mk_snd (Free (nm,HOLogic.mk_prodT (T,change_typ T))))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   351
  | fun_to_time' _ _ _ _ = error "Internal error: invalid function to convert"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   352
fun fun_to_time context origin func = fun_to_time' context origin false func
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   353
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   354
(* Convert arguments of left side of a term *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   355
fun conv_arg _ (Free (nm,T as Type("fun",_))) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   356
    Free (nm, HOLogic.mk_prodT (T, change_typ' (K false) T))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   357
  | conv_arg _ x = x
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   358
fun conv_args ctxt = map (conv_arg ctxt)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   359
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   360
(* Handle function calls *)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   361
fun build_zero (Type ("fun", [T, R])) = Abs ("uu", T, build_zero R)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   362
  | build_zero _ = zero
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   363
fun funcc_use_origin (Free (nm, T as Type ("fun",_))) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   364
    HOLogic.mk_fst (Free (nm,HOLogic.mk_prodT (T, change_typ T)))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   365
  | funcc_use_origin t = t
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   366
fun funcc_conv_arg _ _ (t as (_ $ _)) = map_aterms funcc_use_origin t
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   367
  | funcc_conv_arg _ u (Free (nm, T as Type ("fun",_))) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   368
      if u then Free (nm, HOLogic.mk_prodT (T, change_typ T))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   369
      else HOLogic.mk_snd (Free (nm,HOLogic.mk_prodT (T,change_typ T)))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   370
  | funcc_conv_arg wctxt true (f as Const (_,T as Type ("fun",_))) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   371
  (Const (@{const_name "Product_Type.Pair"},
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   372
      Type ("fun", [T,Type ("fun", [change_typ T, HOLogic.mk_prodT (T,change_typ T)])]))
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   373
    $ f $ (Option.getOpt (fun_to_time (#ctxt wctxt) (#origins wctxt) f, build_zero T)))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   374
  | funcc_conv_arg wctxt false (f as Const (_,T as Type ("fun",_))) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   375
      Option.getOpt (fun_to_time (#ctxt wctxt) (#origins wctxt) f, build_zero T)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   376
  | funcc_conv_arg _ _ t = t
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   377
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   378
fun funcc_conv_args _ _ [] = []
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   379
  | funcc_conv_args wctxt (Type ("fun", [t, ts])) (a::args) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   380
      funcc_conv_arg wctxt (is_Used t) a :: funcc_conv_args wctxt ts args
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   381
  | funcc_conv_args _ _ _ = error "Internal error: Non matching type"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   382
fun funcc wctxt func args =
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   383
let
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   384
  fun get_T (Free (_,T)) = T
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   385
    | get_T (Const (_,T)) = T
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   386
    | get_T (_ $ (Free (_,Type (_, [_, T])))) = T (* Case of snd was constructed *)
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   387
    | get_T _ = error "Internal error: Forgotten type"
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   388
in
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   389
  List.foldr (I #-> plus)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   390
  (case fun_to_time (#ctxt wctxt) (#origins wctxt) func
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   391
    of SOME t => SOME (list_comb (t,funcc_conv_args wctxt (get_T t) args))
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   392
    | NONE => NONE)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   393
  (map (#f wctxt) args)
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   394
end
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   395
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   396
(* Handle case terms *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   397
fun casecIsCase (Type (n1, [_,Type (n2, _)])) = (n1 = "fun" andalso n2 = "fun")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   398
  | casecIsCase _ = false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   399
fun casecLastTyp (Type (n, [T1,T2])) = Type (n, [T1, change_typ T2])
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   400
  | casecLastTyp _ = error "Internal error: Invalid case type"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   401
fun casecTyp (Type (n, [T1, T2])) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   402
      Type (n, [change_typ T1, (if casecIsCase T2 then casecTyp else casecLastTyp) T2])
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   403
  | casecTyp _ = error "Internal error: Invalid case type"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   404
fun casecAbs f (Abs (v,Ta,t)) = (case casecAbs f t of (nconst,t) => (nconst,Abs (v,Ta,t)))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   405
  | casecAbs f t = (case f t of NONE => (false,HOLogic.zero) | SOME t => (true,t))
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   406
fun casecArgs _ [t] = (false, [map_aterms use_origin t])
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   407
  | casecArgs f (t::ar) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   408
    (case casecAbs f t of (nconst, tt) => 
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   409
      casecArgs f ar ||> (fn ar => tt :: ar) |>> (if nconst then K true else I))
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   410
  | casecArgs _ _ = error "Internal error: Invalid case term"
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   411
fun casec wctxt (Const (t,T)) args =
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   412
  if not (casecIsCase T) then error "Internal error: Invalid case type" else
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   413
    let val (nconst, args') = casecArgs (#f wctxt) args in
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   414
      plus
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   415
        ((#f wctxt) (List.last args))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   416
        (if nconst then
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   417
          SOME (list_comb (Const (t,casecTyp T), args'))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   418
         else NONE)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   419
    end
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   420
  | casec _ _ _ = error "Internal error: Invalid case term"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   421
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   422
(* Handle if terms -> drop the term if true and false terms are zero *)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   423
fun ifc wctxt _ cond tt ft =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   424
  let
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   425
    val f = #f wctxt
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   426
    val rcond = map_aterms use_origin cond
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   427
    val tt = f tt
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   428
    val ft = f ft
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   429
  in
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   430
    plus (f cond) (case (tt,ft) of (NONE, NONE) => NONE | _ =>
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   431
       if tt = ft then tt else
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   432
       (SOME ((Const (@{const_name "HOL.If"}, @{typ "bool \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat"})) $ rcond $ (opt_term tt) $ (opt_term ft))))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   433
  end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   434
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   435
fun letc_change_typ (Type ("fun", [T1, Type ("fun", [T2, _])])) = (Type ("fun", [T1, Type ("fun", [change_typ T2, HOLogic.natT])]))
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   436
  | letc_change_typ _ = error "Internal error: invalid let type"
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   437
fun letc wctxt expT exp abs t =
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   438
    plus (#f wctxt exp)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   439
    (if length abs = 0 (* In case of "length nms = 0" the expression got reducted
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   440
                          Here we need Bound 0 to gain non-partial application *)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   441
    then (case #f wctxt (t $ Bound 0) of SOME (t' $ Bound 0) =>
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   442
                                 (SOME (Const (@{const_name "HOL.Let"}, letc_change_typ expT) $ (map_aterms use_origin exp) $ t'))
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   443
                                  (* Expression is not used and can therefore let be dropped *)
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   444
                                | SOME t' => SOME t'
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   445
                                | NONE => NONE)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   446
    else (case #f wctxt t of SOME t' =>
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   447
      SOME (if Term.is_dependent t' then Const (@{const_name "HOL.Let"}, letc_change_typ expT) $ (map_aterms use_origin exp) $ build_abs t' abs
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   448
                                    else Term.subst_bounds([exp],t'))
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   449
    | NONE => NONE))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   450
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   451
(* The converter for timing functions given to the walker *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   452
val converter : term option converter = {
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   453
        constc = fn _ => fn t =>
80036
a594d22e69d6 updated time functions for Array_Braun
nipkow
parents: 79969
diff changeset
   454
          (case t of Const ("HOL.undefined", _) => SOME (Const ("HOL.undefined", @{typ "nat"}))
a594d22e69d6 updated time functions for Array_Braun
nipkow
parents: 79969
diff changeset
   455
                   | _ => NONE),
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   456
        funcc = funcc,
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   457
        ifc = ifc,
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   458
        casec = casec,
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   459
        letc = letc
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   460
    }
79665
0a152b2f73ae time funs: +1 instead of 1+
nipkow
parents: 79542
diff changeset
   461
fun top_converter is_rec _ _ = opt_term o (fn exp => plus exp (if is_rec then SOME one else NONE))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   462
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   463
(* Use converter to convert right side of a term *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   464
fun to_time ctxt origin is_rec term =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   465
  top_converter is_rec ctxt origin (walk ctxt origin converter term)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   466
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   467
(* Converts a term to its running time version *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   468
fun convert_term ctxt (origin: term list) is_rec (pT $ (Const (eqN, _) $ l $ r)) =
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   469
let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   470
  val (l_const, l_params) = strip_comb l
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   471
in
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   472
    pT
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   473
    $ (Const (eqN, @{typ "nat \<Rightarrow> nat \<Rightarrow> bool"})
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   474
      $ (list_comb (l_const |> fun_to_time ctxt origin |> Option.valOf, l_params |> conv_args ctxt))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   475
      $ (to_time ctxt origin is_rec r))
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   476
end
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   477
  | convert_term _ _ _ _ = error "Internal error: invalid term to convert"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   478
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   479
(* 3.5 Support for locales *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   480
fun replaceFstSndFree ctxt (origin: term list) (rfst: term -> term) (rsnd: term -> term) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   481
  (walk ctxt origin {
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   482
          funcc = fn wctxt => fn t => fn args =>
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   483
            case args of
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   484
                 (f as Free _)::args =>
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   485
                   (case t of
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   486
                       Const ("Product_Type.prod.fst", _) =>
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   487
                        list_comb (rfst (t $ f), map (#f wctxt) args)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   488
                     | Const ("Product_Type.prod.snd", _) =>
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   489
                        list_comb (rsnd (t $ f), map (#f wctxt) args)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   490
                     | t => list_comb (t, map (#f wctxt) (f :: args)))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   491
               | args => list_comb (t, map (#f wctxt) args),
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   492
          constc = Iconst,
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   493
          ifc = Iif,
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   494
          casec = Icase,
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   495
          letc = Ilet
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   496
      })
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   497
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   498
(* 4. Tactic to prove "f_dom n" *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   499
fun time_dom_tac ctxt induct_rule domintros =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   500
  (Induction.induction_tac ctxt true [] [[]] [] (SOME [induct_rule]) []
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   501
    THEN_ALL_NEW ((K (auto_tac ctxt)) THEN' (fn i => FIRST' (
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   502
    (if i <= length domintros then [Metis_Tactic.metis_tac [] ATP_Problem_Generate.combsN ctxt [List.nth (domintros, i-1)]] else []) @
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   503
    [Metis_Tactic.metis_tac [] ATP_Problem_Generate.combsN ctxt domintros]) i)))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   504
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   505
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   506
fun get_terms theory (term: term) =
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   507
let
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   508
  val equations = Spec_Rules.retrieve theory term
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   509
      |> map #rules
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   510
      |> map (map Thm.prop_of)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   511
   handle Empty => error "Function or terms of function not found"
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   512
in
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   513
  equations
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   514
    |> filter (List.exists
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   515
        (fn t => typ_comp (t |> get_l |> strip_comb |> fst |> dest_Const |> snd) (term |> strip_comb |> fst |> dest_Const |> snd)))
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   516
    |> hd
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   517
end
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   518
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   519
(* 5. Check for higher-order function if original function is used \<rightarrow> find simplifications *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   520
fun find_used' T_t =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   521
let
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   522
  val (T_ident, T_args) = strip_comb (get_l T_t)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   523
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   524
  fun filter_passed [] = []
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   525
    | filter_passed ((f as Free (_, Type ("Product_Type.prod",[Type ("fun",_), Type ("fun", _)])))::args) = 
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   526
        f :: filter_passed args
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   527
    | filter_passed (_::args) = filter_passed args
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   528
  val frees = (walk @{context} [] {
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   529
          funcc = (fn wctxt => fn t => fn args =>
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   530
              (case t of (Const ("Product_Type.prod.snd", _)) => []
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   531
                  | _ => (if t = T_ident then [] else filter_passed args)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   532
                    @ List.foldr (fn (l,r) => (#f wctxt) l @ r) [] args)),
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   533
          constc = (K o K) [],
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   534
          ifc = (fn wctxt => fn _ => fn cond => fn tt => fn tf => (#f wctxt) cond @ (#f wctxt) tt @ (#f wctxt) tf),
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   535
          casec = (fn wctxt => fn _ => fn cs => List.foldr (fn (l,r) => (#f wctxt) l @ r) [] cs),
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   536
          letc = (fn wctxt => fn _ => fn exp => fn _ => fn t => (#f wctxt) exp @ (#f wctxt) t)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   537
      }) (get_r T_t)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   538
  fun build _ [] = []
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   539
    | build i (a::args) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   540
        (if contains frees a then [(T_ident,i)] else []) @ build (i+1) args
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   541
in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   542
  build 0 T_args
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   543
end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   544
fun find_simplifyble ctxt term terms =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   545
let
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   546
  val used =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   547
    terms
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   548
    |> List.map find_used'
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   549
    |> List.foldr (op @) []
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   550
  val change =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   551
    Option.valOf o fun_to_time ctxt term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   552
  fun detect t i (Type ("fun",_)::args) = 
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   553
    (if contains used (change t,i) then [] else [i]) @ detect t (i+1) args
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   554
    | detect t i (_::args) = detect t (i+1) args
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   555
    | detect _ _ [] = []
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   556
in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   557
  map (fn t => t |> type_of |> strip_type |> fst |> detect t 0) term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   558
end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   559
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   560
fun define_simp' term simplifyable ctxt =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   561
let
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   562
  val base_name = case Named_Target.locale_of ctxt of
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   563
          NONE => ctxt |> Proof_Context.theory_of |> Context.theory_base_name
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   564
        | SOME nm => nm
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   565
  
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   566
  val orig_name = term |> dest_Const_name |> split_name |> List.last
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   567
  val red_name = fun_name_to_time ctxt false orig_name
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   568
  val name = fun_name_to_time' ctxt true true orig_name
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   569
  val full_name = base_name ^ "." ^ name
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   570
  val def_name = red_name ^ "_def"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   571
  val def = Binding.name def_name
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   572
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   573
  val canon = Syntax.read_term (Local_Theory.exit ctxt) name |> strip_comb
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   574
  val canonFrees = canon |> snd
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   575
  val canonType = canon |> fst |> dest_Const_type |> strip_type |> fst |> take (length canonFrees)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   576
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   577
  val types = term |> dest_Const_type |> strip_type |> fst
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   578
  val vars = Variable.variant_fixes (map (K "") types) ctxt |> fst
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   579
  fun l_typs' i ((T as (Type ("fun",_)))::types) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   580
    (if contains simplifyable i
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   581
     then change_typ T
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   582
     else HOLogic.mk_prodT (T,change_typ T))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   583
    :: l_typs' (i+1) types
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   584
    | l_typs' i (T::types) = T :: l_typs' (i+1) types
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   585
    | l_typs' _ [] = []
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   586
  val l_typs = l_typs' 0 types
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   587
  val lhs =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   588
    List.foldl (fn ((v,T),t) => t $ Free (v,T)) (Free (red_name,l_typs ---> HOLogic.natT)) (ListPair.zip (vars,l_typs))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   589
  fun fixType (TFree _) = HOLogic.natT
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   590
    | fixType T = T
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   591
  fun fixUnspecified T = T |> strip_type ||> fixType |> (op --->)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   592
  fun r_terms' i (v::vars) ((T as (Type ("fun",_)))::types) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   593
    (if contains simplifyable i
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   594
    then HOLogic.mk_prod (Const ("HOL.undefined", fixUnspecified T), Free (v,change_typ T))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   595
    else Free (v,HOLogic.mk_prodT (T,change_typ T)))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   596
    :: r_terms' (i+1) vars types
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   597
    | r_terms' i (v::vars) (T::types) = Free (v,T) :: r_terms' (i+1) vars types
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   598
    | r_terms' _ _ _ = []
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   599
  val r_terms = r_terms' 0 vars types
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   600
  val full_type = (r_terms |> map (type_of) ---> HOLogic.natT)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   601
  val full = list_comb (Const (full_name,canonType ---> full_type), canonFrees)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   602
  val rhs = list_comb (full, r_terms)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   603
  val eq = (lhs, rhs) |> HOLogic.mk_eq |> HOLogic.mk_Trueprop
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   604
  val _ = Pretty.writeln (Pretty.block [Pretty.str "Defining simplified version:\n",
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   605
                                        Syntax.pretty_term ctxt eq])
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   606
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   607
  val (_, ctxt') = Specification.definition NONE [] [] ((def, []), eq) ctxt
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   608
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   609
in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   610
  ((def_name, orig_name), ctxt')
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   611
end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   612
fun define_simp simpables ctxt =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   613
let
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   614
  fun cond ((term,simplifyable),(defs,ctxt)) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   615
    define_simp' term simplifyable ctxt |>> (fn def => def :: defs)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   616
in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   617
  List.foldr cond ([], ctxt) simpables
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   618
end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   619
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   620
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   621
fun replace from to =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   622
  map (map_aterms (fn t => if t = from then to else t))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   623
fun replaceAll [] = I
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   624
  | replaceAll ((from,to)::xs) = replaceAll xs o replace from to
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   625
fun calculateSimplifications ctxt T_terms term simpables =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   626
let
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   627
  (* Show where a simplification can take place *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   628
    fun reportReductions (t,(i::is)) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   629
    (Pretty.writeln (Pretty.str
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   630
      ((Term.term_name t |> fun_name_to_time ctxt true)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   631
        ^ " can be simplified because only the time-function component of parameter "
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   632
        ^ (Int.toString (i + 1)) ^ " is used. "));
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   633
        reportReductions (t,is))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   634
      | reportReductions (_,[]) = ()
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   635
    val _ = simpables
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   636
      |> map reportReductions
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   637
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   638
    (* Register definitions for simplified function *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   639
    val (reds, ctxt) = define_simp simpables ctxt
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   640
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   641
    fun genRetype (Const (nm,T),is) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   642
    let
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   643
      val T_name = fun_name_to_time ctxt true nm |> split_name |> List.last
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   644
      val from = Free (T_name,change_typ T)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   645
      val to = Free (T_name,change_typ' (not o contains is) T)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   646
    in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   647
      (from,to)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   648
    end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   649
      | genRetype _ = error "Internal error: invalid term"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   650
    val retyping = map genRetype simpables
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   651
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   652
    fun replaceArgs (pT $ (eq $ l $ r)) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   653
    let
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   654
      val (t,params) = strip_comb l
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   655
      fun match (Const (f_nm,_),_) = 
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   656
            (fun_name_to_time ctxt true f_nm |> Long_Name.base_name) = (dest_Free t |> fst)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   657
        | match _ = false
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   658
      val simps = List.find match simpables |> Option.valOf |> snd
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   659
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   660
      fun dest_Prod_snd (Free (nm, Type (_, [_, T2]))) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   661
            Free (fun_name_to_time ctxt false nm, T2)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   662
        | dest_Prod_snd _ = error "Internal error: Argument is not a pair"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   663
      fun rep _ [] = ([],[])
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   664
        | rep i (x::xs) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   665
      let 
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   666
        val (rs,args) = rep (i+1) xs
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   667
      in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   668
        if contains simps i
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   669
          then (x::rs,dest_Prod_snd x::args)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   670
          else (rs,x::args)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   671
      end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   672
      val (rs,params) = rep 0 params
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   673
      fun fFst _ = error "Internal error: Invalid term to simplify"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   674
      fun fSnd (t as (Const _ $ f)) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   675
        (if contains rs f
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   676
          then dest_Prod_snd f
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   677
          else t)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   678
        | fSnd t = t
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   679
    in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   680
      (pT $ (eq
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   681
          $ (list_comb (t,params))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   682
          $ (replaceFstSndFree ctxt term fFst fSnd r
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   683
              |> (fn t => replaceAll (map (fn t => (t,dest_Prod_snd t)) rs) [t])
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   684
              |> hd
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   685
            )
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   686
      ))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   687
    end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   688
    | replaceArgs _ = error "Internal error: Invalid term"
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   689
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   690
    (* Calculate reduced terms *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   691
    val T_terms_red = T_terms
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   692
      |> replaceAll retyping
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   693
      |> map replaceArgs
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   694
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   695
    val _ = print_lemma ctxt reds T_terms_red
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   696
    val _ = 
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   697
        Pretty.writeln (Pretty.str "If you do not want the simplified T function, use \"time_fun [no_simp]\"")
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   698
in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   699
  ctxt
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   700
end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   701
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   702
(* Register timing function of a given function *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   703
fun reg_time_func (lthy: local_theory) (term: term list) (terms: term list) print simp =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   704
  let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   705
    val _ =
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   706
      case time_term lthy true (hd term)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   707
            handle (ERROR _) => NONE
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   708
        of SOME _ => error ("Timing function already declared: " ^ (Term.term_name (hd term)))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   709
         | NONE => ()
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   710
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   711
    (* Number of terms fixed by locale *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   712
    val fixedNum = term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   713
      |> hd
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   714
      |> strip_comb |> snd
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   715
      |> length
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   716
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   717
    (* 1. Fix all terms *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   718
    (* Exchange Var in types and terms to Free and check constraints *)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   719
    val terms = map
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   720
      (map_aterms freeTerms
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   721
        #> map_types (map_atyps freeTypes)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   722
        #> fixTerms lthy term fixedNum)
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   723
      terms
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   724
    val fixedFrees = (hd term) |> strip_comb |> snd |> take fixedNum 
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   725
    val fixedFreesNames = map (fst o dest_Free) fixedFrees
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   726
    val term = map (shortFunc fixedNum o fst o strip_comb) term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   727
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   728
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   729
    (* 2. Find properties about the function *)
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   730
    (* 2.1 Check if function is recursive *)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   731
    val is_rec = is_rec lthy term terms
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   732
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   733
    (* 3. Convert every equation
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   734
      - Change type of toplevel equation from _ \<Rightarrow> _ \<Rightarrow> bool to nat \<Rightarrow> nat \<Rightarrow> bool
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   735
      - On left side change name of function to timing function
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   736
      - Convert right side of equation with conversion schema
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   737
    *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   738
    fun fFst (t as (Const (_,T) $ Free (nm,_))) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   739
      (if contains fixedFreesNames nm
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   740
        then Free (nm,strip_type T |>> tl |> (op --->))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   741
        else t)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   742
      | fFst t = t
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   743
    fun fSnd (t as (Const (_,T) $ Free (nm,_))) =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   744
      (if contains fixedFreesNames nm
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   745
        then Free (fun_name_to_time lthy false nm,strip_type T |>> tl |> (op --->))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   746
        else t)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   747
      | fSnd t = t
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   748
    val T_terms = map (convert_term lthy term is_rec) terms
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   749
      |> map (map_r (replaceFstSndFree lthy term fFst fSnd))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   750
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   751
    val simpables = (if simp
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   752
      then find_simplifyble lthy term T_terms
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   753
      else map (K []) term)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   754
      |> (fn s => ListPair.zip (term,s))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   755
    (* Determine if something is simpable, if so rename everything *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   756
    val simpable = simpables |> map snd |> exists (not o null)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   757
    (* Rename to secondary if simpable *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   758
    fun genRename (t,_) =
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   759
      let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   760
        val old = fun_to_time lthy term t |> Option.valOf
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   761
        val new = fun_to_time' lthy term true t |> Option.valOf
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   762
      in
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   763
        (old,new)
80624
9f8034d29365 time_function T_map can now be generated automatically.
nipkow
parents: 80036
diff changeset
   764
      end
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   765
    val can_T_terms = if simpable 
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   766
      then replaceAll (map genRename simpables) T_terms
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   767
      else T_terms
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   768
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   769
    (* 4. Register function and prove completeness *)
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   770
    val names = map Term.term_name term
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   771
    val timing_names = map (fun_name_to_time' lthy true simpable) names
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   772
    val bindings = map (fn nm => (Binding.name nm, NONE, NoSyn)) timing_names
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   773
    fun pat_completeness_auto ctxt =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   774
      Pat_Completeness.pat_completeness_tac ctxt 1 THEN auto_tac ctxt
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   775
    val specs = map (fn eq => (((Binding.empty, []), eq), [], [])) can_T_terms
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   776
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   777
    (* Context for printing without showing question marks *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   778
    val print_ctxt = lthy
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   779
      |> Config.put show_question_marks false
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   780
      |> Config.put show_sorts false (* Change it for debugging *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   781
    val print_ctxt = List.foldl (fn (term, ctxt) => Variable.add_fixes_implicit term ctxt) print_ctxt (term @ T_terms)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   782
    (* Print result if print *)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   783
    val _ = if not print then () else
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   784
        let
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   785
          val nms = map (dest_Const_name) term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   786
          val typs = map (dest_Const_type) term
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   787
        in
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   788
          print_timing' print_ctxt { names=nms, terms=terms, typs=typs }
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   789
            { names=timing_names, terms=can_T_terms, typs=map change_typ typs }
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   790
        end
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   791
    
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   792
    (* For partial functions sequential=true is needed in order to support them
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   793
       We need sequential=false to support the automatic proof of termination over dom
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   794
    *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   795
    fun register seq =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   796
      let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   797
        val _ = (if seq then warning "Falling back on sequential function..." else ())
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   798
        val fun_config = Function_Common.FunctionConfig
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   799
          {sequential=seq, default=NONE, domintros=true, partials=false}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   800
      in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   801
        Function.add_function bindings specs fun_config pat_completeness_auto lthy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   802
      end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   803
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   804
    val (info,ctxt) = 
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   805
      register false
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   806
        handle (ERROR _) =>
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   807
          register true
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   808
             | Match =>
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   809
          register true
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   810
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   811
    val ctxt = if simpable then calculateSimplifications ctxt T_terms term simpables else ctxt
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   812
  in
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   813
    (info,ctxt)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   814
  end
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   815
fun proove_termination (term: term list) terms (T_info: Function.info, lthy: local_theory) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   816
  let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   817
    (* Start proving the termination *)  
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   818
    val infos = SOME (map (Function.get_info lthy) term) handle Empty => NONE
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   819
    val timing_names = map (fun_name_to_time lthy true o Term.term_name) term
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   820
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   821
    (* Proof by lexicographic_order_tac *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   822
    val (time_info, lthy') =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   823
      (Function.prove_termination NONE
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   824
        (Lexicographic_Order.lexicographic_order_tac false lthy) lthy)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   825
        handle (ERROR _) =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   826
        let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   827
          val _ = warning "Falling back on proof over dom..."
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   828
          val _ = (if length term > 1 then error "Proof over dom not supported for mutual recursive functions" else ())
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   829
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   830
          fun args (a$(Var ((nm,_),T))) = args a |> (fn ar => (nm,T)::ar)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   831
            | args (a$(Const (_,T))) = args a |> (fn ar => ("uu",T)::ar)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   832
            | args _ = []
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   833
          val dom_vars =
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   834
            terms |> hd |> get_l |> map_types (map_atyps freeTypes)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   835
            |> args |> Variable.variant_frees lthy []
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   836
          val dom_args = 
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   837
            List.foldl (fn (t,p) => HOLogic.mk_prod ((Free t),p)) (Free (hd dom_vars)) (tl dom_vars)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   838
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   839
          val {inducts, ...} = case infos of SOME [i] => i | _ => error "Proof over dom failed as no induct rule was found"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   840
          val induct = (Option.valOf inducts |> hd)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   841
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   842
          val domintros = Proof_Context.get_fact lthy (Facts.named (hd timing_names ^ ".domintros"))
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   843
          val prop = HOLogic.mk_Trueprop (#dom T_info $ dom_args)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   844
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   845
          (* Prove a helper lemma *)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   846
          val dom_lemma = Goal.prove lthy (map fst dom_vars) [] prop
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   847
            (fn {context, ...} => HEADGOAL (time_dom_tac context induct domintros))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   848
          (* Add dom_lemma to simplification set *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   849
          val simp_lthy = Simplifier.add_simp dom_lemma lthy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   850
        in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   851
          (* Use lemma to prove termination *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   852
          Function.prove_termination NONE
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   853
            (auto_tac simp_lthy) lthy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   854
        end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   855
  in
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   856
    (time_info, lthy')
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   857
  end
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   858
fun reg_and_proove_time_func (lthy: local_theory) (term: term list) (terms: term list) print simp =
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   859
  reg_time_func lthy term terms print simp
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   860
  |> proove_termination term terms
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   861
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   862
fun fix_definition (Const ("Pure.eq", _) $ l $ r) = Const ("HOL.Trueprop", @{typ "bool \<Rightarrow> prop"})
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   863
      $ (Const ("HOL.eq", @{typ "bool \<Rightarrow> bool \<Rightarrow> bool"}) $ l $ r)
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   864
  | fix_definition t = t
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   865
fun check_definition [t] = [t]
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   866
  | check_definition _ = error "Only a single definition is allowed"
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   867
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   868
fun isTypeClass' (Const (nm,_)) =
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   869
  (case split_name nm |> rev
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   870
    of (_::nm::_) => String.isSuffix "_class" nm
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   871
     | _ => false)
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   872
  | isTypeClass' _ = false
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   873
val isTypeClass =
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   874
  (List.foldr (fn (a,b) => a orelse b) false) o (map isTypeClass')
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   875
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   876
fun detect_typ (ctxt: local_theory) (term: term) =
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   877
let
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   878
  val class_term =  (case term of Const (nm,_) => Syntax.read_term ctxt nm
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   879
      | _ => error "Could not find term of class")
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   880
  fun find_free (Type (_,class)) (Type (_,inst)) =
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   881
        List.foldl (fn ((c,i),s) => (case s of NONE => find_free c i | t => t)) (NONE) (ListPair.zip (class, inst))
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   882
    | find_free (TFree _) (TFree _) = NONE
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   883
    | find_free (TFree _) (Type (nm,_)) = SOME nm
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   884
    | find_free  _ _ = error "Unhandled case in detecting type"
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   885
in
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   886
  find_free (type_of class_term) (type_of term)
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   887
    |> Option.map (hd o rev o split_name)
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   888
end
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   889
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   890
fun set_suffix (fterms: term list) ctxt =
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   891
let
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   892
  val isTypeClass = isTypeClass fterms
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   893
  val _ = (if length fterms > 1 andalso isTypeClass then error "No mutual recursion inside instantiation allowed" else ())
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   894
  val suffix = (if isTypeClass then detect_typ ctxt (hd fterms) else NONE)
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   895
in
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   896
  (case suffix of NONE => I | SOME s => Config.put bsuffix ("_" ^ s)) ctxt
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   897
end
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   898
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   899
fun check_opts [] = false
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   900
  | check_opts ["no_simp"] = true
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   901
  | check_opts (a::_) = error ("Option " ^ a ^ " is not defined")
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   902
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   903
(* Convert function into its timing function (called by command) *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   904
fun reg_time_fun_cmd ((opts, funcs), thms) (ctxt: local_theory) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   905
let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   906
  val no_simp = check_opts opts
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   907
  val fterms = map (Syntax.read_term ctxt) funcs
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   908
  val ctxt = set_suffix fterms ctxt
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   909
  val (_, ctxt') = reg_and_proove_time_func ctxt fterms
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   910
    (case thms of NONE => get_terms ctxt (hd fterms)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   911
                | SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   912
    true (not no_simp)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   913
in ctxt'
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   914
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   915
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   916
(* Convert function into its timing function (called by command) with termination proof provided by user*)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   917
fun reg_time_function_cmd ((opts, funcs), thms) (ctxt: local_theory) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   918
let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   919
  val no_simp = check_opts opts
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   920
  val fterms = map (Syntax.read_term ctxt) funcs
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   921
  val ctxt = set_suffix fterms ctxt
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   922
  val ctxt' = reg_time_func ctxt fterms
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   923
    (case thms of NONE => get_terms ctxt (hd fterms)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   924
                | SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   925
    true (not no_simp)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   926
    |> snd
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   927
in ctxt'
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   928
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   929
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   930
(* Convert function into its timing function (called by command) *)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   931
fun reg_time_definition_cmd ((opts, funcs), thms) (ctxt: local_theory) =
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   932
let
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   933
  val no_simp = check_opts opts
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   934
  val fterms = map (Syntax.read_term ctxt) funcs
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   935
  val ctxt = set_suffix fterms ctxt
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   936
  val (_, ctxt') = reg_and_proove_time_func ctxt fterms
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   937
    (case thms of NONE => get_terms ctxt (hd fterms) |> check_definition |> map fix_definition
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   938
                | SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of)
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   939
    true (not no_simp)
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   940
in ctxt'
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   941
end
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   942
81147
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   943
val parser = (Parse.opt_attribs >> map (fst o Token.name_of_src))
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   944
             -- Scan.repeat1 Parse.prop
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   945
             -- Scan.option (Parse.keyword_improper "equations" -- Parse.thms1 >> snd)
503e5280ba72 new HO time functions
nipkow
parents: 80734
diff changeset
   946
val _ = Toplevel.local_theory
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   947
val _ = Outer_Syntax.local_theory @{command_keyword "time_fun"}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   948
  "Defines runtime function of a function"
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   949
  (parser >> reg_time_fun_cmd)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   950
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   951
val _ = Outer_Syntax.local_theory @{command_keyword "time_function"}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   952
  "Defines runtime function of a function"
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   953
  (parser >> reg_time_function_cmd)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   954
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   955
val _ = Outer_Syntax.local_theory @{command_keyword "time_definition"}
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   956
  "Defines runtime function of a definition"
80734
7054a1bc8347 new version of time_fun that works for classes; define T_length automatically now
nipkow
parents: 80657
diff changeset
   957
  (parser >> reg_time_definition_cmd)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   958
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   959
end