简体   繁体   中英

Path Dependent Types: Idris to Scala

I wrote an example from Type-Driven Development with Idris in Idris to Scala:

Idris (taken from TDD with Idris)

data PowerSource = Petrol | Pedal

data Vehicle : PowerSource -> Type where
           Bicycle : Vehicle Pedal
           Car : (fuel : Nat) -> Vehicle Petrol
           Bus : (fuel : Nat) -> Vehicle Petrol

refuel : Vehicle Petrol -> Vehicle Petrol
refuel (Car fuel) = Car 100
refuel (Bus fuel) = Bus 200

Scala

scala> sealed trait PowerSource 
defined trait PowerSource

scala> case object Petrol extends PowerSource
defined object Petrol

scala> case object Pedal extends PowerSource
defined object Pedal

scala> sealed abstract class Vehicle {
     |   type A <: PowerSource
     | }
defined class Vehicle

scala> case object Bicycle extends Vehicle {
     |   type A = Pedal.type
     | }

scala> case class Bus(fuel: Int) extends Vehicle {
     |   type A = Petrol.type
     | }
defined class Bus

scala> case class Car(fuel: Int) extends Vehicle {
     |   type A = Petrol.type
     | }

But, I ran into trouble when trying to implement refuel :

scala> def refuel[P <: Petrol.type](vehicle: Vehicle {type A = P} ): Vehicle = vehicle match {
     |   case Car(_) => Car(100)
     |   case Bus(_) => Bus(100)
     | }
<console>:20: warning: unreachable code
         case Bus(_) => Bus(100)
                           ^
refuel: [P <: Petrol.type](vehicle: Vehicle{type A = P})Vehicle

scala> refuel(Car(100))
res6: Vehicle = Car(100)

scala> refuel(Bus(5))
res7: Vehicle = Bus(100)

scala> refuel(Bicycle)
<console>:22: error: inferred type arguments [Bicycle.A] do not conform to method refuel's type parameter bounds [P <: Petrol.type]
       refuel(Bicycle)
       ^
<console>:22: error: type mismatch;
 found   : Bicycle.type
 required: Vehicle{type A = P}
       refuel(Bicycle)
              ^

Please critique/correct my approach, as well as help me with refuel in Scala.

The unreachable code warning appears to be spurious: if it wasn't you'd be able to coerce a Bus to a Car (which is unsound) and cause a ClassCastExeception ... that doesn't appear to be possible.

The spurious warning is a scalac bug. There are related bugs in the Scala issue tracker, but this seems to be a different variant ... you should report it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM