src/Pure/pattern.ML
changeset 15531 08c8dad8e399
parent 14981 e73f8140af78
child 15570 8d8c70b41bab
equal deleted inserted replaced
15530:6f43714517ee 15531:08c8dad8e399
    93        end
    93        end
    94   else ()
    94   else ()
    95 
    95 
    96 fun occurs(F,t,env) =
    96 fun occurs(F,t,env) =
    97     let fun occ(Var(G,_))   = (case Envir.lookup(env,G) of
    97     let fun occ(Var(G,_))   = (case Envir.lookup(env,G) of
    98                                  Some(t) => occ t
    98                                  SOME(t) => occ t
    99                                | None    => F=G)
    99                                | NONE    => F=G)
   100           | occ(t1$t2)      = occ t1 orelse occ t2
   100           | occ(t1$t2)      = occ t1 orelse occ t2
   101           | occ(Abs(_,_,t)) = occ t
   101           | occ(Abs(_,_,t)) = occ t
   102           | occ _           = false
   102           | occ _           = false
   103     in occ t end;
   103     in occ t end;
   104 
   104 
   357   let
   357   let
   358     fun mtch (instsp as (tyinsts,insts)) = fn
   358     fun mtch (instsp as (tyinsts,insts)) = fn
   359         (Var(ixn,T), t)  =>
   359         (Var(ixn,T), t)  =>
   360           if loose_bvar(t,0) then raise MATCH
   360           if loose_bvar(t,0) then raise MATCH
   361           else (case assoc_string_int(insts,ixn) of
   361           else (case assoc_string_int(insts,ixn) of
   362                   None => (typ_match tsig (tyinsts, (T, fastype_of t)),
   362                   NONE => (typ_match tsig (tyinsts, (T, fastype_of t)),
   363                            (ixn,t)::insts)
   363                            (ixn,t)::insts)
   364                 | Some u => if t aeconv u then instsp else raise MATCH)
   364                 | SOME u => if t aeconv u then instsp else raise MATCH)
   365       | (Free (a,T), Free (b,U)) =>
   365       | (Free (a,T), Free (b,U)) =>
   366           if a=b then (typ_match tsig (tyinsts,(T,U)), insts) else raise MATCH
   366           if a=b then (typ_match tsig (tyinsts,(T,U)), insts) else raise MATCH
   367       | (Const (a,T), Const (b,U))  =>
   367       | (Const (a,T), Const (b,U))  =>
   368           if a=b then (typ_match tsig (tyinsts,(T,U)), insts) else raise MATCH
   368           if a=b then (typ_match tsig (tyinsts,(T,U)), insts) else raise MATCH
   369       | (Bound i, Bound j)  =>  if  i=j  then  instsp  else raise MATCH
   369       | (Bound i, Bound j)  =>  if  i=j  then  instsp  else raise MATCH
   413               else rigrig1(typ_match tsg (iTs,(Ta,Tb)), oargs)
   413               else rigrig1(typ_match tsg (iTs,(Ta,Tb)), oargs)
   414     in case ph of
   414     in case ph of
   415          Var(ixn,_) =>
   415          Var(ixn,_) =>
   416            let val is = ints_of pargs
   416            let val is = ints_of pargs
   417            in case assoc_string_int(itms,ixn) of
   417            in case assoc_string_int(itms,ixn) of
   418                 None => (iTs,match_bind(itms,binders,ixn,is,obj))
   418                 NONE => (iTs,match_bind(itms,binders,ixn,is,obj))
   419               | Some u => if obj aeconv (red u is []) then env
   419               | SOME u => if obj aeconv (red u is []) then env
   420                           else raise MATCH
   420                           else raise MATCH
   421            end
   421            end
   422        | _ =>
   422        | _ =>
   423            let val (oh,oargs) = strip_comb obj
   423            let val (oh,oargs) = strip_comb obj
   424            in case (ph,oh) of
   424            in case (ph,oh) of
   484         val t' = subst_bound (Free (x', T), t);
   484         val t' = subst_bound (Free (x', T), t);
   485       in (fn u => Abs (x, T, abstract_over (Free (x', T), u)), t') end;
   485       in (fn u => Abs (x, T, abstract_over (Free (x', T), u)), t') end;
   486 
   486 
   487     fun match_rew tm (tm1, tm2) =
   487     fun match_rew tm (tm1, tm2) =
   488       let val rtm = if_none (Term.rename_abs tm1 tm tm2) tm2
   488       let val rtm = if_none (Term.rename_abs tm1 tm tm2) tm2
   489       in Some (subst_vars (match tsig (tm1, tm)) rtm, rtm)
   489       in SOME (subst_vars (match tsig (tm1, tm)) rtm, rtm)
   490         handle MATCH => None
   490         handle MATCH => NONE
   491       end;
   491       end;
   492 
   492 
   493     fun rew (Abs (_, _, body) $ t) = Some (subst_bound (t, body), skel0)
   493     fun rew (Abs (_, _, body) $ t) = SOME (subst_bound (t, body), skel0)
   494       | rew tm = (case get_first (match_rew tm) rules of
   494       | rew tm = (case get_first (match_rew tm) rules of
   495           None => apsome (rpair skel0) (get_first (fn p => p tm) procs)
   495           NONE => apsome (rpair skel0) (get_first (fn p => p tm) procs)
   496         | x => x);
   496         | x => x);
   497 
   497 
   498     fun rew1 (Var _) _ = None
   498     fun rew1 (Var _) _ = NONE
   499       | rew1 skel tm = (case rew2 skel tm of
   499       | rew1 skel tm = (case rew2 skel tm of
   500           Some tm1 => (case rew tm1 of
   500           SOME tm1 => (case rew tm1 of
   501               Some (tm2, skel') => Some (if_none (rew1 skel' tm2) tm2)
   501               SOME (tm2, skel') => SOME (if_none (rew1 skel' tm2) tm2)
   502             | None => Some tm1)
   502             | NONE => SOME tm1)
   503         | None => (case rew tm of
   503         | NONE => (case rew tm of
   504               Some (tm1, skel') => Some (if_none (rew1 skel' tm1) tm1)
   504               SOME (tm1, skel') => SOME (if_none (rew1 skel' tm1) tm1)
   505             | None => None))
   505             | NONE => NONE))
   506 
   506 
   507     and rew2 skel (tm1 $ tm2) = (case tm1 of
   507     and rew2 skel (tm1 $ tm2) = (case tm1 of
   508             Abs (_, _, body) =>
   508             Abs (_, _, body) =>
   509               let val tm' = subst_bound (tm2, body)
   509               let val tm' = subst_bound (tm2, body)
   510               in Some (if_none (rew2 skel0 tm') tm') end
   510               in SOME (if_none (rew2 skel0 tm') tm') end
   511           | _ =>
   511           | _ =>
   512             let val (skel1, skel2) = (case skel of
   512             let val (skel1, skel2) = (case skel of
   513                 skel1 $ skel2 => (skel1, skel2)
   513                 skel1 $ skel2 => (skel1, skel2)
   514               | _ => (skel0, skel0))
   514               | _ => (skel0, skel0))
   515             in case rew1 skel1 tm1 of
   515             in case rew1 skel1 tm1 of
   516                 Some tm1' => (case rew1 skel2 tm2 of
   516                 SOME tm1' => (case rew1 skel2 tm2 of
   517                     Some tm2' => Some (tm1' $ tm2')
   517                     SOME tm2' => SOME (tm1' $ tm2')
   518                   | None => Some (tm1' $ tm2))
   518                   | NONE => SOME (tm1' $ tm2))
   519               | None => (case rew1 skel2 tm2 of
   519               | NONE => (case rew1 skel2 tm2 of
   520                     Some tm2' => Some (tm1 $ tm2')
   520                     SOME tm2' => SOME (tm1 $ tm2')
   521                   | None => None)
   521                   | NONE => NONE)
   522             end)
   522             end)
   523       | rew2 skel (Abs (x, T, tm)) =
   523       | rew2 skel (Abs (x, T, tm)) =
   524           let
   524           let
   525             val (abs, tm') = variant_absfree (x, T, tm);
   525             val (abs, tm') = variant_absfree (x, T, tm);
   526             val skel' = (case skel of Abs (_, _, skel') => skel' | _ => skel0)
   526             val skel' = (case skel of Abs (_, _, skel') => skel' | _ => skel0)
   527           in case rew1 skel' tm' of
   527           in case rew1 skel' tm' of
   528               Some tm'' => Some (abs tm'')
   528               SOME tm'' => SOME (abs tm'')
   529             | None => None
   529             | NONE => NONE
   530           end
   530           end
   531       | rew2 _ _ = None
   531       | rew2 _ _ = NONE
   532 
   532 
   533   in if_none (rew1 skel0 tm) tm end;
   533   in if_none (rew1 skel0 tm) tm end;
   534 
   534 
   535 end;
   535 end;
   536 
   536