-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: incorporate Eric's html visualisation
- Loading branch information
1 parent
8128754
commit 193176a
Showing
11 changed files
with
706 additions
and
11 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,77 @@ | ||
/- | ||
Copyright (c) 2024 Jon Eugster. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Jon Eugster | ||
-/ | ||
|
||
import Lean | ||
import Batteries.Lean.NameMap | ||
import Batteries.Tactic.OpenPrivate | ||
|
||
open Lean | ||
|
||
namespace ImportGraph | ||
|
||
open Elab Meta in | ||
/-- Filter Lean internal declarations -/ | ||
def isBlackListed {m} [Monad m] [MonadEnv m] (declName : Name) : m Bool := do | ||
if declName == ``sorryAx then return true | ||
if declName matches .str _ "inj" then return true | ||
if declName matches .str _ "noConfusionType" then return true | ||
let env ← getEnv | ||
pure <| declName.isInternalDetail | ||
|| isAuxRecursor env declName | ||
|| isNoConfusion env declName | ||
<||> isRec declName <||> isMatcher declName | ||
|
||
/-- Get all declarations in the specified file. -/ | ||
def getNumberOfDeclsInFile (module : Name) : CoreM (NameSet) := do | ||
let env ← getEnv | ||
match env.moduleIdxForModule? module with | ||
| none => return {} | ||
| some modIdx => | ||
let decls := env.const2ModIdx | ||
let declsIn ← decls.foldM (fun acc n idx => do | ||
if idx == modIdx && (! (← isBlackListed n)) then return acc.insert n else return acc) ({} : NameSet) | ||
return declsIn | ||
|
||
/-- Gexf template for a node in th graph. -/ | ||
def Gexf.nodeTemplate (n module : Name) (size : Nat) := s!"<node id=\"{n}\" label=\"{n}\"><attvalues><attvalue for=\"0\" value=\"{size}\" /><attvalue for=\"1\" value=\"{module.isPrefixOf n}\" /></attvalues></node>\n " | ||
|
||
/-- Gexf template for an edge in the graph -/ | ||
def Gexf.edgeTemplate (source target : Name) := s!"<edge source=\"{source}\" target=\"{target}\" id=\"{source}--{target}\" />\n " | ||
|
||
open Gexf in | ||
/-- Creates a `.gexf` file of the graph, see https://gexf.net/ | ||
Metadata can be stored in forms of attributes, currently we record the following: | ||
* `decl_count` (Nat): number of declarations in the file | ||
* `in_module` (Bool): whether the file belongs to the main module | ||
(used to strip the first part of the name when displaying). | ||
-/ | ||
def Graph.toGexf (graph : NameMap (Array Name)) (module : Name) : CoreM String := do | ||
let sizes : NameMap Nat ← graph.foldM (fun acc n _ => do | ||
pure <| acc.insert n (← getNumberOfDeclsInFile n).size ) {} | ||
-- graph.fold (fun acc _ i => i.foldl (fun acc₂ j => acc₂.insert j ((acc₂.findD j 0) + 1)) acc) {} | ||
|
||
let nodes : String := graph.fold (fun acc n _ => acc ++ nodeTemplate n module (sizes.findD n 0)) "" | ||
let edges : String := graph.fold (fun acc n i => acc ++ (i.foldl (fun b j => b ++ edgeTemplate j n) "")) "" | ||
return s!"<?xml version='1.0' encoding='utf-8'?> | ||
<gexf xmlns=\"http://www.gexf.net/1.2draft\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd\" version=\"1.2\"> | ||
<meta> | ||
<creator>Lean ImportGraph</creator> | ||
</meta> | ||
<graph defaultedgetype=\"directed\" mode=\"static\" name=\"\"> | ||
<attributes mode=\"static\" class=\"node\"> | ||
<attribute id=\"0\" title=\"decl_count\" type=\"long\" /> | ||
<attribute id=\"1\" title=\"in_module\" type=\"boolean\" /> | ||
</attributes> | ||
<nodes> | ||
{nodes.trim} | ||
</nodes> | ||
<edges> | ||
{edges.trim} | ||
</edges> | ||
</graph> | ||
</gexf> | ||
" |
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
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 @@ | ||
imports.gexf |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Eric Wieser | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,25 @@ | ||
# Visualised import graph | ||
|
||
## Instructions | ||
|
||
To test this, place a file `imports.gexf` inside this directory. You can create such a file with | ||
|
||
``` | ||
lake exe graph html-template/imports.gexf | ||
``` | ||
|
||
Then open `index.html` in any browser and you should see the graph. | ||
|
||
## Development | ||
|
||
Currently `lake exe graph output.html` will use the files here to create a stand-alone | ||
HTML file. It does so by search-replacing the JS-scripts, the `fetch('imports.gexf')` | ||
statement, and the `<h1>` header. | ||
|
||
Therefore any modifications to these lines need to be reflected in `ImportGraph/Cli.lean`! | ||
|
||
# Credits | ||
|
||
This tool has been adapted from it's [Lean 3 version](https://github.com/eric-wieser/mathlib-import-graph) written by Eric Wieser. | ||
|
||
Adaptation by Jon Eugster. |
Oops, something went wrong.