Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing constraint kinds and type families with 'limited' constraints
    primarykey
    data
    text
    <p>I'm working on an applicative functor that contains a monoid to "view" the execution. However, sometimes I don't care about this part at all, so the choice of monoid is irrelevant as it will never be consumed. I've simplified what I have into:</p> <pre><code>{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeFamilies #-} import GHC.Exts class Render a b where render :: a -&gt; b instance Render a () where render = const () class Merge a where type Renderer a b :: Constraint merge :: Renderer a b =&gt; a -&gt; b data Foo = Foo Bool instance Merge Foo where type (Renderer Foo) m = (Render Bool m) merge (Foo b) = render b </code></pre> <p><code>Render</code> is used to transform various <code>a</code>s into a single <code>b</code>. <code>Merge</code> is a big simplification of my actual functor, but the point is it contains a type family/constraint and my intention of that is to specify exactly what <code>Render</code>ers a <code>Merge</code> requires.</p> <p>Now, I might want to "run" the <code>Merge</code>, but discard the view, which is akin to something like:</p> <pre><code>runFoo :: Merge a =&gt; a -&gt; Int runFoo x = case merge x of () -&gt; 5 </code></pre> <p>But this will fail because:</p> <blockquote> <p>Could not deduce <code>(Renderer a ())</code> arising from a use of <code>merge</code></p> </blockquote> <p>I chose <code>()</code> as my monoid because forall <code>a</code>, we have an instance of <code>Render a ()</code>. So if there was a way to say that <code>Merge a</code> just means a collection <code>Render</code> constraints then this would work fine. Of course, <code>Merge a</code> is more general than that - it could add arbitrary constraints, which explains the compilation error.</p> <p>Is there anyway to achieve what I want <em>without</em> changing the signature of <code>runFoo</code>?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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