src/Pure/envir.ML
changeset 15531 08c8dad8e399
parent 12496 0a9bd5034e05
child 15570 8d8c70b41bab
equal deleted inserted replaced
15530:6f43714517ee 15531:08c8dad8e399
    72 fun is_empty (Envir {asol, iTs, ...}) = Vartab.is_empty asol andalso Vartab.is_empty iTs;
    72 fun is_empty (Envir {asol, iTs, ...}) = Vartab.is_empty asol andalso Vartab.is_empty iTs;
    73 
    73 
    74 (*Determine if the least index updated exceeds lim*)
    74 (*Determine if the least index updated exceeds lim*)
    75 fun above (lim, Envir {asol, iTs, ...}) =
    75 fun above (lim, Envir {asol, iTs, ...}) =
    76   (case (Vartab.min_key asol, Vartab.min_key iTs) of
    76   (case (Vartab.min_key asol, Vartab.min_key iTs) of
    77      (None, None) => true
    77      (NONE, NONE) => true
    78    | (Some (_, i), None) => i > lim
    78    | (SOME (_, i), NONE) => i > lim
    79    | (None, Some (_, i')) => i' > lim
    79    | (NONE, SOME (_, i')) => i' > lim
    80    | (Some (_, i), Some (_, i')) => i > lim andalso i' > lim);
    80    | (SOME (_, i), SOME (_, i')) => i > lim andalso i' > lim);
    81 
    81 
    82 (*Update, checking Var-Var assignments: try to suppress higher indexes*)
    82 (*Update, checking Var-Var assignments: try to suppress higher indexes*)
    83 fun vupdate((a,t), env) = case t of
    83 fun vupdate((a,t), env) = case t of
    84       Var(name',T) =>
    84       Var(name',T) =>
    85         if a=name' then env     (*cycle!*)
    85         if a=name' then env     (*cycle!*)
    86         else if xless(a, name')  then
    86         else if xless(a, name')  then
    87            (case lookup(env,name') of  (*if already assigned, chase*)
    87            (case lookup(env,name') of  (*if already assigned, chase*)
    88                 None => update((name', Var(a,T)), env)
    88                 NONE => update((name', Var(a,T)), env)
    89               | Some u => vupdate((a,u), env))
    89               | SOME u => vupdate((a,u), env))
    90         else update((a,t), env)
    90         else update((a,t), env)
    91     | _ => update((a,t), env);
    91     | _ => update((a,t), env);
    92 
    92 
    93 
    93 
    94 (*Convert environment to alist*)
    94 (*Convert environment to alist*)
   103 exception SAME;
   103 exception SAME;
   104 
   104 
   105 fun norm_term1 same (asol,t) : term =
   105 fun norm_term1 same (asol,t) : term =
   106   let fun norm (Var (w,T)) =
   106   let fun norm (Var (w,T)) =
   107             (case Vartab.lookup (asol, w) of
   107             (case Vartab.lookup (asol, w) of
   108                 Some u => (norm u handle SAME => u)
   108                 SOME u => (norm u handle SAME => u)
   109               | None   => raise SAME)
   109               | NONE   => raise SAME)
   110         | norm (Abs(a,T,body)) =  Abs(a, T, norm body)
   110         | norm (Abs(a,T,body)) =  Abs(a, T, norm body)
   111         | norm (Abs(_,_,body) $ t) = normh(subst_bound (t, body))
   111         | norm (Abs(_,_,body) $ t) = normh(subst_bound (t, body))
   112         | norm (f $ t) =
   112         | norm (f $ t) =
   113             ((case norm f of
   113             ((case norm f of
   114                Abs(_,_,body) => normh(subst_bound (t, body))
   114                Abs(_,_,body) => normh(subst_bound (t, body))
   119   in (if same then norm else normh) t end
   119   in (if same then norm else normh) t end
   120 
   120 
   121 fun normT iTs (Type (a, Ts)) = Type (a, normTs iTs Ts)
   121 fun normT iTs (Type (a, Ts)) = Type (a, normTs iTs Ts)
   122   | normT iTs (TFree _) = raise SAME
   122   | normT iTs (TFree _) = raise SAME
   123   | normT iTs (TVar(v, _)) = (case Vartab.lookup (iTs, v) of
   123   | normT iTs (TVar(v, _)) = (case Vartab.lookup (iTs, v) of
   124           Some U => normTh iTs U
   124           SOME U => normTh iTs U
   125         | None => raise SAME)
   125         | NONE => raise SAME)
   126 and normTh iTs T = ((normT iTs T) handle SAME => T)
   126 and normTh iTs T = ((normT iTs T) handle SAME => T)
   127 and normTs iTs [] = raise SAME
   127 and normTs iTs [] = raise SAME
   128   | normTs iTs (T :: Ts) =
   128   | normTs iTs (T :: Ts) =
   129       ((normT iTs T :: (normTs iTs Ts handle SAME => Ts))
   129       ((normT iTs T :: (normTs iTs Ts handle SAME => Ts))
   130        handle SAME => T :: normTs iTs Ts);
   130        handle SAME => T :: normTs iTs Ts);
   132 fun norm_term2 same (asol, iTs, t) : term =
   132 fun norm_term2 same (asol, iTs, t) : term =
   133   let fun norm (Const (a, T)) = Const(a, normT iTs T)
   133   let fun norm (Const (a, T)) = Const(a, normT iTs T)
   134         | norm (Free (a, T)) = Free(a, normT iTs T)
   134         | norm (Free (a, T)) = Free(a, normT iTs T)
   135         | norm (Var (w, T)) =
   135         | norm (Var (w, T)) =
   136             (case Vartab.lookup (asol, w) of
   136             (case Vartab.lookup (asol, w) of
   137                 Some u => normh u
   137                 SOME u => normh u
   138               | None   => Var(w, normT iTs T))
   138               | NONE   => Var(w, normT iTs T))
   139         | norm (Abs (a, T, body)) =
   139         | norm (Abs (a, T, body)) =
   140                (Abs (a, normT iTs T, normh body) handle SAME => Abs (a, T, norm body))
   140                (Abs (a, normT iTs T, normh body) handle SAME => Abs (a, T, norm body))
   141         | norm (Abs(_, _, body) $ t) = normh (subst_bound (t, body))
   141         | norm (Abs(_, _, body) $ t) = normh (subst_bound (t, body))
   142         | norm (f $ t) =
   142         | norm (f $ t) =
   143             ((case norm f of
   143             ((case norm f of
   168 (*Put a term into head normal form for unification.*)
   168 (*Put a term into head normal form for unification.*)
   169 
   169 
   170 fun head_norm env t =
   170 fun head_norm env t =
   171   let
   171   let
   172     fun hnorm (Var (v, T)) = (case lookup (env, v) of
   172     fun hnorm (Var (v, T)) = (case lookup (env, v) of
   173           Some u => head_norm env u
   173           SOME u => head_norm env u
   174         | None => raise SAME)
   174         | NONE => raise SAME)
   175       | hnorm (Abs (a, T, body)) =  Abs (a, T, hnorm body)
   175       | hnorm (Abs (a, T, body)) =  Abs (a, T, hnorm body)
   176       | hnorm (Abs (_, _, body) $ t) =
   176       | hnorm (Abs (_, _, body) $ t) =
   177           head_norm env (subst_bound (t, body))
   177           head_norm env (subst_bound (t, body))
   178       | hnorm (f $ t) = (case hnorm f of
   178       | hnorm (f $ t) = (case hnorm f of
   179           Abs (_, _, body) => head_norm env (subst_bound (t, body))
   179           Abs (_, _, body) => head_norm env (subst_bound (t, body))
   189     fun fast Ts (f $ u) =
   189     fun fast Ts (f $ u) =
   190 	(case fast Ts f of
   190 	(case fast Ts f of
   191 	   Type ("fun", [_, T]) => T
   191 	   Type ("fun", [_, T]) => T
   192 	 | TVar(ixn, _) =>
   192 	 | TVar(ixn, _) =>
   193 		(case Vartab.lookup (iTs, ixn) of
   193 		(case Vartab.lookup (iTs, ixn) of
   194 		   Some (Type ("fun", [_, T])) => T
   194 		   SOME (Type ("fun", [_, T])) => T
   195 		 | _ => raise TERM (funerr, [f $ u]))
   195 		 | _ => raise TERM (funerr, [f $ u]))
   196 	 | _ => raise TERM (funerr, [f $ u]))
   196 	 | _ => raise TERM (funerr, [f $ u]))
   197       | fast Ts (Const (_, T)) = T
   197       | fast Ts (Const (_, T)) = T
   198       | fast Ts (Free (_, T)) = T
   198       | fast Ts (Free (_, T)) = T
   199       | fast Ts (Bound i) =
   199       | fast Ts (Bound i) =