2017-02-22 1 views
2

을 감안할 때 :Shapeless로 Zipwith 제품 구현?

// Given an HList of size N, provide evidence of the sum of HList 
// multiplied by _3 (length) :: _2 (length) :: _1 (length) :: HNil 
// Example: input: _1 :: _2 :: _2 -> output: _3 + _4 + _2 :: HNil 
trait HListProductZipped[L <: HList] { 
    type Out <: HList 
} 
object HListProductZipped { 

    type Aux[L <: HList, Out1 <: HList] = HListProductZipped[L] { type Out = Out1 } 

    def apply[L <: HList](implicit ev: HListProductZipped[L]): Aux[L, ev.Out] = ev 

    implicit def induct[H <: Nat, T <: HList, L <: Nat](
    implicit ev: Length.Aux[H :: T, L], 
      prod: Prod[H, L], 
      rest: HListProductZipped[T] 
): HListProductZipped.Aux[H :: T, prod.Out :: rest.Out] = new HListProductZipped[H :: T] { 
    type Out = prod.Out :: rest.Out 
    } 

    implicit val hnilHListProductZipped: HListProductZipped[HNil] = new 
    HListProductZipped[HNil] { 
     type Out = HNil 
    } 

} 

하지만, 작동하지 않는 나는 예상대로 :

import shapeless._ 
import nat._ 
import net.HListProductZipped 

scala> val a = HListProductZipped[_1 :: _2 :: HNil] 
a: net.HListProductZipped[shapeless.::[shapeless.Succ[shapeless._0],shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.HNil]]]{type Out = shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],net.HListProductZipped.hnilHListProductZipped.Out]]} = [email protected] 

scala> val e: a.Out = _2 :: _2 :: HNil 
<console>:19: error: type mismatch; 
found : shapeless.::[shapeless.nat._2,shapeless.::[shapeless.nat._2,shapeless.HNil]] 
    (which expands to) shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.HNil]] 
required: a.Out 
    (which expands to) shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],shapeless.::[shapeless.Succ[shapeless.Succ[shapeless._0]],net.HListProductZipped.hnilHListProductZipped.Out]] 
     val e: a.Out = _2 :: _2 :: HNil 
         ^

날 내가 잘못 뭘하는지 알려 주시기 바랍니다.

+0

실제로, 나는 틀린 것을 알아 냈다. 그러나 아마도이 질문은 가치가있는 것처럼 보이므로 아무도하지 않으면 나는 하루 후 대답 할 것입니다. –

답변

3

반환 유형이 hnilHListProductZipped 인 누락 된 부분이 있습니다. 즉, 컴파일러는 OutHNil임을 알 수 없습니다. Aux[HNil, HNil]으로 변경하면이 작업이 잘됩니다.

+1

오, 그냥 당신의 의견을 보았습니다. :) –

관련 문제