src/Pure/pattern.ML
changeset 14787 724ce6e574e3
parent 14643 130076a81b84
child 14861 ca5cae7fb65a
equal deleted inserted replaced
14786:24a7bc97a27a 14787:724ce6e574e3
    13 *)
    13 *)
    14 
    14 
    15 infix aeconv;
    15 infix aeconv;
    16 
    16 
    17 signature PATTERN =
    17 signature PATTERN =
    18   sig
    18 sig
    19   type type_sig
       
    20   type sg
       
    21   type env
       
    22   val trace_unify_fail  : bool ref
    19   val trace_unify_fail  : bool ref
    23   val aeconv            : term * term -> bool
    20   val aeconv            : term * term -> bool
    24   val eta_contract      : term -> term
    21   val eta_contract      : term -> term
    25   val eta_long          : typ list -> term -> term
    22   val eta_long          : typ list -> term -> term
    26   val beta_eta_contract : term -> term
    23   val beta_eta_contract : term -> term
    27   val eta_contract_atom : term -> term
    24   val eta_contract_atom : term -> term
    28   val match             : type_sig -> term * term
    25   val match             : Type.tsig -> term * term
    29                           -> (indexname*typ)list * (indexname*term)list
    26                           -> (indexname*typ)list * (indexname*term)list
    30   val first_order_match : type_sig -> term * term
    27   val first_order_match : Type.tsig -> term * term
    31                           -> (indexname*typ)list * (indexname*term)list
    28                           -> (indexname*typ)list * (indexname*term)list
    32   val matches           : type_sig -> term * term -> bool
    29   val matches           : Type.tsig -> term * term -> bool
    33   val matches_subterm   : type_sig -> term * term -> bool
    30   val matches_subterm   : Type.tsig -> term * term -> bool
    34   val unify             : sg * env * (term * term)list -> env
    31   val unify             : Sign.sg * Envir.env * (term * term)list -> Envir.env
    35   val first_order       : term -> bool
    32   val first_order       : term -> bool
    36   val pattern           : term -> bool
    33   val pattern           : term -> bool
    37   val rewrite_term      : type_sig -> (term * term) list -> (term -> term option) list
    34   val rewrite_term      : Type.tsig -> (term * term) list -> (term -> term option) list
    38                           -> term -> term
    35                           -> term -> term
    39   exception Unif
    36   exception Unif
    40   exception MATCH
    37   exception MATCH
    41   exception Pattern
    38   exception Pattern
    42   end;
    39 end;
    43 
    40 
    44 structure Pattern : PATTERN =
    41 structure Pattern : PATTERN =
    45 struct
    42 struct
    46 
       
    47 type type_sig = Type.type_sig
       
    48 type sg = Sign.sg
       
    49 type env = Envir.env
       
    50 
    43 
    51 exception Unif;
    44 exception Unif;
    52 exception Pattern;
    45 exception Pattern;
    53 
    46 
    54 val trace_unify_fail = ref false;
    47 val trace_unify_fail = ref false;
   236                      fun lam(is) = mkabs(binders,is,app(H,map (idx is) ks));
   229                      fun lam(is) = mkabs(binders,is,app(H,map (idx is) ks));
   237                  in Envir.update((G,lam js), Envir.update((F,lam is),env'))
   230                  in Envir.update((G,lam js), Envir.update((F,lam is),env'))
   238                  end;
   231                  end;
   239   in if xless(G,F) then ff(F,Fty,is,G,Gty,js) else ff(G,Gty,js,F,Fty,is) end
   232   in if xless(G,F) then ff(F,Fty,is,G,Gty,js) else ff(G,Gty,js,F,Fty,is) end
   240 
   233 
   241 val tsgr = ref(Type.tsig0);
   234 val tsgr = ref(Type.empty_tsig);
   242 
   235 
   243 fun unify_types(T,U, env as Envir.Envir{asol,iTs,maxidx}) =
   236 fun unify_types(T,U, env as Envir.Envir{asol,iTs,maxidx}) =
   244   if T=U then env
   237   if T=U then env
   245   else let val (iTs',maxidx') = Type.unify (!tsgr) (iTs, maxidx) (U, T)
   238   else let val (iTs',maxidx') = Type.unify (!tsgr) (iTs, maxidx) (U, T)
   246        in Envir.Envir{asol=asol,maxidx=maxidx',iTs=iTs'} end
   239        in Envir.Envir{asol=asol,maxidx=maxidx',iTs=iTs'} end
   294 (*Eta-contract a term (fully)*)
   287 (*Eta-contract a term (fully)*)
   295 
   288 
   296 fun eta_contract t =
   289 fun eta_contract t =
   297   let
   290   let
   298     exception SAME;
   291     exception SAME;
   299     fun eta (Abs (a, T, body)) = 
   292     fun eta (Abs (a, T, body)) =
   300       ((case eta body of
   293       ((case eta body of
   301           body' as (f $ Bound 0) => 
   294           body' as (f $ Bound 0) =>
   302 	    if loose_bvar1 (f, 0) then Abs(a, T, body')
   295             if loose_bvar1 (f, 0) then Abs(a, T, body')
   303 	    else incr_boundvars ~1 f
   296             else incr_boundvars ~1 f
   304         | body' => Abs (a, T, body')) handle SAME =>
   297         | body' => Abs (a, T, body')) handle SAME =>
   305        (case body of
   298        (case body of
   306           (f $ Bound 0) => 
   299           (f $ Bound 0) =>
   307 	    if loose_bvar1 (f, 0) then raise SAME
   300             if loose_bvar1 (f, 0) then raise SAME
   308 	    else incr_boundvars ~1 f
   301             else incr_boundvars ~1 f
   309         | _ => raise SAME))
   302         | _ => raise SAME))
   310       | eta (f $ t) =
   303       | eta (f $ t) =
   311           (let val f' = eta f
   304           (let val f' = eta f
   312            in f' $ etah t end handle SAME => f $ eta t)
   305            in f' $ etah t end handle SAME => f $ eta t)
   313       | eta _ = raise SAME
   306       | eta _ = raise SAME
   331 
   324 
   332 (* put a term into eta long beta normal form *)
   325 (* put a term into eta long beta normal form *)
   333 fun eta_long Ts (Abs (s, T, t)) = Abs (s, T, eta_long (T :: Ts) t)
   326 fun eta_long Ts (Abs (s, T, t)) = Abs (s, T, eta_long (T :: Ts) t)
   334   | eta_long Ts t = (case strip_comb t of
   327   | eta_long Ts t = (case strip_comb t of
   335       (Abs _, _) => eta_long Ts (Envir.beta_norm t)
   328       (Abs _, _) => eta_long Ts (Envir.beta_norm t)
   336     | (u, ts) => 
   329     | (u, ts) =>
   337       let
   330       let
   338         val Us = binder_types (fastype_of1 (Ts, t));
   331         val Us = binder_types (fastype_of1 (Ts, t));
   339         val i = length Us
   332         val i = length Us
   340       in list_abs (map (pair "x") Us,
   333       in list_abs (map (pair "x") Us,
   341         list_comb (incr_boundvars i u, map (eta_long (rev Us @ Ts))
   334         list_comb (incr_boundvars i u, map (eta_long (rev Us @ Ts))
   522 
   515 
   523     and rew2 skel (tm1 $ tm2) = (case tm1 of
   516     and rew2 skel (tm1 $ tm2) = (case tm1 of
   524             Abs (_, _, body) =>
   517             Abs (_, _, body) =>
   525               let val tm' = subst_bound (tm2, body)
   518               let val tm' = subst_bound (tm2, body)
   526               in Some (if_none (rew2 skel0 tm') tm') end
   519               in Some (if_none (rew2 skel0 tm') tm') end
   527           | _ => 
   520           | _ =>
   528             let val (skel1, skel2) = (case skel of
   521             let val (skel1, skel2) = (case skel of
   529                 skel1 $ skel2 => (skel1, skel2)
   522                 skel1 $ skel2 => (skel1, skel2)
   530               | _ => (skel0, skel0))
   523               | _ => (skel0, skel0))
   531             in case rew1 skel1 tm1 of
   524             in case rew1 skel1 tm1 of
   532                 Some tm1' => (case rew1 skel2 tm2 of
   525                 Some tm1' => (case rew1 skel2 tm2 of