@@ -50,13 +50,13 @@ inductive Key where
5050 | proj (typeName : Name) (idx nargs : Nat)
5151 deriving Inhabited, BEq
5252
53- set_option backward.privateInPublic true in
54- /-
55- At the root, `.const` is the most common key, and it is very uncommon
53+ /-- Compute the hash of a `RefinedDiscrTree.Key`.
54+
55+ Note: at the root, `.const` is the most common key, and it is very uncommon
5656to get the same constant name with a different arity.
5757So for performance, we just use `hash name` to hash `.const name _`.
5858-/
59- private nonrec def Key.hash : Key → UInt64
59+ protected def Key.hash : Key → UInt64
6060 | .star => 0
6161 | .labelledStar id => mixHash 5 <| hash id
6262 | .opaque => 1
@@ -69,12 +69,10 @@ private nonrec def Key.hash : Key → UInt64
6969 | .«forall » => 4
7070 | .proj name idx nargs => mixHash (hash nargs) <| mixHash (hash name) (hash idx)
7171
72- set_option backward.privateInPublic true in
73- set_option backward.privateInPublic.warn false in
7472instance : Hashable Key := ⟨Key.hash⟩
7573
76- set_option backward.privateInPublic true in
77- private def Key.format : Key → Format
74+ /-- Format a `RefinedDiscrTree.Key`. -/
75+ def Key.format : Key → Format
7876 | .star => f!"*"
7977 | .labelledStar id => f!"*{id}"
8078 | .opaque => "◾"
@@ -88,12 +86,8 @@ private def Key.format : Key → Format
8886 | .forall => "∀"
8987 | .proj name idx nargs => f!"⟨{name}.{idx}, {nargs}⟩"
9088
91- set_option backward.privateInPublic true in
92- set_option backward.privateInPublic.warn false in
9389instance : ToFormat Key := ⟨Key.format⟩
9490
95- set_option backward.privateInPublic true in
96- set_option backward.privateInPublic.warn false in
9791/--
9892Converts an entry (i.e., `List Key`) to the discrimination tree into
9993`MessageData` that is more user-friendly.
@@ -186,13 +180,11 @@ inductive StackEntry where
186180 /-- `.expr` is an expression that will be indexed. -/
187181 | expr (info : ExprInfo)
188182
189- set_option backward.privateInPublic true in
190- private def StackEntry.format : StackEntry → Format
183+ /-- Format a `RefinedDiscrTree.StackEntry`. -/
184+ def StackEntry.format : StackEntry → Format
191185 | .star => f!".star"
192186 | .expr info => f!".expr {info.expr}"
193187
194- set_option backward.privateInPublic true in
195- set_option backward.privateInPublic.warn false in
196188instance : ToFormat StackEntry := ⟨StackEntry.format⟩
197189
198190/-- A `LazyEntry` represents a snapshot of the computation of encoding an `Expr` as `Array Key`.
@@ -240,17 +232,15 @@ def mkInitLazyEntry (labelledStars : Bool) : MetaM LazyEntry :=
240232 labelledStars? := if labelledStars then some #[] else none
241233 }
242234
243- set_option backward.privateInPublic true in
244- private def LazyEntry.format (entry : LazyEntry) : Format := Id.run do
235+ /-- Format a `RefinedDiscrTree.LazyEntry`. -/
236+ def LazyEntry.format (entry : LazyEntry) : Format := Id.run do
245237 let mut parts := #[f!"stack: {entry.stack}" ]
246238 unless entry.computedKeys == [] do
247239 parts := parts.push f!"results: {entry.computedKeys}"
248240 if let some info := entry.previous then
249241 parts := parts.push f!"todo: {info.expr}"
250242 return Format.joinSep parts.toList ", "
251243
252- set_option backward.privateInPublic true in
253- set_option backward.privateInPublic.warn false in
254244instance : ToFormat LazyEntry := ⟨LazyEntry.format⟩
255245
256246/-- Array index of a `Trie α` in the `tries` of a `RefinedDiscrTree`. -/
@@ -302,15 +292,48 @@ namespace RefinedDiscrTree
302292
303293variable {α : Type }
304294
305- set_option backward.privateInPublic true in
306- private partial def format [ToFormat α] (tree : RefinedDiscrTree α) : Format :=
295+ /-- Format a `RefinedDiscrTree` as a flowchart.
296+ - Non-terminal nodes are of the form `{key} =>`, followed by all of the following nodes,
297+ indented with 2 more spaces.
298+ - Terminal nodes have either "entries", containing the return values,
299+ or "pending entries", for nodes that have not been evaluated/expanded.
300+
301+ For example:
302+ ```
303+ Discrimination tree flowchart:
304+ ⟨HMul.hMul, 6⟩ =>
305+ ⟨Int, 0⟩ =>
306+ ⟨Int, 0⟩ =>
307+ * =>
308+ * =>
309+ *0 =>
310+ *0 =>
311+ pending entries: #[mul_self]
312+ *1 =>
313+ entries: #[mul_comm]
314+ ⟨Neg.neg, 3⟩ =>
315+ ⟨Int, 0⟩ =>
316+ * =>
317+ *1 =>
318+ entries: #[mul_neg]
319+ 1 =>
320+ pending entries: #[mul_one]
321+ ⟨Neg.neg, 3⟩ =>
322+ pending entries: #[neg_mul]
323+ 1 =>
324+ *0 =>
325+ entries: #[one_mul]
326+ ```
327+ -/
328+ partial def format [ToFormat α] (tree : RefinedDiscrTree α) : Format :=
307329 let lines := tree.root.fold (init := #[]) fun lines key trie =>
308330 lines.push (Format.nest 2 f!"{key} =>{Format.line}{go trie}" )
309331 if lines.size = 0 then
310332 f!"<empty discrimination tree>"
311333 else
312- "Discrimination tree flowchart:" ++ Format.joinSep lines.toList "\n "
334+ "Discrimination tree flowchart:\n " ++ Format.joinSep lines.toList "\n "
313335where
336+ /-- Auxiliary function for `RefinedDiscrTree.format`. -/
314337 go (trie : TrieIndex) : Format := Id.run do
315338 let { values, star, labelledStars, children, pending } := tree.tries[trie]!
316339 let mut lines := #[]
@@ -329,8 +352,6 @@ where
329352 else
330353 Format.joinSep lines.toList "\n "
331354
332- set_option backward.privateInPublic true in
333- set_option backward.privateInPublic.warn false in
334355instance [ToFormat α] : ToFormat (RefinedDiscrTree α) := ⟨format⟩
335356
336357end Lean.Meta.RefinedDiscrTree
0 commit comments