haskell - Type classes & instances: Pattern parameter becoming a list -- or not? -
haskell newbie here -- ran issue yesterday can't wrap head around.
minimum working exampleit might not minimal, demonstrates issue -- here goes:
class="lang-hs prettyprint-override">data vectorfeatureset feature = vfs [feature] class featureset fs getfeatures :: fs fl -> fl instance featureset vectorfeatureset getfeatures (vfs fl) = fl -- <- error occurs here (background info:) short comment on these types mean: feature geographical entity has shape , optionally attributes. featureset grouping of features belong together.
the error compiler throws on lastly line of code snippet:
couldn't match expected type `fl' actual type `[fl]' `fl' stiff type variable bound type signature getfeatures :: vectorfeatureset fl -> fl @ (mentioned line) in expression: fl in equation `getfeatures': getfeatures (vfs fl) = fl in instance declaration `featureset vectorfeatureset' if alter function definition
getfeatures (vfs [fl]) = fl the compiler accepts it.
(side note: know haskell can create selector functions me if utilize named field notation, i'm learning haskell i'm hand-writing these on purpose.)
questionnow, getfeatures function should serve extract list of features featureset. understanding of patterns if don't need access/process contents of list, don't need specify pattern parameter list notation, can take list pattern , homecoming in function.
but here, haskell wants me utilize function definition fs -> [fl] -> fl bind signature fs -> fl -> fl (at least, that's how understand it).
so question here is: why type of pattern parameter fl different on left , right hand side of equation of function definition?
i sense i'm missing fundamental piece here, cannot figure out part of basics go to straight :(
given definition of vectorfeatureset, constructor vfs has type [fl] -> vectorfeatureset fl, i.e. value "contains" list of fl.
in instance featureset, getfeatures should have type vectorfeatureset fl -> fl.
so when write getfeatures (vfs fl) = fl, returning list of features, not single feature, because variable fl has type [fl] - note types , variables live in different namespaces.
that's type error telling - function expected homecoming of type fl produced of type [fl].
when write getfeatures (vfs [fl]) = fl, pattern-matching single-element list, , function fail if seek phone call either vectorfeatureset [] or vectorfeatureset [x,y] etc.
given name of getfeatures , description of it, sounds should defined as
class featureset fs getfeatures :: fs fl -> [fl] haskell pattern-matching typeclass
No comments:
Post a Comment