type-level-computation

Generic transform/fold/map over tuple/hlist containing some F[_]

℡╲_俬逩灬. 提交于 2019-12-21 05:27:07
问题 I recently asked Map and reduce/fold over HList of scalaz.Validation and got a great answer as to how to transform a fixed sized tuple of Va[T] (which is an alias for scalaz.Validation[String, T] ) into a scalaz.ValidationNel[String, T] . I've since then been studying Shapeless and type level programming in general to try to come up with a solution that works on tuples of any size. This is what I'm starting out with: import scalaz._, Scalaz._, shapeless._, contrib.scalaz._, syntax.std.tuple._

Why certain type projections are rejected and slightly different not?

五迷三道 提交于 2019-12-21 05:17:10
问题 Sorry for non-descriptive title. Typelevel computation are not too common theme in the internet to establish precise terms. I tried to use typelevel computations and suddenly get spurious errors every here and there. I just could not comprehend why so little difference matters. I managed to forge another concise example: trait Sample { type X type DX[I <: X] <: Nothing type EX[I <: X] = Nothing } type Aux[I] = Sample {type X = I} type U1 = Aux[Int]#DX[Int] // ok type U2 = Aux[Int]#EX[Int] //

Materialize the Value for a Type that has One Inhabitant

Deadly 提交于 2019-12-20 10:25:30
问题 Thanks to @MilesSabin's answer I can write a type level Fibonacci sequence: sealed trait Digit case object Zero extends Digit case object One extends Digit sealed trait Dense { type N <: Dense } sealed trait DNil extends Dense { type N = DNil } case object DNil extends DNil final case class ::[+H <: Digit, +T <: Dense](digit: H, tail: T) extends Dense { type N = digit.type :: tail.N } /* The `A`th Fibonacci number is `B` */ trait Fib[A <: Dense, B <: Dense] object Fib { implicit val f0 = new

Summon Aux for higher-kinded type without reference to the original

旧城冷巷雨未停 提交于 2019-12-12 19:00:17
问题 I am trying to use the Aux pattern with a higher kinded type and not have to specify the parameter of the higher-kinded type until afterward. This is similar to the SO question described here but with one significant difference, I'm going the other way around, i.e. from an implicit def back to an aux. // The are types that I want to convert to various things sealed trait ConversionType trait CaseA extends ConversionType object CaseA extends CaseA // In this case, convert to an optional trait

Scala HList manipulation : is a flexible structure possible?

大憨熊 提交于 2019-12-11 01:05:15
问题 I wrote a min example of something I do not manage to do. Am I in the wrong way? object minNotWoringkExample { import shapeless._ def typed[T](t: => T) {} class Bar { type L <: HList } class Foo { type UPDATE[L <: HList] <: HList } class FooI extends Foo { type UPDATE[L <: HList] = FilterNot[L, FooI]#Out } def update[O <: Foo, B <: Bar] = new Bar { type L = O#UPDATE[B#L] } val myBar = new Bar { type L = FooI :: Foo :: HNil } val myUpdatedBar = update[FooI, Bar {type L = FooI :: Foo :: HNil}]

How can I define NFData instance for recursive singleton type?

只愿长相守 提交于 2019-12-10 18:14:26
问题 I'm using singletons library. I have this data type: import Control.DeepSeq import Data.Singletons.Prelude import Data.Singletons.TH data T = A | B [T] genSingletons [''T] I want generated singleton type ST to be instance of NFData . It would be straightforward if type T wouldn't be recursive. I tried to write this: instance NFData (ST a) where rnf SA = () rnf (SB (x `SCons` xs)) = rnf x `seq` rnf xs but this fails on last line with message: Could not deduce (NFData (Sing n1)) arising from a

In Scala, is it possible to “curry” type parameters of a def?

走远了吗. 提交于 2019-12-10 14:33:45
问题 Let's suppose I have a def that takes multiple type parameters: def foo[A, B, C](b: B, c: C)(implicit ev: Writer[A]) However, the intended usage is that type parameters B and C should be inferred (based on the passed-in arguments). And the caller should only need to really specify A explicitly (e.g. to have an appropriate implicit chosen by the compiler). Unfortunately, Scala only allows all or none of the type parameters to be specified by the caller. In a sense, I want the type parameters

How to work with types that change under composition?

亡梦爱人 提交于 2019-12-10 04:00:00
问题 I recently read the very interesting paper Monotonicity Types in which a new HM-language is described that keeps track of monotonicity across operations, so that the programmer does not have to do this manually (and fail at compile-time when a non-monotonic operation is passed to something that requires one). I was thinking that it probably would be possible to model this in Haskell, since the sfun s that the paper describes seem to be 'just another Arrow instance', so I set out to create a

How do you debug typelevel code?

寵の児 提交于 2019-12-09 06:01:43
问题 Most of the time, all you get is an implicit not found error. You don't know where in the chain of implicit construction it failed. Apparently you can't use runtime debug or print statement. So how do you debug type-level program other than staring at your code really hard? 回答1: I wish I had a better answer, but here it goes: Start passing the parameters explicitly, one at a time until it gives you a more useful error. (adding-prinlns-equivalent for implicits params) 回答2: You can use ??? for

Simulate a path-dependent type in Haskell

China☆狼群 提交于 2019-12-07 13:54:33
问题 Here is a simplified example of what I want to do. Let's say you have a HList of pairs: let hlist = HCons (1, "1") (HCons ("0", 2) (HCons ("0", 1.5) HNil)) Now I want to write a function replaceAll which will replace all the "keys" of a given type with the first "value" of the same type. For example, with the HList above I would like to replace all the String keys with "1" which is the first value of type String found in the HList replaceAll @String hlist = HCons (1, "1") (HCons ("1", 2)