src/HOL/Tools/Quotient/quotient_term.ML
changeset 42361 23f352990944
parent 41451 892e67be8304
child 44413 80d460bc6fa8
equal deleted inserted replaced
42360:da8817d01e7c 42361:23f352990944
    62     AbsF => Const (@{const_name comp}, dummyT) $ trm1 $ trm2
    62     AbsF => Const (@{const_name comp}, dummyT) $ trm1 $ trm2
    63   | RepF => Const (@{const_name comp}, dummyT) $ trm2 $ trm1
    63   | RepF => Const (@{const_name comp}, dummyT) $ trm2 $ trm1
    64 
    64 
    65 fun get_mapfun ctxt s =
    65 fun get_mapfun ctxt s =
    66   let
    66   let
    67     val thy = ProofContext.theory_of ctxt
    67     val thy = Proof_Context.theory_of ctxt
    68     val mapfun = #mapfun (Quotient_Info.maps_lookup thy s) handle Quotient_Info.NotFound =>
    68     val mapfun = #mapfun (Quotient_Info.maps_lookup thy s) handle Quotient_Info.NotFound =>
    69       raise LIFT_MATCH ("No map function for type " ^ quote s ^ " found.")
    69       raise LIFT_MATCH ("No map function for type " ^ quote s ^ " found.")
    70   in
    70   in
    71     Const (mapfun, dummyT)
    71     Const (mapfun, dummyT)
    72   end
    72   end
    99 (* looks up the (varified) rty and qty for
    99 (* looks up the (varified) rty and qty for
   100    a quotient definition
   100    a quotient definition
   101 *)
   101 *)
   102 fun get_rty_qty ctxt s =
   102 fun get_rty_qty ctxt s =
   103   let
   103   let
   104     val thy = ProofContext.theory_of ctxt
   104     val thy = Proof_Context.theory_of ctxt
   105     val qdata = Quotient_Info.quotdata_lookup thy s handle Quotient_Info.NotFound =>
   105     val qdata = Quotient_Info.quotdata_lookup thy s handle Quotient_Info.NotFound =>
   106       raise LIFT_MATCH ("No quotient type " ^ quote s ^ " found.")
   106       raise LIFT_MATCH ("No quotient type " ^ quote s ^ " found.")
   107   in
   107   in
   108     (#rtyp qdata, #qtyp qdata)
   108     (#rtyp qdata, #qtyp qdata)
   109   end
   109   end
   120   end
   120   end
   121 
   121 
   122 (* matches a type pattern with a type *)
   122 (* matches a type pattern with a type *)
   123 fun match ctxt err ty_pat ty =
   123 fun match ctxt err ty_pat ty =
   124   let
   124   let
   125     val thy = ProofContext.theory_of ctxt
   125     val thy = Proof_Context.theory_of ctxt
   126   in
   126   in
   127     Sign.typ_match thy (ty_pat, ty) Vartab.empty
   127     Sign.typ_match thy (ty_pat, ty) Vartab.empty
   128       handle Type.TYPE_MATCH => err ctxt ty_pat ty
   128       handle Type.TYPE_MATCH => err ctxt ty_pat ty
   129   end
   129   end
   130 
   130 
   256 *)
   256 *)
   257 
   257 
   258 (* instantiates TVars so that the term is of type ty *)
   258 (* instantiates TVars so that the term is of type ty *)
   259 fun force_typ ctxt trm ty =
   259 fun force_typ ctxt trm ty =
   260   let
   260   let
   261     val thy = ProofContext.theory_of ctxt
   261     val thy = Proof_Context.theory_of ctxt
   262     val trm_ty = fastype_of trm
   262     val trm_ty = fastype_of trm
   263     val ty_inst = Sign.typ_match thy (trm_ty, ty) Vartab.empty
   263     val ty_inst = Sign.typ_match thy (trm_ty, ty) Vartab.empty
   264   in
   264   in
   265     map_types (Envir.subst_type ty_inst) trm
   265     map_types (Envir.subst_type ty_inst) trm
   266   end
   266   end
   271 fun mk_rel_compose (trm1, trm2) =
   271 fun mk_rel_compose (trm1, trm2) =
   272   Const (@{const_abbrev "rel_conj"}, dummyT) $ trm1 $ trm2
   272   Const (@{const_abbrev "rel_conj"}, dummyT) $ trm1 $ trm2
   273 
   273 
   274 fun get_relmap ctxt s =
   274 fun get_relmap ctxt s =
   275   let
   275   let
   276     val thy = ProofContext.theory_of ctxt
   276     val thy = Proof_Context.theory_of ctxt
   277     val relmap = #relmap (Quotient_Info.maps_lookup thy s) handle Quotient_Info.NotFound =>
   277     val relmap = #relmap (Quotient_Info.maps_lookup thy s) handle Quotient_Info.NotFound =>
   278       raise LIFT_MATCH ("get_relmap (no relation map function found for type " ^ s ^ ")")
   278       raise LIFT_MATCH ("get_relmap (no relation map function found for type " ^ s ^ ")")
   279   in
   279   in
   280     Const (relmap, dummyT)
   280     Const (relmap, dummyT)
   281   end
   281   end
   294     fold_rev Term.lambda vs' (mk_relmap_aux rty)
   294     fold_rev Term.lambda vs' (mk_relmap_aux rty)
   295   end
   295   end
   296 
   296 
   297 fun get_equiv_rel ctxt s =
   297 fun get_equiv_rel ctxt s =
   298   let
   298   let
   299     val thy = ProofContext.theory_of ctxt
   299     val thy = Proof_Context.theory_of ctxt
   300   in
   300   in
   301     #equiv_rel (Quotient_Info.quotdata_lookup thy s) handle Quotient_Info.NotFound =>
   301     #equiv_rel (Quotient_Info.quotdata_lookup thy s) handle Quotient_Info.NotFound =>
   302       raise LIFT_MATCH ("get_quotdata (no quotient found for type " ^ s ^ ")")
   302       raise LIFT_MATCH ("get_quotdata (no quotient found for type " ^ s ^ ")")
   303   end
   303   end
   304 
   304 
   552         else term_mismatch "regularize (relation mismatch)" ctxt rel rel'
   552         else term_mismatch "regularize (relation mismatch)" ctxt rel rel'
   553       end
   553       end
   554 
   554 
   555   | (_, Const _) =>
   555   | (_, Const _) =>
   556       let
   556       let
   557         val thy = ProofContext.theory_of ctxt
   557         val thy = Proof_Context.theory_of ctxt
   558         fun same_const (Const (s, T)) (Const (s', T')) = (s = s') andalso matches_typ thy T T'
   558         fun same_const (Const (s, T)) (Const (s', T')) = (s = s') andalso matches_typ thy T T'
   559           | same_const _ _ = false
   559           | same_const _ _ = false
   560       in
   560       in
   561         if same_const rtrm qtrm then rtrm
   561         if same_const rtrm qtrm then rtrm
   562         else
   562         else
   705 *)
   705 *)
   706 fun subst_typ ctxt ty_subst rty =
   706 fun subst_typ ctxt ty_subst rty =
   707   case rty of
   707   case rty of
   708     Type (s, rtys) =>
   708     Type (s, rtys) =>
   709       let
   709       let
   710         val thy = ProofContext.theory_of ctxt
   710         val thy = Proof_Context.theory_of ctxt
   711         val rty' = Type (s, map (subst_typ ctxt ty_subst) rtys)
   711         val rty' = Type (s, map (subst_typ ctxt ty_subst) rtys)
   712 
   712 
   713         fun matches [] = rty'
   713         fun matches [] = rty'
   714           | matches ((rty, qty)::tail) =
   714           | matches ((rty, qty)::tail) =
   715               case try (Sign.typ_match thy (rty, rty')) Vartab.empty of
   715               case try (Sign.typ_match thy (rty, rty')) Vartab.empty of
   727   | Free(n, ty) => Free(n, subst_typ ctxt ty_subst ty)
   727   | Free(n, ty) => Free(n, subst_typ ctxt ty_subst ty)
   728   | Var(n, ty) => Var(n, subst_typ ctxt ty_subst ty)
   728   | Var(n, ty) => Var(n, subst_typ ctxt ty_subst ty)
   729   | Bound i => Bound i
   729   | Bound i => Bound i
   730   | Const (a, ty) =>
   730   | Const (a, ty) =>
   731       let
   731       let
   732         val thy = ProofContext.theory_of ctxt
   732         val thy = Proof_Context.theory_of ctxt
   733 
   733 
   734         fun matches [] = Const (a, subst_typ ctxt ty_subst ty)
   734         fun matches [] = Const (a, subst_typ ctxt ty_subst ty)
   735           | matches ((rconst, qconst)::tail) =
   735           | matches ((rconst, qconst)::tail) =
   736               case try (Pattern.match thy (rconst, rtrm)) (Vartab.empty, Vartab.empty) of
   736               case try (Pattern.match thy (rconst, rtrm)) (Vartab.empty, Vartab.empty) of
   737                 NONE => matches tail
   737                 NONE => matches tail
   747      true:  quotient -> raw
   747      true:  quotient -> raw
   748      false: raw -> quotient
   748      false: raw -> quotient
   749 *)
   749 *)
   750 fun mk_ty_subst qtys direction ctxt =
   750 fun mk_ty_subst qtys direction ctxt =
   751   let
   751   let
   752     val thy = ProofContext.theory_of ctxt
   752     val thy = Proof_Context.theory_of ctxt
   753   in
   753   in
   754     Quotient_Info.quotdata_dest ctxt
   754     Quotient_Info.quotdata_dest ctxt
   755     |> map (fn x => (#rtyp x, #qtyp x))
   755     |> map (fn x => (#rtyp x, #qtyp x))
   756     |> filter (fn (_, qty) => member (Sign.typ_instance thy o swap) qtys qty)
   756     |> filter (fn (_, qty) => member (Sign.typ_instance thy o swap) qtys qty)
   757     |> map (if direction then swap else I)
   757     |> map (if direction then swap else I)