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