in Education by
Still new to Haskell, I have hit a wall with the following: I am trying to define some type classes to generalize a bunch of functions that use gaussian elimination to solve linear systems of equations. Given a linear system M x = k the type a of the elements m(i,j) \elem M can be different from the type b of x and k. To be able to solve the system, a should be an instance of Num and b should have multiplication/addition operators with b, like in the following: class MixedRing b where (.+.) :: b -> b -> b (.*.) :: (Num a) => b -> a -> b (./.) :: (Num a) => b -> a -> b Now, even in the most trivial implementation of these operators, I'll get Could not deduce a ~ Int. a is a rigid type variable errors (Let's forget about ./. which requires Fractional) data Wrap = W { get :: Int } instance MixedRing Wrap where (.+.) w1 w2 = W $ (get w1) + (get w2) (.*.) w s = W $ ((get w) * s) I have read several tutorials on type classes but I can find no pointer to what actually goes wrong. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Let us have a look at the type of the implementation that you would have to provide for (.*.) to make Wrap an instance of MixedRing. Substituting Wrap for b in the type of the method yields (.*.) :: Num a => Wrap -> a -> Wrap As Wrap is isomorphic to Int and to not have to think about wrapping and unwrapping with Wrap and get, let us reduce our goal to finding an implementation of (.*.) :: Num a => Int -> a -> Int (You see that this doesn't make the challenge any easier or harder, don't you?) Now, observe that such an implementation will need to be able to operate on all types a that happen to be in the type class Num. (This is what a type variable in such a type denotes: universal quantification.) Note: this is not the same (actually, it's the opposite) of saying that your implementation can itself choose what a to operate on); yet that is what you seem to suggest in your question: that your implementation should be allowed to pick Int as a choice for a. Now, as you want to implement this particular (.*.) in terms of the (*) for values of type Int, we need something of the form n .*. s = n * f s with f :: Num a => a -> Int I cannot think of a function that converts from an arbitary Num-type a to Int in a meaningful way. I'd therefore say that there is no meaningful way to make Int (and, hence, Wrap) an instance of MixedRing; that is, not such that the instance behaves as you would probably expect it to do.

Related questions

0 votes
    I am trying to define instances for Functor,Applicative and Monad for the following type: data BTree a=Leaf ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    What would be a good place to go to understand arrows? Ideally, I am just looking for some place ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm learning Haskell in the hope that it will help me get closer to functional programming. Previously, I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
0 votes
    I want to create a function which will check if it is a correct hour to make an action, but I ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    The __________ requires that each transaction Ti executes in two or three different phases in its lifetime ... topic in section Concurrency Control of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    State true or false: SAP systems are divided into different clients depending on business requirement, the data can be accessed within the client only. A. True B. False...
asked Feb 20, 2023 in Technology by JackTerrance
0 votes
    I've been struggling with some issues relating to referencing child controls within a FormView. Another developer ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I have the dict as below, if same value is found more than once then my dict key must be created with incremental ... : ['COMPILE'], } Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    The .. becomes different shapes depending on the task you are performing. (a) Active tab (b) Insertion point (c) ... (e) None of above Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    What does the span of time waiting for the page to be useful depending on? (a) Runtime performance ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 22, 2021 in Education by JackTerrance
0 votes
    Annotation type definition looks similar to which of the following? (a) Method (b) Class (c) Interface (d) ... & Annotations of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
0 votes
    Different kinds of the object involved in a class definition are ________ (a) Public object, Private object, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    What will do you with a missing value in an observation? Options 1 from the point of definition onward in the ... the function. Tin Select the correct answer from above options...
asked Nov 28, 2021 in Education by JackTerrance
0 votes
    let's say i have an interface as below public interface ConditionChecker { boolean isInCondition(Person p); ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
...