package sbt
import Types._
sealed trait KList[+M[_], HL <: HList]
{
	type Raw = HL
	
	def down(implicit ev: M ~> Id): HL
	
	def transform[N[_]](f: M ~> N): KList[N, HL]
	
	def toList: List[M[_]]
	
	def combine[N[X] >: M[X]]: HL#Wrap[N]
	def foldr[P[_ <: HList],N[X] >: M[X]](f: KFold[N,P]): P[HL]
}
trait KFold[M[_],P[_ <: HList]]
{
	def kcons[H,T <: HList](h: M[H], acc: P[T]): P[H :+: T]
	def knil: P[HNil]
}
final case class KCons[H, T <: HList, +M[_]](head: M[H], tail: KList[M,T]) extends KList[M, H :+: T]
{
	def down(implicit f: M ~> Id) = HCons(f(head), tail down f)
	def transform[N[_]](f: M ~> N) = KCons( f(head), tail transform f )
	
	def :^: [N[X] >: M[X], G](g: N[G]) = KCons(g, this)
	def toList = head :: tail.toList
	
	def combine[N[X] >: M[X]]: (H :+: T)#Wrap[N] = HCons(head, tail.combine)
	override def toString = head + " :^: " + tail.toString
	def foldr[P[_ <: HList],N[X] >: M[X]](f: KFold[N,P]) = f.kcons(head, tail foldr f)
}
sealed class KNil extends KList[Nothing, HNil]
{
	def down(implicit f: Nothing ~> Id) = HNil
	def transform[N[_]](f: Nothing ~> N) = KNil
	def :^: [M[_], H](h: M[H]) = KCons(h, this)
	def toList = Nil
	def combine[N[X]] = HNil
	override def foldr[P[_ <: HList],N[_]](f: KFold[N,P]) = f.knil
	override def toString = "KNil"
}
object KNil extends KNil
object KList
{
	
	val :^: = KCons
	
	def fromList[M[_]](s: Seq[M[_]]): KList[M, _ <: HList] = if(s.isEmpty) KNil else KCons(s.head, fromList(s.tail))
	
	
	implicit def kcons[H, T <: HList, M[_]](kl: KList[M, H :+: T]): KCons[H,T,M] =
		kl.asInstanceOf[KCons[H,T,M]]
	
	implicit def knil[M[_]](kl: KList[M, HNil]): KNil = KNil
}