[Haskell-beginners] Writing a polymorphic function in Haskell

David McBride toad3k at gmail.com
Fri Sep 22 20:16:55 UTC 2017


It is a mistake to use record syntax with data types with multiple
constructors.  It creates a partial function that dies when you try to
use it on a constructor that does not have that field.  It is a flaw
in the language that many people would like to see removed because it
allows programs to compile that will fail at run time, possibly
unexpectedly.

Arg can have a constructor that does not have an evi.  Also what
happens if you compare an IndArg to a MetaArg?  Or a MetaArg to
another MetaArg?

You should remove all of these record names, and then write
checkConflict as follows (warning untested):

checkConflict :: Arg -> Arg -> Bool
checkConflict (IndArg _ at1 at2 _ adir) (IndArg _ bt1 bt2 _ bdir) =
at1 == bt1 && at2 == bt2 && adir /= bdir
-- checkConflict (IndArg ...) (MetaArg) = ???
-- checkConflict a@(MetaArg ...) b@(IndArg ...) = checkConflict b a
-- checkConflict (MetaArg _) (MetaArg _) = ???
checkConflict _ _ -> False -- perhaps a catchall?

However, you may keep the records as long as you are absolutely sure
you are using them only in places where you have the appropriate
constructor.

On Fri, Sep 22, 2017 at 3:53 PM, Matt Williams
<matt.williams45.mw at gmail.com> wrote:
> Dear All,
>
> I am having problems writing a polymorphic function.
>
> I have a type that has two constructors:
>
> data Arg = IndArg {evi::Evi, t1::Treatment, t2::Treatment, out::Outcome,
> dir::Direction}
>             | MetaArg {target::EviType}
>             deriving (Show)
>
> I have a function checks for conflict:
>
> checkConflict :: Arg -> Arg -> Bool
> checkConflict a b = if t1 a == t1 b && t2 a == t2 b && dir a /= dir b then
> True
>                      else False
>
> However, I can't make this work with both types of Argument - if I pass it
> MetaArg, it raises an error.
>
> In another language, I would write something like:
>
> checkConflict(ArgA,ArgB):
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>


More information about the Beginners mailing list