Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can not have anything like </p> <pre><code>data Sub = SubA | SubB data Super = Sub | SuperB </code></pre> <p>Suppose the above syntax is allowed, then the problem is given the value constructor <code>SubA</code> you can not tell whether its type is <code>Sub</code> or <code>Super</code>. That's why you need to wrap your type in a constructor.</p> <p>For your second example, the way you are doing should be the default way of doing but you can do a hack to make it easier although I don't recommend doing this as using show is much slower. You can try some other hack similar to this. </p> <pre><code>import Data.List data Super = SubA | SubB | SuperB deriving Show isSub :: Super -&gt; Bool isSub m = isPrefixOf "Sub" (show m) </code></pre> <p>If you really want to have something like above it is better to define function like you did. Using TH might save you sometime.</p> <p>Your third case is what I would really recommend doing. You need to have a wrapper constructor like <code>SuperA</code> because of the reasons I told above. That's why you can't have exactly type1 is of type2 plus x,y,z. The closest thing you can have is to wrap elements in a constructor or using a typeclass. </p> <pre><code>data Sub = SubA | SubB data Sup = SuperA | SuperB class Super a where isSub :: a -&gt; Bool isSub _ = True instance Super Sup where isSub _ = False instance Super Sub data SupSup = SuperSuperA | SuperSuperB class SuperSuper a where isSuper :: a -&gt; Bool isSuper _ = True instance SuperSuper SupSup where isSuper _ = False instance SuperSuper Sup instance SuperSuper Sub </code></pre> <p>You can think here <code>Super</code> (which is a typeclass and not a type) contains <code>Sub</code> and someting extra (<code>Sup</code>).</p>
 

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