Friday, 15 April 2011

Haskell still can't deduce type equality -



Haskell still can't deduce type equality -

this question follow on question haskell can't deduce type equality. have tried implementing basic polynomial approximator using trainable type described in previous question next code:

module machinelearning.polynomial (polynomial, polynomialtrf ) import machinelearning.training import machinelearning.utils polynomialtrf :: floating n => [n] -> n -> n polynomialtrf coeff var = helper 0 coeff var 0 helper _ [] var acc = acc helper 0 (c:cs) var acc = helper 1 cs var c helper deg (c:cs) var acc = helper (deg+1) cs var (acc+(c*(var^deg))) polynomialcost :: floating n => n -> n -> [n] -> n polynomialcost var target coeff = sqcost (polynomialtrf coeff var) target polynomialsv :: (floating n) => trainable n n polynomialsv = trainable polynomialtrf polynomialcost

here sqcost sqcost b = (a-b) ^ 2. next error message compiler:

src/machinelearning/polynomial.hs:18:26: not deduce (n1 ~ n) context (floating n) bound type signature polynomialsv :: floating n => trainable n n @ src/machinelearning/polynomial.hs:18:1-53 or (floating n1) bound type expected context: floating n1 => [n1] -> n -> n @ src/machinelearning/polynomial.hs:18:16-53 `n1' stiff type variable bound type expected context: floating n1 => [n1] -> n -> n @ src/machinelearning/polynomial.hs:18:16 `n' stiff type variable bound type signature polynomialsv :: floating n => trainable n n @ src/machinelearning/polynomial.hs:18:1 expected type: [n] -> n1 -> n1 actual type: [n] -> n -> n in first argument of `trainable', namely `polynomialtrf' in expression: trainable polynomialtrf polynomialcost src/machinelearning/polynomial.hs:18:40: not deduce (n ~ n1) context (floating n) bound type signature polynomialsv :: floating n => trainable n n @ src/machinelearning/polynomial.hs:18:1-53 or (floating n1) bound type expected context: floating n1 => n -> n -> [n1] -> n1 @ src/machinelearning/polynomial.hs:18:16-53 `n' stiff type variable bound type signature polynomialsv :: floating n => trainable n n @ src/machinelearning/polynomial.hs:18:1 `n1' stiff type variable bound type expected context: floating n1 => n -> n -> [n1] -> n1 @ src/machinelearning/polynomial.hs:18:16 expected type: n -> n -> [n1] -> n1 actual type: n -> n -> [n] -> n in sec argument of `trainable', namely `polynomialcost' in expression: trainable polynomialtrf polynomialcost

my question problem coming from? how can solve it? me feels clear 2 types equal, there possibility misunderstand in type system.

i'm afraid rank 2 type

data trainable b = trainable (forall n. floating n => [n] -> -> b) (forall n. floating n => -> b -> [n] -> n)

won't help you. concentrated on type error in other question, , didn't notice floating isn't rich plenty create usable. since can't generically convert floating type other type or floating type reals (realtofrac), can't write many interesting functions of given polymorphic types in general, sorry.

the problem here above type demands functions passed trainable constructor work floating types n (whatever specified a , b), implementations polynomialtrf , polynomialcost work 1 specific floating type, 1 given (both) parameters. polynomialtrf has type floating n => [n] -> n -> n, need have type (floating n, floating f) => [f] -> n -> n eligible passed trainable constructor.

with 3rd type parameter, have

trainable polynomialtrf polynomialcost :: floating n => trainable n n n

and trainsgdfull, need type

trainsgdfull :: (floating n, ord n, mode s) => trainable b (ad s n) -> [n] -> -> b -> [[n]]

to able utilize gradientdescent.

i don't know how proper solution problem might like.

haskell

No comments:

Post a Comment