-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add mutable map TR * move to dotty specific
- Loading branch information
1 parent
874df4c
commit 2fa1c55
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
frontends/benchmarks/dotty-specific/valid/MutableMapTR.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import scala.annotation.tailrec | ||
import stainless.lang.* | ||
import stainless.collection.* | ||
import stainless.annotation.* | ||
|
||
object MTailList: | ||
sealed abstract class MutableList[T] | ||
case class MNil[T]() extends MutableList[T] | ||
case class MCons[T](val hd: T, var tail: MutableList[T]) extends MutableList[T] | ||
|
||
extension[T] (ml: MutableList[T]) | ||
@pure | ||
def toList: List[T] = | ||
ml match | ||
case MNil() => Nil() | ||
case MCons(h, t) => Cons(h, t.toList) | ||
|
||
def mapTR[T,U](l: MutableList[T], f: T => U): MutableList[U] = { | ||
l match | ||
case MNil() => MNil[U]() | ||
case MCons(hd, tl) => | ||
val acc: MCons[U] = MCons[U](f(hd), MNil()) | ||
mapTRWorker[T,U](tl, f, acc) | ||
acc | ||
} ensuring(_.toList == l.toList.map(f)) | ||
|
||
@tailrec | ||
def mapTRWorker[T,U]( | ||
l: MutableList[T], | ||
f: T => U, | ||
acc: MCons[U] | ||
): Unit = { | ||
require(acc.tail == MNil[U]()) | ||
l match | ||
case MNil() => () | ||
case MCons(h, t) => | ||
acc.tail = MCons[U](f(h), MNil()) | ||
mapTRWorker(t, f, acc.tail.asInstanceOf[MCons[U]]) | ||
assert(acc.tail.asInstanceOf[MCons[U]].toList == f(h) :: t.toList.map(f)) | ||
} ensuring(_ => acc.toList == old(acc).hd :: l.toList.map(f)) |