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

Hackage example does not work in source file #349

Open
codygman opened this issue Jun 21, 2020 · 3 comments
Open

Hackage example does not work in source file #349

codygman opened this issue Jun 21, 2020 · 3 comments
Labels
bug Something isn't working documentation needs documentation

Comments

@codygman
Copy link

-- example :: Members '[State String, Error String] r => Sem r String

If I past the above example into a source file, resulting in:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
import Data.Function
import Polysemy
import Polysemy.State
import Polysemy.Error

example :: Members '[State String, Error String] r => Sem r String
example = do
  put "start"
  let throwing, catching :: Members '[State String, Error String] r => Sem r String
      throwing = do
        modify (++"-throw")
        throw "error"
        get
      catching = do
        modify (++"-catch")
        get
  catch @String throwing (\ _ -> catching)

main = example
    & runError
    & fmap (either id id)
    & evalState ""
    & runM
    & (print =<<)

I get the following:

[nix-shell:~/hci]$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.8.3

[nix-shell:~/hci]$ ghc bootstrap.hs 
[1 of 1] Compiling Main             ( bootstrap.hs, bootstrap.o )

bootstrap.hs:24:8: error:
    • Couldn't match type ‘Polysemy.Internal.Union.IndexOf
                             '[State s0, Embed IO]
                             (Polysemy.Internal.Union.Found
                                '[State s0, Embed IO] (State String))’
                     with ‘State String’
        arising from a use of ‘example’
      The type variable ‘s0’ is ambiguous
    • In the first argument of ‘(&)’, namely ‘example’
      In the first argument of ‘(&)’, namely ‘example & runError’
      In the first argument of ‘(&)’, namely
        ‘example & runError & fmap (either id id)’
   |
24 | main = example
   |        ^^^^^^^
@codygman
Copy link
Author

codygman commented Jun 21, 2020

Ah, it just needs a type application:

-     & evalState ""
+     & evalState @String ""

@TheMatten
Copy link
Collaborator

You probably should have plugin enabled - use -fplugin=Polysemy.Plugin either through options_ghc pragma or project configuration.

codygman added a commit to codygman/polysemy that referenced this issue Jun 21, 2020
This adds some noise, which I'm not a huge fan of. However if I hadn't
had my cup of coffee and not tried reading the type error, I might have
pushed back "actually use polysemy in the real world" to "later".

Ideally I think I would have read the docs, but I suspect that "paste
examples verbatim into ghc source file without reading docs" is a
popular method to try libraries out.

Fixes polysemy-research#349
@codygman
Copy link
Author

Thanks for the quick response @TheMatten, it works perfectly. I had forgotten all about the plugin 😜

@TheMatten TheMatten added bug Something isn't working documentation needs documentation labels Jun 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation needs documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants