Skip to content

Commit

Permalink
Add mutable map TR (#1484)
Browse files Browse the repository at this point in the history
* add mutable map TR

* move to dotty specific
  • Loading branch information
samuelchassot authored Nov 13, 2023
1 parent 874df4c commit 2fa1c55
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions frontends/benchmarks/dotty-specific/valid/MutableMapTR.scala
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))

0 comments on commit 2fa1c55

Please sign in to comment.