src/Pure/General/properties.scala
author wenzelm
Sun, 28 Jul 2019 14:37:32 +0200
changeset 70431 dbb32c2d5c2c
parent 68018 3747fe57eb67
child 71601 97ccf48c2f0c
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/properties.scala
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     3
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     4
Property lists.
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     5
*/
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     6
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     7
package isabelle
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     8
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     9
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    10
object Properties
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    11
{
65624
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    12
  /* entries */
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    13
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    14
  type Entry = (java.lang.String, java.lang.String)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    15
  type T = List[Entry]
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    16
64358
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    17
  def defined(props: T, name: java.lang.String): java.lang.Boolean =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    18
    props.exists({ case (x, _) => x == name })
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    19
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    20
  def get(props: T, name: java.lang.String): Option[java.lang.String] =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    21
    props.collectFirst({ case (x, y) if x == name => y })
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    22
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    23
  def put(props: T, entry: Entry): T =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    24
  {
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    25
    val (x, y) = entry
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    26
    def update(ps: T): T =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    27
      ps match {
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    28
        case (p @ (x1, _)) :: rest =>
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    29
          if (x1 == x) (x1, y) :: rest else p :: update(rest)
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    30
        case Nil => Nil
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    31
      }
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    32
    if (defined(props, x)) update(props) else entry :: props
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    33
  }
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    34
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    35
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    36
  /* external storage */
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    37
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    38
  def encode(ps: T): Bytes = Bytes(YXML.string_of_body(XML.Encode.properties(ps)))
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    39
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    40
  def decode(bs: Bytes, xml_cache: Option[XML.Cache] = None): T =
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    41
  {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    42
    val ps = XML.Decode.properties(YXML.parse_body(bs.text))
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    43
    xml_cache match {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    44
      case None => ps
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    45
      case Some(cache) => cache.props(ps)
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    46
    }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    47
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    48
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    49
  def compress(ps: List[T],
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    50
    options: XZ.Options = XZ.options(),
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    51
    cache: XZ.Cache = XZ.cache()): Bytes =
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    52
  {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    53
    if (ps.isEmpty) Bytes.empty
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    54
    else {
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    55
      Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    56
        compress(options = options, cache = cache)
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    57
    }
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    58
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    59
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    60
  def uncompress(bs: Bytes,
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    61
    cache: XZ.Cache = XZ.cache(),
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    62
    xml_cache: Option[XML.Cache] = None): List[T] =
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    63
  {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    64
    if (bs.isEmpty) Nil
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    65
    else {
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    66
      val ps =
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    67
        XML.Decode.list(XML.Decode.properties)(YXML.parse_body(bs.uncompress(cache = cache).text))
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    68
      xml_cache match {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    69
        case None => ps
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    70
        case Some(cache) => ps.map(cache.props(_))
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    71
      }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    72
    }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    73
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    74
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    75
65624
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    76
  /* multi-line entries */
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    77
65932
db5e701b691a clarified modules;
wenzelm
parents: 65857
diff changeset
    78
  def encode_lines(props: T): T = props.map({ case (a, b) => (a, Library.encode_lines(b)) })
db5e701b691a clarified modules;
wenzelm
parents: 65857
diff changeset
    79
  def decode_lines(props: T): T = props.map({ case (a, b) => (a, Library.decode_lines(b)) })
65624
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    80
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    81
  def lines_nonempty(x: java.lang.String, ys: List[java.lang.String]): Properties.T =
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    82
    if (ys.isEmpty) Nil else List((x, cat_lines(ys)))
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    83
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    84
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    85
  /* entry types */
64358
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    86
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    87
  class String(val name: java.lang.String)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    88
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    89
    def apply(value: java.lang.String): T = List((name, value))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    90
    def unapply(props: T): Option[java.lang.String] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    91
      props.find(_._1 == name).map(_._2)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    92
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    93
48344
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    94
  class Boolean(val name: java.lang.String)
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    95
  {
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    96
    def apply(value: scala.Boolean): T = List((name, Value.Boolean(value)))
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    97
    def unapply(props: T): Option[scala.Boolean] =
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    98
      props.find(_._1 == name) match {
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    99
        case None => None
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
   100
        case Some((_, value)) => Value.Boolean.unapply(value)
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
   101
      }
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
   102
  }
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
   103
48015
878de88db080 tuned signature;
wenzelm
parents: 45673
diff changeset
   104
  class Int(val name: java.lang.String)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   105
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   106
    def apply(value: scala.Int): T = List((name, Value.Int(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   107
    def unapply(props: T): Option[scala.Int] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   108
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   109
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   110
        case Some((_, value)) => Value.Int.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   111
      }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   112
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   113
48015
878de88db080 tuned signature;
wenzelm
parents: 45673
diff changeset
   114
  class Long(val name: java.lang.String)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   115
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   116
    def apply(value: scala.Long): T = List((name, Value.Long(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   117
    def unapply(props: T): Option[scala.Long] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   118
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   119
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   120
        case Some((_, value)) => Value.Long.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   121
      }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   122
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   123
48015
878de88db080 tuned signature;
wenzelm
parents: 45673
diff changeset
   124
  class Double(val name: java.lang.String)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   125
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   126
    def apply(value: scala.Double): T = List((name, Value.Double(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   127
    def unapply(props: T): Option[scala.Double] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   128
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   129
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   130
        case Some((_, value)) => Value.Double.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   131
      }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   132
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   133
}