src/Pure/envir.ML
changeset 12231 4a25f04bea61
parent 11513 2f6fe5b01521
child 12496 0a9bd5034e05
equal deleted inserted replaced
12230:b06cc3834ee5 12231:4a25f04bea61
    25   val norm_term_same: env -> term -> term
    25   val norm_term_same: env -> term -> term
    26   val norm_type: env -> typ -> typ
    26   val norm_type: env -> typ -> typ
    27   val norm_type_same: env -> typ -> typ
    27   val norm_type_same: env -> typ -> typ
    28   val norm_types_same: env -> typ list -> typ list
    28   val norm_types_same: env -> typ list -> typ list
    29   val beta_norm: term -> term
    29   val beta_norm: term -> term
       
    30   val head_norm: env -> term -> term
       
    31   val fastype: env -> typ list -> term -> typ
    30 end;
    32 end;
    31 
    33 
    32 structure Envir : ENVIR =
    34 structure Envir : ENVIR =
    33 struct
    35 struct
    34 
    36 
   159 
   161 
   160 fun norm_types_same (Envir {iTs, ...}) =
   162 fun norm_types_same (Envir {iTs, ...}) =
   161   if Vartab.is_empty iTs then raise SAME else normTs iTs;
   163   if Vartab.is_empty iTs then raise SAME else normTs iTs;
   162 
   164 
   163 
   165 
       
   166 (*Put a term into head normal form for unification.*)
       
   167 
       
   168 fun head_norm env t =
       
   169   let
       
   170     fun hnorm (Var (v, T)) = (case lookup (env, v) of
       
   171           Some u => head_norm env u
       
   172         | None => raise SAME)
       
   173       | hnorm (Abs (a, T, body)) =  Abs (a, T, hnorm body)
       
   174       | hnorm (Abs (_, _, body) $ t) =
       
   175           head_norm env (subst_bound (t, body))
       
   176       | hnorm (f $ t) = (case hnorm f of
       
   177           Abs (_, _, body) => head_norm env (subst_bound (t, body))
       
   178         | nf => nf $ t)
       
   179 	  | hnorm _ =  raise SAME
       
   180   in hnorm t handle SAME => t end;
       
   181 
       
   182 
       
   183 (*finds type of term without checking that combinations are consistent
       
   184   Ts holds types of bound variables*)
       
   185 fun fastype (Envir {iTs, ...}) =
       
   186 let val funerr = "fastype: expected function type";
       
   187     fun fast Ts (f $ u) =
       
   188 	(case fast Ts f of
       
   189 	   Type ("fun", [_, T]) => T
       
   190 	 | TVar(ixn, _) =>
       
   191 		(case Vartab.lookup (iTs, ixn) of
       
   192 		   Some (Type ("fun", [_, T])) => T
       
   193 		 | _ => raise TERM (funerr, [f $ u]))
       
   194 	 | _ => raise TERM (funerr, [f $ u]))
       
   195       | fast Ts (Const (_, T)) = T
       
   196       | fast Ts (Free (_, T)) = T
       
   197       | fast Ts (Bound i) =
       
   198 	(nth_elem (i, Ts)
       
   199   	 handle LIST _=> raise TERM ("fastype: Bound", [Bound i]))
       
   200       | fast Ts (Var (_, T)) = T 
       
   201       | fast Ts (Abs (_, T, u)) = T --> fast (T :: Ts) u
       
   202 in fast end;
       
   203 
   164 end;
   204 end;