title: Checksum at the Type Level using Shapeless
body: <p>I came up with the following per this <a href="http://codingdojo.org/kata/BankOCR/" rel="nofollow noreferrer">problem</a>:</p>
  
  <p><a href="https://i.stack.imgur.com/L5ukB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/L5ukB.png" alt="enter image description here"></a></p>
  
  <pre><code>package net
  
  import shapeless.{::, HNil, _}
  import ops.hlist.IsHCons
  import nat._
  import ops.nat.{Mod, Prod, Sum}
  
  trait IsValid[A]
  object IsValid {
  
    def apply[L &lt;: HList](implicit ev: IsValid[L]) = ev
  
    //  account number:  3  4  5  8  8  2  8  6  5
    //  position names:  d9 d8 d7 d6 d5 d4 d3 d2 d1
    //
    //  checksum calculation:
    //    (d1+2*d2+3*d3 +..+9*d9) mod 11 = 0
    implicit def validNumber[D9 &lt;: Nat, D8 &lt;: Nat, D7 &lt;: Nat, D6 &lt;: Nat, D5 &lt;: Nat, D4 &lt;: Nat, D3 &lt;: Nat, D2 &lt;: Nat, D1 &lt;: Nat,
                             P9 &lt;: Nat, P8 &lt;: Nat, P7 &lt;: Nat, P6 &lt;: Nat, P5 &lt;: Nat, P4 &lt;: Nat, P3 &lt;: Nat, P2 &lt;: Nat, P1 &lt;: Nat,
                             S1 &lt;: Nat, S2 &lt;: Nat, S3 &lt;: Nat, S4 &lt;: Nat, S5 &lt;: Nat, S6 &lt;: Nat, S7 &lt;: Nat, S8 &lt;: Nat](
      implicit p9: Prod.Aux[D9, _9, P9],
      p8 : Prod.Aux[D8, _8, P8],
      p7 : Prod.Aux[D7, _7, P7],
      p6 : Prod.Aux[D6, _6, P6],
      p5 : Prod.Aux[D5, _5, P5],
      p4 : Prod.Aux[D4, _4, P4],
      p3 : Prod.Aux[D3, _3, P3],
      p2 : Prod.Aux[D2, _2, P2],
      p1 : Prod.Aux[D1, _1, P1],
      s1: Sum.Aux[P9, P8, S1],
      s2: Sum.Aux[P7, S1, S2],
      s3: Sum.Aux[P6, S2, S3],
      s4: Sum.Aux[P5, S3, S4],
      s5: Sum.Aux[P4, S4, S5],
      s6: Sum.Aux[P3, S5, S6],
      s7: Sum.Aux[P2, S6, S7],
      s8: Sum.Aux[P1, S7, S8],
      mod: Mod.Aux[S8, _11, _0]
    ): IsValid[D9 :: D8 :: D7 :: D6 :: D5 :: D4 :: D3 :: D2 :: D1 :: HNil] = {
      new IsValid[D9 :: D8 :: D7 :: D6 :: D5 :: D4 :: D3 :: D2 :: D1 :: HNil] {}
    }
  
  
  }
  </code></pre>
  
  <p>It appears to work for the <code>0</code> case:</p>
  
  <pre><code>scala&gt; import net.IsValid, shapeless._, nat._
  import net.IsValid
  import shapeless._
  import nat._
  
  scala&gt; IsValid[_0 :: _0 :: _0 :: _0 :: _0 :: _0 :: _0 :: _0 :: _0 :: HNil]
  res0: net.IsValid[shapeless.::...
  </code></pre>
  
  <p>But, when I call the following "valid" combination, the console shows no output, i.e. hangs. </p>
  
  <pre><code>scala&gt; IsValid[_3 :: _4 :: _5 :: _8 :: _8 :: _2 :: _8 :: _6 :: _5 :: HNil]
  </code></pre>
  
  <p>I only waited ~2 minutes before asking this question. </p>
  
  <p>I'm assuming that it's doing the implicit lookups, but I'm not sure.</p>
  
  <p>Why does the above call hang? Perhaps there's an error in my code?</p>