Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't really need a custom parser. You can just reflect on the TypeRep value you receive. For instance the following would work:</p> <pre><code>module ModuleReflect where import Data.Typeable import Control.Arrow foo :: (Int, Bool, String) -&gt; String -&gt; String foo = undefined -- | We first want to in the case that no result is returned from splitTyConApp -- to just return the input type, this. If we find an arrow constructor (-&gt;) -- we want to get the start of the list and then recurse on the tail if any. -- if we get anything else, like [] Char then we want to return the original type -- [Char] values :: Typeable a =&gt; a -&gt; [TypeRep] values x = split (typeOf x) where split t = case first tyConString (splitTyConApp t) of (_ , []) -&gt; [t] ("-&gt;", [x]) -&gt; [x] ("-&gt;", x) -&gt; let current = init x next = last x in current ++ split next (_ , _) -&gt; [t] inputs :: Typeable a =&gt; a -&gt; [TypeRep] inputs x = case values x of (x:xs) | not (null xs) -&gt; x : init xs _ -&gt; [] output :: Typeable a =&gt; a -&gt; TypeRep output x = last $ values x </code></pre> <p>and a sample session</p> <pre><code>Ok, modules loaded: ModuleReflect. *ModuleReflect Data.Function&gt; values foo [(Int,Bool,[Char]),[Char],[Char]] *ModuleReflect Data.Function&gt; output foo [Char] *ModuleReflect Data.Function&gt; inputs foo [(Int,Bool,[Char]),[Char]] </code></pre> <p>This is just some quick barely tested code, I'm sure it can be cleaned up. And the reason I don't use typeRepArgs is that in the case of other constructors they would be broken up to, e.g [] Char returns Char instead of [Char].</p> <p>This version does not treat elements of tuples as seperate results, but it can be easily changed to add that.</p> <p>However as mentioned before this has a limitation, It'll only work on monomorphic types. If you want to be able to find this for any type, then you should probably use the GHC api. You can load the module, ask it to Typecheck and then inspect the Typecheck AST for the function and thus find it's type. Or you can use Hint to ask for the type of a function. and parse that. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload