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

feat: upstream ListM from mathlib4 #165

Merged
merged 11 commits into from
Aug 8, 2023
Merged

Conversation

kim-em
Copy link
Collaborator

@kim-em kim-em commented Jun 26, 2023

This migrates ListM upstream from Mathlib, renaming it to MLList (for "monadic lazy list", returning to the mathlib3 name), and making some adjustments so more operations are lazy.

@fgdorais
Copy link
Collaborator

fgdorais commented Jun 26, 2023

Can be merged as-is (mathlib down-port).

Just a nitpick: shouldn't it be listMAPI instead of listMApi? This is not important and so I'm accepting this "incorrect" spelling.

@digama0
Copy link
Member

digama0 commented Jun 26, 2023

the naming convention from core is ListM.Spec

@fgdorais
Copy link
Collaborator

the naming convention from core is ListM.Spec

Sounds a lot better to me! @semorrison ?

Std/Data/ListM/Basic.lean Outdated Show resolved Hide resolved
Std/Data/ListM/Basic.lean Outdated Show resolved Hide resolved
Std/Data/ListM/Basic.lean Outdated Show resolved Hide resolved
Std/Data/ListM/Basic.lean Outdated Show resolved Hide resolved
@digama0
Copy link
Member

digama0 commented Jun 29, 2023

@semorrison I made a bunch of modifications here. Most notably, some of the function arguments have gained a Unit -> because we want to run them lazily (e.g. in append), and also the new Thunk type is added as a constructor so that you can have haskell style "evaluated and the result is remembered so you don't have to evaluate it again next time". It is used to prevent deep recursion or infinite recursion in pure functions here, rather than building up a long cons chain immediately.

@fgdorais
Copy link
Collaborator

fgdorais commented Jul 3, 2023

Since @digama0 has baked-in laziness, maybe we should consider renaming this LazyListT and specialize to the new type LazyList when m:=Id?

One reason is that the specialization to m:=Id is actually interesting and it deserves a cool name! LazyListM would also work but I think that we're moving away from that after the OptionM/Option debacle.

Another reason is that I would expect ListM to reduce to something essentially equivalent to List when m:=Id. But now we have something substantially different from List when m:=Id.

In my mind, ListT m α should implement what may be informally described by m (α × m (α × ...)) (in the same sense that List α can be informally described by α × (α × (α × ...))). The monad m could be used to implement some laziness but that is not necessarily the goal since List is strict. Therefore, I think we are now looking at a fundamentally new type.

PS: Why this was called ListM instead of ListT. Is that a historical accident?

@digama0
Copy link
Member

digama0 commented Jul 3, 2023

PS: Why this was called ListM instead of ListT. Is that a historical accident?

MList might be a better name, it means essentially "monadic (lazy) list". In lean 3 it was called mllist. ListM looks a bit more similar to others but it's probably a false cognate since it's not a monad (or rather, it is but that's not the focus of the type).

The reason for baking in the laziness is because you can end up with a completely different order of operations when using do notation than what you would expect, when the monad is something "strict" like Id, Option or Except. This is basically the same reason that andThen, orElse etc have a Unit -> thunk around their second argument, there were actual bug reports about this where things would get lifted out of the bottom half of a do block and executed unconditionally, leading to extremely difficult to interpret behavior after optimizations.

@fgdorais
Copy link
Collaborator

fgdorais commented Jul 3, 2023

I see. The M is because this is a "list type that works well within monadic code" not that it is a monad transformer. In that case, putting the M at the end is confusing. I think it's good to have Lazy in there since that's a key feature. MLazyList sounds nice?

@kim-em
Copy link
Collaborator Author

kim-em commented Aug 6, 2023

Sorry, I haven't looked at this in a while. I am slightly in favour of MList over MLazyList, as I think the lazyness is subordinate to the monadicness.

However the much bigger chore that remains is to building mathlib against Mario's changes and see if everything still works. I will try to make this a priority.

@kim-em
Copy link
Collaborator Author

kim-em commented Aug 7, 2023

Downstream I used consOption (called just cons in the Mathlib version, with cons' being the cons in this PR) all the time. So having it disappear is a bit of a surprise.

EDIT: looking at the code, I actually never need it! Nice. :-)

@kim-em
Copy link
Collaborator Author

kim-em commented Aug 7, 2023

Remaining tasks here:

@digama0
Copy link
Member

digama0 commented Aug 7, 2023

I'm basically happy with the current state of things. @semorrison, what do you think about the name MLList, returning to the old lean 3 name?

@kim-em kim-em requested a review from digama0 August 8, 2023 01:24
@kim-em kim-em added the awaiting-review This PR is ready for review; the author thinks it is ready to be merged. label Aug 8, 2023
@digama0 digama0 merged commit d0e10bf into leanprover-community:main Aug 8, 2023
1 check passed
bors bot pushed a commit to leanprover-community/mathlib4 that referenced this pull request Aug 9, 2023
`ListM` has been [moved to `Std`](leanprover-community/batteries#165) and renamed as `MLList`.

There are some slight API functions, that ensure operations are lazy, requiring the addition of some `fun _ =>` here in Mathlib.



Co-authored-by: Scott Morrison <scott.morrison@anu.edu.au>
Co-authored-by: Mario Carneiro <di.gama@gmail.com>
Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
Co-authored-by: Bulhwi Cha <chabulhwi@semmalgil.com>
kim-em added a commit to leanprover-community/mathlib4 that referenced this pull request Aug 14, 2023
`ListM` has been [moved to `Std`](leanprover-community/batteries#165) and renamed as `MLList`.

There are some slight API functions, that ensure operations are lazy, requiring the addition of some `fun _ =>` here in Mathlib.

Co-authored-by: Scott Morrison <scott.morrison@anu.edu.au>
Co-authored-by: Mario Carneiro <di.gama@gmail.com>
Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
Co-authored-by: Bulhwi Cha <chabulhwi@semmalgil.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review This PR is ready for review; the author thinks it is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants