2013-02-06 3 views
0

이 질문은 후속 질문입니다 (Haskell can't deduce type equality). 나는 다음과 같은 코드로 내 앞의 질문에 설명 된 학습 가능한 유형을 사용하여 기본 다항식 approximator 구현하는 시도 :하스켈은 여전히 ​​유형 평등을 추론 할 수 없습니다.

여기
module MachineLearning.Polynomial 
     (polynomial, 
     polynomialTrf 
     ) where 
import MachineLearning.Training 
import MachineLearning.Utils 

polynomialTrf :: Floating n => [n] -> n -> n 
polynomialTrf coeff var = helper 0 coeff var 0 where 
    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 

sqcost 그냥 sqcost a b = (a-b)^2입니다. 컴파일러에서 다음과 같은 오류 메시지가 나타납니다.

src/MachineLearning/Polynomial.hs:18:26: 
    Could not deduce (n1 ~ n) 
    from the context (Floating n) 
     bound by the type signature for 
       polynomialSV :: Floating n => Trainable n n 
     at src/MachineLearning/Polynomial.hs:18:1-53 
    or from (Floating n1) 
     bound by a type expected by the context: 
       Floating n1 => [n1] -> n -> n 
     at src/MachineLearning/Polynomial.hs:18:16-53 
     `n1' is a rigid type variable bound by 
      a type expected by the context: Floating n1 => [n1] -> n -> n 
      at src/MachineLearning/Polynomial.hs:18:16 
     `n' is a rigid type variable bound by 
      the type signature for polynomialSV :: Floating n => Trainable n n 
      at src/MachineLearning/Polynomial.hs:18:1 
    Expected type: [n] -> n1 -> n1 
     Actual type: [n] -> n -> n 
    In the first argument of `Trainable', namely `polynomialTrf' 
    In the expression: Trainable polynomialTrf polynomialCost 

src/MachineLearning/Polynomial.hs:18:40: 
    Could not deduce (n ~ n1) 
    from the context (Floating n) 
     bound by the type signature for 
       polynomialSV :: Floating n => Trainable n n 
     at src/MachineLearning/Polynomial.hs:18:1-53 
    or from (Floating n1) 
     bound by a type expected by the context: 
       Floating n1 => n -> n -> [n1] -> n1 
     at src/MachineLearning/Polynomial.hs:18:16-53 
     `n' is a rigid type variable bound by 
      the type signature for polynomialSV :: Floating n => Trainable n n 
      at src/MachineLearning/Polynomial.hs:18:1 
     `n1' is a rigid type variable bound by 
      a type expected by the context: Floating n1 => n -> n -> [n1] -> n1 
      at src/MachineLearning/Polynomial.hs:18:16 
    Expected type: n -> n -> [n1] -> n1 
     Actual type: n -> n -> [n] -> n 
    In the second argument of `Trainable', namely `polynomialCost' 
    In the expression: Trainable polynomialTrf polynomialCost 

내 질문은 어디에서 발생합니까? 어떻게 해결할 수 있습니까? 저에게는 두 가지 유형이 동등하다는 것이 명확하게 느껴 지므로 형식 체계에서 어떤 것을 오해 할 가능성이 있습니다.

+0

여기에있는 코드는 원래 있던 유형의 실존 유형을 원합니다. –

+1

'Trainable'에서'n'을 숨길 필요가 있습니까? 그냥 'Trainable n a b'로 만들 수 없습니까? –

+0

'훈련 모듈 (trainable nab) '을 쓸데없이 재 작성했지만 컴파일되지는 않는다.'trainSgdFull'에서'n'과'AD'를 비교할 수 없다. – laci37

답변

2

나는

data Trainable a b 
    = Trainable (forall n. Floating n => [n] -> a -> b) 
       (forall n. Floating n => a -> b -> [n] -> n) 

은 도움이되지 않습니다 순위 2 종류를 두려워. 나는 다른 질문의 유형 오류에만 집중해서 Floating이 실제로 사용할 수있을만큼 풍부하지 않다는 사실을 알지 못했습니다. 일반적으로 Floating 유형을 다른 유형으로 변환하거나 (realToFrac) 이외의 형식을 사용하여 Floating 유형으로 변환 할 수 없기 때문에 일반적으로 주어진 다형성 유형의 많은 흥미로운 기능을 작성할 수 없습니다. 여기

문제는 위의 유형합니다 (ab 지정된 무엇이든) 모든 Floating 유형 nTrainable 생성자 작업에 전달하는 기능 만 구현 polynomialTrfpolynomialCost는 하나 개의 특정 Floating 유형의 일 것을 요구한다는 것입니다 매개 변수 (둘 다)로 주어진 값. polynomialTrfFloating n => [n] -> n -> n 유형이지만 Trainable 생성자에 전달하려면 (Floating n, Floating f) => [f] -> n -> n 유형이 있어야합니다.

세 번째 유형 매개 변수와

, 당신은

Trainable polynomialTrf polynomialCost :: Floating n => Trainable n n n 

trainSgdFull에 대한

, 당신은

trainSgdFull :: (Floating n, Ord n, Mode s) => Trainable a b (AD s n) -> [n] -> a -> b -> [[n]] 

gradientDescent을 사용할 수 있도록 유형을해야 할 것이다.

문제점에 대한 적절한 해결책이 어떤 모양인지 모르겠습니다.

+0

'polynomialSV :: Floating n => Trainable n n n '의 문제는 훈련 입력과 목표를'AD'로 제공해야한다는 것입니다. – laci37

+0

나는 당신이 뭘하려고하는지 전혀 모르니까, 나는 그 문제가 실제로 무엇인지 이해하지 못한다. 미안하다. 나는 너가 너가 원하는 무슨을 더 설명하면 이해할지도 모르지 않는다. –

관련 문제