progs/sml/re-bit.ML
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 11 Jun 2016 13:28:45 +0100
changeset 196 5fa8344a5176
parent 159 940530087f30
child 317 db0ff630bbb7
permissions -rw-r--r--
added test processing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
datatype rexp =
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
     3
   ZERO
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
     4
 | ONE 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
 | CHAR of char
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
     6
 | ALT  of rexp * rexp
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
     7
 | SEQ  of rexp * rexp 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
 | STAR of rexp 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
 | RECD of string * rexp
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    11
datatype arexp =
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    12
   AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    13
 | AONE  of (bool list)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    14
 | ACHAR of (bool list) * char
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    15
 | AALT  of (bool list) * arexp * arexp
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    16
 | ASEQ  of (bool list) * arexp * arexp 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    17
 | ASTAR of (bool list) * arexp 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
    18
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
datatype value =
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    20
   Empty
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
 | Chr of char
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
 | Sequ of value * value
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
 | Left of value
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
 | Right of value
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
 | Stars of value list
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
 | Rec of string * value
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
(* some helper functions for strings *)   
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
fun string_repeat s n = String.concat (List.tabulate (n, fn _ => s))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
(* some helper functions for rexps *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
fun seq s = case s of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    33
    [] => ONE
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  | [c] => CHAR(c)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  | c::cs => SEQ(CHAR(c), seq cs)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
fun chr c = CHAR(c)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
fun str s = seq(explode s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
fun plus r = SEQ(r, STAR(r))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
infix 9 ++
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
infix 9 --
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
infix 9 $
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
fun op ++ (r1, r2) = ALT(r1, r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
fun op -- (r1, r2) = SEQ(r1, r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
fun op $ (x, r) = RECD(x, r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
fun alts rs = case rs of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    54
    [] => ZERO
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  | [r] => r
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  | r::rs => List.foldl (op ++) r rs
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
(* size of a regular expressions - for testing purposes *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
fun size r = case r of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    60
    ZERO => 1
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    61
  | ONE => 1
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  | CHAR(_) => 1
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  | ALT(r1, r2) => 1 + (size r1) + (size r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  | SEQ(r1, r2) => 1 + (size r1) + (size r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
  | STAR(r) => 1 + (size r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  | RECD(_, r) => 1 + (size r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
(* nullable function: tests whether the regular 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
   expression can recognise the empty string *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
fun nullable r = case r of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    71
    ZERO => false
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    72
  | ONE => true
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
  | CHAR(_) => false
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
  | ALT(r1, r2) => nullable(r1) orelse nullable(r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  | SEQ(r1, r2) => nullable(r1) andalso nullable(r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
  | STAR(_) => true
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
  | RECD(_, r) => nullable(r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
(* derivative of a regular expression r w.r.t. a character c *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
fun der c r = case r of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    81
    ZERO => ZERO
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    82
  | ONE => ZERO
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    83
  | CHAR(d) => if c = d then ONE else ZERO
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  | ALT(r1, r2) => ALT(der c r1, der c r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  | SEQ(r1, r2) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
      if nullable r1 then ALT(SEQ(der c r1, r2), der c r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
      else SEQ(der c r1, r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
  | STAR(r) => SEQ(der c r, STAR(r))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  | RECD(_, r) => der c r
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
(* derivative w.r.t. a list of chars (iterates der) *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
fun ders s r = case s of 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
    [] => r
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  | c::s => ders s (der c r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
(* extracts a string from value *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
fun flatten v = case v of 
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
    98
    Empty => ""
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  | Chr(c) => Char.toString c
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
  | Left(v) => flatten v
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
  | Right(v) => flatten v
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  | Sequ(v1, v2) => flatten v1 ^ flatten v2
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  | Stars(vs) => String.concat (List.map flatten vs)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
  | Rec(_, v) => flatten v
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
(* extracts an environment from a value *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
fun env v = case v of 
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   109
    Empty => []
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
  | Chr(c) => []
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
  | Left(v) => env v
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  | Right(v) => env v
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
  | Sequ(v1, v2) => env v1 @ env v2
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
  | Stars(vs) => List.foldr (op @) [] (List.map env vs)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
  | Rec(x, v) => (x, flatten v) :: env v
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
fun string_of_pair (x, s) = "(" ^ x ^ "," ^ s ^ ")"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
fun string_of_env xs = String.concatWith "," (List.map string_of_pair xs)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
(* the value for a nullable rexp *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
fun mkeps r = case r of 
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   123
    ONE => Empty
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
  | ALT(r1, r2) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
      if nullable r1 then Left(mkeps r1) else Right(mkeps r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
  | SEQ(r1, r2) => Sequ(mkeps r1, mkeps r2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
  | STAR(r) => Stars([])
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
  | RECD(x, r) => Rec(x, mkeps r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
exception Error
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
(* injection of a char into a value *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
fun inj r c v = case (r, v) of
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
    (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj r c v1 :: vs)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
  | (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj r1 c v1, v2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
  | (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj r1 c v1, v2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
  | (SEQ(r1, r2), Right(v2)) => Sequ(mkeps r1, inj r2 c v2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
  | (ALT(r1, r2), Left(v1)) => Left(inj r1 c v1)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
  | (ALT(r1, r2), Right(v2)) => Right(inj r2 c v2)
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   140
  | (CHAR(d), Empty) => Chr(d) 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
  | (RECD(x, r1), _) => Rec(x, inj r1 c v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
  | _ => (print ("\nr: " ^ PolyML.makestring r ^ "\n");
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
          print ("v: " ^ PolyML.makestring v ^ "\n");
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
          raise Error)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
(* some "rectification" functions for simplification *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
fun f_id v = v
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
fun f_right f = fn v => Right(f v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
fun f_left f = fn v => Left(f v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
fun f_alt f1 f2 = fn v => case v of
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
    Right(v) => Right(f2 v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
  | Left(v) => Left(f1 v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
fun f_seq f1 f2 = fn v => case v of 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
  Sequ(v1, v2) => Sequ(f1 v1, f2 v2)
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   155
fun f_seq_Empty1 f1 f2 = fn v => Sequ(f1 Empty, f2 v)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   156
fun f_seq_Empty2 f1 f2 = fn v => Sequ(f1 v, f2 Empty)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
fun f_rec f = fn v => case v of
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
    Rec(x, v) => Rec(x, f v)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
exception ShouldNotHappen
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
fun f_error v = raise ShouldNotHappen
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
(* simplification of regular expressions returning also an 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
   rectification function; no simplification under STARs *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
fun simp r = case r of
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
    ALT(r1, r2) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
      let val (r1s, f1s) = simp r1  
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
          val (r2s, f2s) = simp r2 in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
        (case (r1s, r2s) of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   171
            (ZERO, _) => (r2s, f_right f2s)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   172
          | (_, ZERO) => (r1s, f_left f1s)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
          | (_, _)    => if r1s = r2s then (r1s, f_left f1s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
                         else (ALT (r1s, r2s), f_alt f1s f2s))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
      end 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
  | SEQ(r1, r2) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
      let val (r1s, f1s) = simp r1 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
          val (r2s, f2s) = simp r2 in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
        (case (r1s, r2s) of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   180
          (ZERO, _)  => (ZERO, f_error)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   181
        | (_, ZERO)  => (ZERO, f_error)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   182
        | (ONE, _) => (r2s, f_seq_Empty1 f1s f2s)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   183
        | (_, ONE) => (r1s, f_seq_Empty2 f1s f2s)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
        | (_, _)     => (SEQ(r1s, r2s), f_seq f1s f2s))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
      end  
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
  | RECD(x, r1) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
      let val (r1s, f1s) = simp r1 in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
        (RECD(x, r1s), f_rec f1s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
      end
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
  | r => (r, f_id)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
fun der_simp c r = case r of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   193
    ZERO => (ZERO, f_id)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   194
  | ONE => (ZERO, f_id)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   195
  | CHAR(d) => ((if c = d then ONE else ZERO), f_id)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
  | ALT(r1, r2) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
      let 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
        val (r1d, f1d) = der_simp c r1 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
        val (r2d, f2d) = der_simp c r2 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
      in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
        case (r1d, r2d) of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   202
          (ZERO, _) => (r2d, f_right f2d)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   203
        | (_, ZERO) => (r1d, f_left f1d)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
        | (_, _)    => if r1d = r2d then (r1d, f_left f1d)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
                       else (ALT (r1d, r2d), f_alt f1d f2d)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
      end
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
  | SEQ(r1, r2) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
      if nullable r1 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
      then 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
        let 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
          val (r1d, f1d) = der_simp c r1 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
          val (r2d, f2d) = der_simp c r2
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
          val (r2s, f2s) = simp r2 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
        in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
          case (r1d, r2s, r2d) of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   216
            (ZERO, _, _)  => (r2d, f_right f2d)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   217
          | (_, ZERO, _)  => (r2d, f_right f2d)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   218
          | (_, _, ZERO)  => (SEQ(r1d, r2s), f_left (f_seq f1d f2s))
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   219
          | (ONE, _, _) => (ALT(r2s, r2d), f_alt (f_seq_Empty1 f1d f2s) f2d)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   220
          | (_, ONE, _) => (ALT(r1d, r2d), f_alt (f_seq_Empty2 f1d f2s) f2d)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
          | (_, _, _)     => (ALT(SEQ(r1d, r2s), r2d), f_alt (f_seq f1d f2s) f2d)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
        end
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
      else 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
        let 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
          val (r1d, f1d) = der_simp c r1 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
          val (r2s, f2s) = simp r2
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
        in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
          case (r1d, r2s) of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   229
            (ZERO, _) => (ZERO, f_error)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   230
          | (_, ZERO) => (ZERO, f_error)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   231
          | (ONE, _) => (r2s, f_seq_Empty1 f1d f2s)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   232
          | (_, ONE) => (r1d, f_seq_Empty2 f1d f2s)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
          | (_, _) => (SEQ(r1d, r2s), f_seq f1d f2s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
  	end	  
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
  | STAR(r1) => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
      let 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
        val (r1d, f1d) = der_simp c r1 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
      in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
        case r1d of
156
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   240
          ZERO => (ZERO, f_error)
6a43ea9305ba updated implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3
diff changeset
   241
        | ONE => (STAR r1, f_seq_Empty1 f1d f_id)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
        | _ => (SEQ(r1d, STAR(r1)), f_seq f1d f_id)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
      end
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
  | RECD(x, r1) => der_simp c r1 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
(* matcher function *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
fun matcher r s = nullable(ders (explode s) r)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
(* lexing function (produces a value) *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
exception LexError
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
fun lex r s = case s of 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
    [] => if (nullable r) then mkeps r else raise LexError
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
  | c::cs => inj r c (lex (der c r) cs)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
fun lexing r s = lex r (explode s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
(* lexing with simplification *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
fun lex_simp r s = case s of 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
    [] => if (nullable r) then mkeps r else raise LexError
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
  | c::cs => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
    let val (r_simp, f_simp) = simp (der c r) in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
      inj r c (f_simp (lex_simp r_simp cs))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
    end
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
fun lexing_simp r s = lex_simp r (explode s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
fun lex_simp2 r s = case s of 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
    [] => if (nullable r) then mkeps r else raise LexError
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
  | c::cs => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
    let val (r_simp, f_simp) = der_simp c r in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
      inj r c (f_simp (lex_simp2 r_simp cs))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
    end
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
fun lexing_simp2 r s = lex_simp2 r (explode s)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
fun lex_acc r s f = case s of 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
    [] => if (nullable r) then f (mkeps r) else raise LexError
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
  | c::cs => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
    let val (r_simp, f_simp) = simp (der c r) in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
      lex_acc r_simp cs (fn v => f (inj r c (f_simp v)))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
    end
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   284
fun lexing_acc r s  = lex_acc r (explode s) (f_id)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   285
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   286
fun lex_acc2 r s f = case s of 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
    [] => if (nullable r) then f (mkeps r) else raise LexError
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
  | c::cs => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
    let val (r_simp, f_simp) = der_simp c r in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   290
      lex_acc2 r_simp cs (fn v => f (inj r c (f_simp v)))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   291
    end
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   292
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   293
fun lexing_acc2 r s  = lex_acc2 r (explode s) (f_id)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   294
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   295
(* bit-coded version *)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   296
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   297
fun fuse bs r = case r of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   298
  AZERO => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   299
| AONE(cs) => AONE(bs @ cs)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   300
| ACHAR(cs, c) => ACHAR(bs @ cs, c)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   301
| AALT(cs, r1, r2) => AALT(bs @ cs, r1, r2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   302
| ASEQ(cs, r1, r2) => ASEQ(bs @ cs, r1, r2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   303
| ASTAR(cs, r) => ASTAR(bs @ cs, r)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   304
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   305
fun internalise r = case r of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   306
  ZERO => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   307
| ONE => AONE([])
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   308
| CHAR(c) => ACHAR([], c)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   309
| ALT(r1, r2) => AALT([], fuse [false] (internalise r1), fuse [true] (internalise r2))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   310
| SEQ(r1, r2) => ASEQ([], internalise r1, internalise r2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   311
| STAR(r) => ASTAR([], internalise r)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   312
| RECD(x, r) => internalise r
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   313
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   314
fun decode_aux r bs = case (r, bs) of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   315
  (ONE, bs) => (Empty, bs)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   316
| (CHAR(c), bs) => (Chr(c), bs)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   317
| (ALT(r1, r2), false::bs) => 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   318
     let val (v, bs1) = decode_aux r1 bs
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   319
     in (Left(v), bs1) end
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   320
| (ALT(r1, r2), true::bs) => 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   321
     let val (v, bs1) = decode_aux r2 bs
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   322
     in (Right(v), bs1) end
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   323
| (SEQ(r1, r2), bs) => 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   324
    let val (v1, bs1) = decode_aux r1 bs 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   325
        val (v2, bs2) = decode_aux r2 bs1 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   326
    in (Sequ(v1, v2), bs2) end
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   327
| (STAR(r1), false::bs) => 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   328
    let val (v, bs1) = decode_aux r1 bs 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   329
        val (Stars(vs), bs2) = decode_aux (STAR r1) bs1
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   330
    in (Stars(v::vs), bs2) end
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   331
| (STAR(_), true::bs) => (Stars [], bs)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   332
| (RECD(x, r1), bs) => 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   333
    let val (v, bs1) = decode_aux r1 bs
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   334
    in (Rec(x, v), bs1) end
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   335
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   336
exception DecodeError
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   337
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   338
fun decode r bs = case (decode_aux r bs) of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   339
  (v, []) => v
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   340
| _ => raise DecodeError
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   341
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   342
fun anullable r = case r of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   343
  AZERO => false
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   344
| AONE(_) => true
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   345
| ACHAR(_,_) => false
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   346
| AALT(_, r1, r2) => anullable(r1) orelse anullable(r2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   347
| ASEQ(_, r1, r2) => anullable(r1) andalso anullable(r2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   348
| ASTAR(_, _) => true
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   349
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   350
fun mkepsBC r = case r of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   351
  AONE(bs) => bs
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   352
| AALT(bs, r1, r2) => 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   353
    if anullable(r1) then bs @ mkepsBC(r1) else bs @ mkepsBC(r2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   354
| ASEQ(bs, r1, r2) => bs @ mkepsBC(r1) @ mkepsBC(r2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   355
| ASTAR(bs, r) => bs @ [true]
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   356
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   357
fun ader c r = case r of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   358
  AZERO => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   359
| AONE(_) => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   360
| ACHAR(bs, d) => if c = d then AONE(bs) else AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   361
| AALT(bs, r1, r2) => AALT(bs, ader c r1, ader c r2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   362
| ASEQ(bs, r1, r2) => 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   363
    if (anullable r1) then AALT(bs, ASEQ([], ader c r1, r2), fuse (mkepsBC r1) (ader c r2))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   364
    else ASEQ(bs, ader c r1, r2)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   365
| ASTAR(bs, r) => ASEQ(bs, fuse [false] (ader c r), ASTAR([], r))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   366
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   367
fun aders s r = case s of 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   368
  [] => r
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   369
| c::s => aders s (ader c r)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   370
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   371
fun alex r s = case s of 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   372
    [] => if (anullable r) then mkepsBC r else raise LexError
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   373
  | c::cs => alex (ader c r) cs
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   374
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   375
fun alexing r s = decode r (alex (internalise r) (explode s))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   376
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   377
fun asimp r = case r of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   378
  ASEQ(bs1, r1, r2) => (case (asimp r1, asimp r2) of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   379
      (AZERO, _) => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   380
    | (_, AZERO) => AZERO
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   381
    | (AONE(bs2), r2s) => fuse (bs1 @ bs2) r2s
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   382
    | (r1s, r2s) => ASEQ(bs1, r1s, r2s)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   383
  )
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   384
| AALT(bs1, r1, r2) => (case (asimp r1, asimp r2) of
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   385
      (AZERO, r2s) => fuse bs1 r2s
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   386
    | (r1s, AZERO) => fuse bs1 r1s
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   387
    | (r1s, r2s) => AALT(bs1, r1s, r2s)
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   388
  )
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   389
| r => r
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   390
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   391
fun alex_simp r s = case s of 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   392
    [] => if (anullable r) then mkepsBC r else raise LexError
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   393
  | c::cs => alex_simp (asimp (ader c r)) cs
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   394
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   395
fun alexing_simp r s = decode r (alex_simp (internalise r) (explode s))
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   396
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   397
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   398
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   399
(* Lexing rules for a small WHILE language *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   400
val sym = alts (List.map chr (explode "abcdefghijklmnopqrstuvwxyz"))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   401
val digit = alts (List.map chr (explode "0123456789"))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   402
val idents =  sym -- STAR(sym ++ digit)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   403
val nums = plus(digit)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   404
val keywords = alts (List.map str ["skip", "while", "do", "if", "then", "else", "read", "write", "true", "false"])
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   405
val semicolon = str ";"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   406
val ops = alts (List.map str [":=", "==", "-", "+", "*", "!=", "<", ">", "<=", ">=", "%", "/"])
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   407
val whitespace = plus(str " " ++ str "\n" ++ str "\t")
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   408
val rparen = str ")"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   409
val lparen = str "("
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   410
val begin_paren = str "{"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   411
val end_paren = str "}"
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   412
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   413
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   414
val while_regs = STAR(("k" $ keywords) ++
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   415
                      ("i" $ idents) ++
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   416
                      ("o" $ ops) ++ 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   417
                      ("n" $ nums) ++ 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   418
                      ("s" $ semicolon) ++ 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   419
                      ("p" $ (lparen ++ rparen)) ++ 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   420
                      ("b" $ (begin_paren ++ end_paren)) ++ 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   421
                      ("w" $ whitespace))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   422
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   423
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   424
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   425
(* Some Tests
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   426
  ============ *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   427
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   428
fun time f x =
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   429
  let
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   430
  val t_start = Timer.startCPUTimer()
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   431
  val f_x = (f x; f x; f x; f x; f x; f x; f x; f x; f x; f x)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   432
  val t_end = Time.toReal(#usr(Timer.checkCPUTimer(t_start))) / 10.0
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   433
in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   434
  (print ((Real.toString t_end) ^ "\n"); f_x)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   435
end
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   436
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   437
val prog = "ab";
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   438
val reg = ("x" $ ((str "a") -- (str "b")));
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   439
print("Simp: " ^ PolyML.makestring (lexing_simp reg prog) ^ "\n");
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   440
print("Acc:  " ^ PolyML.makestring (lexing_acc  reg prog) ^ "\n");
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   441
print("Env   " ^ string_of_env (env (lexing_acc reg prog)) ^ "\n");
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   442
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   443
fun fst (x, y) = x;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   444
fun snd (x, y) = y;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   445
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   446
val derS = [reg,
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   447
            der #"a" reg,
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   448
            fst (simp (der #"a" reg)),
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   449
            fst (der_simp #"a" reg)];
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   450
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   451
val vS = [(snd (simp (der #"a" reg))) (Chr(#"b")),
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   452
          (snd (der_simp #"a" reg)) (Chr(#"b"))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   453
         ];
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   454
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   455
print("Ders: \n" ^ 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   456
       String.concatWith "\n" (List.map PolyML.makestring derS)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   457
       ^ "\n\n");
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   458
print("Vs: \n" ^ 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   459
       String.concatWith "\n" (List.map PolyML.makestring vS)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   460
       ^ "\n\n");
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   461
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   462
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   463
val prog0 = "read n";
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   464
print("Env0 is: \n" ^  string_of_env (env (lexing_acc while_regs prog0)) ^ "\n");
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   465
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   466
val prog1 = "read  n; write (n)";
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   467
print("Env1 is: \n" ^ string_of_env (env (lexing_acc while_regs prog1)) ^ "\n");
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   468
print("Env1 is: \n" ^ string_of_env (env (alexing while_regs prog1)) ^ "\n");
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   469
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   470
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   471
val prog2 = String.concatWith "\n" 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   472
  ["i := 2;",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   473
   "max := 100;",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   474
   "while i < max do {",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   475
   "  isprime := 1;",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   476
   "  j := 2;",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   477
   "  while (j * j) <= i + 1  do {",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   478
   "    if i % j == 0 then isprime := 0  else skip;",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   479
   "    j := j + 1",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   480
   "  };",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   481
   " if isprime == 1 then write i else skip;",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   482
   " i := i + 1",
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   483
   "}"];
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   484
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   485
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   486
let 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   487
  val tst = (lexing_simp while_regs prog2 = lexing_acc while_regs prog2)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   488
in
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   489
  print("Sanity test: >>" ^ (PolyML.makestring tst) ^ "<<\n")
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   490
end;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   491
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   492
(* loops in ML *)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   493
datatype for = to of int * int
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   494
infix to 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   495
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   496
val for =
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   497
  fn lo to up =>
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   498
    (fn f => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   499
       let fun loop lo = 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   500
         if lo > up then () else (f lo; loop (lo + 1))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   501
       in loop lo end)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   502
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   503
fun forby n =
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   504
  fn lo to up =>
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   505
    (fn f => 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   506
       let fun loop lo = 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   507
         if lo > up then () else (f lo; loop (lo + n))
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   508
       in loop lo end)
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   509
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   510
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   511
fun step_simp i = 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   512
  (print ((Int.toString i) ^ ": ") ;
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   513
   time (lexing_simp while_regs) (string_repeat prog2 i)); 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   514
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   515
fun step_simp2 i = 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   516
  (print ((Int.toString i) ^ ": ") ;
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   517
   time (lexing_simp2 while_regs) (string_repeat prog2 i));
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   518
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   519
fun step_acc i = 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   520
  (print ((Int.toString i) ^ ": ") ;
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   521
   time (lexing_acc while_regs) (string_repeat prog2 i));
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   522
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   523
fun step_acc2 i = 
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   524
  (print ((Int.toString i) ^ ": ") ;
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   525
   time (lexing_acc2 while_regs) (string_repeat prog2 i));
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   526
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   527
fun astep_basic i = 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   528
  (print ((Int.toString i) ^ ": ") ;
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   529
   time (alexing while_regs) (string_repeat prog2 i)); 
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   530
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   531
fun astep_simp i = 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   532
  (print ((Int.toString i) ^ ": ") ;
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   533
   time (alexing_simp while_regs) (string_repeat prog2 i)); 
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   534
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   535
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   536
(*
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   537
val main1 = forby 1000 (1000 to 5000) step_simp;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   538
print "\n";
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   539
val main2 = forby 1000 (1000 to 5000) step_simp2;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   540
print "\n";
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   541
val main3 = forby 1000 (1000 to 5000) step_acc;
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   542
print "\n";
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   543
val main4 = forby 1000 (1000 to 5000) step_acc2; 
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   544
*)
3
94824659f6d7 added all toy implementations
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   545
159
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   546
print "\n";
940530087f30 updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 156
diff changeset
   547
val main5 = forby 1 (1 to 5) astep_simp;