-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): Add
#where
command, dumping environment info
The command tells you your current namespace (wherever you write it), the current `include`s, and the current `variables` which have been used at least once.
- Loading branch information
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import tactic.basic | ||
|
||
open lean.parser tactic | ||
|
||
namespace where | ||
|
||
meta def mk_flag (let_var : option name := none) : lean.parser (name × ℕ) := | ||
do n ← mk_user_fresh_name, | ||
emit_code_here $ match let_var with | ||
| none := sformat!"def {n} : unit := ()" | ||
| some v := sformat!"def {n} : unit := let {v} := {v} in ()" | ||
end, | ||
nfull ← resolve_constant n, | ||
return (nfull, n.components.length) | ||
|
||
meta def get_namespace_core : name × ℕ → name | ||
| (nfull, l) := nfull.get_nth_prefix l | ||
|
||
meta def trace_namespace (ns : name) : lean.parser unit := | ||
do let str := match ns with | ||
| name.anonymous := "[root namespace]" | ||
| ns := to_string ns | ||
end, | ||
trace format!"namespace {str}" | ||
|
||
meta def strip_pi_binders : expr → list (name × binder_info × expr) | ||
| (expr.pi n bi t b) := (n, bi, t) :: strip_pi_binders b | ||
| _ := [] | ||
|
||
meta def get_includes (flag : name) : tactic (list (name × binder_info × expr)) := | ||
strip_pi_binders <$> (mk_const flag >>= infer_type) | ||
|
||
meta def binder_brackets : binder_info → string × string | ||
| binder_info.implicit := ("{", "}") | ||
| binder_info.strict_implicit := ("{", "}") | ||
| binder_info.inst_implicit := ("[", "]") | ||
| _ := ("(", ")") | ||
|
||
meta def format_binder : name × binder_info × expr → tactic string | ||
| (n, bi, e) := do let (l, r) := binder_brackets bi, | ||
e ← pp e, | ||
return sformat!"{l}{n} : {e}{r}" | ||
|
||
meta def get_all_in_namespace (ns : name) : tactic (list name) := | ||
do e ← get_env, | ||
return $ e.fold [] $ λ d l, | ||
if ns.is_prefix_of d.to_name then d.to_name :: l else l | ||
|
||
meta def erase_duplicates {α : Type} [decidable_eq α] : list α → list α | ||
| [] := [] | ||
| (x :: t) := (x :: erase_duplicates (t.filter $ λ a, a ≠ x)) | ||
|
||
meta def get_def_variables (n : name) : tactic (list (name × binder_info × expr)) := | ||
strip_pi_binders <$> (mk_const n >>= infer_type) | ||
|
||
meta def fetch_potential_variable_names (ns : name) : tactic (list name) := | ||
do l ← get_all_in_namespace ns, | ||
l ← l.mmap get_def_variables, | ||
return $ erase_duplicates $ l.join.map prod.fst | ||
|
||
-- Note: we don't call `list.find` in order that this file can be | ||
-- imported in places like `data.nat.basic` without cyclic import hell. | ||
meta def find_var (n' : name) : list (name × binder_info × expr) → option (name × binder_info × expr) | ||
| [] := none | ||
| ((n, bi, e) :: rest) := if n = n' then some (n, bi, e) else find_var rest | ||
|
||
meta def is_variable_name (n : name) : lean.parser (option (name × binder_info × expr)) := | ||
(do (f, _) ← mk_flag n, | ||
l ← get_def_variables f, | ||
return $ find_var n l | ||
) <|> return none | ||
|
||
meta def trace_variables (ns : name) : lean.parser unit := | ||
do l ← fetch_potential_variable_names ns, | ||
l ← list.filter_map id <$> l.mmap is_variable_name, | ||
str ← of_tactic' $ string.intercalate " " <$> l.mmap format_binder, | ||
if l.length = 0 then skip | ||
else trace format!"variables {str}" | ||
|
||
meta def trace_includes (l : list (name × binder_info × expr)) : tactic unit := | ||
do let str := string.intercalate " " $ l.map $ λ n, to_string n.1, | ||
if l.empty then skip | ||
else trace format!"include {str}" | ||
|
||
meta def trace_nl : ℕ → tactic unit | ||
| 0 := skip | ||
| (n + 1) := trace "" >> trace_nl n | ||
|
||
meta def trace_end (ns : name) : tactic unit := | ||
trace format!"end {ns}" | ||
|
||
meta def trace_where : lean.parser unit := | ||
do (f, n) ← mk_flag, | ||
let ns := get_namespace_core (f, n), | ||
incs ← get_includes f, | ||
trace_namespace ns, | ||
trace_nl 1, | ||
trace_variables ns, | ||
trace_includes incs, | ||
trace_nl 3, | ||
trace_end ns | ||
|
||
open interactive | ||
|
||
reserve prefix `#where`:max | ||
|
||
@[user_command] | ||
meta def where_cmd (_ : decl_meta_info) (_ : parse $ tk "#where") : lean.parser unit := trace_where | ||
|
||
end where | ||
|
||
namespace lean.parser | ||
|
||
open where | ||
|
||
meta def get_namespace : lean.parser name := | ||
get_namespace_core <$> mk_flag | ||
|
||
end lean.parser |