These are chat archives for typelevel/scala

30th
Sep 2016
Miles Sabin
@milessabin
Sep 30 2016 13:47
Hi folks ... I'm still on the lookout for really good SI-2712 before/after diffs that I can use in my talk tomorrow ... does anyone have one I can use?
Yilin Wei
@yilinwei
Sep 30 2016 13:57
@soronpo bit late but that looks awesome
Miles Sabin
@milessabin
Sep 30 2016 13:57
It certainly does :-)
@soronpo could I bother you for an example like the While one, but which doesn't look quite so "imperative"? I'd like to use it as an example in my talk tomorrow, but I'd rather not have to explain "it isn't how it looks, that's really not mutable state" ;-)
Oron Port
@soronpo
Sep 30 2016 14:07
@milessabin OK, I'll try to write something more useful now.
Miles Sabin
@milessabin
Sep 30 2016 14:08
I doesn't have to be useful, just cool.
And fit on a slide :-)
Anyhow, thank you :-)
Oron Port
@soronpo
Sep 30 2016 14:54
@milessabin
  type I = "$I"
  type Prime = "$Prime"
  type IsPrime[Num] =
  ITE[
    Num <= 1,
    false,
      (Prime := true) ==>
      (I := 2) ==>
      While[
        (I * I <= Num) && Prime,
          (Prime := (Num % I != 0)) ==>
          (I += 1),
        Prime
      ]
  ]

  def demoPrime[Num <: XInt](implicit op : IsPrime[Num]): op.Out {} = op.value
  val ex1 : false = demoPrime[1]
  val ex2 : true = demoPrime[2]
  val ex3 : true = demoPrime[3]
  val ex4 : false = demoPrime[4]
  val ex5 : true = demoPrime[5]

  def incPrime[Num <: XInt](implicit op : Num + 1, check : Require[IsPrime[Num]]) : op.Out {} = op.value
  val inc5 : 6 = incPrime[5]
  val inc4 : 5 = incPrime[4] //Error:(41, 26) could not find implicit value for parameter check: singleton.ops.Require[singleton.ops.VarSpec.IsPrime[4]]
Remove what you don't want
Oron Port
@soronpo
Sep 30 2016 15:02
The type loop is equivalent to this:
  def isPrime(num : Int) : Boolean = {
    if (num <= 1) return false
    var i = 2
    var prime = true
    while (i * i <= num && prime) {
      prime = num % i != 0
      i += 1
    }
    prime
  }
BennyHill
@BennyHill
Sep 30 2016 15:02
Would scale conversion be useful here (was in C++, so apologies if this is spam)- eg useful if underlying code is in binary, but there are some "constants" that make more sense in decimal eg if (x < binary[64])....
Miles Sabin
@milessabin
Sep 30 2016 15:04
@soronpo yeah, the complicating factor here are the "vars" and the "mutable state" (in quotes because it doesn't exist at runtime) ... do you have something that looks cool, and also not imperative?
Oron Port
@soronpo
Sep 30 2016 15:15
Unfortunately Whilekind of forces an imperative statement. I have not implemented a For/Map. ATM the internal "variables" are mutable.
BennyHill
@BennyHill
Sep 30 2016 15:46
And in case you're wondering what sparked the prime/binary connection ;)
Oron Port
@soronpo
Sep 30 2016 15:49
@milessabin Not as cool, but:
  type RightTriangle[A,B,C] =
  ((A * A) + (B * B) == (C * C)) ||
  ((A * A) + (C * C) == (B * B)) ||
  ((B * B) + (C * C) == (A * A))

  def fooWith90DegTriangle[A <: XDouble, B <: XDouble, C <: XDouble](implicit check : Require[RightTriangle[A,B,C]]) : Unit = {}
  fooWith90DegTriangle[5.0,3.0,5.830951894845301] //OK!
  fooWith90DegTriangle[5.0,3.1,5.830951894845301] //Error:(17, 23) could not find implicit value for parameter check: singleton.ops.Require[singleton.ops.ITESpec.RightTriangle[5.0,3.1,5.830951894845301]]
Of course, the validity of this check is dependent on accuracy of the numbers. There may be examples where this check will fail due to FP rounding.
So RightTriangle may be modified to allow more tolerance instead of ==
Miles Sabin
@milessabin
Sep 30 2016 15:54
@soronpo that's great, thanks!
A type level fold would be awesome :-)
Is XDouble an alias for Double with Singleton?
Oron Port
@soronpo
Sep 30 2016 15:55
Yep, XDouble is an alias for Double with Singleton?
BennyHill
@BennyHill
Sep 30 2016 16:14
TLS.js :stuck_out_tongue:
BennyHill
@BennyHill
Sep 30 2016 17:05
Oh...your talk is tomorrow! Implementing MPL (inc fold) in easy-to-read scala would for sure impress everyone ;)
BennyHill
@BennyHill
Sep 30 2016 17:15
And on a more serious note, that MPL paper has a revision date of 2004, two years prior to the release of scala 2.0
Oron Port
@soronpo
Sep 30 2016 18:31
@milessabin I may be able to quickly implement a simplified For for the IsPrime example.
BennyHill
@BennyHill
Sep 30 2016 19:01
heh - looks like Hana is Boost's latest "library for C++ metaprogramming suited for computations on both types and values." I was toying with the idea of linking that on the dotty requirements for generic programing, but if the thesis was not made for EPFL ....
"Catégories et métaprogrammation en C++"
Greg Pfeil
@sellout
Sep 30 2016 21:59
Is the upstream Pr for custom predefs something that would get into TLS?
Miles Sabin
@milessabin
Sep 30 2016 22:07
Yes, definitely.
andy scott
@andyscott
Sep 30 2016 22:17
I better hurry up with the last bit of that