Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The trick is to make a type class for which you will define an instance for functions, and an instance for the return type. The fact that it's a <code>Bool</code> is not a problem at all.</p> <p>We're trying to write a function which takes a variadic argument and returns a <code>Bool</code>, so we'll define a type class with such a function.</p> <pre><code>class Stmt a where tautology :: a -&gt; Bool </code></pre> <p>Next, we define an instance for the return type of the variadic function. In this case, that's <code>Bool</code>.</p> <pre><code>-- A Bool is a tautology if it's True. instance Stmt Bool where tautology = id </code></pre> <p>The key part is the next instance for functions that take a <code>Bool</code> argument, and whose return type is some type from our class. That way, this instance will be applied multiple times if a function takes multiple arguments.</p> <pre><code>-- A function is a tautology if it always returns a tautology. instance Stmt b =&gt; Stmt (Bool -&gt; b) where tautology f = tautology (f True) &amp;&amp; tautology (f False) </code></pre> <p>Writing it this way requires <code>FlexibleInstances</code> because of the <code>Bool</code> in the second instance head. To do the same with pure Haskell 98, we'll need to use a suitably-constrained type variable instead. We can for example use <code>Bounded</code> and <code>Enum</code> (there are instances for both for <code>Bool</code>), or you can make your own class that will let you construct the appropriate inputs.</p> <pre><code>instance (Enum a, Bounded a, Stmt b) =&gt; Stmt (a -&gt; b) where tautology f = all (tautology . f) [minBound .. maxBound] </code></pre> <p>And we're done. Let's try it out:</p> <pre><code>&gt; tautology $ \x y -&gt; (not x &amp;&amp; not y) == not (x &amp;&amp; y) False &gt; tautology $ \x y -&gt; (not x &amp;&amp; not y) == not (x || y) True </code></pre>
 

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