author | wenzelm |
Sun, 01 Mar 2009 23:36:12 +0100 | |
changeset 30190 | 479806475f3c |
parent 30188 | 82144a95f9ec |
child 30206 | 48507466d9d2 |
permissions | -rw-r--r-- |
30143 | 1 |
(* Title: Pure/Tools/find_consts.ML |
29884 | 2 |
Author: Timothy Bourke and Gerwin Klein, NICTA |
3 |
||
30143 | 4 |
Hoogle-like (http://www-users.cs.york.ac.uk/~ndm/hoogle) searching by |
5 |
type over constants, but matching is not fuzzy. |
|
29884 | 6 |
*) |
7 |
||
8 |
signature FIND_CONSTS = |
|
9 |
sig |
|
30143 | 10 |
datatype criterion = |
11 |
Strict of string |
|
12 |
| Loose of string |
|
13 |
| Name of string |
|
29884 | 14 |
|
15 |
val default_criteria : (bool * criterion) list ref |
|
16 |
||
17 |
val find_consts : Proof.context -> (bool * criterion) list -> unit |
|
18 |
end; |
|
19 |
||
20 |
structure FindConsts : FIND_CONSTS = |
|
21 |
struct |
|
22 |
||
30143 | 23 |
(* search criteria *) |
24 |
||
25 |
datatype criterion = |
|
26 |
Strict of string |
|
27 |
| Loose of string |
|
28 |
| Name of string; |
|
29884 | 29 |
|
30 |
val default_criteria = ref [(false, Name ".sko_")]; |
|
31 |
||
30143 | 32 |
|
33 |
(* matching types/consts *) |
|
29884 | 34 |
|
30143 | 35 |
fun add_tye (_, (_, t)) n = Term.size_of_typ t + n; |
36 |
||
37 |
fun matches_subtype thy typat = |
|
38 |
let |
|
29884 | 39 |
val p = can (fn ty => Sign.typ_match thy (typat, ty) Vartab.empty); |
40 |
||
41 |
fun fs [] = false |
|
30143 | 42 |
| fs (t :: ts) = f t orelse fs ts |
29884 | 43 |
|
44 |
and f (t as Type (_, ars)) = p t orelse fs ars |
|
45 |
| f t = p t; |
|
46 |
in f end; |
|
47 |
||
30143 | 48 |
fun check_const p (nm, (ty, _)) = |
49 |
if p (nm, ty) |
|
50 |
then SOME (Term.size_of_typ ty) |
|
51 |
else NONE; |
|
29884 | 52 |
|
30143 | 53 |
fun opt_not f (c as (_, (ty, _))) = |
54 |
if is_some (f c) |
|
55 |
then NONE else SOME (Term.size_of_typ ty); |
|
29884 | 56 |
|
29895
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
57 |
fun filter_const (_, NONE) = NONE |
30143 | 58 |
| filter_const (f, (SOME (c, r))) = |
59 |
Option.map (pair c o (curry Int.min r)) (f c); |
|
60 |
||
61 |
||
62 |
(* pretty results *) |
|
29895
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
63 |
|
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
64 |
fun pretty_criterion (b, c) = |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
65 |
let |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
66 |
fun prfx s = if b then s else "-" ^ s; |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
67 |
in |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
68 |
(case c of |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
69 |
Strict pat => Pretty.str (prfx "strict: " ^ quote pat) |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
70 |
| Loose pat => Pretty.str (prfx (quote pat)) |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
71 |
| Name name => Pretty.str (prfx "name: " ^ quote name)) |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
72 |
end; |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
73 |
|
30143 | 74 |
fun pretty_const ctxt (nm, ty) = |
75 |
let |
|
29895
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
76 |
val ty' = Logic.unvarifyT ty; |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
77 |
in |
30143 | 78 |
Pretty.block |
79 |
[Pretty.quote (Pretty.str nm), Pretty.fbrk, |
|
80 |
Pretty.str "::", Pretty.brk 1, |
|
81 |
Pretty.quote (Syntax.pretty_typ ctxt ty')] |
|
29895
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
82 |
end; |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
83 |
|
30143 | 84 |
|
85 |
(* find_consts *) |
|
86 |
||
87 |
fun find_consts ctxt raw_criteria = |
|
88 |
let |
|
29895
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
89 |
val start = start_timing (); |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
90 |
|
29884 | 91 |
val thy = ProofContext.theory_of ctxt; |
92 |
val low_ranking = 10000; |
|
93 |
||
30143 | 94 |
fun make_pattern crit = ProofContext.read_term_pattern ctxt ("_::" ^ crit) |> Term.type_of; |
29884 | 95 |
|
96 |
fun make_match (Strict arg) = |
|
97 |
let val qty = make_pattern arg; in |
|
30143 | 98 |
fn (_, (ty, _)) => |
99 |
let |
|
29884 | 100 |
val tye = Sign.typ_match thy (qty, ty) Vartab.empty; |
101 |
val sub_size = Vartab.fold add_tye tye 0; |
|
102 |
in SOME sub_size end handle MATCH => NONE |
|
103 |
end |
|
104 |
||
105 |
| make_match (Loose arg) = |
|
106 |
check_const (matches_subtype thy (make_pattern arg) o snd) |
|
107 |
||
108 |
| make_match (Name arg) = check_const (match_string arg o fst); |
|
109 |
||
110 |
fun make_criterion (b, crit) = (if b then I else opt_not) (make_match crit); |
|
30143 | 111 |
val criteria = map make_criterion (! default_criteria @ raw_criteria); |
29884 | 112 |
|
113 |
val (_, consts) = (#constants o Consts.dest o Sign.consts_of) thy; |
|
30190 | 114 |
fun eval_entry c = List.foldl filter_const (SOME (c, low_ranking)) criteria; |
29884 | 115 |
|
30143 | 116 |
val matches = |
117 |
Symtab.fold (cons o eval_entry) consts [] |
|
118 |
|> map_filter I |
|
119 |
|> sort (rev_order o int_ord o pairself snd) |
|
120 |
|> map ((apsnd fst) o fst); |
|
29895
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
121 |
|
30188
82144a95f9ec
avoid fragile parsing of end_timing result -- would have produced GC time on MosML, for example;
wenzelm
parents:
30143
diff
changeset
|
122 |
val end_msg = " in " ^ Time.toString (#all (end_timing start)) ^ " secs"; |
29884 | 123 |
in |
29895
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
124 |
Pretty.big_list "searched for:" (map pretty_criterion raw_criteria) |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
125 |
:: Pretty.str "" |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
126 |
:: (Pretty.str o concat) |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
127 |
(if null matches |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
128 |
then ["nothing found", end_msg] |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
129 |
else ["found ", (string_of_int o length) matches, |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
130 |
" constants", end_msg, ":"]) |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
131 |
:: Pretty.str "" |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
132 |
:: map (pretty_const ctxt) matches |
0e70a29d3e02
find_consts: display the search criteria. (by Timothy Bourke)
kleing
parents:
29884
diff
changeset
|
133 |
|> Pretty.chunks |
29884 | 134 |
|> Pretty.writeln |
30143 | 135 |
end handle ERROR s => Output.error_msg s; |
29884 | 136 |
|
30142
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
137 |
|
30143 | 138 |
(* command syntax *) |
30142
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
139 |
|
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
140 |
fun find_consts_cmd spec = |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
141 |
Toplevel.unknown_theory o Toplevel.keep (fn state => |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
142 |
find_consts (Proof.context_of (Toplevel.enter_proof_body state)) spec); |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
143 |
|
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
144 |
local |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
145 |
|
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
146 |
structure P = OuterParse and K = OuterKeyword; |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
147 |
|
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
148 |
val criterion = |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
149 |
P.reserved "strict" |-- P.!!! (P.$$$ ":" |-- P.xname) >> Strict || |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
150 |
P.reserved "name" |-- P.!!! (P.$$$ ":" |-- P.xname) >> Name || |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
151 |
P.xname >> Loose; |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
152 |
|
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
153 |
in |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
154 |
|
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
155 |
val _ = |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
156 |
OuterSyntax.improper_command "find_consts" "search constants by type pattern" K.diag |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
157 |
(Scan.repeat (((Scan.option P.minus >> is_none) -- criterion)) |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
158 |
>> (Toplevel.no_timing oo find_consts_cmd)); |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
159 |
|
29884 | 160 |
end; |
161 |
||
30142
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
162 |
end; |
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
wenzelm
parents:
29895
diff
changeset
|
163 |