src/Pure/Tools/find_consts.ML
author wenzelm
Sat Apr 26 21:49:31 2014 +0200 (2014-04-26)
changeset 56758 d203b9c400a2
parent 56062 8ae2965ddc80
child 56760 ef5df088e022
permissions -rw-r--r--
PIDE support for find_consts;
     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.big_list "searched for:" (map pretty_criterion raw_criteria) ::
   117     Pretty.str "" ::
   118     Pretty.str
   119      (if null matches
   120       then "nothing found"
   121       else "found " ^ string_of_int (length matches) ^ " constant(s):") ::
   122     Pretty.str "" ::
   123     map (pretty_const ctxt) matches
   124   end |> Pretty.fbreaks |> curry Pretty.blk 0;
   125 
   126 fun find_consts ctxt args = Pretty.writeln (pretty_consts ctxt args);
   127 
   128 
   129 (* command syntax *)
   130 
   131 local
   132 
   133 val criterion =
   134   Parse.reserved "strict" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Strict ||
   135   Parse.reserved "name" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Name ||
   136   Parse.xname >> Loose;
   137 
   138 val query = Scan.repeat ((Scan.option Parse.minus >> is_none) -- criterion);
   139 
   140 in
   141 
   142 fun read_query pos str =
   143   Outer_Syntax.scan pos str
   144   |> filter Token.is_proper
   145   |> Scan.error (Scan.finite Token.stopper (Parse.!!! (query --| Scan.ahead Parse.eof)))
   146   |> #1;
   147 
   148 val _ =
   149   Outer_Syntax.improper_command @{command_spec "find_consts"}
   150     "find constants by name / type patterns"
   151     (query >> (fn spec =>
   152       Toplevel.keep (fn st =>
   153         Pretty.writeln (pretty_consts (Toplevel.context_of st) spec))));
   154 
   155 end;
   156 
   157 
   158 (* PIDE query operation *)
   159 
   160 val _ =
   161   Query_Operation.register "find_consts" (fn {state, args, output_result} =>
   162     (case try Toplevel.context_of state of
   163       SOME ctxt =>
   164         let
   165           val [query_arg] = args;
   166           val query = read_query Position.none query_arg;
   167         in output_result (Pretty.string_of (pretty_consts ctxt query)) end
   168     | NONE => error "Unknown context"));
   169 
   170 end;
   171