510 | subst (f$t, lev) = subst(f, lev) $ subst(t, lev) |
522 | subst (f$t, lev) = subst(f, lev) $ subst(t, lev) |
511 | subst (t, _) = t |
523 | subst (t, _) = t |
512 in subst (t, 0) end; |
524 in subst (t, 0) end; |
513 |
525 |
514 (* instantiate let; necessary for ass_up *) |
526 (* instantiate let; necessary for ass_up *) |
515 fun inst_abs (Const sT) = Const sT (*TODO.WN100907 drop thy*) |
527 fun inst_abs (Const sT) = Const sT |
516 | inst_abs (Free sT) = Free sT |
528 | inst_abs (Free sT) = Free sT |
517 | inst_abs (Bound n) = Bound n |
529 | inst_abs (Bound n) = Bound n |
518 | inst_abs (Var iT) = Var iT |
530 | inst_abs (Var iT) = Var iT |
519 | inst_abs (Const ("HOL.Let",T1) $ e $ (Abs (v, T2, b))) = |
531 | inst_abs (Const ("HOL.Let",T1) $ e $ (Abs (v, T2, b))) = |
520 let val b' = subst_bound (Free (v, T2), b); |
532 let val b' = subst_bound (Free (v, T2), b); (*fun variant_abs: term.ML*) |
521 (*fun variant_abs: term.ML*) |
|
522 in Const ("HOL.Let", T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end |
533 in Const ("HOL.Let", T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end |
523 | inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2 |
534 | inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2 |
524 | inst_abs t = t; |
535 | inst_abs t = t; |
525 (*val scr = |
|
526 "Script Make_fun_by_explicit (f_::real) (v_::real) (eqs_::bool list) = \ |
|
527 \ (let h_ = (hd o (filterVar f_)) eqs_; \ |
|
528 \ e_1 = hd (dropWhile (ident h_) eqs_); \ |
|
529 \ vs_ = dropWhile (ident f_) (Vars h_); \ |
|
530 \ v_1 = hd (dropWhile (ident v_) vs_); \ |
|
531 \ (s_1::bool list)=(SubProblem(DiffApp_,[univar,equation],[no_met])\ |
|
532 \ [BOOL e_1, REAL v_1])\ |
|
533 \ in Substitute [(v_1 = (rhs o hd) s_1)] h_)"; |
|
534 > val ttt = (Thm.term_of o the o (parse thy)) scr; |
|
535 > tracing(term2str ttt); |
|
536 > atomt ttt; |
|
537 *** ------------- |
|
538 *** Const ( DiffApp.Make'_fun'_by'_explicit) |
|
539 *** . Free ( f_, ) |
|
540 *** . Free ( v_, ) |
|
541 *** . Free ( eqs_, ) |
|
542 *** . Const ( Let) |
|
543 *** . . Const ( Fun.op o) |
|
544 *** . . . Const ( List.hd) |
|
545 *** . . . Const ( DiffApp.filterVar) |
|
546 *** . . . . Free ( f_, ) |
|
547 *** . . . Free ( eqs_, ) |
|
548 *** . . Abs( h_,.. |
|
549 *** . . . Const ( Let) |
|
550 *** . . . . Const ( List.hd) |
|
551 *** . . . . . Const ( List.dropWhile) |
|
552 *** . . . . . . Const ( Atools.ident) |
|
553 *** . . . . . . . Bound 0 <---- Free ( h_, ) |
|
554 *** . . . . . . Free ( eqs_, ) |
|
555 *** . . . . Abs( e_1,.. |
|
556 *** . . . . . Const ( Let) |
|
557 *** . . . . . . Const ( List.dropWhile) |
|
558 *** . . . . . . . Const ( Atools.ident) |
|
559 *** . . . . . . . . Free ( f_, ) |
|
560 *** . . . . . . . Const ( Tools.Vars) |
|
561 *** . . . . . . . . Bound 1 <---- Free ( h_, ) |
|
562 *** . . . . . . Abs( vs_,.. |
|
563 *** . . . . . . . Const ( Let) |
|
564 *** . . . . . . . . Const ( List.hd) |
|
565 *** . . . . . . . . . Const ( List.dropWhile) |
|
566 *** . . . . . . . . . . Const ( Atools.ident) |
|
567 *** . . . . . . . . . . . Free ( v_, ) |
|
568 *** . . . . . . . . . . Bound 0 <---- Free ( vs_, ) |
|
569 *** . . . . . . . . Abs( v_1,.. |
|
570 *** . . . . . . . . . Const ( Let) |
|
571 *** . . . . . . . . . . Const ( Script.SubProblem) |
|
572 *** . . . . . . . . . . . Const ( Pair) |
|
573 *** . . . . . . . . . . . . Free ( DiffApp_, ) |
|
574 *** . . . . . . . . . . . . Const ( Pair) |
|
575 *** . . . . . . . . . . . . . Const ( List.list.Cons) |
|
576 *** . . . . . . . . . . . . . . Free ( univar, ) |
|
577 *** . . . . . . . . . . . . . . Const ( List.list.Cons) |
|
578 *** . . . . . . . . . . . . . . . Free ( equation, ) |
|
579 *** . . . . . . . . . . . . . . . Const ( List.list.Nil) |
|
580 *** . . . . . . . . . . . . . Const ( List.list.Cons) |
|
581 *** . . . . . . . . . . . . . . Free ( no_met, ) |
|
582 *** . . . . . . . . . . . . . . Const ( List.list.Nil) |
|
583 *** . . . . . . . . . . . Const ( List.list.Cons) |
|
584 *** . . . . . . . . . . . . Const ( Script.BOOL) |
|
585 *** . . . . . . . . . . . . . Bound 2 <----- Free ( e_1, ) |
|
586 *** . . . . . . . . . . . . Const ( List.list.Cons) |
|
587 *** . . . . . . . . . . . . . Const ( Script.real_) |
|
588 *** . . . . . . . . . . . . . . Bound 0 <----- Free ( v_1, ) |
|
589 *** . . . . . . . . . . . . . Const ( List.list.Nil) |
|
590 *** . . . . . . . . . . Abs( s_1,.. |
|
591 *** . . . . . . . . . . . Const ( Script.Substitute) |
|
592 *** . . . . . . . . . . . . Const ( List.list.Cons) |
|
593 *** . . . . . . . . . . . . . Const ( Pair) |
|
594 *** . . . . . . . . . . . . . . Bound 1 <----- Free ( v_1, ) |
|
595 *** . . . . . . . . . . . . . . Const ( Fun.op o) |
|
596 *** . . . . . . . . . . . . . . . Const ( Tools.rhs) |
|
597 *** . . . . . . . . . . . . . . . Const ( List.hd) |
|
598 *** . . . . . . . . . . . . . . . Bound 0 <----- Free ( s_1, ) |
|
599 *** . . . . . . . . . . . . . Const ( List.list.Nil) |
|
600 *** . . . . . . . . . . . . Bound 4 <----- Free ( h_, ) |
|
601 |
|
602 > val ttt' = inst_abs thy ttt; |
|
603 > tracing(term2str ttt'); |
|
604 Script Make_fun_by_explicit f_ v_ eqs_ = |
|
605 ... as above ... |
|
606 > atomt ttt'; |
|
607 *** ------------- |
|
608 *** Const ( DiffApp.Make'_fun'_by'_explicit) |
|
609 *** . Free ( f_, ) |
|
610 *** . Free ( v_, ) |
|
611 *** . Free ( eqs_, ) |
|
612 *** . Const ( Let) |
|
613 *** . . Const ( Fun.op o) |
|
614 *** . . . Const ( List.hd) |
|
615 *** . . . Const ( DiffApp.filterVar) |
|
616 *** . . . . Free ( f_, ) |
|
617 *** . . . Free ( eqs_, ) |
|
618 *** . . Abs( h_,.. |
|
619 *** . . . Const ( Let) |
|
620 *** . . . . Const ( List.hd) |
|
621 *** . . . . . Const ( List.dropWhile) |
|
622 *** . . . . . . Const ( Atools.ident) |
|
623 *** . . . . . . . Free ( h_, ) <---- Bound 0 |
|
624 *** . . . . . . Free ( eqs_, ) |
|
625 *** . . . . Abs( e_1,.. |
|
626 *** . . . . . Const ( Let) |
|
627 *** . . . . . . Const ( List.dropWhile) |
|
628 *** . . . . . . . Const ( Atools.ident) |
|
629 *** . . . . . . . . Free ( f_, ) |
|
630 *** . . . . . . . Const ( Tools.Vars) |
|
631 *** . . . . . . . . Free ( h_, ) <---- Bound 1 |
|
632 *** . . . . . . Abs( vs_,.. |
|
633 *** . . . . . . . Const ( Let) |
|
634 *** . . . . . . . . Const ( List.hd) |
|
635 *** . . . . . . . . . Const ( List.dropWhile) |
|
636 *** . . . . . . . . . . Const ( Atools.ident) |
|
637 *** . . . . . . . . . . . Free ( v_, ) |
|
638 *** . . . . . . . . . . Free ( vs_, ) <---- Bound 0 |
|
639 *** . . . . . . . . Abs( v_1,.. |
|
640 *** . . . . . . . . . Const ( Let) |
|
641 *** . . . . . . . . . . Const ( Script.SubProblem) |
|
642 *** . . . . . . . . . . . Const ( Pair) |
|
643 *** . . . . . . . . . . . . Free ( DiffApp_, ) |
|
644 *** . . . . . . . . . . . . Const ( Pair) |
|
645 *** . . . . . . . . . . . . . Const ( List.list.Cons) |
|
646 *** . . . . . . . . . . . . . . Free ( univar, ) |
|
647 *** . . . . . . . . . . . . . . Const ( List.list.Cons) |
|
648 *** . . . . . . . . . . . . . . . Free ( equation, ) |
|
649 *** . . . . . . . . . . . . . . . Const ( List.list.Nil) |
|
650 *** . . . . . . . . . . . . . Const ( List.list.Cons) |
|
651 *** . . . . . . . . . . . . . . Free ( no_met, ) |
|
652 *** . . . . . . . . . . . . . . Const ( List.list.Nil) |
|
653 *** . . . . . . . . . . . Const ( List.list.Cons) |
|
654 *** . . . . . . . . . . . . Const ( Script.BOOL) |
|
655 *** . . . . . . . . . . . . . Free ( e_1, ) <----- Bound 2 |
|
656 *** . . . . . . . . . . . . Const ( List.list.Cons) |
|
657 *** . . . . . . . . . . . . . Const ( Script.real_) |
|
658 *** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 0 |
|
659 *** . . . . . . . . . . . . . Const ( List.list.Nil) |
|
660 *** . . . . . . . . . . Abs( s_1,.. |
|
661 *** . . . . . . . . . . . Const ( Script.Substitute) |
|
662 *** . . . . . . . . . . . . Const ( List.list.Cons) |
|
663 *** . . . . . . . . . . . . . Const ( Pair) |
|
664 *** . . . . . . . . . . . . . . Free ( v_1, ) <----- Bound 1 |
|
665 *** . . . . . . . . . . . . . . Const ( Fun.op o) |
|
666 *** . . . . . . . . . . . . . . . Const ( Tools.rhs) |
|
667 *** . . . . . . . . . . . . . . . Const ( List.hd) |
|
668 *** . . . . . . . . . . . . . . . Free ( s_1, ) <----- Bound 0 |
|
669 *** . . . . . . . . . . . . . Const ( List.list.Nil) |
|
670 *** . . . . . . . . . . . . Free ( h_, ) <----- Bound 4 |
|
671 |
|
672 Note numbering of de Bruijn indexes ! |
|
673 |
|
674 Script Make_fun_by_explicit f_ v_ eqs_ = |
|
675 let h_ = (hd o filterVar f_) eqs_; |
|
676 e_1 = hd (dropWhile (ident h_ BOUND_0) eqs_); |
|
677 vs_ = dropWhile (ident f_) (Vars h_ BOUND_1); |
|
678 v_1 = hd (dropWhile (ident v_) vs_ BOUND_0); |
|
679 s_1 = |
|
680 SubProblem (DiffApp_, [univar, equation], [no_met]) |
|
681 [BOOL e_1 BOUND_2, REAL v_1 BOUND_0] |
|
682 in Substitute [(v_1 BOUND_1 = (rhs o hd) s_1 BOUND_0)] h_ BOUND_4 |
|
683 *) |
|
684 |
536 |
685 (* for parse and parse_patt: fix all types to real *) |
537 (* for parse and parse_patt: fix all types to real *) |
686 fun T_a2real (Type (s, [])) = |
538 fun T_a2real (Type (s, [])) = |
687 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT |
539 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, []) |
688 else Type (s, []) |
|
689 | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts) |
540 | T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts) |
690 | T_a2real (TFree (s, srt)) = |
541 | T_a2real (TFree (s, srt)) = |
691 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT |
542 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt) |
692 else TFree (s, srt) |
543 | T_a2real (TVar (("DUMMY", _), _)) = HOLogic.realT |
693 | T_a2real (TVar ((s, i), srt)) = |
544 | T_a2real (TVar ((s, i), srt)) = |
694 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT |
545 if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TVar ((s, i), srt) |
695 else TVar ((s, i), srt) |
|
696 | T_a2real (TVar (("DUMMY",_), srt)) = HOLogic.realT; |
|
697 |
|
698 (*FIXME .. fixes the type (+see Typefix.thy*) |
|
699 fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T)) |
546 fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T)) |
700 | typ_a2real (Free( s, T)) = (Free( s, T_a2real T)) |
547 | typ_a2real (Free( s, T)) = (Free( s, T_a2real T)) |
701 | typ_a2real (Var( n, T)) = (Var( n, T_a2real T)) |
548 | typ_a2real (Var( n, T)) = (Var( n, T_a2real T)) |
702 | typ_a2real (Bound i) = (Bound i) |
549 | typ_a2real (Bound i) = (Bound i) |
703 | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t) |
550 | typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t) |
704 | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2); |
551 | typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2); |
705 |
552 |
706 |
553 (* TODO clarify parse with Test_Isac *) |
707 (* before 2002 *) |
554 fun parseold thy str = (* before 2002 *) |
708 fun parseold thy str = |
555 (let val t = ((*typ_a2real o*) numbers_to_string) (Syntax.read_term_global thy str) |
709 (let val t = ((*typ_a2real o*) numbers_to_string) |
|
710 (Syntax.read_term_global thy str) |
|
711 in SOME (Thm.global_cterm_of thy t) end) |
556 in SOME (Thm.global_cterm_of thy t) end) |
712 handle _ => NONE; |
557 handle _(*EXN? ..Inner syntax error Failed to parse term*) => NONE; |
713 (*2002 fun parseN thy str = |
558 fun parseN thy str = (* introduced 2002 *) |
714 (let |
559 (let val t = (*(typ_a2real o numbers_to_string)*) (Syntax.read_term_global thy str) |
715 val sgn = sign_of thy; |
|
716 val t = ((*typ_a2real o app_num_tr'1 o*) term_of) |
|
717 (read_cterm sgn (str,(TVar(("DUMMY",0),[])))); |
|
718 in SOME (Thm.global_cterm_of sgn t) end) |
|
719 handle _ => NONE;*) |
|
720 fun parseN thy str = |
|
721 (let val t = (*(typ_a2real o numbers_to_string)*) |
|
722 (Syntax.read_term_global thy str) |
|
723 in SOME (Thm.global_cterm_of thy t) end) |
560 in SOME (Thm.global_cterm_of thy t) end) |
724 handle _ => NONE; |
561 handle _(*EXN? ..Inner syntax error Failed to parse term*) => NONE; |
725 (*2002 fun parse thy str = |
562 fun parse thy str = (* introduced 2010 *) |
726 (let |
563 (let val t = (typ_a2real o numbers_to_string) (Syntax.read_term_global thy str) |
727 val sgn = sign_of thy; |
564 in SOME (Thm.global_cterm_of thy t) end) |
728 val t = (typ_a2real o app_num_tr'1 o Thm.term_of) |
565 handle _(*EXN? ..Inner syntax error Failed to parse term*) => NONE; |
729 (read_cterm sgn (str,(TVar(("DUMMY",0),[])))); |
|
730 in SOME (Thm.global_cterm_of sgn t) end) (*FIXXXXME 10.8.02: return term !!!*) |
|
731 handle _ => NONE;*) |
|
732 (*2010 fun parse thy str = |
|
733 (let val t = (typ_a2real o app_num_tr'1) (Syntax.read_term_global thy str) |
|
734 in SOME (Thm.global_cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*) |
|
735 handle _ => NONE;*) |
|
736 fun parse thy str = |
|
737 (let val t = (typ_a2real o numbers_to_string) |
|
738 (Syntax.read_term_global thy str) |
|
739 in SOME (Thm.global_cterm_of thy t) end) (*FIXXXXME 10.8.02: return term !!!*) |
|
740 handle _ => NONE; |
|
741 (* |
|
742 > val (SOME ct) = parse thy "(-#5)^^^#3"; |
|
743 > atomty (Thm.term_of ct); |
|
744 *** ------------- |
|
745 *** Const ( Nat.op ^, ['a, nat] => 'a) |
|
746 *** Const ( uminus, 'a => 'a) |
|
747 *** Free ( #5, 'a) |
|
748 *** Free ( #3, nat) |
|
749 > val (SOME ct) = parse thy "R=R"; |
|
750 > atomty (Thm.term_of ct); |
|
751 *** ------------- |
|
752 *** Const ( op =, [real, real] => bool) |
|
753 *** Free ( R, real) |
|
754 *** Free ( R, real) |
|
755 |
|
756 THIS IS THE OUTPUT FOR VERSION (3) above at typ_a2real !!!!! |
|
757 *** ------------- |
|
758 *** Const ( op =, [RealDef.real, RealDef.real] => bool) |
|
759 *** Free ( R, RealDef.real) |
|
760 *** Free ( R, RealDef.real) *) |
|
761 |
566 |
762 (*WN110317 parseNEW will replace parse after introduction of ctxt completed*) |
567 (*WN110317 parseNEW will replace parse after introduction of ctxt completed*) |
763 fun parseNEW ctxt str = SOME (Syntax.read_term ctxt str |> numbers_to_string) |
568 fun parseNEW ctxt str = SOME (Syntax.read_term ctxt str |> numbers_to_string) |
764 handle _ => NONE; |
569 handle _ => NONE; |
765 |
|
766 (* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation |
570 (* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation |
767 WN130613 probably compare to |
571 WN130613 probably compare to |
768 http://www.mail-archive.com/isabelle-dev@mailbroy.informatik.tu-muenchen.de/msg04249.html*) |
572 http://www.mail-archive.com/isabelle-dev@mailbroy.informatik.tu-muenchen.de/msg04249.html*) |
769 fun parse_patt thy str = (thy, str) |>> thy2ctxt |
573 fun parse_patt thy str = |
770 |-> Proof_Context.read_term_pattern |
574 (thy, str) |>> thy2ctxt |
771 |> numbers_to_string (*TODO drop*) |
575 |-> Proof_Context.read_term_pattern |
772 |> typ_a2real; (*TODO drop*) |
576 |> numbers_to_string (*TODO drop*) |
773 |
577 |> typ_a2real; (*TODO drop*) |
774 (*version for testing local to theories*) |
|
775 fun str2term_ thy str = (Thm.term_of o the o (parse thy)) str; |
|
776 (*WN110520 |
|
777 fun str2term str = (Thm.term_of o the o (parse (Thy_Info_get_theory "Isac"))) str;*) |
|
778 fun str2term str = parse_patt (Thy_Info_get_theory "Isac") str |
578 fun str2term str = parse_patt (Thy_Info_get_theory "Isac") str |
779 fun strs2terms ss = map str2term ss; |
579 |
780 fun str2termN str = (Thm.term_of o the o (parseN (Thy_Info_get_theory "Isac"))) str; |
580 (* TODO decide with Test_Isac *) |
781 |
581 fun is_atom t = length (vars t) = 1 |
782 (*+ makes a substitution from the output of Pattern.match +*) |
|
783 (*fun mk_subs ((id, _):indexname, t:term) = (Free (id,type_of t), t);*) |
|
784 fun mk_subs (subs: ((string * int) * (Term.typ * Term.term)) list) = |
|
785 let fun mk_sub ((id, _), (ty, tm)) = (Free (id, ty), tm) in |
|
786 map mk_sub subs end; |
|
787 |
|
788 val atomthm = atomt o #prop o Thm.rep_thm; |
|
789 |
|
790 (*.instantiate #prop thm with bound variables (as Free).*) |
|
791 fun inst_bdv [] t = t : term |
|
792 | inst_bdv (instl: (term*term) list) t = |
|
793 let fun subst (v as Var((s,_),T)) = |
|
794 (case Symbol.explode s of |
|
795 "b"::"d"::"v"::_ => |
|
796 if_none (assoc(instl,Free(s,T))) (Free(s,T)) |
|
797 | _ => v) |
|
798 | subst (Abs(a,T,body)) = Abs(a, T, subst body) |
|
799 | subst (f$t') = subst f $ subst t' |
|
800 | subst t = if_none (assoc(instl,t)) t |
|
801 in subst t end; |
|
802 |
|
803 |
|
804 (*WN050829 caution: is_atom (str2term"q_0/2 * L * x") = true !!! |
|
805 use length (vars term) = 1 instead*) |
|
806 fun is_atom (Const ("Float.Float",_) $ _) = true |
582 fun is_atom (Const ("Float.Float",_) $ _) = true |
807 | is_atom (Const ("ComplexI.I'_'_",_)) = true |
583 | is_atom (Const ("ComplexI.I'_'_",_)) = true |
808 | is_atom (Const ("Groups.times_class.times",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t |
584 | is_atom (Const ("Groups.times_class.times",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t |
809 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1 |
585 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1 |
810 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ |
586 | is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ |