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