Repository navigation
feat(typeclass_stats): scrape typeclass hierarchy into JSON #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jcommelin
wants to merge
1
commit into
master
Choose a base branch
from
typeclass-stats
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,122 @@ | ||
| import algebra.group.defs | ||
|
|
||
| open tactic declaration environment native | ||
|
|
||
| meta def pos_line (p : option pos) : string := | ||
| match p with | ||
| | some x := to_string x.line | ||
| | _ := "null" | ||
| end | ||
|
|
||
| def prepare_filename₁ : string → list string := | ||
| λ xs, ((xs.split_on '/').drop_while (λ x, x ≠ "library" ∧ x ≠ "src")) | ||
|
|
||
| def prepare_filename₂ : list string → list string | ||
| | ("library" :: l) := ("core" :: l) | ||
| | ("src" :: l) := l | ||
| | _ := [] | ||
|
|
||
| def filename : string → string := | ||
| "/".intercalate ∘ prepare_filename₂ ∘ prepare_filename₁ | ||
|
|
||
| def topic : string → string := | ||
| list.head ∘ prepare_filename₂ ∘ prepare_filename₁ | ||
|
|
||
| structure item := | ||
| (file : string) | ||
| (line : string) | ||
| (name : string) | ||
| (topic : string) | ||
|
|
||
| structure node extends item. | ||
|
|
||
| namespace node | ||
|
|
||
| def jsonify : node → string := | ||
| λ n, | ||
| "{ \"name\" : \"" ++ n.name ++ "\",\n" ++ | ||
| "\"topic\" : \"" ++ n.topic ++ "\",\n" ++ | ||
| "\"file\" : \"" ++ n.file ++ "\",\n" ++ | ||
| "\"line\" : " ++ n.line ++ " }\n" | ||
|
|
||
| instance : has_to_string node := ⟨jsonify⟩ | ||
|
|
||
| end node | ||
|
|
||
| structure edge extends item := | ||
| (source : string) | ||
| (target : string) | ||
|
|
||
| namespace edge | ||
|
|
||
| def jsonify : edge → string := | ||
| λ e, | ||
| "{ \"name\" : \"" ++ e.name ++ "\",\n" ++ | ||
| "\"topic\" : \"" ++ e.topic ++ "\",\n" ++ | ||
| "\"source\" : \"" ++ e.source ++ "\",\n" ++ | ||
| "\"target\" : \"" ++ e.target ++ "\",\n" ++ | ||
| "\"file\" : \"" ++ e.file ++ "\",\n" ++ | ||
| "\"line\" : " ++ e.line ++ " }\n" | ||
|
|
||
| instance : has_to_string edge := ⟨jsonify⟩ | ||
|
|
||
| end edge | ||
|
|
||
| /- parses information about `decl` if it is an instance or a class. -/ | ||
| meta def parse_decl (env : environment) (decl : declaration) : | ||
| tactic (option (node ⊕ edge)) := | ||
| let name := decl.to_name in | ||
| do | ||
| if (env.decl_olean name).is_some | ||
| then do | ||
| olean_file ← env.decl_olean name, | ||
| let I : item := | ||
| { name := to_string name, | ||
| file := filename olean_file, | ||
| line := pos_line (env.decl_pos name), | ||
| topic := topic olean_file }, | ||
| (l, tgt) ← return decl.type.pi_binders, | ||
| guard (l.tail.all $ λ b, b.info = binder_info.inst_implicit), | ||
| is_c ← succeeds (tactic.has_attribute `class name), | ||
| if is_c then do | ||
| let N : node := { .. I }, | ||
| return (some (sum.inl N)) | ||
| else do | ||
| is_i ← succeeds (tactic.has_attribute `instance name), | ||
| if is_i then do | ||
| guard (tgt.get_app_args.head.is_var && l.ilast.type.get_app_args.head.is_var), | ||
| let src := to_string l.ilast.type.erase_annotations.get_app_fn.const_name, | ||
| let tgt := to_string tgt.erase_annotations.get_app_fn.const_name, | ||
| guard (src ≠ tgt), | ||
| let E : edge := { source := src, target := tgt, .. I }, | ||
| return (some (sum.inr E)) | ||
| else do return none | ||
| else do return none | ||
|
|
||
| /-- prints information about unary classes and forgetful instances in the environment. | ||
| It only prints instances and classes that have at most 1 argument | ||
| that is not a type-class argument (within square brackets), | ||
| and the instances can only be forgetful instances | ||
| (where the conclusion is a class applied to a variable) -/ | ||
| meta def print_content : tactic unit := | ||
| do curr_env ← get_env, | ||
| o ← (curr_env.fold [] list.cons).mmap_filter (λ x, parse_decl curr_env x <|> pure none), | ||
| let (ns, es) := o.partition_map id, | ||
| trace "{ \"nodes\" : \n", | ||
| trace (to_string ns), | ||
| trace ",\n \"edges\" :\n", | ||
| trace (to_string es), | ||
| trace "}", | ||
| skip | ||
|
|
||
| meta def test : tactic unit := | ||
| do curr_env ← get_env, | ||
| d ← get_decl `comm_monoid.to_comm_semigroup, | ||
| trace (to_string d.to_name), | ||
| o ← parse_decl curr_env d, | ||
| trace (to_string o), | ||
| skip | ||
|
|
||
| -- run_cmd test | ||
|
|
||
| run_cmd print_content | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's now an API for building json, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's right. Feel free to hack on this. I mostly put up this PR so that we don't loose the script.