src/Pure/envir.ML
changeset 11513 2f6fe5b01521
parent 10485 f1576723371f
child 12231 4a25f04bea61
equal deleted inserted replaced
11512:da3a96ab5630 11513:2f6fe5b01521
     9 *)
     9 *)
    10 
    10 
    11 signature ENVIR =
    11 signature ENVIR =
    12 sig
    12 sig
    13   datatype env = Envir of {asol: term Vartab.table, iTs: typ Vartab.table, maxidx: int}
    13   datatype env = Envir of {asol: term Vartab.table, iTs: typ Vartab.table, maxidx: int}
       
    14   exception SAME
    14   val genvars: string -> env * typ list -> env * term list
    15   val genvars: string -> env * typ list -> env * term list
    15   val genvar: string -> env * typ -> env * term
    16   val genvar: string -> env * typ -> env * term
    16   val lookup: env * indexname -> term option
    17   val lookup: env * indexname -> term option
    17   val update: (indexname * term) * env -> env
    18   val update: (indexname * term) * env -> env
    18   val empty: int -> env
    19   val empty: int -> env
    19   val is_empty: env -> bool
    20   val is_empty: env -> bool
    20   val above: int * env -> bool
    21   val above: int * env -> bool
    21   val vupdate: (indexname * term) * env -> env
    22   val vupdate: (indexname * term) * env -> env
    22   val alist_of: env -> (indexname * term) list
    23   val alist_of: env -> (indexname * term) list
    23   val norm_term: env -> term -> term
    24   val norm_term: env -> term -> term
       
    25   val norm_term_same: env -> term -> term
       
    26   val norm_type: env -> typ -> typ
       
    27   val norm_type_same: env -> typ -> typ
       
    28   val norm_types_same: env -> typ list -> typ list
    24   val beta_norm: term -> term
    29   val beta_norm: term -> term
    25 end;
    30 end;
    26 
    31 
    27 structure Envir : ENVIR =
    32 structure Envir : ENVIR =
    28 struct
    33 struct
    91      Does not check types, so could loop. ***)
    96      Does not check types, so could loop. ***)
    92 
    97 
    93 (*raised when norm has no effect on a term, to do sharing instead of copying*)
    98 (*raised when norm has no effect on a term, to do sharing instead of copying*)
    94 exception SAME;
    99 exception SAME;
    95 
   100 
    96 fun norm_term1 (asol,t) : term =
   101 fun norm_term1 same (asol,t) : term =
    97   let fun norm (Var (w,T)) =
   102   let fun norm (Var (w,T)) =
    98             (case Vartab.lookup (asol, w) of
   103             (case Vartab.lookup (asol, w) of
    99                 Some u => (norm u handle SAME => u)
   104                 Some u => (norm u handle SAME => u)
   100               | None   => raise SAME)
   105               | None   => raise SAME)
   101         | norm (Abs(a,T,body)) =  Abs(a, T, norm body)
   106         | norm (Abs(a,T,body)) =  Abs(a, T, norm body)
   105                Abs(_,_,body) => normh(subst_bound (t, body))
   110                Abs(_,_,body) => normh(subst_bound (t, body))
   106              | nf => nf $ (norm t handle SAME => t))
   111              | nf => nf $ (norm t handle SAME => t))
   107             handle SAME => f $ norm t)
   112             handle SAME => f $ norm t)
   108         | norm _ =  raise SAME
   113         | norm _ =  raise SAME
   109       and normh t = norm t handle SAME => t
   114       and normh t = norm t handle SAME => t
   110   in normh t end
   115   in (if same then norm else normh) t end
   111 
   116 
   112 and norm_term2(asol,iTs,t) : term =
   117 fun normT iTs (Type (a, Ts)) = Type (a, normTs iTs Ts)
   113   let fun normT(Type(a,Ts)) = Type(a, normTs Ts)
   118   | normT iTs (TFree _) = raise SAME
   114         | normT(TFree _) = raise SAME
   119   | normT iTs (TVar(v, _)) = (case Vartab.lookup (iTs, v) of
   115         | normT(TVar(v,_)) = (case Vartab.lookup (iTs, v) of
   120           Some U => normTh iTs U
   116                 Some(U) => normTh U
   121         | None => raise SAME)
   117               | None => raise SAME)
   122 and normTh iTs T = ((normT iTs T) handle SAME => T)
   118       and normTh T = ((normT T) handle SAME => T)
   123 and normTs iTs [] = raise SAME
   119       and normTs([]) = raise SAME
   124   | normTs iTs (T :: Ts) =
   120         | normTs(T::Ts) = ((normT T :: (normTs Ts handle SAME => Ts))
   125       ((normT iTs T :: (normTs iTs Ts handle SAME => Ts))
   121                            handle SAME => T :: normTs Ts)
   126        handle SAME => T :: normTs iTs Ts);
   122       and norm(Const(a,T)) = Const(a, normT T)
   127 
   123         | norm(Free(a,T)) = Free(a, normT T)
   128 fun norm_term2 same (asol, iTs, t) : term =
   124         | norm(Var (w,T)) =
   129   let fun norm (Const (a, T)) = Const(a, normT iTs T)
       
   130         | norm (Free (a, T)) = Free(a, normT iTs T)
       
   131         | norm (Var (w, T)) =
   125             (case Vartab.lookup (asol, w) of
   132             (case Vartab.lookup (asol, w) of
   126                 Some u => normh u
   133                 Some u => normh u
   127               | None   => Var(w,normT T))
   134               | None   => Var(w, normT iTs T))
   128         | norm(Abs(a,T,body)) =
   135         | norm (Abs (a, T, body)) =
   129               (Abs(a,normT T,normh body) handle SAME => Abs(a, T, norm body))
   136                (Abs (a, normT iTs T, normh body) handle SAME => Abs (a, T, norm body))
   130         | norm(Abs(_,_,body) $ t) = normh(subst_bound (t, body))
   137         | norm (Abs(_, _, body) $ t) = normh (subst_bound (t, body))
   131         | norm(f $ t) =
   138         | norm (f $ t) =
   132             ((case norm f of
   139             ((case norm f of
   133                Abs(_,_,body) => normh(subst_bound (t, body))
   140                Abs(_, _, body) => normh (subst_bound (t, body))
   134              | nf => nf $ normh t)
   141              | nf => nf $ normh t)
   135             handle SAME => f $ norm t)
   142             handle SAME => f $ norm t)
   136         | norm _ =  raise SAME
   143         | norm _ =  raise SAME
   137       and normh t = (norm t) handle SAME => t
   144       and normh t = (norm t) handle SAME => t
   138   in normh t end;
   145   in (if same then norm else normh) t end;
   139 
   146 
   140 fun norm_term (env as Envir{asol,iTs,...}) t : term =
   147 fun gen_norm_term same (env as Envir{asol,iTs,...}) t : term =
   141   if Vartab.is_empty iTs then norm_term1(asol, t) else norm_term2(asol,iTs, t)
   148   if Vartab.is_empty iTs then norm_term1 same (asol, t)
       
   149   else norm_term2 same (asol, iTs, t);
       
   150 
       
   151 val norm_term = gen_norm_term false;
       
   152 val norm_term_same = gen_norm_term true;
   142 
   153 
   143 val beta_norm = norm_term (empty 0);
   154 val beta_norm = norm_term (empty 0);
   144 
   155 
       
   156 fun norm_type (Envir {iTs, ...}) = normTh iTs;
       
   157 fun norm_type_same (Envir {iTs, ...}) =
       
   158   if Vartab.is_empty iTs then raise SAME else normT iTs;
       
   159 
       
   160 fun norm_types_same (Envir {iTs, ...}) =
       
   161   if Vartab.is_empty iTs then raise SAME else normTs iTs;
       
   162 
       
   163 
   145 end;
   164 end;