src/Tools/nbe.ML
changeset 40730 2aa0390a2da7
parent 40564 6827505e96e1
child 41037 6d6f23b3a879
     1.1 --- a/src/Tools/nbe.ML	Fri Nov 26 18:07:00 2010 +0100
     1.2 +++ b/src/Tools/nbe.ML	Fri Nov 26 23:13:58 2010 +0100
     1.3 @@ -18,7 +18,7 @@
     1.4    val apps: Univ -> Univ list -> Univ        (*explicit applications*)
     1.5    val abss: int -> (Univ list -> Univ) -> Univ
     1.6                                               (*abstractions as closures*)
     1.7 -  val same: Univ -> Univ -> bool
     1.8 +  val eq_Univ: Univ * Univ -> bool
     1.9  
    1.10    val put_result: (unit -> Univ list -> Univ list) -> Proof.context -> Proof.context
    1.11    val trace: bool Unsynchronized.ref
    1.12 @@ -170,11 +170,6 @@
    1.13    | Abs of (int * (Univ list -> Univ)) * Univ list
    1.14                                         (*abstractions as closures*);
    1.15  
    1.16 -fun same (Const (k, xs)) (Const (l, ys)) = k = l andalso forall2 same xs ys
    1.17 -  | same (DFree (s, k)) (DFree (t, l)) = s = t andalso k = l
    1.18 -  | same (BVar (k, xs)) (BVar (l, ys)) = k = l andalso forall2 same xs ys
    1.19 -  | same _ _ = false;
    1.20 -
    1.21  
    1.22  (* constructor functions *)
    1.23  
    1.24 @@ -188,6 +183,28 @@
    1.25    | apps (Const (name, xs)) ys = Const (name, ys @ xs)
    1.26    | apps (BVar (n, xs)) ys = BVar (n, ys @ xs);
    1.27  
    1.28 +fun satisfy_abs (abs as ((n, f), xs)) some_k =
    1.29 +  let
    1.30 +    val ys = case some_k
    1.31 +     of NONE => replicate n (BVar (0, []))
    1.32 +      | SOME k => map_range (fn l => BVar (k + l, [])) n;
    1.33 +  in (apps (Abs abs) ys, ys) end;
    1.34 +
    1.35 +fun maxidx (Const (_, xs)) = fold maxidx xs
    1.36 +  | maxidx (DFree _) = I
    1.37 +  | maxidx (BVar (l, xs)) = fold maxidx xs #> Integer.max (l + 1)
    1.38 +  | maxidx (Abs abs) = maxidx (fst (satisfy_abs abs NONE));
    1.39 +
    1.40 +fun eq_Univ (Const (k, xs), Const (l, ys)) = k = l andalso eq_list eq_Univ (xs, ys)
    1.41 +  | eq_Univ (DFree (s, k), DFree (t, l)) = s = t andalso k = l
    1.42 +  | eq_Univ (BVar (k, xs), BVar (l, ys)) = k = l andalso eq_list eq_Univ (xs, ys)
    1.43 +  | eq_Univ (x as Abs abs, y) =
    1.44 +      let
    1.45 +        val (x, ys) = satisfy_abs abs ((SOME o maxidx x o maxidx y) 0)
    1.46 +      in eq_Univ (x, apps y ys) end
    1.47 +  | eq_Univ (x, y as Abs _) = eq_Univ (y, x)
    1.48 +  | eq_Univ _ = false;
    1.49 +
    1.50  
    1.51  (** assembling and compiling ML code from terms **)
    1.52  
    1.53 @@ -241,7 +258,7 @@
    1.54    val name_const =  prefix ^ "Const";
    1.55    val name_abss =   prefix ^ "abss";
    1.56    val name_apps =   prefix ^ "apps";
    1.57 -  val name_same =   prefix ^ "same";
    1.58 +  val name_eq =     prefix ^ "eq_Univ";
    1.59  in
    1.60  
    1.61  val univs_cookie = (Univs.get, put_result, name_put);
    1.62 @@ -267,7 +284,7 @@
    1.63  fun nbe_abss 0 f = f `$` ml_list []
    1.64    | nbe_abss n f = name_abss `$$` [string_of_int n, f];
    1.65  
    1.66 -fun nbe_same v1 v2 = "(" ^ name_same ^ " " ^ nbe_bound v1 ^ " " ^ nbe_bound v2 ^ ")";
    1.67 +fun nbe_eq (v1, v2) = "(" ^ name_eq ^ " (" ^ nbe_bound v1 ^ ", " ^ nbe_bound v2 ^ "))";
    1.68  
    1.69  end;
    1.70  
    1.71 @@ -353,7 +370,7 @@
    1.72          val (samepairs, args') = subst_nonlin_vars args;
    1.73          val s_args = map assemble_arg args';
    1.74          val s_rhs = if null samepairs then assemble_rhs rhs
    1.75 -          else ml_if (ml_and (map (uncurry nbe_same) samepairs))
    1.76 +          else ml_if (ml_and (map nbe_eq samepairs))
    1.77              (assemble_rhs rhs) default_rhs;
    1.78          val eqns = if is_eval then
    1.79              [([ml_list (rev (dicts @ s_args))], s_rhs)]