src/Pure/Tools/find_consts.ML
author wenzelm
Thu May 08 13:47:17 2014 +0200 (2014-05-08)
changeset 56908 734f7e6151c9
parent 56891 48899c43b07d
child 56912 293cd4dcfebc
permissions -rw-r--r--
tuned message: more compact, imitate actual command line;
     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 read_query: Position.T -> string -> (bool * criterion) list
    15   val find_consts : Proof.context -> (bool * criterion) list -> unit
    16 end;
    17 
    18 structure Find_Consts : FIND_CONSTS =
    19 struct
    20 
    21 (* search criteria *)
    22 
    23 datatype criterion =
    24     Strict of string
    25   | Loose of string
    26   | Name of string;
    27 
    28 
    29 (* matching types/consts *)
    30 
    31 fun matches_subtype thy typat =
    32   Term.exists_subtype (fn ty => Sign.typ_instance thy (ty, typat));
    33 
    34 fun check_const pred (nm, (ty, _)) =
    35   if pred (nm, ty) then SOME (Term.size_of_typ ty) else NONE;
    36 
    37 fun opt_not f (c as (_, (ty, _))) =
    38   if is_some (f c) then NONE else SOME (Term.size_of_typ ty);
    39 
    40 fun filter_const _ _ NONE = NONE
    41   | filter_const c f (SOME rank) =
    42       (case f c of
    43         NONE => NONE
    44       | SOME i => SOME (Int.min (rank, i)));
    45 
    46 
    47 (* pretty results *)
    48 
    49 fun pretty_criterion (b, c) =
    50   let
    51     fun prfx s = if b then s else "-" ^ s;
    52   in
    53     (case c of
    54       Strict pat => Pretty.str (prfx "strict: " ^ quote pat)
    55     | Loose pat => Pretty.str (prfx (quote pat))
    56     | Name name => Pretty.str (prfx "name: " ^ quote name))
    57   end;
    58 
    59 fun pretty_const ctxt (c, ty) =
    60   let
    61     val ty' = Logic.unvarifyT_global ty;
    62     val const_space = Consts.space_of (Sign.consts_of (Proof_Context.theory_of ctxt));
    63     val markup = Name_Space.markup const_space c;
    64   in
    65     Pretty.block
    66      [Pretty.mark markup (Pretty.str c), Pretty.str " ::", Pretty.brk 1,
    67       Pretty.quote (Syntax.pretty_typ ctxt ty')]
    68   end;
    69 
    70 
    71 (* find_consts *)
    72 
    73 fun pretty_consts ctxt raw_criteria =
    74   let
    75     val thy = Proof_Context.theory_of ctxt;
    76     val low_ranking = 10000;
    77 
    78     fun user_visible consts (nm, _) =
    79       if Consts.is_concealed consts nm then NONE else SOME low_ranking;
    80 
    81     fun make_pattern crit =
    82       let
    83         val raw_T = Syntax.parse_typ ctxt crit;
    84         val t =
    85           Syntax.check_term
    86             (Proof_Context.set_mode Proof_Context.mode_pattern ctxt)
    87             (Term.dummy_pattern raw_T);
    88       in Term.type_of t end;
    89 
    90     fun make_match (Strict arg) =
    91           let val qty = make_pattern arg; in
    92             fn (_, (ty, _)) =>
    93               let
    94                 val tye = Sign.typ_match thy (qty, ty) Vartab.empty;
    95                 val sub_size =
    96                   Vartab.fold (fn (_, (_, t)) => fn n => Term.size_of_typ t + n) tye 0;
    97               in SOME sub_size end handle Type.TYPE_MATCH => NONE
    98           end
    99       | make_match (Loose arg) =
   100           check_const (matches_subtype thy (make_pattern arg) o snd)
   101       | make_match (Name arg) = check_const (match_string arg o fst);
   102 
   103     fun make_criterion (b, crit) = (if b then I else opt_not) (make_match crit);
   104     val criteria = map make_criterion raw_criteria;
   105 
   106     val consts = Sign.consts_of thy;
   107     val {constants, ...} = Consts.dest consts;
   108     fun eval_entry c =
   109       fold (filter_const c) (user_visible consts :: criteria) (SOME low_ranking);
   110 
   111     val matches =
   112       fold (fn c => (case eval_entry c of NONE => I | SOME rank => cons (rank, c))) constants []
   113       |> sort (prod_ord (rev_order o int_ord) (string_ord o pairself fst))
   114       |> map (apsnd fst o snd);
   115   in
   116     Pretty.block
   117       (Pretty.fbreaks (Pretty.keyword1 "find_consts" :: map pretty_criterion raw_criteria)) ::
   118     Pretty.str "" ::
   119     (if null matches then [Pretty.str "found nothing"]
   120      else
   121        Pretty.str ("found " ^ string_of_int (length matches) ^ " constant(s):") ::
   122        grouped 10 Par_List.map (Pretty.item o single o pretty_const ctxt) matches)
   123   end |> Pretty.fbreaks |> curry Pretty.blk 0;
   124 
   125 fun find_consts ctxt args = Pretty.writeln (pretty_consts ctxt args);
   126 
   127 
   128 (* command syntax *)
   129 
   130 local
   131 
   132 val criterion =
   133   Parse.reserved "strict" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Strict ||
   134   Parse.reserved "name" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Name ||
   135   Parse.xname >> Loose;
   136 
   137 val query = Scan.repeat ((Scan.option Parse.minus >> is_none) -- criterion);
   138 
   139 in
   140 
   141 fun read_query pos str =
   142   Outer_Syntax.scan pos str
   143   |> filter Token.is_proper
   144   |> Scan.error (Scan.finite Token.stopper (Parse.!!! (query --| Scan.ahead Parse.eof)))
   145   |> #1;
   146 
   147 val _ =
   148   Outer_Syntax.improper_command @{command_spec "find_consts"}
   149     "find constants by name / type patterns"
   150     (query >> (fn spec =>
   151       Toplevel.keep (fn st =>
   152         Pretty.writeln (pretty_consts (Toplevel.context_of st) spec))));
   153 
   154 end;
   155 
   156 
   157 (* PIDE query operation *)
   158 
   159 val _ =
   160   Query_Operation.register "find_consts" (fn {state, args, output_result} =>
   161     (case try Toplevel.context_of state of
   162       SOME ctxt =>
   163         let
   164           val [query_arg] = args;
   165           val query = read_query Position.none query_arg;
   166         in output_result (Pretty.string_of (pretty_consts ctxt query)) end
   167     | NONE => error "Unknown context"));
   168 
   169 end;
   170