lean4-htt/src/Init/Data/Format/Instances.lean
2026-02-05 09:10:32 +00:00

54 lines
1.6 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
module
prelude
import Init.Data.String.Search
public import Init.Data.ToString.Basic
import Init.Data.Iterators.Consumers.Collect
public section
open Std
instance (priority := low) [ToString α] : ToFormat α :=
⟨Std.Format.text ∘ toString⟩
def List.format [ToFormat α] : List α → Format
| [] => "[]"
| xs => Format.sbracket <| Format.joinSep xs ("," ++ Format.line)
instance [ToFormat α] : ToFormat (List α) where
format := List.format
instance [ToFormat α] : ToFormat (Array α) where
format a := "#" ++ format a.toList
/--
Formats an optional value, with no expectation that the Lean parser should be able to parse the
result.
This function is usually accessed through the `ToFormat (Option α)` instance.
-/
def Option.format {α : Type u} [ToFormat α] : Option α → Format
| none => "none"
| some a => "some " ++ Std.format a
instance {α : Type u} [ToFormat α] : ToFormat (Option α) :=
⟨Option.format⟩
instance {α : Type u} {β : Type v} [ToFormat α] [ToFormat β] : ToFormat (Prod α β) where
format := fun (a, b) => Format.paren <| format a ++ "," ++ Format.line ++ format b
/--
Converts a string to a pretty-printer document, replacing newlines in the string with
`Std.Format.line`.
-/
def String.toFormat (s : String) : Std.Format :=
Std.Format.joinSep (s.split '\n').toList Std.Format.line
instance : ToFormat String.Pos.Raw where
format p := format p.byteIdx