80274
|
1 |
/* Title: Pure/update.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Update data (with formal name).
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
object Update {
|
|
11 |
sealed abstract class Op[A]
|
|
12 |
case class Delete[A](name: String) extends Op[A]
|
|
13 |
case class Insert[A](item: A) extends Op[A]
|
|
14 |
|
|
15 |
def data[A <: Name.T](old_data: Name.Data[A], updates: List[Op[A]]): Name.Data[A] =
|
|
16 |
updates.foldLeft(old_data) {
|
|
17 |
case (map, Delete(name)) => map - name
|
|
18 |
case (map, Insert(item)) => map + (item.name -> item)
|
|
19 |
}
|
|
20 |
|
|
21 |
val empty: Update = Update()
|
|
22 |
|
|
23 |
def make[A](a: Name.Data[A], b: Name.Data[A], kind: Int = 0): Update =
|
|
24 |
if (a eq b) empty
|
|
25 |
else {
|
|
26 |
val delete = List.from(for ((x, y) <- a.iterator if !b.get(x).contains(y)) yield x)
|
|
27 |
val insert = List.from(for ((x, y) <- b.iterator if !a.get(x).contains(y)) yield x)
|
|
28 |
Update(delete = delete, insert = insert, kind = kind)
|
|
29 |
}
|
|
30 |
}
|
|
31 |
|
|
32 |
sealed case class Update(
|
|
33 |
delete: List[String] = Nil,
|
|
34 |
insert: List[String] = Nil,
|
|
35 |
kind: Int = 0
|
|
36 |
) {
|
|
37 |
def deletes: Boolean = delete.nonEmpty
|
|
38 |
def inserts: Boolean = insert.nonEmpty
|
|
39 |
def defined: Boolean = deletes || inserts
|
|
40 |
lazy val domain: Set[String] = delete.toSet ++ insert
|
|
41 |
}
|