last day (15 days later) » 

4:44 PM
0
Q: Correct way to achieve pattern matching on type class(es) of arguments in Haskell function

EMSThe function (^) has type (^) :: (Integral b, Num a) => a -> b -> a and the function (**) has type (**) :: Floating a => a -> a -> a. Suppose I want to create a function my_pow that accepts a Num compliant first argument and a Num compliant second argument. If both arguments are Floating compli...

 
This isn't really possible. For one thing, what if I make a type (incorrectly, but that's not the point) that implements both Floating and Integral? How would the compiler know which to use? This is particularly the case if I implemented (^) and (**) to have different behavior. While this makes no sense from a mathematical point of view, in general this can be something that happens within an application with typeclasses.
 
EMS
The answer seems obvious to me: it will detect the first matching pattern and execute that one, and it is the programmer's responsibility to know how that will work with the type classes involved. If the programmer relies on two incompatible type classes that can be mixed, then an error would be the correct output, and the patterns would be changed to reflect the precedence of pattern matching that is desired. I have an example where the two patterns are not mutually exclusive and the first one is selected, but it's too big for a comment.
I'm also surprised there isn't a way to put an ordering on the type classes themselves, so that in the case of number types, for example, there would be a hierarchy from Int to SignedInt to Rational to Real (Floating, possibly ordered on precision) to Complex, and that Num meant you are at least compliant with one of them, and that being compliant with one means you are compliant with any predecessors as well. But I can see how that abstraction might not be desirable in the context of programming where you want types to embody non-math things as well.
 
Being a Num does not make you a Floating. Being a Floating makes you a Num. There is a hierarchy, it just goes from most general to most specific, and only with type classes. There is no direct type inheritance in Haskell, so you can't say that an Int is also a Float, even though there is a lossless conversion from Int to Float. Instead, you can convert an Int to a Num a => a, and since Float is an instance of that it can be substituted in as being more specific.
 
EMS
I don't think anything I wrote contradicted your first sentence. I only need Num because it means "you are compliant to possibly one or more of some other bunch of type classes, or just Num", which is all I need. Then, I need to check which of those others it is compliant to, and do different handling, in an order I choose, based on that.
 
Also, what you're really wanting is multiple dispatch on type classes, Haskell does not have that, sorry. And I can definitely think of more complicated cases where having these constructs would lead to ambiguous programs. What if my function to overload performs IO, and one implementation launches missiles and the other implementation releases a swarm of adorable kittens? It would be a dangerous program if you have to hope that the patterns are matched in just the right way so that you don't accidentally start WW3 instead of releasing a wave of cute.
 
EMS
4:45 PM
You are right that I am trying to see how to do multiple dispatch on type classes. It's not clear to me how this could lead to ambiguous programs under the rule that the first matched pattern is the one executed.
Let me share a small snippet that shows what I mean by matching the first pattern
data Point = Point Float Float deriving (Show)
data Shape = Circle Point Float | Rectangle Point Point
    deriving (Show)

area (Circle _ r) = pi * r^2
area (Rectangle (Point x1 y1) (Point x2 y2)) =
    (abs $ x1 - x2) * (abs $ y1 - y2)

some_func :: [Float] -> Shape -> [Float]
some_func _ (Circle p r) = [r]
some_func (x:xs) _ = [x]
 
What you've actually contradicted is that in your implementation, you have Num a and Num b, but in one case you want a ~ b and Floating a => a, and in the other you have two different types. What if I had the expression my_pow 3.0 4? This would be a floating and either an integral or a floating. Without explicit type signatures the compiler can't know if this means my_pow (3.0 :: Double) (4 :: Double) or my_pow (3.0 :: Double) (4 :: Int).
if you have to put in explicit types, then it's no longer really multiple dispatch and you don't gain anything
 
EMS
my_pow of 3.0 4 should give a pattern match error, since it doesn't match the pattern
 
it matches the pattern of having two Nums
 
EMS
but that's not in the definition anywhere
only Floating, Floating, or Num, Integral
 
my_pow :: (Num a, Num b) => a -> b -> a
 
EMS
4:48 PM
i realize that, i still don't understand
 
If you turn off the monomorphism restriction, in GHCi you'd get:
`> :t 1`
`1 :: Num a => a`
 
EMS
if i make a function with some type signature, and then enumerate some patterns, the patterns might not be exhaustive
 
> :t 1.0
1.0 :: Fractional a => a
 
EMS
so the args might match the type signature, but not find any pattern to match them
 
which makes it ambiguous. You've said that this function should accept values of all numeric types, not just some numeric types
 
EMS
4:50 PM
yes, and an error is the unambiguously right thing if the pattern isn't found
i don't see how that's different than regular pattern matching where we remove my goal to do it on the type class
 
if you were using the forall syntax, the signature would actually be my_pow :: forall a, b . (Num a, Num b) => a -> b -> a
When you use a typeclass in Haskell, that function has to work for all instances of that typeclass
not just some
 
EMS
we need to slow down a minute
because my confusion is that i don't understand how that fails to be true when pattern matching on typeclasses that would be compatible with the signature's restrictions on the type classes
any more so than it would work on regular arguments
for example
i could say

some_func :: [a] -> b -> [a]
some_func [] b = [b]
this does not define what to do for a non-empty first argument, and maybe i want that to raise an error
but the pattern will still match if called with the empty list
 
So, what you want to do is write a function that if you have two floating arguments, use one function, and instead if you have an Num argument and an Integral argument use a different function. But you haven't specified what happens if you call the function with two Num arguments.
 
EMS
yes, and i want it to hit the pattern match error that will result
and in the last sentence, we should be careful
because "does not define what to do if given two Num arguments" means "aside from the patterns that could have been matched"
 
The difference is that the compiler has to know about all types at compile type, but obviously can't know about all values at compile time. The pattern match failure for values occurs at run time, so the pattern match failure for types should occur at compile time.
However, you would potentially want to package this code up as a library, upload it to hackage, and let others use it. Other people could then define their own instances that don't match those patterns that you couldn't have possibly predicted. These new types then cause a compiler error due to code that was already compiled? Because of these ambiguities, the type system does not allow these kinds of constructs
 
EMS
4:58 PM
i still don't understand how that's different than my some_func above with the empty list
i could package that and give it to someone else who calls it with a non empty list
it won't match my function's patterns and will then raise a pattern match error
 
True, but their error occurs at runtime, not at compile time
 
EMS
right, but i don't see why the same error wouldn't occur at run time for the types involved
 
you can't have a function definition that compiles in some cases and not others depending on what instances are in scope at the call site
 
EMS
i don't understand why it wouldn't compile
if the some_func one can compile even though it then allowed to be called with unmatched args
 
I'll have to continue this discussion another time, it's my lunch break
sorry
 
EMS
5:00 PM
no worries. thanks for trying. i will keep reading.
 

last day (15 days later) »