src/HOL/Data_Structures/Define_Time_0.ML
author haftmann
Mon, 16 Jun 2025 15:25:38 +0200
changeset 82730 3b98b1b57435
parent 79969 4aeb25ba90f3
permissions -rw-r--r--
more explicit theorem names for list quantifiers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     1
signature ZERO_FUNCS =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     2
sig
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     3
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     4
val is_zero : theory -> string * typ -> bool
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     5
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     6
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     7
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     8
structure Zero_Funcs : ZERO_FUNCS =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
     9
struct
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    10
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    11
fun get_constrs thy (Type (n, _)) = these (Ctr_Sugar.ctr_sugar_of_global thy n |> Option.map #ctrs)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    12
  | get_constrs _ _ = []
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    13
(* returns if something is a constructor (found in smt_normalize.ML) *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    14
fun is_constr thy (n, T) =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    15
    let fun match (Const (m, U)) = m = n andalso Sign.typ_instance thy (T, U)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    16
          | match _ = error "Internal error: unknown constructor"
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    17
    in can (the o find_first match o get_constrs thy o Term.body_type) T end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    18
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    19
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    20
structure ZeroFuns = Theory_Data(
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    21
  type T = Symtab.set
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    22
  val empty = Symtab.empty
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    23
  val merge = Symtab.merge (K true));
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    24
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    25
fun add_zero f = ZeroFuns.map (Symtab.insert_set f);
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    26
fun is_zero_decl lthy f = Option.isSome ((Symtab.lookup o ZeroFuns.get) lthy f);
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    27
(* Zero should be everything which is a constructor or something like a constant or variable *)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    28
fun is_zero ctxt (n, (T as Type (Tn,_))) = (Tn <> "fun" orelse is_constr ctxt (n, T) orelse is_zero_decl ctxt n)
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    29
  | is_zero _ _  = false
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    30
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    31
fun save func thy =
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    32
let
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    33
val fterm = Syntax.read_term (Proof_Context.init_global thy) func;
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    34
val name = (case fterm of Const (n,_) => n | _ => raise Fail "Invalid function")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    35
val _ = writeln ("Adding \"" ^ name ^ "\" to 0 functions")
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    36
in add_zero name thy
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    37
end
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    38
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    39
val _ =
79969
4aeb25ba90f3 more uniform command names
nipkow
parents: 79490
diff changeset
    40
  Outer_Syntax.command \<^command_keyword>\<open>time_fun_0\<close> "ML setup for global theory"
79490
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    41
    (Parse.prop >> (Toplevel.theory o save));
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    42
b287510a4202 Added time function automation
nipkow
parents:
diff changeset
    43
end