DeepSpec / InteractionTrees

A Library for Representing Recursive and Impure Programs in Coq
MIT License
194 stars 49 forks source link

[WIP] Moving to stdpp #267

Open YaZko opened 2 months ago

YaZko commented 2 months ago

Goal

This ongoing PR experiments with switching from ext-lib to stdpp. The essential rational is the increasing traction that the latter has had: it is largely both used and contributed to. Potential more concrete benefits include:

Ongoing issues

Reduction behaviour of mbind (m := itree E)

Broken proof for cat_iter in KTreeFacts.v at the moment. Running cbn unfolds mbind and exposes the cofix brutally.

Changes and observations

Classes: Monad vs. MBind and MRet

There are already two relevant classes defined in stdpp, exclusively for notation purposes: MRet and MBind. They therefore replace the Monad class that used to package both. This change comes with three immediate additional side effects:

  1. Naming: the operations are called mret and mbind (instead of ret and bind)

  2. Notations: the associated notations live at different levels than what we were used too. Furthermore, they are slightly distinct.

    • x <- c;; k becomes x ← c ; k (only one ";", and a \gets ← instead of ascii <-)
    • x >>= k becomes x ≫= k (a \gg ≫ instead of ascii >>)
  3. Change of order of arguments in mbind: mbind takes the continuation first, similar to what we usually call subst

Reduction behaviour

Considering for instance theories/Basics/MonadState.v. We used to have a very aggressive unfolding behaviour over the state monad. For instance, in:

  Goal forall {A B} (c : stateT S M A) (k : A -> stateT S M B),
      bind c k = bind c k.
 intros.
 cbn.

Would completely expose the definition of bind, reducing bind c k to (fun s : S => bind (c s) (fun sa : S * A => k (snd sa) (fst sa))). In contrast with mbind, it is inert.

Ensembles.Ensemble vs. propset

It seems to make sense to use propset, for instance when it comes to the so-called Prop monad. Although it's still too early to tell really.

Existing coercion: Is_true

ERRATUM: the coercion and the tactic come from the standard library!!

stdpp already has a coercion replacing is_true. Its definition is however slightly different, requiring to destruct b instead of rewriting the coerced hypothesis. As far as I understand, the typical way to do so would be via the destr_bool tactic. However, this tactic currently loops¹ in presence of a section variable boolean, making it a little less smooth than desired.

¹ See https://github.com/coq/coq/issues/18858

Chaining class constraints

I discovered in the process the following syntax:

Definition foo {M} `{MRet M, MBind M, ...}

to list class constraints.

Progress

gmalecha commented 2 months ago

We are pushed to use stdpp with iris and it generally works well. Their monad library is pretty minimal, but we've done a lot of work at bedrock building universe polymorphic monads, traversable and applicative as well as monads like state, reader, etc. we would like to get those released at some point.

YaZko commented 2 months ago

We are pushed to use stdpp with iris and it generally works well. Their monad library is pretty minimal, but we've done a lot of work at bedrock building universe polymorphic monads, traversable and applicative as well as monads like state, reader, etc. we would like to get those released at some point.

Hey @gmalecha , That sounds awesome, that would certainly save us some overhead in porting our libraries. Do you have any idea how soon such a release could take place?

gmalecha commented 2 months ago

I think we can have it out by the end of the week. Would that work?

YaZko commented 2 months ago

Most certainly yes, that would be great!

gmalecha commented 2 months ago

@YaZko Here is the repository that we have right now. https://github.com/bedrocksystems/BRiCk/tree/master/coq-upoly It is currently licensed using LGPL2 with a BedRock exception, but I don't think it will be a problem to drop the exception. We would welcome contributions.

YaZko commented 2 months ago

Thanks @gmalecha . I need to take more time to dive into and play with it, but this looks great!

gmalecha commented 2 months ago

Thanks go to David Swasey for doing much of the development and the upstreaming work.