freer-simple - How can I generate a list of effect members at runtime?












0















I am trying to create a function to generate a string representation of effects at runtime.



-- In a shared module



... definitions for TestConfig, RunConfig

data GenericTest tc rc i effs as vs = GenericTest {
configuration :: tc,
components :: ItemClass i vs => TestComponents rc i effs as vs
} deriving Typeable

type Test = GenericTest TestConfig RunConfig

type EFFLogger effs = Member Logger effs
type EFFFileSystem effs = Members '[Logger, Ensure, FileSystem] effs


-- Child module 1



.... definitions for items, iterator etc 

type Effects effs = EFFFileSystem effs

test :: forall effs. Effects effs => Test Item effs ApState ValState
test = GenericTest {
configuration = config {address = moduleOf ''ApState},
components = TestComponents {
testItems = items,
testInteractor = interactor,
testPrepState = prepState
}
}


-- Child module 2 (the same as module 1 but different effects)



.... definitions for items, iterator etc 

type Effects effs = EFFLogger effs

test :: forall effs. Effects effs => Test Item effs ApState ValState
test = GenericTest {
configuration = config {address = moduleOf ''ApState},
components = TestComponents {
testItems = items,
testInteractor = interactor,
testPrepState = prepState
}
}


At run time I want a function f such that:



> f ChildMod1.test 
> ["Logger", "Ensure", "FileSystem"]
>
> f ChildMod2.test
> ["Logger"]


From repl with Child module 1 loaded I can get the following, which if I could get something similar in non-interpreted code would be enough to get me what I want:



> :t test
> test
:: (Data.OpenUnion.Internal.FindElem Logger effs,
Data.OpenUnion.Internal.FindElem Ensure effs,
Data.OpenUnion.Internal.FindElem FileSystem effs) =>
Test Item effs ApState ValState


I have tried using Typeable as suggested in the following:



How can I read the metadata of a type at runtime?



but typeOf gives me issues which I have no clue how to solve:



> typeOf test

<interactive>:5:1-11: error:
* No instance for (Typeable effs0) arising from a use of `typeOf'
* In the expression: typeOf test
In an equation for `it': it = typeOf test

<interactive>:5:8-11: error:
* Ambiguous type variable `effs0' arising from a use of `test'
prevents the constraint `(Data.OpenUnion.Internal.FindElem
Logger effs0)' from being solved.
Probable fix: use a type annotation to specify what `effs0' should be.
These potential instances exist:
two instances involving out-of-scope types
instance [overlappable] Data.OpenUnion.Internal.FindElem t r =>
Data.OpenUnion.Internal.FindElem t (t' : r)
-- Defined in `Data.OpenUnion.Internal'
instance Data.OpenUnion.Internal.FindElem t (t : r)
-- Defined in `Data.OpenUnion.Internal'
* In the first argument of `typeOf', namely `test'
In the expression: typeOf test
In an equation for `it': it = typeOf test









share|improve this question



























    0















    I am trying to create a function to generate a string representation of effects at runtime.



    -- In a shared module



    ... definitions for TestConfig, RunConfig

    data GenericTest tc rc i effs as vs = GenericTest {
    configuration :: tc,
    components :: ItemClass i vs => TestComponents rc i effs as vs
    } deriving Typeable

    type Test = GenericTest TestConfig RunConfig

    type EFFLogger effs = Member Logger effs
    type EFFFileSystem effs = Members '[Logger, Ensure, FileSystem] effs


    -- Child module 1



    .... definitions for items, iterator etc 

    type Effects effs = EFFFileSystem effs

    test :: forall effs. Effects effs => Test Item effs ApState ValState
    test = GenericTest {
    configuration = config {address = moduleOf ''ApState},
    components = TestComponents {
    testItems = items,
    testInteractor = interactor,
    testPrepState = prepState
    }
    }


    -- Child module 2 (the same as module 1 but different effects)



    .... definitions for items, iterator etc 

    type Effects effs = EFFLogger effs

    test :: forall effs. Effects effs => Test Item effs ApState ValState
    test = GenericTest {
    configuration = config {address = moduleOf ''ApState},
    components = TestComponents {
    testItems = items,
    testInteractor = interactor,
    testPrepState = prepState
    }
    }


    At run time I want a function f such that:



    > f ChildMod1.test 
    > ["Logger", "Ensure", "FileSystem"]
    >
    > f ChildMod2.test
    > ["Logger"]


    From repl with Child module 1 loaded I can get the following, which if I could get something similar in non-interpreted code would be enough to get me what I want:



    > :t test
    > test
    :: (Data.OpenUnion.Internal.FindElem Logger effs,
    Data.OpenUnion.Internal.FindElem Ensure effs,
    Data.OpenUnion.Internal.FindElem FileSystem effs) =>
    Test Item effs ApState ValState


    I have tried using Typeable as suggested in the following:



    How can I read the metadata of a type at runtime?



    but typeOf gives me issues which I have no clue how to solve:



    > typeOf test

    <interactive>:5:1-11: error:
    * No instance for (Typeable effs0) arising from a use of `typeOf'
    * In the expression: typeOf test
    In an equation for `it': it = typeOf test

    <interactive>:5:8-11: error:
    * Ambiguous type variable `effs0' arising from a use of `test'
    prevents the constraint `(Data.OpenUnion.Internal.FindElem
    Logger effs0)' from being solved.
    Probable fix: use a type annotation to specify what `effs0' should be.
    These potential instances exist:
    two instances involving out-of-scope types
    instance [overlappable] Data.OpenUnion.Internal.FindElem t r =>
    Data.OpenUnion.Internal.FindElem t (t' : r)
    -- Defined in `Data.OpenUnion.Internal'
    instance Data.OpenUnion.Internal.FindElem t (t : r)
    -- Defined in `Data.OpenUnion.Internal'
    * In the first argument of `typeOf', namely `test'
    In the expression: typeOf test
    In an equation for `it': it = typeOf test









    share|improve this question

























      0












      0








      0








      I am trying to create a function to generate a string representation of effects at runtime.



      -- In a shared module



      ... definitions for TestConfig, RunConfig

      data GenericTest tc rc i effs as vs = GenericTest {
      configuration :: tc,
      components :: ItemClass i vs => TestComponents rc i effs as vs
      } deriving Typeable

      type Test = GenericTest TestConfig RunConfig

      type EFFLogger effs = Member Logger effs
      type EFFFileSystem effs = Members '[Logger, Ensure, FileSystem] effs


      -- Child module 1



      .... definitions for items, iterator etc 

      type Effects effs = EFFFileSystem effs

      test :: forall effs. Effects effs => Test Item effs ApState ValState
      test = GenericTest {
      configuration = config {address = moduleOf ''ApState},
      components = TestComponents {
      testItems = items,
      testInteractor = interactor,
      testPrepState = prepState
      }
      }


      -- Child module 2 (the same as module 1 but different effects)



      .... definitions for items, iterator etc 

      type Effects effs = EFFLogger effs

      test :: forall effs. Effects effs => Test Item effs ApState ValState
      test = GenericTest {
      configuration = config {address = moduleOf ''ApState},
      components = TestComponents {
      testItems = items,
      testInteractor = interactor,
      testPrepState = prepState
      }
      }


      At run time I want a function f such that:



      > f ChildMod1.test 
      > ["Logger", "Ensure", "FileSystem"]
      >
      > f ChildMod2.test
      > ["Logger"]


      From repl with Child module 1 loaded I can get the following, which if I could get something similar in non-interpreted code would be enough to get me what I want:



      > :t test
      > test
      :: (Data.OpenUnion.Internal.FindElem Logger effs,
      Data.OpenUnion.Internal.FindElem Ensure effs,
      Data.OpenUnion.Internal.FindElem FileSystem effs) =>
      Test Item effs ApState ValState


      I have tried using Typeable as suggested in the following:



      How can I read the metadata of a type at runtime?



      but typeOf gives me issues which I have no clue how to solve:



      > typeOf test

      <interactive>:5:1-11: error:
      * No instance for (Typeable effs0) arising from a use of `typeOf'
      * In the expression: typeOf test
      In an equation for `it': it = typeOf test

      <interactive>:5:8-11: error:
      * Ambiguous type variable `effs0' arising from a use of `test'
      prevents the constraint `(Data.OpenUnion.Internal.FindElem
      Logger effs0)' from being solved.
      Probable fix: use a type annotation to specify what `effs0' should be.
      These potential instances exist:
      two instances involving out-of-scope types
      instance [overlappable] Data.OpenUnion.Internal.FindElem t r =>
      Data.OpenUnion.Internal.FindElem t (t' : r)
      -- Defined in `Data.OpenUnion.Internal'
      instance Data.OpenUnion.Internal.FindElem t (t : r)
      -- Defined in `Data.OpenUnion.Internal'
      * In the first argument of `typeOf', namely `test'
      In the expression: typeOf test
      In an equation for `it': it = typeOf test









      share|improve this question














      I am trying to create a function to generate a string representation of effects at runtime.



      -- In a shared module



      ... definitions for TestConfig, RunConfig

      data GenericTest tc rc i effs as vs = GenericTest {
      configuration :: tc,
      components :: ItemClass i vs => TestComponents rc i effs as vs
      } deriving Typeable

      type Test = GenericTest TestConfig RunConfig

      type EFFLogger effs = Member Logger effs
      type EFFFileSystem effs = Members '[Logger, Ensure, FileSystem] effs


      -- Child module 1



      .... definitions for items, iterator etc 

      type Effects effs = EFFFileSystem effs

      test :: forall effs. Effects effs => Test Item effs ApState ValState
      test = GenericTest {
      configuration = config {address = moduleOf ''ApState},
      components = TestComponents {
      testItems = items,
      testInteractor = interactor,
      testPrepState = prepState
      }
      }


      -- Child module 2 (the same as module 1 but different effects)



      .... definitions for items, iterator etc 

      type Effects effs = EFFLogger effs

      test :: forall effs. Effects effs => Test Item effs ApState ValState
      test = GenericTest {
      configuration = config {address = moduleOf ''ApState},
      components = TestComponents {
      testItems = items,
      testInteractor = interactor,
      testPrepState = prepState
      }
      }


      At run time I want a function f such that:



      > f ChildMod1.test 
      > ["Logger", "Ensure", "FileSystem"]
      >
      > f ChildMod2.test
      > ["Logger"]


      From repl with Child module 1 loaded I can get the following, which if I could get something similar in non-interpreted code would be enough to get me what I want:



      > :t test
      > test
      :: (Data.OpenUnion.Internal.FindElem Logger effs,
      Data.OpenUnion.Internal.FindElem Ensure effs,
      Data.OpenUnion.Internal.FindElem FileSystem effs) =>
      Test Item effs ApState ValState


      I have tried using Typeable as suggested in the following:



      How can I read the metadata of a type at runtime?



      but typeOf gives me issues which I have no clue how to solve:



      > typeOf test

      <interactive>:5:1-11: error:
      * No instance for (Typeable effs0) arising from a use of `typeOf'
      * In the expression: typeOf test
      In an equation for `it': it = typeOf test

      <interactive>:5:8-11: error:
      * Ambiguous type variable `effs0' arising from a use of `test'
      prevents the constraint `(Data.OpenUnion.Internal.FindElem
      Logger effs0)' from being solved.
      Probable fix: use a type annotation to specify what `effs0' should be.
      These potential instances exist:
      two instances involving out-of-scope types
      instance [overlappable] Data.OpenUnion.Internal.FindElem t r =>
      Data.OpenUnion.Internal.FindElem t (t' : r)
      -- Defined in `Data.OpenUnion.Internal'
      instance Data.OpenUnion.Internal.FindElem t (t : r)
      -- Defined in `Data.OpenUnion.Internal'
      * In the first argument of `typeOf', namely `test'
      In the expression: typeOf test
      In an equation for `it': it = typeOf test






      haskell reflection






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 0:23









      John WalkerJohn Walker

      10711




      10711
























          1 Answer
          1






          active

          oldest

          votes


















          2














          First, given a (type-level) list of effects effs, we can obtain a string of it via Typeable:



          import Type.Reflection

          showEffs :: forall effs. Typeable effs => String
          showEffs = show (typeRep @effs)


          Now the problem is for a function f to grab the constraint in the type of its argument. As you have witnessed, a naive attempt will fail: f test will specialize test and propagate the constraints upwards, resulting in errors about instance resolution and ambiguous type variables.



          A better solution is to replace => with a regular data type, which is "matchable".



          newtype WithEffects_ es0 es1 a = WithEffects { unWithEffects :: Members es0 es1 => a }


          The type synonyms also need some refactoring.



          type EFileSystem = '[Logger, Ensure, FileSystem]
          type WithEffects = WithEffects_ EFileSystem


          Now a test looks like this:



          test :: forall effs. WithEffects effs (Test Item effs ApState ValState)
          test = WithEffects $ ... -- the rest unchanged


          and you'll need to unwrap it explicitly with unWithEffects test. We can now extract a runtime representation of the effects es0 from WithEffects_ es0 es1 a:



          import Type.Reflection

          effsRepTest :: Typeable es0 => WithEffects_ es0 es1 a -> TypeRep es
          effsRepTest _ = typeRep

          showEffsTest :: Typeable es0 => WithEffects_ es0 es1 a -> String
          showEffsTest = show . effsRepTest


          So, to extract a string representing es0, we can write:



          showEffsTest test :: String




          EDITED: you may find traces of an old version of this answer in the comments, which proposed to use newtype c ==> a = Arr { unArr :: c => a }, but that doesn't work here as Members is a type family. So you need another type that carries the list of effects more explicitly es0 like WithEffects_.





          EDITED, again:



          Here is a minimal compilable gist: https://gist.github.com/Lysxia/d7b6bdc23bcb43cb40439b7e037e8145



          The above answer actually prints this:



          ': (* -> *) Logger (': (* -> *) Ensure (': (* -> *) FileSystem (' (* -> *))))


          For a better looking result, I implemented a custom printer in that gist (the ShowTypes class).






          share|improve this answer


























          • Think I missing something here. I assume the caveat doesn't apply in my case as the only constraint on test in my case is on effs. I have tried to apply the this solution in one of the child modules as follows:

            – John Walker
            Nov 13 '18 at 3:05











          • Try putting your code and error messages (if any) on gist.github.com or some other pastebin.

            – Li-yao Xia
            Nov 13 '18 at 3:08











          • OK doing it now

            – John Walker
            Nov 13 '18 at 3:09











          • gist summarising issues I am having compiling this: gist.github.com/theGhostJW/2b751dba4323fd5100fad711b9821bc0

            – John Walker
            Nov 13 '18 at 3:30











          • Oh I see, Members is a type family, so Members es0 e0 ~ Members es1 e1 does not imply es0 ~ es1, and I was relying on that implication... So I think the next simplest way is to change ==> to a different type that directly exposes the effects list. I'll make an edit.

            – Li-yao Xia
            Nov 13 '18 at 3:36











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272036%2ffreer-simple-how-can-i-generate-a-list-of-effect-members-at-runtime%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          First, given a (type-level) list of effects effs, we can obtain a string of it via Typeable:



          import Type.Reflection

          showEffs :: forall effs. Typeable effs => String
          showEffs = show (typeRep @effs)


          Now the problem is for a function f to grab the constraint in the type of its argument. As you have witnessed, a naive attempt will fail: f test will specialize test and propagate the constraints upwards, resulting in errors about instance resolution and ambiguous type variables.



          A better solution is to replace => with a regular data type, which is "matchable".



          newtype WithEffects_ es0 es1 a = WithEffects { unWithEffects :: Members es0 es1 => a }


          The type synonyms also need some refactoring.



          type EFileSystem = '[Logger, Ensure, FileSystem]
          type WithEffects = WithEffects_ EFileSystem


          Now a test looks like this:



          test :: forall effs. WithEffects effs (Test Item effs ApState ValState)
          test = WithEffects $ ... -- the rest unchanged


          and you'll need to unwrap it explicitly with unWithEffects test. We can now extract a runtime representation of the effects es0 from WithEffects_ es0 es1 a:



          import Type.Reflection

          effsRepTest :: Typeable es0 => WithEffects_ es0 es1 a -> TypeRep es
          effsRepTest _ = typeRep

          showEffsTest :: Typeable es0 => WithEffects_ es0 es1 a -> String
          showEffsTest = show . effsRepTest


          So, to extract a string representing es0, we can write:



          showEffsTest test :: String




          EDITED: you may find traces of an old version of this answer in the comments, which proposed to use newtype c ==> a = Arr { unArr :: c => a }, but that doesn't work here as Members is a type family. So you need another type that carries the list of effects more explicitly es0 like WithEffects_.





          EDITED, again:



          Here is a minimal compilable gist: https://gist.github.com/Lysxia/d7b6bdc23bcb43cb40439b7e037e8145



          The above answer actually prints this:



          ': (* -> *) Logger (': (* -> *) Ensure (': (* -> *) FileSystem (' (* -> *))))


          For a better looking result, I implemented a custom printer in that gist (the ShowTypes class).






          share|improve this answer


























          • Think I missing something here. I assume the caveat doesn't apply in my case as the only constraint on test in my case is on effs. I have tried to apply the this solution in one of the child modules as follows:

            – John Walker
            Nov 13 '18 at 3:05











          • Try putting your code and error messages (if any) on gist.github.com or some other pastebin.

            – Li-yao Xia
            Nov 13 '18 at 3:08











          • OK doing it now

            – John Walker
            Nov 13 '18 at 3:09











          • gist summarising issues I am having compiling this: gist.github.com/theGhostJW/2b751dba4323fd5100fad711b9821bc0

            – John Walker
            Nov 13 '18 at 3:30











          • Oh I see, Members is a type family, so Members es0 e0 ~ Members es1 e1 does not imply es0 ~ es1, and I was relying on that implication... So I think the next simplest way is to change ==> to a different type that directly exposes the effects list. I'll make an edit.

            – Li-yao Xia
            Nov 13 '18 at 3:36
















          2














          First, given a (type-level) list of effects effs, we can obtain a string of it via Typeable:



          import Type.Reflection

          showEffs :: forall effs. Typeable effs => String
          showEffs = show (typeRep @effs)


          Now the problem is for a function f to grab the constraint in the type of its argument. As you have witnessed, a naive attempt will fail: f test will specialize test and propagate the constraints upwards, resulting in errors about instance resolution and ambiguous type variables.



          A better solution is to replace => with a regular data type, which is "matchable".



          newtype WithEffects_ es0 es1 a = WithEffects { unWithEffects :: Members es0 es1 => a }


          The type synonyms also need some refactoring.



          type EFileSystem = '[Logger, Ensure, FileSystem]
          type WithEffects = WithEffects_ EFileSystem


          Now a test looks like this:



          test :: forall effs. WithEffects effs (Test Item effs ApState ValState)
          test = WithEffects $ ... -- the rest unchanged


          and you'll need to unwrap it explicitly with unWithEffects test. We can now extract a runtime representation of the effects es0 from WithEffects_ es0 es1 a:



          import Type.Reflection

          effsRepTest :: Typeable es0 => WithEffects_ es0 es1 a -> TypeRep es
          effsRepTest _ = typeRep

          showEffsTest :: Typeable es0 => WithEffects_ es0 es1 a -> String
          showEffsTest = show . effsRepTest


          So, to extract a string representing es0, we can write:



          showEffsTest test :: String




          EDITED: you may find traces of an old version of this answer in the comments, which proposed to use newtype c ==> a = Arr { unArr :: c => a }, but that doesn't work here as Members is a type family. So you need another type that carries the list of effects more explicitly es0 like WithEffects_.





          EDITED, again:



          Here is a minimal compilable gist: https://gist.github.com/Lysxia/d7b6bdc23bcb43cb40439b7e037e8145



          The above answer actually prints this:



          ': (* -> *) Logger (': (* -> *) Ensure (': (* -> *) FileSystem (' (* -> *))))


          For a better looking result, I implemented a custom printer in that gist (the ShowTypes class).






          share|improve this answer


























          • Think I missing something here. I assume the caveat doesn't apply in my case as the only constraint on test in my case is on effs. I have tried to apply the this solution in one of the child modules as follows:

            – John Walker
            Nov 13 '18 at 3:05











          • Try putting your code and error messages (if any) on gist.github.com or some other pastebin.

            – Li-yao Xia
            Nov 13 '18 at 3:08











          • OK doing it now

            – John Walker
            Nov 13 '18 at 3:09











          • gist summarising issues I am having compiling this: gist.github.com/theGhostJW/2b751dba4323fd5100fad711b9821bc0

            – John Walker
            Nov 13 '18 at 3:30











          • Oh I see, Members is a type family, so Members es0 e0 ~ Members es1 e1 does not imply es0 ~ es1, and I was relying on that implication... So I think the next simplest way is to change ==> to a different type that directly exposes the effects list. I'll make an edit.

            – Li-yao Xia
            Nov 13 '18 at 3:36














          2












          2








          2







          First, given a (type-level) list of effects effs, we can obtain a string of it via Typeable:



          import Type.Reflection

          showEffs :: forall effs. Typeable effs => String
          showEffs = show (typeRep @effs)


          Now the problem is for a function f to grab the constraint in the type of its argument. As you have witnessed, a naive attempt will fail: f test will specialize test and propagate the constraints upwards, resulting in errors about instance resolution and ambiguous type variables.



          A better solution is to replace => with a regular data type, which is "matchable".



          newtype WithEffects_ es0 es1 a = WithEffects { unWithEffects :: Members es0 es1 => a }


          The type synonyms also need some refactoring.



          type EFileSystem = '[Logger, Ensure, FileSystem]
          type WithEffects = WithEffects_ EFileSystem


          Now a test looks like this:



          test :: forall effs. WithEffects effs (Test Item effs ApState ValState)
          test = WithEffects $ ... -- the rest unchanged


          and you'll need to unwrap it explicitly with unWithEffects test. We can now extract a runtime representation of the effects es0 from WithEffects_ es0 es1 a:



          import Type.Reflection

          effsRepTest :: Typeable es0 => WithEffects_ es0 es1 a -> TypeRep es
          effsRepTest _ = typeRep

          showEffsTest :: Typeable es0 => WithEffects_ es0 es1 a -> String
          showEffsTest = show . effsRepTest


          So, to extract a string representing es0, we can write:



          showEffsTest test :: String




          EDITED: you may find traces of an old version of this answer in the comments, which proposed to use newtype c ==> a = Arr { unArr :: c => a }, but that doesn't work here as Members is a type family. So you need another type that carries the list of effects more explicitly es0 like WithEffects_.





          EDITED, again:



          Here is a minimal compilable gist: https://gist.github.com/Lysxia/d7b6bdc23bcb43cb40439b7e037e8145



          The above answer actually prints this:



          ': (* -> *) Logger (': (* -> *) Ensure (': (* -> *) FileSystem (' (* -> *))))


          For a better looking result, I implemented a custom printer in that gist (the ShowTypes class).






          share|improve this answer















          First, given a (type-level) list of effects effs, we can obtain a string of it via Typeable:



          import Type.Reflection

          showEffs :: forall effs. Typeable effs => String
          showEffs = show (typeRep @effs)


          Now the problem is for a function f to grab the constraint in the type of its argument. As you have witnessed, a naive attempt will fail: f test will specialize test and propagate the constraints upwards, resulting in errors about instance resolution and ambiguous type variables.



          A better solution is to replace => with a regular data type, which is "matchable".



          newtype WithEffects_ es0 es1 a = WithEffects { unWithEffects :: Members es0 es1 => a }


          The type synonyms also need some refactoring.



          type EFileSystem = '[Logger, Ensure, FileSystem]
          type WithEffects = WithEffects_ EFileSystem


          Now a test looks like this:



          test :: forall effs. WithEffects effs (Test Item effs ApState ValState)
          test = WithEffects $ ... -- the rest unchanged


          and you'll need to unwrap it explicitly with unWithEffects test. We can now extract a runtime representation of the effects es0 from WithEffects_ es0 es1 a:



          import Type.Reflection

          effsRepTest :: Typeable es0 => WithEffects_ es0 es1 a -> TypeRep es
          effsRepTest _ = typeRep

          showEffsTest :: Typeable es0 => WithEffects_ es0 es1 a -> String
          showEffsTest = show . effsRepTest


          So, to extract a string representing es0, we can write:



          showEffsTest test :: String




          EDITED: you may find traces of an old version of this answer in the comments, which proposed to use newtype c ==> a = Arr { unArr :: c => a }, but that doesn't work here as Members is a type family. So you need another type that carries the list of effects more explicitly es0 like WithEffects_.





          EDITED, again:



          Here is a minimal compilable gist: https://gist.github.com/Lysxia/d7b6bdc23bcb43cb40439b7e037e8145



          The above answer actually prints this:



          ': (* -> *) Logger (': (* -> *) Ensure (': (* -> *) FileSystem (' (* -> *))))


          For a better looking result, I implemented a custom printer in that gist (the ShowTypes class).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 0:27

























          answered Nov 13 '18 at 1:07









          Li-yao XiaLi-yao Xia

          12k1327




          12k1327













          • Think I missing something here. I assume the caveat doesn't apply in my case as the only constraint on test in my case is on effs. I have tried to apply the this solution in one of the child modules as follows:

            – John Walker
            Nov 13 '18 at 3:05











          • Try putting your code and error messages (if any) on gist.github.com or some other pastebin.

            – Li-yao Xia
            Nov 13 '18 at 3:08











          • OK doing it now

            – John Walker
            Nov 13 '18 at 3:09











          • gist summarising issues I am having compiling this: gist.github.com/theGhostJW/2b751dba4323fd5100fad711b9821bc0

            – John Walker
            Nov 13 '18 at 3:30











          • Oh I see, Members is a type family, so Members es0 e0 ~ Members es1 e1 does not imply es0 ~ es1, and I was relying on that implication... So I think the next simplest way is to change ==> to a different type that directly exposes the effects list. I'll make an edit.

            – Li-yao Xia
            Nov 13 '18 at 3:36



















          • Think I missing something here. I assume the caveat doesn't apply in my case as the only constraint on test in my case is on effs. I have tried to apply the this solution in one of the child modules as follows:

            – John Walker
            Nov 13 '18 at 3:05











          • Try putting your code and error messages (if any) on gist.github.com or some other pastebin.

            – Li-yao Xia
            Nov 13 '18 at 3:08











          • OK doing it now

            – John Walker
            Nov 13 '18 at 3:09











          • gist summarising issues I am having compiling this: gist.github.com/theGhostJW/2b751dba4323fd5100fad711b9821bc0

            – John Walker
            Nov 13 '18 at 3:30











          • Oh I see, Members is a type family, so Members es0 e0 ~ Members es1 e1 does not imply es0 ~ es1, and I was relying on that implication... So I think the next simplest way is to change ==> to a different type that directly exposes the effects list. I'll make an edit.

            – Li-yao Xia
            Nov 13 '18 at 3:36

















          Think I missing something here. I assume the caveat doesn't apply in my case as the only constraint on test in my case is on effs. I have tried to apply the this solution in one of the child modules as follows:

          – John Walker
          Nov 13 '18 at 3:05





          Think I missing something here. I assume the caveat doesn't apply in my case as the only constraint on test in my case is on effs. I have tried to apply the this solution in one of the child modules as follows:

          – John Walker
          Nov 13 '18 at 3:05













          Try putting your code and error messages (if any) on gist.github.com or some other pastebin.

          – Li-yao Xia
          Nov 13 '18 at 3:08





          Try putting your code and error messages (if any) on gist.github.com or some other pastebin.

          – Li-yao Xia
          Nov 13 '18 at 3:08













          OK doing it now

          – John Walker
          Nov 13 '18 at 3:09





          OK doing it now

          – John Walker
          Nov 13 '18 at 3:09













          gist summarising issues I am having compiling this: gist.github.com/theGhostJW/2b751dba4323fd5100fad711b9821bc0

          – John Walker
          Nov 13 '18 at 3:30





          gist summarising issues I am having compiling this: gist.github.com/theGhostJW/2b751dba4323fd5100fad711b9821bc0

          – John Walker
          Nov 13 '18 at 3:30













          Oh I see, Members is a type family, so Members es0 e0 ~ Members es1 e1 does not imply es0 ~ es1, and I was relying on that implication... So I think the next simplest way is to change ==> to a different type that directly exposes the effects list. I'll make an edit.

          – Li-yao Xia
          Nov 13 '18 at 3:36





          Oh I see, Members is a type family, so Members es0 e0 ~ Members es1 e1 does not imply es0 ~ es1, and I was relying on that implication... So I think the next simplest way is to change ==> to a different type that directly exposes the effects list. I'll make an edit.

          – Li-yao Xia
          Nov 13 '18 at 3:36


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272036%2ffreer-simple-how-can-i-generate-a-list-of-effect-members-at-runtime%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues