TFL/tfl.sml
changeset 10015 8c16ec5ba62b
parent 9878 b145613939c1
equal deleted inserted replaced
10014:d41ab495ab14 10015:8c16ec5ba62b
   160  *---------------------------------------------------------------------------*)
   160  *---------------------------------------------------------------------------*)
   161 fun mk_group Name rows =
   161 fun mk_group Name rows =
   162   U.itlist (fn (row as ((prfx, p::rst), rhs)) =>
   162   U.itlist (fn (row as ((prfx, p::rst), rhs)) =>
   163             fn (in_group,not_in_group) =>
   163             fn (in_group,not_in_group) =>
   164                let val (pc,args) = S.strip_comb p
   164                let val (pc,args) = S.strip_comb p
   165                in if ((#1(dest_Const pc) = Name) handle _ => false)
   165                in if ((#1(dest_Const pc) = Name) handle _ => false) (* FIXME do not handle _ !!! *)
   166                   then (((prfx,args@rst), rhs)::in_group, not_in_group)
   166                   then (((prfx,args@rst), rhs)::in_group, not_in_group)
   167                   else (in_group, row::not_in_group) end)
   167                   else (in_group, row::not_in_group) end)
   168       rows ([],[]);
   168       rows ([],[]);
   169 
   169 
   170 (*---------------------------------------------------------------------------
   170 (*---------------------------------------------------------------------------
   326                    (Int.toString (length fs) ^
   326                    (Int.toString (length fs) ^
   327                     " distinct function names being declared")
   327                     " distinct function names being declared")
   328 in
   328 in
   329 fun mk_functional thy clauses =
   329 fun mk_functional thy clauses =
   330  let val (L,R) = ListPair.unzip (map HOLogic.dest_eq clauses)
   330  let val (L,R) = ListPair.unzip (map HOLogic.dest_eq clauses)
   331                    handle _ => raise TFL_ERR
   331                    handle _ => raise TFL_ERR (* FIXME do not handle _ !!! *)
   332                        {func = "mk_functional",
   332                        {func = "mk_functional",
   333                         mesg = "recursion equations must use the = relation"}
   333                         mesg = "recursion equations must use the = relation"}
   334      val (funcs,pats) = ListPair.unzip (map (fn (t$u) =>(t,u)) L)
   334      val (funcs,pats) = ListPair.unzip (map (fn (t$u) =>(t,u)) L)
   335      val atom = single (gen_distinct (op aconv) funcs)
   335      val atom = single (gen_distinct (op aconv) funcs)
   336      val (fname,ftype) = dest_atom atom
   336      val (fname,ftype) = dest_atom atom
   345      val ty_match = Thry.match_type thy
   345      val ty_match = Thry.match_type thy
   346      val range_ty = type_of (hd R)
   346      val range_ty = type_of (hd R)
   347      val (patts, case_tm) = mk_case ty_info ty_match (aname::names) range_ty
   347      val (patts, case_tm) = mk_case ty_info ty_match (aname::names) range_ty
   348                                     {path=[a], rows=rows}
   348                                     {path=[a], rows=rows}
   349      val patts1 = map (fn (_,tag,[pat]) => (pat,tag)) patts
   349      val patts1 = map (fn (_,tag,[pat]) => (pat,tag)) patts
   350           handle _ => mk_functional_err "error in pattern-match translation"
   350           handle _ => mk_functional_err "error in pattern-match translation" (* FIXME do not handle _ !!! *)
   351      val patts2 = U.sort(fn p1=>fn p2=> row_of_pat p1 < row_of_pat p2) patts1
   351      val patts2 = U.sort(fn p1=>fn p2=> row_of_pat p1 < row_of_pat p2) patts1
   352      val finals = map row_of_pat patts2
   352      val finals = map row_of_pat patts2
   353      val originals = map (row_of_pat o #2) rows
   353      val originals = map (row_of_pat o #2) rows
   354      val dummy = case (originals\\finals)
   354      val dummy = case (originals\\finals)
   355              of [] => ()
   355              of [] => ()
   734          in case cntxt
   734          in case cntxt
   735               of [] => (P_y, (tm,[]))
   735               of [] => (P_y, (tm,[]))
   736                | _  => let
   736                | _  => let
   737                     val imp = S.list_mk_conj cntxt ==> P_y
   737                     val imp = S.list_mk_conj cntxt ==> P_y
   738                     val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals)
   738                     val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals)
   739                     val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs
   739                     val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs (* FIXME do not handle _ !!! *)
   740                     in (S.list_mk_forall(locals,imp), (tm,locals)) end
   740                     in (S.list_mk_forall(locals,imp), (tm,locals)) end
   741          end
   741          end
   742  in case TCs
   742  in case TCs
   743     of [] => (S.list_mk_forall(globals, P$pat), [])
   743     of [] => (S.list_mk_forall(globals, P$pat), [])
   744      |  _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs)
   744      |  _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs)
   763          in case cntxt
   763          in case cntxt
   764               of [] => (P_y, (tm,[]))
   764               of [] => (P_y, (tm,[]))
   765                | _  => let
   765                | _  => let
   766                     val imp = S.list_mk_conj cntxt ==> P_y
   766                     val imp = S.list_mk_conj cntxt ==> P_y
   767                     val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals)
   767                     val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals)
   768                     val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs
   768                     val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs (* FIXME do not handle _ !!! *)
   769                     in (S.list_mk_forall(locals,imp), (tm,locals)) end
   769                     in (S.list_mk_forall(locals,imp), (tm,locals)) end
   770          end
   770          end
   771  in case TCs
   771  in case TCs
   772     of [] => (S.list_mk_forall(pat_vars, P$pat), [])
   772     of [] => (S.list_mk_forall(pat_vars, P$pat), [])
   773      |  _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs)
   773      |  _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs)
   791      fun nested tm = is_some (S.find_term (curry (op aconv) f) tm)
   791      fun nested tm = is_some (S.find_term (curry (op aconv) f) tm)
   792      fun get_cntxt TC = tych(#ant(S.dest_imp(#2(S.strip_forall(concl TC)))))
   792      fun get_cntxt TC = tych(#ant(S.dest_imp(#2(S.strip_forall(concl TC)))))
   793      fun mk_ih ((TC,locals),th2,nested) =
   793      fun mk_ih ((TC,locals),th2,nested) =
   794          R.GENL (map tych locals)
   794          R.GENL (map tych locals)
   795             (if nested
   795             (if nested
   796               then R.DISCH (get_cntxt TC) th2 handle _ => th2
   796               then R.DISCH (get_cntxt TC) th2 handle _ => th2 (* FIXME do not handle _ !!! *)
   797                else if S.is_imp(concl TC)
   797                else if S.is_imp(concl TC)
   798                      then R.IMP_TRANS TC th2
   798                      then R.IMP_TRANS TC th2
   799                       else R.MP th2 TC)
   799                       else R.MP th2 TC)
   800  in
   800  in
   801  R.DISCH antc
   801  R.DISCH antc
   872     val dc' = U.itlist (R.GEN o tych) vars
   872     val dc' = U.itlist (R.GEN o tych) vars
   873                        (R.SPEC (tych(S.mk_vstruct Parg_ty vars)) dc)
   873                        (R.SPEC (tych(S.mk_vstruct Parg_ty vars)) dc)
   874 in
   874 in
   875    R.GEN (tych P) (R.DISCH (tych(concl Rinduct_assum)) dc')
   875    R.GEN (tych P) (R.DISCH (tych(concl Rinduct_assum)) dc')
   876 end
   876 end
   877 handle _ => raise TFL_ERR{func = "mk_induction", mesg = "failed derivation"};
   877 handle _ => raise TFL_ERR{func = "mk_induction", mesg = "failed derivation"}; (* FIXME do not handle _ !!! *)
   878 
   878 
   879 
   879 
   880 
   880 
   881 
   881 
   882 (*---------------------------------------------------------------------------
   882 (*---------------------------------------------------------------------------
   920    val (rules1,induction1)  =
   920    val (rules1,induction1)  =
   921        let val thm = R.prove(tych(HOLogic.mk_Trueprop
   921        let val thm = R.prove(tych(HOLogic.mk_Trueprop
   922                                   (hd(#1(R.dest_thm rules)))),
   922                                   (hd(#1(R.dest_thm rules)))),
   923                              wf_tac)
   923                              wf_tac)
   924        in (R.PROVE_HYP thm rules,  R.PROVE_HYP thm induction)
   924        in (R.PROVE_HYP thm rules,  R.PROVE_HYP thm induction)
   925        end handle _ => (rules,induction)
   925        end handle _ => (rules,induction) (* FIXME do not handle _ !!! *)
   926 
   926 
   927    (*----------------------------------------------------------------------
   927    (*----------------------------------------------------------------------
   928     * The termination condition (tc) is simplified to |- tc = tc' (there
   928     * The termination condition (tc) is simplified to |- tc = tc' (there
   929     * might not be a change!) and then 3 attempts are made:
   929     * might not be a change!) and then 3 attempts are made:
   930     *
   930     *
   946            val _ = print_cterms "TC before simplification: " [tc1]
   946            val _ = print_cterms "TC before simplification: " [tc1]
   947            val tc_eq = simplifier tc1
   947            val tc_eq = simplifier tc1
   948            val _ = print_thms "result: " [tc_eq]
   948            val _ = print_thms "result: " [tc_eq]
   949        in
   949        in
   950        elim_tc (R.MATCH_MP Thms.eqT tc_eq) (r,ind)
   950        elim_tc (R.MATCH_MP Thms.eqT tc_eq) (r,ind)
   951        handle _ =>
   951        handle _ => (* FIXME do not handle _ !!! *)
   952         (elim_tc (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq)
   952         (elim_tc (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq)
   953                   (R.prove(tych(HOLogic.mk_Trueprop(S.rhs(concl tc_eq))),
   953                   (R.prove(tych(HOLogic.mk_Trueprop(S.rhs(concl tc_eq))),
   954                            terminator)))
   954                            terminator)))
   955                  (r,ind)
   955                  (r,ind)
   956          handle _ =>
   956          handle _ => (* FIXME do not handle _ !!! *)
   957           (R.UNDISCH(R.MATCH_MP (R.MATCH_MP Thms.simp_thm r) tc_eq),
   957           (R.UNDISCH(R.MATCH_MP (R.MATCH_MP Thms.simp_thm r) tc_eq),
   958            simplify_induction theory tc_eq ind))
   958            simplify_induction theory tc_eq ind))
   959        end
   959        end
   960 
   960 
   961    (*----------------------------------------------------------------------
   961    (*----------------------------------------------------------------------
   974    fun simplify_nested_tc tc =
   974    fun simplify_nested_tc tc =
   975       let val tc_eq = simplifier (tych (#2 (S.strip_forall tc)))
   975       let val tc_eq = simplifier (tych (#2 (S.strip_forall tc)))
   976       in
   976       in
   977       R.GEN_ALL
   977       R.GEN_ALL
   978        (R.MATCH_MP Thms.eqT tc_eq
   978        (R.MATCH_MP Thms.eqT tc_eq
   979         handle _
   979         handle _ (* FIXME do not handle _ !!! *)
   980         => (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq)
   980         => (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq)
   981                       (R.prove(tych(HOLogic.mk_Trueprop (S.rhs(concl tc_eq))),
   981                       (R.prove(tych(HOLogic.mk_Trueprop (S.rhs(concl tc_eq))),
   982                                terminator))
   982                                terminator))
   983             handle _ => tc_eq))
   983             handle _ => tc_eq)) (* FIXME do not handle _ !!! *)
   984       end
   984       end
   985 
   985 
   986    (*-------------------------------------------------------------------
   986    (*-------------------------------------------------------------------
   987     * Attempt to simplify the termination conditions in each rule and
   987     * Attempt to simplify the termination conditions in each rule and
   988     * in the induction theorem.
   988     * in the induction theorem.