src/Pure/Tools/find_consts.ML
author wenzelm
Wed Nov 26 20:05:34 2014 +0100 (2014-11-26)
changeset 59058 a78612c67ec0
parent 58928 23d0ffd48006
child 59083 88b0b1f28adc
permissions -rw-r--r--
renamed "pairself" to "apply2", in accordance to @{apply 2};
     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 apply2 fst))
   114       |> map (apsnd fst o snd);
   115 
   116     val position_markup = Position.markup (Position.thread_data ()) Markup.position;
   117   in
   118     Pretty.block
   119       (Pretty.fbreaks
   120         (Pretty.mark position_markup (Pretty.keyword1 "find_consts") ::
   121           map pretty_criterion raw_criteria)) ::
   122     Pretty.str "" ::
   123     (if null matches then [Pretty.str "found nothing"]
   124      else
   125        Pretty.str ("found " ^ string_of_int (length matches) ^ " constant(s):") ::
   126        grouped 10 Par_List.map (Pretty.item o single o pretty_const ctxt) matches)
   127   end |> Pretty.fbreaks |> curry Pretty.blk 0;
   128 
   129 fun find_consts ctxt args = Pretty.writeln (pretty_consts ctxt args);
   130 
   131 
   132 (* command syntax *)
   133 
   134 local
   135 
   136 val criterion =
   137   Parse.reserved "strict" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Strict ||
   138   Parse.reserved "name" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Name ||
   139   Parse.xname >> Loose;
   140 
   141 val query = Scan.repeat ((Scan.option Parse.minus >> is_none) -- criterion);
   142 
   143 val query_keywords = Keyword.add_keywords [(":", NONE)] Keyword.empty_keywords;
   144 
   145 in
   146 
   147 fun read_query pos str =
   148   Outer_Syntax.scan query_keywords pos str
   149   |> filter Token.is_proper
   150   |> Scan.error (Scan.finite Token.stopper (Parse.!!! (query --| Scan.ahead Parse.eof)))
   151   |> #1;
   152 
   153 val _ =
   154   Outer_Syntax.command @{command_spec "find_consts"}
   155     "find constants by name / type patterns"
   156     (query >> (fn spec =>
   157       Toplevel.keep (fn st =>
   158         Pretty.writeln (pretty_consts (Toplevel.context_of st) spec))));
   159 
   160 end;
   161 
   162 
   163 (* PIDE query operation *)
   164 
   165 val _ =
   166   Query_Operation.register "find_consts" (fn {state, args, output_result} =>
   167     (case try Toplevel.context_of state of
   168       SOME ctxt =>
   169         let
   170           val [query_arg] = args;
   171           val query = read_query Position.none query_arg;
   172         in output_result (Pretty.string_of (pretty_consts ctxt query)) end
   173     | NONE => error "Unknown context"));
   174 
   175 end;
   176