2017-02-22 2 views
2

Gegeben:Zipwith Produkteinführung mit Shapeless?

// 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 
    } 

} 

aber es funktioniert ist nicht wie ich erwartet hatte:

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 
         ^

Bitte lassen Sie mich wissen, was ich falsch mache.

+0

Eigentlich habe ich gerade herausgefunden, was los ist. Aber vielleicht scheint sich diese Frage zu lohnen, also werde ich sie nach einem Tag beantworten, wenn niemand es tut. –

Antwort

3

Sie fehlen die Verfeinerung im Rückgabetyp hnilHListProductZipped, was bedeutet, dass der Compiler nicht wissen kann, dass sein OutHNil ist. Ändern Sie es zu Aux[HNil, HNil] wird dies gut funktionieren.

+1

Oh, habe gerade deinen Kommentar gesehen. :) –

Verwandte Themen