src/Pure/Tools/find_consts.ML
author wenzelm
Mon Sep 21 23:22:11 2015 +0200 (2015-09-21)
changeset 61223 dfccf6c06201
parent 60667 d86c449d30ba
child 61335 43848476063c
permissions -rw-r--r--
clarified markup;
tuned signature;
wenzelm@30143
     1
(*  Title:      Pure/Tools/find_consts.ML
kleing@29884
     2
    Author:     Timothy Bourke and Gerwin Klein, NICTA
kleing@29884
     3
wenzelm@30143
     4
Hoogle-like (http://www-users.cs.york.ac.uk/~ndm/hoogle) searching by
wenzelm@30143
     5
type over constants, but matching is not fuzzy.
kleing@29884
     6
*)
kleing@29884
     7
kleing@29884
     8
signature FIND_CONSTS =
kleing@29884
     9
sig
wenzelm@30143
    10
  datatype criterion =
wenzelm@30143
    11
      Strict of string
wenzelm@30143
    12
    | Loose of string
wenzelm@30143
    13
    | Name of string
wenzelm@56758
    14
  val read_query: Position.T -> string -> (bool * criterion) list
kleing@29884
    15
  val find_consts : Proof.context -> (bool * criterion) list -> unit
kleing@29884
    16
end;
kleing@29884
    17
wenzelm@33301
    18
structure Find_Consts : FIND_CONSTS =
kleing@29884
    19
struct
kleing@29884
    20
wenzelm@30143
    21
(* search criteria *)
wenzelm@30143
    22
wenzelm@30143
    23
datatype criterion =
wenzelm@30143
    24
    Strict of string
wenzelm@30143
    25
  | Loose of string
wenzelm@30143
    26
  | Name of string;
kleing@29884
    27
wenzelm@31684
    28
wenzelm@30143
    29
(* matching types/consts *)
kleing@29884
    30
wenzelm@30143
    31
fun matches_subtype thy typat =
wenzelm@38335
    32
  Term.exists_subtype (fn ty => Sign.typ_instance thy (ty, typat));
kleing@29884
    33
wenzelm@38335
    34
fun check_const pred (nm, (ty, _)) =
wenzelm@38335
    35
  if pred (nm, ty) then SOME (Term.size_of_typ ty) else NONE;
kleing@29884
    36
wenzelm@30143
    37
fun opt_not f (c as (_, (ty, _))) =
wenzelm@38335
    38
  if is_some (f c) then NONE else SOME (Term.size_of_typ ty);
kleing@29884
    39
wenzelm@46979
    40
fun filter_const _ _ NONE = NONE
wenzelm@46979
    41
  | filter_const c f (SOME rank) =
wenzelm@31684
    42
      (case f c of
wenzelm@31684
    43
        NONE => NONE
wenzelm@46979
    44
      | SOME i => SOME (Int.min (rank, i)));
wenzelm@30143
    45
wenzelm@30143
    46
wenzelm@30143
    47
(* pretty results *)
kleing@29895
    48
kleing@29895
    49
fun pretty_criterion (b, c) =
kleing@29895
    50
  let
kleing@29895
    51
    fun prfx s = if b then s else "-" ^ s;
wenzelm@60667
    52
    val show_pat = quote o Input.source_content o Syntax.read_input;
kleing@29895
    53
  in
kleing@29895
    54
    (case c of
wenzelm@60667
    55
      Strict pat => Pretty.str (prfx "strict: " ^ show_pat pat)
wenzelm@60667
    56
    | Loose pat => Pretty.str (prfx (show_pat pat))
kleing@29895
    57
    | Name name => Pretty.str (prfx "name: " ^ quote name))
kleing@29895
    58
  end;
kleing@29895
    59
wenzelm@49886
    60
fun pretty_const ctxt (c, ty) =
wenzelm@30143
    61
  let
wenzelm@35845
    62
    val ty' = Logic.unvarifyT_global ty;
wenzelm@56025
    63
    val const_space = Consts.space_of (Sign.consts_of (Proof_Context.theory_of ctxt));
wenzelm@56025
    64
    val markup = Name_Space.markup const_space c;
kleing@29895
    65
  in
wenzelm@30143
    66
    Pretty.block
wenzelm@49886
    67
     [Pretty.mark markup (Pretty.str c), Pretty.str " ::", Pretty.brk 1,
wenzelm@30143
    68
      Pretty.quote (Syntax.pretty_typ ctxt ty')]
kleing@29895
    69
  end;
kleing@29895
    70
wenzelm@31684
    71
wenzelm@30143
    72
(* find_consts *)
wenzelm@30143
    73
wenzelm@56758
    74
fun pretty_consts ctxt raw_criteria =
wenzelm@30143
    75
  let
wenzelm@42360
    76
    val thy = Proof_Context.theory_of ctxt;
kleing@29884
    77
    val low_ranking = 10000;
kleing@29884
    78
wenzelm@33158
    79
    fun user_visible consts (nm, _) =
wenzelm@33158
    80
      if Consts.is_concealed consts nm then NONE else SOME low_ranking;
Timothy@30206
    81
Timothy@30207
    82
    fun make_pattern crit =
Timothy@30207
    83
      let
Timothy@30207
    84
        val raw_T = Syntax.parse_typ ctxt crit;
wenzelm@31684
    85
        val t =
wenzelm@31684
    86
          Syntax.check_term
wenzelm@42360
    87
            (Proof_Context.set_mode Proof_Context.mode_pattern ctxt)
wenzelm@31684
    88
            (Term.dummy_pattern raw_T);
Timothy@30207
    89
      in Term.type_of t end;
kleing@29884
    90
kleing@29884
    91
    fun make_match (Strict arg) =
kleing@29884
    92
          let val qty = make_pattern arg; in
wenzelm@30143
    93
            fn (_, (ty, _)) =>
wenzelm@30143
    94
              let
kleing@29884
    95
                val tye = Sign.typ_match thy (qty, ty) Vartab.empty;
wenzelm@31684
    96
                val sub_size =
wenzelm@31684
    97
                  Vartab.fold (fn (_, (_, t)) => fn n => Term.size_of_typ t + n) tye 0;
wenzelm@32790
    98
              in SOME sub_size end handle Type.TYPE_MATCH => NONE
kleing@29884
    99
          end
kleing@29884
   100
      | make_match (Loose arg) =
kleing@29884
   101
          check_const (matches_subtype thy (make_pattern arg) o snd)
kleing@29884
   102
      | make_match (Name arg) = check_const (match_string arg o fst);
kleing@29884
   103
kleing@29884
   104
    fun make_criterion (b, crit) = (if b then I else opt_not) (make_match crit);
Timothy@30206
   105
    val criteria = map make_criterion raw_criteria;
kleing@29884
   106
Timothy@30206
   107
    val consts = Sign.consts_of thy;
wenzelm@56025
   108
    val {constants, ...} = Consts.dest consts;
wenzelm@31684
   109
    fun eval_entry c =
wenzelm@46979
   110
      fold (filter_const c) (user_visible consts :: criteria) (SOME low_ranking);
kleing@29884
   111
wenzelm@30143
   112
    val matches =
wenzelm@56062
   113
      fold (fn c => (case eval_entry c of NONE => I | SOME rank => cons (rank, c))) constants []
wenzelm@59058
   114
      |> sort (prod_ord (rev_order o int_ord) (string_ord o apply2 fst))
wenzelm@46979
   115
      |> map (apsnd fst o snd);
wenzelm@56912
   116
wenzelm@56912
   117
    val position_markup = Position.markup (Position.thread_data ()) Markup.position;
kleing@29884
   118
  in
wenzelm@56891
   119
    Pretty.block
wenzelm@56912
   120
      (Pretty.fbreaks
wenzelm@56912
   121
        (Pretty.mark position_markup (Pretty.keyword1 "find_consts") ::
wenzelm@56912
   122
          map pretty_criterion raw_criteria)) ::
wenzelm@38335
   123
    Pretty.str "" ::
wenzelm@56908
   124
    (if null matches then [Pretty.str "found nothing"]
wenzelm@56908
   125
     else
wenzelm@56908
   126
       Pretty.str ("found " ^ string_of_int (length matches) ^ " constant(s):") ::
wenzelm@56908
   127
       grouped 10 Par_List.map (Pretty.item o single o pretty_const ctxt) matches)
wenzelm@56758
   128
  end |> Pretty.fbreaks |> curry Pretty.blk 0;
wenzelm@56758
   129
wenzelm@56758
   130
fun find_consts ctxt args = Pretty.writeln (pretty_consts ctxt args);
kleing@29884
   131
wenzelm@30142
   132
wenzelm@30143
   133
(* command syntax *)
wenzelm@30142
   134
wenzelm@38334
   135
local
wenzelm@30142
   136
wenzelm@30142
   137
val criterion =
wenzelm@36950
   138
  Parse.reserved "name" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Name ||
wenzelm@60664
   139
  Parse.reserved "strict" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.typ) >> Strict ||
wenzelm@60664
   140
  Parse.typ >> Loose;
wenzelm@30142
   141
wenzelm@56758
   142
val query = Scan.repeat ((Scan.option Parse.minus >> is_none) -- criterion);
wenzelm@56758
   143
wenzelm@59934
   144
val query_keywords = Keyword.add_keywords [((":", @{here}), NONE)] Keyword.empty_keywords;
wenzelm@58905
   145
wenzelm@38334
   146
in
wenzelm@38334
   147
wenzelm@56758
   148
fun read_query pos str =
wenzelm@59083
   149
  Token.explode query_keywords pos str
wenzelm@56758
   150
  |> filter Token.is_proper
wenzelm@56758
   151
  |> Scan.error (Scan.finite Token.stopper (Parse.!!! (query --| Scan.ahead Parse.eof)))
wenzelm@56758
   152
  |> #1;
wenzelm@56758
   153
wenzelm@30142
   154
val _ =
wenzelm@59936
   155
  Outer_Syntax.command @{command_keyword find_consts}
wenzelm@56758
   156
    "find constants by name / type patterns"
wenzelm@56758
   157
    (query >> (fn spec =>
wenzelm@56758
   158
      Toplevel.keep (fn st =>
wenzelm@56758
   159
        Pretty.writeln (pretty_consts (Toplevel.context_of st) spec))));
wenzelm@30142
   160
kleing@29884
   161
end;
kleing@29884
   162
wenzelm@56758
   163
wenzelm@56758
   164
(* PIDE query operation *)
wenzelm@56758
   165
wenzelm@56758
   166
val _ =
wenzelm@60610
   167
  Query_Operation.register {name = "find_consts", pri = Task_Queue.urgent_pri}
wenzelm@61223
   168
    (fn {state, args, writeln_result, ...} =>
wenzelm@60610
   169
      (case try Toplevel.context_of state of
wenzelm@60610
   170
        SOME ctxt =>
wenzelm@60610
   171
          let
wenzelm@60610
   172
            val [query_arg] = args;
wenzelm@60610
   173
            val query = read_query Position.none query_arg;
wenzelm@61223
   174
          in writeln_result (Pretty.string_of (pretty_consts ctxt query)) end
wenzelm@60610
   175
      | NONE => error "Unknown context"));
wenzelm@56758
   176
wenzelm@38334
   177
end;
wenzelm@38334
   178