Skip to content
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

[Merged by Bors] - feat: port: Data.DList.Basic #616

Closed
wants to merge 11 commits into from
45 changes: 45 additions & 0 deletions Mathlib/Data/DList/Basic.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import Std.Data.DList
import Mathlib.Mathport.Rename


/-!
# Difference list

This file provides a few results about `DList`, which is defined in `Std`.

A difference list is a function that, given a list, returns the original content of the
difference list prepended to the given list. It is useful to represent elements of a given type
as `a₁ + ... + aₙ` where `+ : α → α → α` is any operation, without actually computing.

This structure supports `O(1)` `append` and `concat` operations on lists, making it
useful for append-heavy uses such as logging and pretty printing.
-/


arienmalec marked this conversation as resolved.
Show resolved Hide resolved
/-- Concatenates a list of difference lists to form a single difference list. Similar to
`list.join`. -/
arienmalec marked this conversation as resolved.
Show resolved Hide resolved
def DList.join {α : Type _} : List (Std.DList α) → Std.DList α
| [] => Std.DList.empty
| x :: xs => x ++ DList.join xs
#align dlist.join DList.join

/-- Convert a lazily-evaluated `List` to a `DList` -/
-- Ported from Lean 3 core
def DList.lazy_OfList (l : Thunk (List α)) : Std.DList α :=
⟨fun xs => l.get ++ xs, fun t => by simp⟩
#align dlist.lazy_of_list DList.lazy_OfList

@[simp]
theorem DList_singleton {α : Type _} {a : α} : Std.DList.singleton a = DList.lazy_OfList [a] :=
rfl
#align dlist_singleton DList_singleton

@[simp]
theorem DList_lazy {α : Type _} {l : List α} : DList.lazy_OfList l = Std.DList.ofList l :=
Ruben-VandeVelde marked this conversation as resolved.
Show resolved Hide resolved
rfl
#align dlist_lazy DList_lazy