Skip to content

Add MonadDeclare #32

Closed
Closed

Description

@fizruk

swagger2 makes use of a DeclareT monad transformer which lies between the WriterT and StateT.
It also provides an mtl-style MonadDeclare type class with some laws.
Since the construction is not specific to swagger2, it might make sense to add it to mtl and transformers.

A corresponding issue for transformers is here.

The rationale for Declare is generating a list of Swagger schema definitions.
To avoid duplicates and to fix recursive schemas we need to look at the previous definitions.
But we don't want a full State because we don't want someone to accidentally erase/change all/some previous definitions in a ToSchema instance for a user data type.
The laziness plays nice part here and allows (mutually) recursive schemas to be written easily (e.g. see sources for declareSchemaRef).

Activity

  1. ElvishJerricco commented on Jul 8, 2016

    @ElvishJerricco
    Contributor

    It is possible, when working directly in DeclareT, to erase declared values.

    ghci> execDeclare [] $ do
      declare [1]
      DeclareT $ const $ return ([], ())
    []

    Now, I see that the laws are carefully constructed such that this isn't a violation of any sort. But it does make me question the usefulness of those laws. When given an arbitrary MonadDeclare d m => m a, you have no idea if it behaves as a state monad or a declare monad, as it might wipe any declarations you make before using it. I imagine this should be seen as a problem?

  2. fizruk commented on Jul 8, 2016

    @fizruk
    Author

    @ElvishJerricco I suspect you didn't actually run that in GHCi:

    • execDeclare receives arguments in different order;
    • DeclareT $ const $ return ([], ()) is essentially declare [];
    • the output is actually [1], as expected.

    Here's what I get:

    $ stack ghci
    ...
    >>> flip execDeclare [] $ do { declare [1]; DeclareT $ const $ return ([], ()); }
    [1]
    

    I believe the laws are useful and although you made me doubt it, I still think they don't allow any MonadDeclare instance to erase output. However they probably might be better and/or clearer and I am open to suggestions!

  3. ElvishJerricco commented on Jul 8, 2016

    @ElvishJerricco
    Contributor

    Oh geez you're right. That's what I get for browsing github issues on my phone =P My bad!

  4. fizruk commented on May 24, 2017

    @fizruk
    Author

    Starting with transformers-0.5.4.0 there's Control.Monad.Trans.Accum which is DeclareT under a different (more general) name — AccumT.

  5. chessai commented on Jun 24, 2019

    @chessai
    Member

    Since AccumT is now in transformers, and #44 is an open issue, I am going to close this. Anyone feel free to re-open or ping to re-open if I'm missing something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions