polysemy-research / polysemy

:gemini: higher-order, no-boilerplate monads
BSD 3-Clause "New" or "Revised" License
1.03k stars 72 forks source link

Hackage example does not work in source file #349

Open codygman opened 4 years ago

codygman commented 4 years ago

https://github.com/polysemy-research/polysemy/blob/3c731186cb5ebcfc9319586e0b691b985c6730e2/src/Polysemy/Internal.hs#L95

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 commented 4 years ago

Ah, it just needs a type application:

-     & evalState ""
+     & evalState @String ""
TheMatten commented 4 years ago

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

codygman commented 4 years ago

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