progs/sml/re.ML
changeset 317 db0ff630bbb7
parent 156 6a43ea9305ba
child 359 fedc16924b76
--- a/progs/sml/re.ML	Sat Mar 16 11:15:22 2019 +0000
+++ b/progs/sml/re.ML	Thu Apr 11 17:37:00 2019 +0100
@@ -237,7 +237,6 @@
   | RECD(x, r1) => der_simp c r1 
 
 
-
 (* matcher function *)
 fun matcher r s = nullable(ders (explode s) r)
 
@@ -251,6 +250,14 @@
 fun lexing r s = lex r (explode s)
 
 (* lexing with simplification *)
+
+fun fst (a, b) = a
+
+fun ders_simp r s = case s of 
+  [] => r
+| c::s => ders_simp (fst (simp (der c r))) s
+
+
 fun lex_simp r s = case s of 
     [] => if (nullable r) then mkeps r else raise LexError
   | c::cs => 
@@ -260,6 +267,7 @@
 
 fun lexing_simp r s = lex_simp r (explode s)
 
+(* does derivatives and simplificatiomn in one step *)
 fun lex_simp2 r s = case s of 
     [] => if (nullable r) then mkeps r else raise LexError
   | c::cs => 
@@ -269,6 +277,7 @@
 
 fun lexing_simp2 r s = lex_simp2 r (explode s)
 
+(* uses an accumulator for the rectification functions *)
 fun lex_acc r s f = case s of 
     [] => if (nullable r) then f (mkeps r) else raise LexError
   | c::cs => 
@@ -285,7 +294,7 @@
       lex_acc2 r_simp cs (fn v => f (inj r c (f_simp v)))
     end
 
-fun lexing_acc2 r s  = lex_acc2 r (explode s) (f_id)
+fun lexing_acc2 r s  = lex_acc2 r (explode s) f_id
 
 
 (* Lexing rules for a small WHILE language *)
@@ -317,11 +326,12 @@
 (* Some Tests
   ============ *)
 
+
 fun time f x =
   let
   val t_start = Timer.startCPUTimer()
-  val f_x = (f x; f x; f x; f x; f x; f x; f x; f x; f x; f x)
-  val t_end = Time.toReal(#usr(Timer.checkCPUTimer(t_start))) / 10.0
+  val f_x = (f x; f x; f x; f x; f x)
+  val t_end = Time.toReal(#usr(Timer.checkCPUTimer(t_start))) / 5.0
 in
   (print ((Real.toString t_end) ^ "\n"); f_x)
 end
@@ -374,12 +384,21 @@
    "}"];
 
 
+print("The prog2 string is of length: >>" ^ (PolyML.makestring (String.size prog2)) ^ "<<\n");
+
 let 
   val tst = (lexing_simp while_regs prog2 = lexing_acc while_regs prog2)
 in
   print("Sanity test: >>" ^ (PolyML.makestring tst) ^ "<<\n")
 end;
 
+print("Size after 50: " ^ 
+  PolyML.makestring(size (ders_simp while_regs (explode (string_repeat prog2 50)))) ^ "\n");
+
+print("Size after 5000: " ^ 
+  PolyML.makestring(size (ders_simp while_regs (explode (string_repeat prog2 5000)))) ^ "\n");
+
+
 (* loops in ML *)
 datatype for = to of int * int
 infix to 
@@ -389,38 +408,47 @@
     (fn f => 
        let fun loop lo = 
          if lo > up then () else (f lo; loop (lo + 1))
-       in loop lo end)
+       in loop lo end);
 
 fun forby n =
   fn lo to up =>
     (fn f => 
        let fun loop lo = 
          if lo > up then () else (f lo; loop (lo + n))
-       in loop lo end)
+       in loop lo end);
 
 
 fun step_simp i = 
   (print ((Int.toString i) ^ ": ") ;
-   time (lexing_simp while_regs) (string_repeat prog2 i)) 
+   time (lexing_simp while_regs) (string_repeat prog2 i)); 
 
 fun step_simp2 i = 
   (print ((Int.toString i) ^ ": ") ;
-   time (lexing_simp2 while_regs) (string_repeat prog2 i)) 
+   time (lexing_simp2 while_regs) (string_repeat prog2 i)); 
 
 fun step_acc i = 
   (print ((Int.toString i) ^ ": ") ;
-   time (lexing_acc while_regs) (string_repeat prog2 i))
+   time (lexing_acc while_regs) (string_repeat prog2 i));
 
 fun step_acc2 i = 
   (print ((Int.toString i) ^ ": ") ;
-   time (lexing_acc2 while_regs) (string_repeat prog2 i)) 
+   time (lexing_acc2 while_regs) (string_repeat prog2 i)); 
 
-val main1 = forby 1000 (1000 to 5000) step_simp;
-print "\n";
-val main2 = forby 1000 (1000 to 5000) step_simp2;
-print "\n";
-val main3 = forby 1000 (1000 to 5000) step_acc;
-print "\n";
-val main4 = forby 1000 (1000 to 5000) step_acc2; 
 
 
+print("\nTest step_simp\n");
+val main1 = forby 1000 (1000 to 5000) step_simp;
+
+print("\nTest step_simp2\n");
+val main2 = forby 1000 (1000 to 5000) step_simp2;
+
+(*
+print("\nTest step_acc\n"); 
+val main3 = forby 1000 (1000 to 5000) step_acc;
+
+print("\nTest step_acc2\n");
+val main4 = forby 1000 (1000 to 5000) step_acc2;
+*)
+
+
+