src/Pure/Tools/find_consts.ML
author wenzelm
Mon Feb 27 19:54:50 2012 +0100 (2012-02-27)
changeset 46716 c45a4427db39
parent 46713 e6e1ec6d5c1c
child 46961 5c6955f487e5
permissions -rw-r--r--
discontinued slightly odd built-in timing (cf. 53fd5cc685b4) -- the Isar toplevel does that already (e.g. via Toplevel.timing or Toplevel.profiling);
     1 (*  Title:      Pure/Tools/find_consts.ML
     2     Author:     Timothy Bourke and Gerwin Klein, NICTA
     3 
     4 Hoogle-like (http://www-users.cs.york.ac.uk/~ndm/hoogle) searching by
     5 type over constants, but matching is not fuzzy.
     6 *)
     7 
     8 signature FIND_CONSTS =
     9 sig
    10   datatype criterion =
    11       Strict of string
    12     | Loose of string
    13     | Name of string
    14   val find_consts : Proof.context -> (bool * criterion) list -> unit
    15 end;
    16 
    17 structure Find_Consts : FIND_CONSTS =
    18 struct
    19 
    20 (* search criteria *)
    21 
    22 datatype criterion =
    23     Strict of string
    24   | Loose of string
    25   | Name of string;
    26 
    27 
    28 (* matching types/consts *)
    29 
    30 fun matches_subtype thy typat =
    31   Term.exists_subtype (fn ty => Sign.typ_instance thy (ty, typat));
    32 
    33 fun check_const pred (nm, (ty, _)) =
    34   if pred (nm, ty) then SOME (Term.size_of_typ ty) else NONE;
    35 
    36 fun opt_not f (c as (_, (ty, _))) =
    37   if is_some (f c) then NONE else SOME (Term.size_of_typ ty);
    38 
    39 fun filter_const _ NONE = NONE
    40   | filter_const f (SOME (c, r)) =
    41       (case f c of
    42         NONE => NONE
    43       | SOME i => SOME (c, Int.min (r, i)));
    44 
    45 
    46 (* pretty results *)
    47 
    48 fun pretty_criterion (b, c) =
    49   let
    50     fun prfx s = if b then s else "-" ^ s;
    51   in
    52     (case c of
    53       Strict pat => Pretty.str (prfx "strict: " ^ quote pat)
    54     | Loose pat => Pretty.str (prfx (quote pat))
    55     | Name name => Pretty.str (prfx "name: " ^ quote name))
    56   end;
    57 
    58 fun pretty_const ctxt (nm, ty) =
    59   let
    60     val ty' = Logic.unvarifyT_global ty;
    61   in
    62     Pretty.block
    63      [Pretty.str nm, Pretty.str " ::", Pretty.brk 1,
    64       Pretty.quote (Syntax.pretty_typ ctxt ty')]
    65   end;
    66 
    67 
    68 (* find_consts *)
    69 
    70 fun find_consts ctxt raw_criteria =
    71   let
    72     val thy = Proof_Context.theory_of ctxt;
    73     val low_ranking = 10000;
    74 
    75     fun user_visible consts (nm, _) =
    76       if Consts.is_concealed consts nm then NONE else SOME low_ranking;
    77 
    78     fun make_pattern crit =
    79       let
    80         val raw_T = Syntax.parse_typ ctxt crit;
    81         val t =
    82           Syntax.check_term
    83             (Proof_Context.set_mode Proof_Context.mode_pattern ctxt)
    84             (Term.dummy_pattern raw_T);
    85       in Term.type_of t end;
    86 
    87     fun make_match (Strict arg) =
    88           let val qty = make_pattern arg; in
    89             fn (_, (ty, _)) =>
    90               let
    91                 val tye = Sign.typ_match thy (qty, ty) Vartab.empty;
    92                 val sub_size =
    93                   Vartab.fold (fn (_, (_, t)) => fn n => Term.size_of_typ t + n) tye 0;
    94               in SOME sub_size end handle Type.TYPE_MATCH => NONE
    95           end
    96       | make_match (Loose arg) =
    97           check_const (matches_subtype thy (make_pattern arg) o snd)
    98       | make_match (Name arg) = check_const (match_string arg o fst);
    99 
   100     fun make_criterion (b, crit) = (if b then I else opt_not) (make_match crit);
   101     val criteria = map make_criterion raw_criteria;
   102 
   103     val consts = Sign.consts_of thy;
   104     val (_, consts_tab) = #constants (Consts.dest consts);
   105     fun eval_entry c =
   106       fold filter_const (user_visible consts :: criteria)
   107         (SOME (c, low_ranking));
   108 
   109     val matches =
   110       Symtab.fold (cons o eval_entry) consts_tab []
   111       |> map_filter I
   112       |> sort (rev_order o int_ord o pairself snd)
   113       |> map (apsnd fst o fst);
   114   in
   115     Pretty.big_list "searched for:" (map pretty_criterion raw_criteria) ::
   116     Pretty.str "" ::
   117     Pretty.str
   118      (if null matches
   119       then "nothing found"
   120       else "found " ^ string_of_int (length matches) ^ " constant(s):") ::
   121     Pretty.str "" ::
   122     map (pretty_const ctxt) matches
   123   end |> Pretty.chunks |> Pretty.writeln;
   124 
   125 
   126 (* command syntax *)
   127 
   128 local
   129 
   130 val criterion =
   131   Parse.reserved "strict" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Strict ||
   132   Parse.reserved "name" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Name ||
   133   Parse.xname >> Loose;
   134 
   135 in
   136 
   137 val _ =
   138   Outer_Syntax.improper_command "find_consts" "search constants by type pattern" Keyword.diag
   139     (Scan.repeat ((Scan.option Parse.minus >> is_none) -- criterion)
   140       >> (fn spec => Toplevel.no_timing o
   141         Toplevel.keep (fn state => find_consts (Toplevel.context_of state) spec)));
   142 
   143 end;
   144 
   145 end;
   146