<div dir="ltr">Dear All,<div><br></div><div>Thanks so much for your help. It was largely a syntactic issue around pattern matching - but I also learnt about the @ operator for patter-matching.</div><div><br></div><div>The correct code is posted below in case it helps anyone else. The only other think would be to rewrite eviCheck as an inline function using where, but I am happy with this for now.</div><div><br></div><div>Thanks for your speedy help.</div><div><br></div><div>BW,</div><div>Matt</div><div><br></div><div><div>checkConflict3 :: Arg -> Arg -> Bool</div><div>checkConflict3 (IndArg _ at1 at2 _ adir) (IndArg _ bt1 bt2 _ bdir) =</div><div> if at1 == bt1 && at2 == bt2 && adir /= bdir then True</div><div> else if at1 == bt2 && at2 == bt1 && revDir (adir) /= bdir then True</div><div> else False</div><div>checkConflict3 (IndArg e _ _ _ _) (MetaArg a) = eviCheck e a</div><div>checkConflict3 a@(MetaArg a) b@(IndArg e _ _ _ _) = eviCheck b a</div><div>checkConflict3 (MetaArg _) (MetaArg _) = False</div><div>checkConflict3 _ _ = False --A final catchall</div><div><br></div><div>eviCheck:: Evi -> EviType -> Bool</div><div>eviCheck (SimpEvi _ _ _ _ type1) type2 = if type1 == type2 then True</div><div> else False</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 22 September 2017 at 21:16, David McBride <span dir="ltr"><<a href="mailto:toad3k@gmail.com" target="_blank">toad3k@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">It is a mistake to use record syntax with data types with multiple<br>
constructors. It creates a partial function that dies when you try to<br>
use it on a constructor that does not have that field. It is a flaw<br>
in the language that many people would like to see removed because it<br>
allows programs to compile that will fail at run time, possibly<br>
unexpectedly.<br>
<br>
Arg can have a constructor that does not have an evi. Also what<br>
happens if you compare an IndArg to a MetaArg? Or a MetaArg to<br>
another MetaArg?<br>
<br>
You should remove all of these record names, and then write<br>
checkConflict as follows (warning untested):<br>
<span class=""><br>
checkConflict :: Arg -> Arg -> Bool<br>
</span>checkConflict (IndArg _ at1 at2 _ adir) (IndArg _ bt1 bt2 _ bdir) =<br>
at1 == bt1 && at2 == bt2 && adir /= bdir<br>
-- checkConflict (IndArg ...) (MetaArg) = ???<br>
-- checkConflict a@(MetaArg ...) b@(IndArg ...) = checkConflict b a<br>
-- checkConflict (MetaArg _) (MetaArg _) = ???<br>
checkConflict _ _ -> False -- perhaps a catchall?<br>
<br>
However, you may keep the records as long as you are absolutely sure<br>
you are using them only in places where you have the appropriate<br>
constructor.<br>
<div class="HOEnZb"><div class="h5"><br>
On Fri, Sep 22, 2017 at 3:53 PM, Matt Williams<br>
<<a href="mailto:matt.williams45.mw@gmail.com">matt.williams45.mw@gmail.com</a>> wrote:<br>
> Dear All,<br>
><br>
> I am having problems writing a polymorphic function.<br>
><br>
> I have a type that has two constructors:<br>
><br>
> data Arg = IndArg {evi::Evi, t1::Treatment, t2::Treatment, out::Outcome,<br>
> dir::Direction}<br>
> | MetaArg {target::EviType}<br>
> deriving (Show)<br>
><br>
> I have a function checks for conflict:<br>
><br>
> checkConflict :: Arg -> Arg -> Bool<br>
> checkConflict a b = if t1 a == t1 b && t2 a == t2 b && dir a /= dir b then<br>
> True<br>
> else False<br>
><br>
> However, I can't make this work with both types of Argument - if I pass it<br>
> MetaArg, it raises an error.<br>
><br>
> In another language, I would write something like:<br>
><br>
> checkConflict(ArgA,ArgB):<br>
><br>
><br>
><br>
</div></div><div class="HOEnZb"><div class="h5">> ______________________________<wbr>_________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
><br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br></div>