bookshelf-doc/DocGen4/Hierarchy.lean

83 lines
2.4 KiB
Plaintext
Raw Normal View History

2021-12-12 12:28:52 +00:00
/-
Copyright (c) 2021 Henrik Böving. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Henrik Böving
-/
2021-12-12 12:20:44 +00:00
import Lean
import Std.Data.HashMap
namespace DocGen4
open Lean Std Name
def getNLevels (name : Name) (levels: Nat) : Name :=
(components.drop (components.length - levels)).reverse.foldl (· ++ ·) Name.anonymous
where
components := name.components'
inductive Hierarchy where
| node (name : Name) (isFile : Bool) (children : RBNode Name (λ _ => Hierarchy)) : Hierarchy
2021-12-12 12:20:44 +00:00
instance : Inhabited Hierarchy := ⟨Hierarchy.node Name.anonymous false RBNode.leaf⟩
2021-12-12 12:20:44 +00:00
abbrev HierarchyMap := RBNode Name (λ _ => Hierarchy)
-- Everything in this namespace is adapted from stdlib's RBNode
namespace HierarchyMap
def toList : HierarchyMap → List (Name × Hierarchy)
| t => t.revFold (fun ps k v => (k, v)::ps) []
def hForIn [Monad m] (t : HierarchyMap) (init : σ) (f : (Name × Hierarchy) → σ → m (ForInStep σ)) : m σ :=
t.forIn init (fun a b acc => f (a, b) acc)
instance : ForIn m HierarchyMap (Name × Hierarchy) where
forIn := HierarchyMap.hForIn
end HierarchyMap
namespace Hierarchy
def empty (n : Name) (isFile : Bool) : Hierarchy :=
node n isFile RBNode.leaf
2021-12-12 12:20:44 +00:00
def getName : Hierarchy → Name
| node n _ _ => n
2021-12-12 12:20:44 +00:00
def getChildren : Hierarchy → HierarchyMap
| node _ _ c => c
def isFile : Hierarchy → Bool
| node _ f _ => f
2021-12-12 12:20:44 +00:00
2021-12-12 12:33:24 +00:00
partial def insert! (h : Hierarchy) (n : Name) : Hierarchy := Id.run $ do
2021-12-12 12:20:44 +00:00
let hn := h.getName
let mut cs := h.getChildren
assert! getNumParts hn ≤ getNumParts n
if getNumParts hn + 1 == getNumParts n then
2021-12-12 12:20:44 +00:00
match cs.find Name.cmp n with
| none =>
node hn h.isFile (cs.insert Name.cmp n $ empty n true)
| some (node _ true _) => h
| some hierarchy@(node _ false ccs) =>
cs := cs.erase Name.cmp n
node hn h.isFile (cs.insert Name.cmp n $ node n true ccs)
2021-12-12 12:20:44 +00:00
else
let leveledName := getNLevels n (getNumParts hn + 1)
2021-12-12 12:20:44 +00:00
match cs.find Name.cmp leveledName with
| some nextLevel =>
cs := cs.erase Name.cmp leveledName
-- BUG?
node hn h.isFile $ cs.insert Name.cmp leveledName (nextLevel.insert! n)
2021-12-12 12:20:44 +00:00
| none =>
let child := (insert! (empty leveledName false) n)
node hn h.isFile $ cs.insert Name.cmp leveledName child
2021-12-12 12:20:44 +00:00
partial def fromArray (names : Array Name) : Hierarchy :=
names.foldl insert! (empty anonymous false)
2021-12-12 12:20:44 +00:00
end Hierarchy
end DocGen4