src/HOL/Data_Structures/Define_Time_Function.ML
author nipkow
Wed, 27 Mar 2024 16:48:23 +0100
changeset 80036 a594d22e69d6
parent 79969 4aeb25ba90f3
child 80624 9f8034d29365
permissions -rw-r--r--
updated time functions for Array_Braun
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
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     4
type 'a converter = {
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
     5
  constc : local_theory -> term list -> (term -> 'a) -> term -> 'a,
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
     6
  funcc : local_theory -> term list -> (term -> 'a) -> term -> term list -> 'a,
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
     7
  ifc : local_theory -> term list -> (term -> 'a) -> typ -> term -> term -> term -> 'a,
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
     8
  casec : local_theory -> term list -> (term -> 'a) -> term -> term list -> 'a,
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
     9
  letc : local_theory -> term list -> (term -> 'a) -> typ -> term -> string list -> typ list -> term -> 'a
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    10
};
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    11
val walk : local_theory -> term list -> 'a converter -> term -> 'a
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    12
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    13
type pfunc = { names : string list, terms : term list, typs : typ list }
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    14
val fun_pretty':  Proof.context -> pfunc -> Pretty.T
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    15
val fun_pretty:  Proof.context -> Function.info -> Pretty.T
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    16
val print_timing':  Proof.context -> pfunc -> pfunc -> unit
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    17
val print_timing:  Proof.context -> Function.info -> Function.info -> unit
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    18
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
    19
val reg_and_proove_time_func: theory -> term list -> term list
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    20
      -> bool -> Function.info * theory
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
    21
val reg_time_func: theory -> term list -> term list
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
    22
      -> bool -> theory
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    23
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    24
val time_dom_tac: Proof.context -> thm -> thm list -> int -> tactic
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    25
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    26
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    27
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    28
structure Timing_Functions : TIMING_FUNCTIONS =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    29
struct
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    30
(* Configure config variable to adjust the prefix *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    31
val bprefix = Attrib.setup_config_string @{binding "time_prefix"} (K "T_")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    32
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    33
(* some default values to build terms easier *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    34
val zero = Const (@{const_name "Groups.zero"}, HOLogic.natT)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    35
val one = Const (@{const_name "Groups.one"}, HOLogic.natT)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    36
(* Extracts terms from function info *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    37
fun terms_of_info (info: Function.info) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    38
let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    39
  val {simps, ...} = info
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    40
in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    41
  map Thm.prop_of (case simps of SOME s => s | NONE => error "No terms of function found in info")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    42
end;
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    43
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    44
type pfunc = {
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    45
  names : string list,
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    46
  terms : term list,
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    47
  typs : typ list
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    48
}
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    49
fun info_pfunc (info: Function.info): pfunc =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    50
let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    51
  val {defname, fs, ...} = info;
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    52
  val T = case hd fs of (Const (_,T)) => T | _ => error "Internal error: Invalid info to print"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    53
in
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    54
  { names=[Binding.name_of defname], terms=terms_of_info info, typs=[T] }
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    55
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    56
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    57
(* Auxiliary functions for printing functions *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    58
fun fun_pretty' ctxt (pfunc: pfunc) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    59
let
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    60
  val {names, terms, typs} = pfunc;
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    61
  val header_beg = Pretty.str "fun ";
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    62
  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
    63
  val header_content =
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    64
     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
    65
  val header_end = Pretty.str " where\n  ";
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    66
  val header = [header_beg] @ header_content @ [header_end];
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    67
  fun separate sep prts =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    68
    flat (Library.separate [Pretty.str sep] (map single prts));
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    69
  val ptrms = (separate "\n| " (map (Syntax.pretty_term ctxt) terms));
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    70
in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    71
  Pretty.text_fold (header @ ptrms)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    72
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    73
fun fun_pretty ctxt = fun_pretty' ctxt o info_pfunc
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    74
fun print_timing' ctxt (opfunc: pfunc) (tpfunc: pfunc) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    75
let
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    76
  val {names, ...} = opfunc;
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    77
  val poriginal = Pretty.item [Pretty.str "Original function:\n", fun_pretty' ctxt opfunc]
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    78
  val ptiming = Pretty.item [Pretty.str ("Running time function:\n"), fun_pretty' ctxt tpfunc]
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    79
in
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
    80
  Pretty.writeln (Pretty.text_fold [Pretty.str ("Converting " ^ (hd names) ^ (String.concat (map (fn nm => ", " ^ nm) (tl names))) ^ "\n"), poriginal, Pretty.str "\n", ptiming])
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    81
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    82
fun print_timing ctxt (oinfo: Function.info) (tinfo: Function.info) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    83
  print_timing' ctxt (info_pfunc oinfo) (info_pfunc tinfo)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    84
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    85
val If_name = @{const_name "HOL.If"}
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    86
val Let_name = @{const_name "HOL.Let"}
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    87
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    88
(* returns true if it's an if term *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    89
fun is_if (Const (n,_)) = (n = If_name)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    90
  | is_if _ = false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    91
(* returns true if it's a case term *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    92
fun is_case (Const (n,_)) = String.isPrefix "case_" (List.last (String.fields (fn s => s = #".") n))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    93
  | is_case _ = false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    94
(* returns true if it's a let term *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    95
fun is_let (Const (n,_)) = (n = Let_name)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    96
  | is_let _ = false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    97
(* change type of original function to new type (_ \<Rightarrow> ... \<Rightarrow> _ to _ \<Rightarrow> ... \<Rightarrow> nat)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    98
    and replace all function arguments f with (t*T_f) *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    99
fun change_typ (Type ("fun", [T1, T2])) = Type ("fun", [check_for_fun T1, change_typ T2])
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   100
  | change_typ _ = HOLogic.natT
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   101
and check_for_fun (f as Type ("fun", [_,_])) = HOLogic.mk_prodT (f, change_typ f)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   102
  | check_for_fun (Type ("Product_Type.prod", [t1,t2])) = HOLogic.mk_prodT (check_for_fun t1, check_for_fun t2)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   103
  | check_for_fun f = f
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   104
(* Convert string name of function to its timing equivalent *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   105
fun fun_name_to_time ctxt name =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   106
let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   107
  val prefix = Config.get ctxt bprefix
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   108
  fun replace_last_name [n] = [prefix ^ n]
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   109
    | replace_last_name (n::ns) = n :: (replace_last_name ns)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   110
    | replace_last_name _ = error "Internal error: Invalid function name to convert"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   111
  val parts = String.fields (fn s => s = #".") name
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   112
in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   113
  String.concatWith "." (replace_last_name parts)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   114
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   115
(* Count number of arguments of a function *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   116
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
   117
  | count_args _ = 0
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   118
(* Check if number of arguments matches function *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   119
fun check_args s (Const (_,T), args) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   120
    (if length args = count_args T then () else error ("Partial applications/Lambdas not allowed (" ^ s ^ ")"))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   121
  | check_args s (Free (_,T), args) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   122
    (if length args = count_args T then () else error ("Partial applications/Lambdas not allowed (" ^ s ^ ")"))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   123
  | check_args s _ = error ("Partial applications/Lambdas not allowed (" ^ s ^ ")")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   124
(* Removes Abs *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   125
fun rem_abs f (Abs (_,_,t)) = rem_abs f t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   126
  | rem_abs f t = f t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   127
(* Map right side of equation *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   128
fun map_r f (pT $ (eq $ l $ r)) = (pT $ (eq $ l $ f r))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   129
  | map_r _ _ = error "Internal error: No right side of equation found"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   130
(* Get left side of equation *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   131
fun get_l (_ $ (_ $ l $ _)) = l
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   132
  | get_l _ = error "Internal error: No left side of equation found"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   133
(* Get right side of equation *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   134
fun get_r (_ $ (_ $ _ $ r)) = r
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   135
  | get_r _ = error "Internal error: No right side of equation found"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   136
(* Return name of Const *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   137
fun Const_name (Const (nm,_)) = SOME nm
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   138
  | Const_name _ = NONE
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   139
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   140
fun time_term ctxt (Const (nm,T)) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   141
let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   142
  val T_nm = fun_name_to_time ctxt nm
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   143
  val T_T = change_typ T
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   144
in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   145
(SOME (Syntax.check_term ctxt (Const (T_nm,T_T))))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   146
  handle (ERROR _) =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   147
    case Syntax.read_term ctxt (Long_Name.base_name T_nm)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   148
      of (Const (nm,_)) => SOME (Const (nm,T_T))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   149
       | _ => error ("Timing function of " ^ nm ^ " is not defined")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   150
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   151
  | time_term _ _ = error "Internal error: No valid function given"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   152
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   153
type 'a converter = {
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   154
  constc : local_theory -> term list -> (term -> 'a) -> term -> 'a,
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   155
  funcc : local_theory -> term list -> (term -> 'a) -> term -> term list -> 'a,
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   156
  ifc : local_theory -> term list -> (term -> 'a) -> typ -> term -> term -> term -> 'a,
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   157
  casec : local_theory -> term list -> (term -> 'a) -> term -> term list -> 'a,
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   158
  letc : local_theory -> term list -> (term -> 'a) -> typ -> term -> string list -> typ list -> term -> 'a
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   159
};
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   160
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   161
(* Walks over term and calls given converter *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   162
fun walk_func (t1 $ t2) ts = walk_func t1 (t2::ts)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   163
  | walk_func t ts = (t, ts)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   164
fun build_func (f, []) = f
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   165
  | build_func (f, (t::ts)) = build_func (f$t, ts)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   166
fun walk_abs (Abs (nm,T,t)) nms Ts = walk_abs t (nm::nms) (T::Ts)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   167
  | walk_abs t nms Ts = (t, nms, Ts)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   168
fun build_abs t (nm::nms) (T::Ts) = build_abs (Abs (nm,T,t)) nms Ts
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   169
  | build_abs t [] [] = t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   170
  | build_abs _ _ _ = error "Internal error: Invalid terms to build abs"
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   171
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
   172
  let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   173
    val (f, args) = walk_func t []
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   174
    val this = (walk ctxt origin conv)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   175
    val _ = (case f of Abs _ => error "Lambdas not supported" | _ => ())
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   176
  in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   177
    (if is_if f then
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   178
      (case f of (Const (_,T)) =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   179
        (case args of [cond, t, f] => ifc ctxt origin this T cond t f
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   180
                   | _ => error "Partial applications not supported (if)")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   181
               | _ => error "Internal error: invalid if term")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   182
      else if is_case f then casec ctxt origin this f args
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   183
      else if is_let f then
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   184
      (case f of (Const (_,lT)) =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   185
         (case args of [exp, t] => 
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   186
            let val (t,nms,Ts) = walk_abs t [] [] in letc ctxt origin this lT exp nms Ts t end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   187
                     | _ => error "Partial applications not allowed (let)")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   188
               | _ => error "Internal error: invalid let term")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   189
      else funcc ctxt origin this f args)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   190
  end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   191
  | walk ctxt origin (conv as {constc, ...}) c = 
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   192
      constc ctxt origin (walk ctxt origin conv) c
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   193
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   194
(* 1. Fix all terms *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   195
(* Exchange Var in types and terms to Free *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   196
fun fixTerms (Var(ixn,T)) = Free (fst ixn, T)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   197
  | fixTerms t = t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   198
fun fixTypes (TVar ((t, _), T)) = TFree (t, T)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   199
  | fixTypes t = t
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   200
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   201
fun noFun (Type ("fun",_)) = error "Functions in datatypes are not allowed in case constructions"
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   202
  | noFun _ = ()
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   203
fun casecBuildBounds n t = if n > 0 then casecBuildBounds (n-1) (t $ (Bound (n-1))) else t
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   204
fun casecAbs ctxt f n (Type (_,[T,Tr])) (Abs (v,Ta,t)) = (noFun T; Abs (v,Ta,casecAbs ctxt f n Tr t))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   205
  | casecAbs ctxt f n (Type (Tn,[T,Tr])) t =
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   206
    (noFun T; case Variable.variant_fixes ["x"] ctxt of ([v],ctxt) =>
79714
80cb54976c1c timing function generation bug fix by Jonas Stahl
nipkow
parents: 79665
diff changeset
   207
    (if Tn = "fun" then Abs (v,T,casecAbs ctxt f (n + 1) Tr t) else f t)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   208
    | _ => error "Internal error: could not fix variable")
79714
80cb54976c1c timing function generation bug fix by Jonas Stahl
nipkow
parents: 79665
diff changeset
   209
  | casecAbs _ f n _ t = f (casecBuildBounds n (Term.incr_bv n 0 t))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   210
fun fixCasecCases _ _ _ [t] = [t]
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   211
  | fixCasecCases ctxt f (Type (_,[T,Tr])) (t::ts) = casecAbs ctxt f 0 T t :: fixCasecCases ctxt f Tr ts
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   212
  | fixCasecCases _ _ _ _ = error "Internal error: invalid case types/terms"
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   213
fun fixCasec ctxt _ f (t as Const (_,T)) args =
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   214
      (check_args "cases" (t,args); build_func (t,fixCasecCases ctxt f T args))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   215
  | fixCasec _ _ _ _ _ = error "Internal error: invalid case term"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   216
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   217
fun fixPartTerms ctxt (term: term list) t =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   218
  let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   219
    val _ = check_args "args" (walk_func (get_l t) [])
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   220
  in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   221
    map_r (walk ctxt term {
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   222
          funcc = (fn _ => fn _ => fn f => fn t => fn args =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   223
              (check_args "func" (t,args); build_func (t, map f args))),
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   224
          constc = (fn _ => fn _ => fn _ => fn c => (case c of Abs _ => error "Lambdas not supported" | _ => c)),
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   225
          ifc = (fn _ => fn _ => fn f => fn T => fn cond => fn tt => fn tf =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   226
            ((Const (If_name, T)) $ f cond $ (f tt) $ (f tf))),
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   227
          casec = fixCasec,
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   228
          letc = (fn _ => fn _ => fn f => fn expT => fn exp => fn nms => fn Ts => fn t =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   229
              let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   230
                val f' = if length nms = 0 then
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   231
                (case f (t$exp) of t$_ => t | _ => error "Internal error: case could not be fixed (let)")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   232
                else f t
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   233
              in (Const (Let_name,expT) $ (f exp) $ build_abs f' nms Ts) end)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   234
      }) t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   235
  end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   236
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   237
(* 2. Check if function is recursive *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   238
fun or f (a,b) = f a orelse b
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   239
fun find_rec ctxt term = (walk ctxt term {
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   240
          funcc = (fn _ => fn _ => fn f => fn t => fn args => List.exists (fn term => Const_name t = Const_name term) term orelse List.foldr (or f) false args),
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   241
          constc = (K o K o K o K) false,
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   242
          ifc = (fn _ => fn _ => fn f => fn _ => fn cond => fn tt => fn tf => f cond orelse f tt orelse f tf),
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   243
          casec = (fn _ => fn _ => fn f => fn t => fn cs => f t orelse List.foldr (or (rem_abs f)) false cs),
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   244
          letc = (fn _ => fn _ => fn f => fn _ => fn exp => fn _ => fn _ => fn t => f exp orelse f t)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   245
      }) o get_r
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   246
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
   247
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   248
(* 3. Convert equations *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   249
(* Some Helper *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   250
val plusTyp = @{typ "nat => nat => nat"}
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   251
fun plus (SOME a) (SOME b) = SOME (Const (@{const_name "Groups.plus"}, plusTyp) $ a $ b)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   252
  | plus (SOME a) NONE = SOME a
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   253
  | plus NONE (SOME b) = SOME b
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   254
  | plus NONE NONE = NONE
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   255
fun opt_term NONE = HOLogic.zero
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   256
  | opt_term (SOME t) = t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   257
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   258
(* Converting of function term *)
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   259
fun fun_to_time ctxt (origin: term list) (func as Const (nm,T)) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   260
let
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   261
  val full_name_origin = map (fst o dest_Const) origin
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   262
  val prefix = Config.get ctxt bprefix
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   263
in
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   264
  if List.exists (fn nm_orig => nm = nm_orig) full_name_origin then SOME (Free (prefix ^ Term.term_name func, change_typ T)) else
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   265
  if Zero_Funcs.is_zero (Proof_Context.theory_of ctxt) (nm,T) then NONE else
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   266
    time_term ctxt func
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   267
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   268
  | fun_to_time _ _ (Free (nm,T)) = SOME (HOLogic.mk_snd (Free (nm,HOLogic.mk_prodT (T,change_typ T))))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   269
  | fun_to_time _ _ _ = error "Internal error: invalid function to convert"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   270
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   271
(* Convert arguments of left side of a term *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   272
fun conv_arg _ _ (Free (nm,T as Type("fun",_))) = Free (nm, HOLogic.mk_prodT (T, change_typ T))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   273
  | conv_arg ctxt origin (f as Const (_,T as Type("fun",_))) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   274
  (Const (@{const_name "Product_Type.Pair"},
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   275
      Type ("fun", [T,Type ("fun",[change_typ T, HOLogic.mk_prodT (T,change_typ T)])]))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   276
    $ f $ (fun_to_time ctxt origin f |> Option.valOf))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   277
  | conv_arg ctxt origin ((Const ("Product_Type.Pair", _)) $ l $ r) = HOLogic.mk_prod (conv_arg ctxt origin l, conv_arg ctxt origin r)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   278
  | conv_arg _ _ x = x
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   279
fun conv_args ctxt origin = map (conv_arg ctxt origin)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   280
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   281
(* Handle function calls *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   282
fun build_zero (Type ("fun", [T, R])) = Abs ("x", T, build_zero R)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   283
  | build_zero _ = zero
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   284
fun funcc_use_origin _ _ (Free (nm, T as Type ("fun",_))) = HOLogic.mk_fst (Free (nm,HOLogic.mk_prodT (T, change_typ T)))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   285
  | funcc_use_origin _ _ t = t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   286
fun funcc_conv_arg ctxt origin (t as (_ $ _)) = map_aterms (funcc_use_origin ctxt origin) t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   287
  | funcc_conv_arg _ _ (Free (nm, T as Type ("fun",_))) = (Free (nm, HOLogic.mk_prodT (T, change_typ T)))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   288
  | funcc_conv_arg ctxt origin (f as Const (_,T as Type ("fun",_))) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   289
  (Const (@{const_name "Product_Type.Pair"},
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   290
      Type ("fun", [T,Type ("fun",[change_typ T, HOLogic.mk_prodT (T,change_typ T)])]))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   291
    $ f $ (Option.getOpt (fun_to_time ctxt origin f, build_zero T)))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   292
  | funcc_conv_arg _ _ t = t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   293
fun funcc_conv_args ctxt origin = map (funcc_conv_arg ctxt origin)
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   294
fun funcc ctxt (origin: term list) f func args = List.foldr (I #-> plus)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   295
  (case fun_to_time ctxt origin func of SOME t => SOME (build_func (t,funcc_conv_args ctxt origin args))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   296
                                      | NONE => NONE)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   297
  (map f args)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   298
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   299
(* Handle case terms *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   300
fun casecIsCase (Type (n1, [_,Type (n2, _)])) = (n1 = "fun" andalso n2 = "fun")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   301
  | casecIsCase _ = false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   302
fun casecLastTyp (Type (n, [T1,T2])) = Type (n, [T1, change_typ T2])
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   303
  | casecLastTyp _ = error "Internal error: invalid case type"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   304
fun casecTyp (Type (n, [T1, T2])) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   305
      Type (n, [change_typ T1, (if casecIsCase T2 then casecTyp else casecLastTyp) T2])
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   306
  | casecTyp _ = error "Internal error: invalid case type"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   307
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
   308
  | casecAbs f t = (case f t of NONE => (false,HOLogic.zero) | SOME t => (true,t))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   309
fun casecArgs _ [t] = (false, [t])
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   310
  | casecArgs f (t::ar) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   311
    (case casecAbs f t of (nconst, tt) => 
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   312
      casecArgs f ar ||> (fn ar => tt :: ar) |>> (if nconst then K true else I))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   313
  | casecArgs _ _ = error "Internal error: invalid case term"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   314
fun casec _ _ f (Const (t,T)) args =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   315
  if not (casecIsCase T) then error "Internal error: invalid case type" else
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   316
    let val (nconst, args') = casecArgs f args in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   317
      plus
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   318
        (f (List.last args))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   319
        (if nconst then
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   320
          SOME (build_func (Const (t,casecTyp T), args'))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   321
         else NONE)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   322
    end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   323
  | casec _ _ _ _ _ = error "Internal error: invalid case term"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   324
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   325
(* Handle if terms -> drop the term if true and false terms are zero *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   326
fun ifc ctxt origin f _ cond tt ft =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   327
  let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   328
    fun use_origin _ _ (Free (nm, T as Type ("fun",_))) = HOLogic.mk_fst (Free (nm,HOLogic.mk_prodT (T, change_typ T)))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   329
      | use_origin _ _ t = t
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   330
    val rcond = map_aterms (use_origin ctxt origin) cond
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   331
    val tt = f tt
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   332
    val ft = f ft
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   333
  in
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   334
    plus (f cond) (case (tt,ft) of (NONE, NONE) => NONE | _ =>
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   335
       (SOME ((Const (If_name, @{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
   336
  end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   337
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   338
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
   339
  | letc_change_typ _ = error "Internal error: invalid let type"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   340
fun letc _ _ f expT exp nms Ts t =
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   341
    plus (f exp)
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   342
    (if length nms = 0 (* In case of "length nms = 0" the expression got reducted
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   343
                          Here we need Bound 0 to gain non-partial application *)
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   344
    then (case f (t $ Bound 0) of SOME (t' $ Bound 0) =>
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   345
                                 (SOME (Const (Let_name, letc_change_typ expT) $ exp $ t'))
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   346
                                  (* Expression is not used and can therefore let be dropped *)
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   347
                                | SOME t' => SOME t'
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   348
                                | NONE => NONE)
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   349
    else (case f t of SOME t' =>
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   350
      SOME (if Term.is_dependent t' then Const (Let_name, letc_change_typ expT) $ exp $ build_abs t' nms Ts
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   351
                                    else Term.subst_bounds([exp],t'))
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   352
                               | NONE => NONE))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   353
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   354
(* The converter for timing functions given to the walker *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   355
val converter : term option converter = {
80036
a594d22e69d6 updated time functions for Array_Braun
nipkow
parents: 79969
diff changeset
   356
        constc = fn _ => fn _ => fn _ => fn t =>
a594d22e69d6 updated time functions for Array_Braun
nipkow
parents: 79969
diff changeset
   357
          (case t of Const ("HOL.undefined", _) => SOME (Const ("HOL.undefined", @{typ "nat"}))
a594d22e69d6 updated time functions for Array_Braun
nipkow
parents: 79969
diff changeset
   358
                   | _ => NONE),
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   359
        funcc = funcc,
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   360
        ifc = ifc,
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   361
        casec = casec,
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   362
        letc = letc
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   363
    }
79665
0a152b2f73ae time funs: +1 instead of 1+
nipkow
parents: 79542
diff changeset
   364
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
   365
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   366
(* Use converter to convert right side of a term *)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   367
fun to_time ctxt origin is_rec term =
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   368
  top_converter is_rec ctxt origin (walk ctxt origin converter term)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   369
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   370
(* Converts a term to its running time version *)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   371
fun convert_term ctxt (origin: term list) is_rec (pT $ (Const (eqN, _) $ l $ r)) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   372
      pT
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   373
      $ (Const (eqN, @{typ "nat \<Rightarrow> nat \<Rightarrow> bool"})
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   374
        $ (build_func ((walk_func l []) |>> (fun_to_time ctxt origin) |>> Option.valOf ||> conv_args ctxt origin))
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   375
        $ (to_time ctxt origin is_rec r))
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   376
  | convert_term _ _ _ _ = error "Internal error: invalid term to convert"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   377
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   378
(* 4. Tactic to prove "f_dom n" *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   379
fun time_dom_tac ctxt induct_rule domintros =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   380
  (Induction.induction_tac ctxt true [] [[]] [] (SOME [induct_rule]) []
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   381
    THEN_ALL_NEW ((K (auto_tac ctxt)) THEN' (fn i => FIRST' (
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   382
    (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
   383
    [Metis_Tactic.metis_tac [] ATP_Problem_Generate.combsN ctxt domintros]) i)))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   384
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   385
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   386
fun get_terms theory (term: term) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   387
  Spec_Rules.retrieve_global theory term
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   388
      |> hd |> #rules
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   389
      |> map Thm.prop_of
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   390
   handle Empty => error "Function or terms of function not found"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   391
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   392
(* Register timing function of a given function *)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   393
fun reg_and_proove_time_func (theory: theory) (term: term list) (terms: term list) print =
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   394
  reg_time_func theory term terms false
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   395
  |> proove_termination term terms print
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   396
and reg_time_func (theory: theory) (term: term list) (terms: term list) print =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   397
  let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   398
    val lthy = Named_Target.theory_init theory
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   399
    val _ =
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   400
      case time_term lthy (hd term)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   401
            handle (ERROR _) => NONE
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   402
        of SOME _ => error ("Timing function already declared: " ^ (Term.term_name (hd term)))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   403
         | NONE => ()
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   404
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   405
    (* 1. Fix all terms *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   406
    (* Exchange Var in types and terms to Free and check constraints *)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   407
    val terms = map
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   408
      (map_aterms fixTerms
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   409
        #> map_types (map_atyps fixTypes)
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   410
        #> fixPartTerms lthy term)
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   411
      terms
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   412
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   413
    (* 2. Check if function is recursive *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   414
    val is_rec = is_rec lthy term terms
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   415
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   416
    (* 3. Convert every equation
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   417
      - Change type of toplevel equation from _ \<Rightarrow> _ \<Rightarrow> bool to nat \<Rightarrow> nat \<Rightarrow> bool
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   418
      - On left side change name of function to timing function
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   419
      - Convert right side of equation with conversion schema
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   420
    *)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   421
    val timing_terms = map (convert_term lthy term is_rec) terms
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   422
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   423
    (* 4. Register function and prove termination *)
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   424
    val names = map Term.term_name term
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   425
    val timing_names = map (fun_name_to_time lthy) names
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   426
    val bindings = map (fn nm => (Binding.name nm, NONE, NoSyn)) timing_names
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   427
    fun pat_completeness_auto ctxt =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   428
      Pat_Completeness.pat_completeness_tac ctxt 1 THEN auto_tac ctxt
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   429
    val specs = map (fn eq => (((Binding.empty, []), eq), [], [])) timing_terms
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   430
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   431
    (* For partial functions sequential=true is needed in order to support them
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   432
       We need sequential=false to support the automatic proof of termination over dom
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   433
    *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   434
    fun register seq =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   435
      let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   436
        val _ = (if seq then warning "Falling back on sequential function..." else ())
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   437
        val fun_config = Function_Common.FunctionConfig
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   438
          {sequential=seq, default=NONE, domintros=true, partials=false}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   439
      in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   440
        Function.add_function bindings specs fun_config pat_completeness_auto lthy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   441
      end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   442
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   443
    (* Context for printing without showing question marks *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   444
    val print_ctxt = lthy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   445
      |> Config.put show_question_marks false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   446
      |> Config.put show_sorts false (* Change it for debugging *)
79542
b941924a407d define_time_function: avoid unused let's
nipkow
parents: 79494
diff changeset
   447
    val print_ctxt =  List.foldl (fn (term, ctxt) => Variable.add_fixes_implicit term ctxt) print_ctxt (term@timing_terms)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   448
    (* Print result if print *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   449
    val _ = if not print then () else
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   450
        let
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   451
          val nms = map (fst o dest_Const) term
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   452
          val typs = map (snd o dest_Const) term
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   453
        in
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   454
          print_timing' print_ctxt { names=nms, terms=terms, typs=typs } { names=timing_names, terms=timing_terms, typs=map change_typ typs }
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   455
        end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   456
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   457
    (* Register function *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   458
    val (_, lthy) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   459
      register false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   460
      handle (ERROR _) =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   461
        register true
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   462
           | Match =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   463
        register true
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   464
  in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   465
    Local_Theory.exit_global lthy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   466
  end
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   467
and proove_termination (term: term list) terms print (theory: theory) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   468
  let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   469
    val lthy = Named_Target.theory_init theory
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   470
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   471
    (* Start proving the termination *)  
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   472
    val infos = SOME (map (Function.get_info lthy) term) handle Empty => NONE
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   473
    val timing_names = map (fun_name_to_time lthy o Term.term_name) term
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   474
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   475
    (* Proof by lexicographic_order_tac *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   476
    val (time_info, lthy') =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   477
      (Function.prove_termination NONE
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   478
        (Lexicographic_Order.lexicographic_order_tac false lthy) lthy)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   479
        handle (ERROR _) =>
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   480
        let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   481
          val _ = warning "Falling back on proof over dom..."
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   482
          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
   483
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   484
          fun args (a$(Var ((nm,_),T))) = args a |> (fn ar => (nm,T)::ar)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   485
            | args (a$(Const (_,T))) = args a |> (fn ar => ("x",T)::ar)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   486
            | args _ = []
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   487
          val dom_args =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   488
            terms |> hd |> get_l |> args
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   489
            |> Variable.variant_frees lthy []
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   490
            |> map fst
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   491
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   492
          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
   493
          val induct = (Option.valOf inducts |> hd)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   494
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   495
          val domintros = Proof_Context.get_fact lthy (Facts.named (hd timing_names ^ ".domintros"))
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   496
          val prop = (hd timing_names ^ "_dom (" ^ (String.concatWith "," dom_args) ^ ")")
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   497
                      |> Syntax.read_prop lthy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   498
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   499
          (* Prove a helper lemma *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   500
          val dom_lemma = Goal.prove lthy dom_args [] prop
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   501
            (fn {context, ...} => HEADGOAL (time_dom_tac context induct domintros))
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   502
          (* Add dom_lemma to simplification set *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   503
          val simp_lthy = Simplifier.add_simp dom_lemma lthy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   504
        in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   505
          (* Use lemma to prove termination *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   506
          Function.prove_termination NONE
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   507
            (auto_tac simp_lthy) lthy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   508
        end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   509
    
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   510
    (* Context for printing without showing question marks *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   511
    val print_ctxt = lthy'
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   512
      |> Config.put show_question_marks false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   513
      |> Config.put show_sorts false (* Change it for debugging *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   514
    (* Print result if print *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   515
    val _ = if not print then () else
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   516
        let
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   517
          val nms = map (fst o dest_Const) term
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   518
          val typs = map (snd o dest_Const) term
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   519
        in
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   520
          print_timing' print_ctxt { names=nms, terms=terms, typs=typs } (info_pfunc time_info)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   521
        end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   522
  in
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   523
    (time_info, Local_Theory.exit_global lthy')
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   524
  end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   525
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   526
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
   527
      $ (Const ("HOL.eq", @{typ "bool \<Rightarrow> bool \<Rightarrow> bool"}) $ l $ r)
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   528
  | fix_definition t = t
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   529
fun check_definition [t] = [t]
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   530
  | check_definition _ = error "Only a single defnition is allowed"
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   531
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   532
(* Convert function into its timing function (called by command) *)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   533
fun reg_time_fun_cmd (funcs, thms) (theory: theory) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   534
let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   535
  val ctxt = Proof_Context.init_global theory
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   536
  val fterms = map (Syntax.read_term ctxt) funcs
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   537
  val (_, lthy') = reg_and_proove_time_func theory fterms
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   538
    (case thms of NONE => get_terms theory (hd fterms)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   539
                | SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   540
    true
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   541
in lthy'
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   542
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   543
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   544
(* Convert function into its timing function (called by command) with termination proof provided by user*)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   545
fun reg_time_function_cmd (funcs, thms) (theory: theory) =
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   546
let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   547
  val ctxt = Proof_Context.init_global theory
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   548
  val fterms = map (Syntax.read_term ctxt) funcs
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   549
  val theory = reg_time_func theory fterms
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   550
    (case thms of NONE => get_terms theory (hd fterms)
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   551
                | SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of)
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   552
    true
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   553
in theory
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   554
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   555
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   556
(* Convert function into its timing function (called by command) *)
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   557
fun reg_time_definition_cmd (funcs, thms) (theory: theory) =
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   558
let
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   559
  val ctxt = Proof_Context.init_global theory
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   560
  val fterms = map (Syntax.read_term ctxt) funcs
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   561
  val (_, lthy') = reg_and_proove_time_func theory fterms
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   562
    (case thms of NONE => get_terms theory (hd fterms) |> check_definition |> map fix_definition
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   563
                | SOME thms => thms |> Attrib.eval_thms ctxt |> List.map Thm.prop_of)
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   564
    true
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   565
in lthy'
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   566
end
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   567
79494
c7536609bb9b translation to time functions now with canonical let.
nipkow
parents: 79490
diff changeset
   568
val parser = (Scan.repeat1 Parse.prop) -- (Scan.option (Parse.keyword_improper "equations" -- Parse.thms1 >> snd))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   569
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   570
val _ = Outer_Syntax.command @{command_keyword "time_fun"}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   571
  "Defines runtime function of a function"
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   572
  (parser >> (fn p => Toplevel.theory (reg_time_fun_cmd p)))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   573
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   574
val _ = Outer_Syntax.command @{command_keyword "time_function"}
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   575
  "Defines runtime function of a function"
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   576
  (parser >> (fn p => Toplevel.theory (reg_time_function_cmd p)))
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   577
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   578
val _ = Outer_Syntax.command @{command_keyword "time_definition"}
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   579
  "Defines runtime function of a definition"
4aeb25ba90f3 more uniform command names
nipkow
parents: 79714
diff changeset
   580
  (parser >> (fn p => Toplevel.theory (reg_time_definition_cmd p)))
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   581
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
   582
end