879 (*Appy is only (final) returnvalue, not argument during search *) |
881 (*Appy is only (final) returnvalue, not argument during search *) |
880 Napp_ (* ev. detects 'script is not appropriate for this example' *) |
882 Napp_ (* ev. detects 'script is not appropriate for this example' *) |
881 | Skip_; (* detects 'script successfully finished' |
883 | Skip_; (* detects 'script successfully finished' |
882 also used as init-value for resuming; this works, |
884 also used as init-value for resuming; this works, |
883 because 'nxt_up Or e1' treats as Appy *) |
885 because 'nxt_up Or e1' treats as Appy *) |
884 |
|
885 fun appy thy ptp E l (Const ("HOL.Let",_) $ e $ (Abs (i,T,b))) a v = |
|
886 (case appy thy ptp E (l @ [Celem.L, Celem.R]) e a v of |
|
887 Skip (res, E) => |
|
888 let val E' = LTool.upd_env E (Free (i,T), res); |
|
889 in appy thy ptp E' (l @ [Celem.R, Celem.D]) b a v end |
|
890 | ay => ay) |
|
891 | appy (thy as (th,sr)) ptp E l (Const ("Script.While"(*1*),_) $ c $ e $ a) _ v = |
|
892 (if Rewrite.eval_true_ th sr (subst_atomic (LTool.upd_env E (a,v)) c) |
|
893 then appy thy ptp E (l @ [Celem.L, Celem.R]) e (SOME a) v |
|
894 else Skip (v, E)) |
|
895 | appy (thy as (th,sr)) ptp E l (Const ("Script.While"(*2*),_) $ c $ e) a v = |
|
896 (if Rewrite.eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c) |
|
897 then appy thy ptp E (l @ [Celem.R]) e a v |
|
898 else Skip (v, E)) |
|
899 | appy (thy as (th,sr)) ptp E l (Const ("If",_) $ c $ e1 $ e2) a v = |
|
900 (if Rewrite.eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c) |
|
901 then appy thy ptp E (l @ [Celem.L, Celem.R]) e1 a v |
|
902 else appy thy ptp E (l @ [Celem.R]) e2 a v) |
|
903 | appy thy ptp E l (Const ("Script.Repeat"(*1*),_) $ e $ a) _ v = |
|
904 appy thy ptp E (l @ [Celem.L, Celem.R]) e (SOME a) v |
|
905 | appy thy ptp E l (Const ("Script.Repeat"(*2*),_) $ e) a v = appy thy ptp E (l @ [Celem.R]) e a v |
|
906 | appy thy ptp E l (Const ("Script.Try"(*2*),_) $ e $ a) _ v = |
|
907 (case appy thy ptp E (l @ [Celem.L, Celem.R]) e (SOME a) v of |
|
908 Napp E => (Skip (v, E)) |
|
909 | ay => ay) |
|
910 | appy thy ptp E l(Const ("Script.Try"(*1*),_) $ e) a v = |
|
911 (case appy thy ptp E (l @ [Celem.R]) e a v of |
|
912 Napp E => (Skip (v, E)) |
|
913 | ay => ay) |
|
914 | appy thy ptp E l (Const ("Script.Or"(*1*),_) $e1 $ e2 $ a) _ v = |
|
915 (case appy thy ptp E (l @ [Celem.L, Celem.L, Celem.R]) e1 (SOME a) v of |
|
916 Appy lme => Appy lme |
|
917 | _ => appy thy ptp E (*LTool.env*) (l @ [Celem.L, Celem.R]) e2 (SOME a) v) |
|
918 | appy thy ptp E l (Const ("Script.Or"(*2*),_) $e1 $ e2) a v = |
|
919 (case appy thy ptp E (l @ [Celem.L, Celem.R]) e1 a v of |
|
920 Appy lme => Appy lme |
|
921 | _ => appy thy ptp E (l @ [Celem.R]) e2 a v) |
|
922 | appy thy ptp E l (Const ("Script.Seq"(*2*),_) $ e1 $ e2 $ a) _ v = |
|
923 (case appy thy ptp E (l @ [Celem.L, Celem.L, Celem.R]) e1 (SOME a) v of |
|
924 Skip (v,E) => appy thy ptp E (l @ [Celem.L, Celem.R]) e2 (SOME a) v |
|
925 | ay => ay) |
|
926 | appy thy ptp E l (Const ("Script.Seq"(*1*),_) $ e1 $ e2) a v = |
|
927 (case appy thy ptp E (l @ [Celem.L, Celem.R]) e1 a v of |
|
928 Skip (v,E) => appy thy ptp E (l @ [Celem.R]) e2 a v |
|
929 | ay => ay) |
|
930 (* a leaf has been found *) |
|
931 | appy ((th,sr)) (pt, p) E l t a v = |
|
932 case handle_leaf "next " th sr E a v t of |
|
933 (_, LTool.Expr s) => Skip (s, E) |
|
934 | (a', LTool.STac stac) => |
|
935 let val (m,m') = stac2tac_ pt (Celem.assoc_thy th) stac |
|
936 in case m of |
|
937 Tac.Subproblem _ => Appy (m', (E,l,a',tac_2res m',Selem.Sundef,false)) |
|
938 | _ => |
|
939 (case Applicable.applicable_in p pt m of |
|
940 Chead.Appl m' => (Appy (m', (E,l,a',tac_2res m',Selem.Sundef,false))) |
|
941 | _ => Napp E) |
|
942 end |
|
943 |
|
944 fun nxt_up thy ptp (scr as (Rule.Prog sc)) E l ay (Const ("HOL.Let", _) $ _) a v = (*comes from let=...*) |
|
945 if ay = Napp_ |
|
946 then nstep_up thy ptp scr E (drop_last l) Napp_ a v |
|
947 else (*Skip_*) |
|
948 let |
|
949 val up = drop_last l |
|
950 val (i, T, body) = |
|
951 (case go up sc of |
|
952 Const ("HOL.Let",_) $ _ $ (Abs aa) => aa |
|
953 | t => error ("nxt_up..HOL.Let $ _ with " ^ Rule.term2str t)) |
|
954 val i = TermC.mk_Free (i, T) |
|
955 val E = LTool.upd_env E (i, v) |
|
956 in |
|
957 case appy thy ptp E (up @ [Celem.R, Celem.D]) body a v of |
|
958 Appy lre => Appy lre |
|
959 | Napp E => nstep_up thy ptp scr E up Napp_ a v |
|
960 | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v |
|
961 end |
|
962 | nxt_up thy ptp scr E l ay (Abs _) a v = nstep_up thy ptp scr E l ay a v |
|
963 | nxt_up thy ptp scr E l ay (Const ("HOL.Let",_) $ _ $ (Abs _)) a v = |
|
964 nstep_up thy ptp scr E l ay a v |
|
965 (*no appy_: never causes Napp -> Helpless*) |
|
966 | nxt_up (thy as (th, sr)) ptp scr E l _ (Const ("Script.While"(*1*), _) $ c $ e $ _) a v = |
|
967 if Rewrite.eval_true_ th sr (subst_atomic (upd_env_opt E (a, v)) c) |
|
968 then case appy thy ptp E (l @ [Celem.L, Celem.R]) e a v of |
|
969 Appy lr => Appy lr |
|
970 | Napp E => nstep_up thy ptp scr E l Skip_ a v |
|
971 | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v |
|
972 else nstep_up thy ptp scr E l Skip_ a v |
|
973 (*no appy_: never causes Napp - Helpless*) |
|
974 | nxt_up (thy as (th, sr)) ptp scr E l _ (Const ("Script.While"(*2*), _) $ c $ e) a v = |
|
975 if Rewrite.eval_true_ th sr (subst_atomic (upd_env_opt E (a, v)) c) |
|
976 then case appy thy ptp E (l @ [Celem.R]) e a v of |
|
977 Appy lr => Appy lr |
|
978 | Napp E => nstep_up thy ptp scr E l Skip_ a v |
|
979 | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v |
|
980 else nstep_up thy ptp scr E l Skip_ a v |
|
981 | nxt_up thy ptp scr E l ay (Const ("If", _) $ _ $ _ $ _) a v = nstep_up thy ptp scr E l ay a v |
|
982 | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*) |
|
983 (Const ("Script.Repeat"(*1*), _) $ e $ _) a v = |
|
984 (case appy thy ptp (*upd_env*) E (*a,v)*) (l @ [Celem.L, Celem.R]) e a v of |
|
985 Appy lr => Appy lr |
|
986 | Napp E => nstep_up thy ptp scr E l Skip_ a v |
|
987 | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v) |
|
988 | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*) |
|
989 (Const ("Script.Repeat"(*2*), _) $ e) a v = |
|
990 (case appy thy ptp (*upd_env*) E (*a,v)*) (l @ [Celem.R]) e a v of |
|
991 Appy lr => Appy lr |
|
992 | Napp E => nstep_up thy ptp scr E l Skip_ a v |
|
993 | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v) |
|
994 | nxt_up thy ptp scr E l _ (Const ("Script.Try"(*2*),_) $ _ $ _) a v = (*makes Napp to Skip*) |
|
995 nstep_up thy ptp scr E l Skip_ a v |
|
996 |
|
997 | nxt_up thy ptp scr E l _ (Const ("Script.Try"(*1*), _) $ _) a v = (*makes Napp to Skip*) |
|
998 nstep_up thy ptp scr E l Skip_ a v |
|
999 | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ $ _ $ _) a v = |
|
1000 nstep_up thy ptp scr E l ay a v |
|
1001 | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ $ _) a v = nstep_up thy ptp scr E l ay a v |
|
1002 | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ ) a v = |
|
1003 nstep_up thy ptp scr E (drop_last l) ay a v |
|
1004 | nxt_up thy ptp scr E l ay (Const ("Script.Seq"(*1*),_) $ _ $ _ $ _) a v = |
|
1005 (*all has been done in (*2*) below*) nstep_up thy ptp scr E l ay a v |
|
1006 | nxt_up thy ptp scr E l ay (Const ("Script.Seq"(*2*),_) $ _ $ _) a v = (*comes from e2*) |
|
1007 nstep_up thy ptp scr E l ay a v |
|
1008 | nxt_up thy ptp (scr as Rule.Prog sc) E l ay (Const ("Script.Seq"(*3*),_) $ _) a v = (*comes from e1*) |
|
1009 if ay = Napp_ |
|
1010 then nstep_up thy ptp scr E (drop_last l) Napp_ a v |
|
1011 else (*Skip_*) |
|
1012 let val up = drop_last l; |
|
1013 val e2 = |
|
1014 (case go up sc of |
|
1015 Const ("Script.Seq"(*2*), _) $ _ $ e2 => e2 |
|
1016 | t => error ("nxt_up..Script.Seq $ _ with " ^ Rule.term2str t)) |
|
1017 in case appy thy ptp E (up @ [Celem.R]) e2 a v of |
|
1018 Appy lr => Appy lr |
|
1019 | Napp E => nstep_up thy ptp scr E up Napp_ a v |
|
1020 | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v end |
|
1021 | nxt_up _ _ _ _ _ _ t _ _ = error ("nxt_up not impl for " ^ Rule.term2str t) |
|
1022 and nstep_up thy ptp (Rule.Prog sc) E l ay a v = |
|
1023 if 1 < length l |
|
1024 then |
|
1025 let val up = drop_last l; |
|
1026 in (nxt_up thy ptp (Rule.Prog sc) E up ay (go up sc) a v ) end |
|
1027 else (*interpreted to end*) |
|
1028 if ay = Skip_ then Skip (v, E) else Napp E |
|
1029 | nstep_up _ _ _ _ l _ _ _ = error ("nstep_up: uncovered fun-def at " ^ Celem.loc_2str l) |
|
1030 |
|
1031 (* decide for the next applicable stac in the script; |
|
1032 returns (stactic, value) - the value in case the script is finished |
|
1033 12.8.02: ~~~~~ and no assumptions ??? FIXME ??? |
|
1034 20.8.02: must return p in case of finished, because the next script |
|
1035 consulted need not be the calling script: |
|
1036 in case of detail ie. _inserted_ PrfObjs, the next stac |
|
1037 has to searched in a script with PblObj.status<>Complete ! |
|
1038 (.. not true for other details ..PrfObj ?????????????????? |
|
1039 20.8.02: do NOT return safe (is only changed in locate !!!) |
|
1040 *) |
|
1041 fun next_tac (thy,_) _ (Rule.Rfuns {next_rule, ...}) (Selem.RrlsState(f, f', rss, _), ctxt) = |
|
1042 if f = f' |
|
1043 then (Tac.End_Detail' (f',[])(*8.6.03*), (Selem.Uistate, ctxt), |
|
1044 (f', Selem.Sundef(*FIXME is no value of next_tac! vor 8.6.03*))) (*finished*) |
|
1045 else |
|
1046 (case next_rule rss f of |
|
1047 NONE => (Tac.Empty_Tac_, (Selem.Uistate, ctxt), (Rule.e_term, Selem.Sundef)) (*helpless*) |
|
1048 | SOME (Rule.Thm thm'')(*8.6.03: muss auch f' liefern ?!!*) => |
|
1049 (Tac.Rewrite' (thy, "e_rew_ord", Rule.e_rls, false, thm'', f, (Rule.e_term, [(*!?!8.6.03*)])), |
|
1050 (Selem.Uistate, ctxt), (Rule.e_term, Selem.Sundef)) (*next stac*) |
|
1051 | _ => error "next_tac: uncovered case next_rule") |
|
1052 | next_tac thy (ptp as (pt, (p, _))) (sc as Rule.Prog prog) |
|
1053 (Selem.ScrState (E,l,a,v,s,_), ctxt) = |
|
1054 (case if l = [] then appy thy ptp E [Celem.R] (LTool.body_of prog) NONE v |
|
1055 else nstep_up thy ptp sc E l Skip_ a v of |
|
1056 Skip (v, _) => (*finished*) |
|
1057 (case par_pbl_det pt p of |
|
1058 (true, p', _) => |
|
1059 let |
|
1060 val (_,pblID,_) = get_obj g_spec pt p'; |
|
1061 in (Tac.Check_Postcond' (pblID, (v, [(*assigned in next step*)])), |
|
1062 (Selem.e_istate, ctxt), (v,s)) |
|
1063 end |
|
1064 | _ => (Tac.End_Detail' (Rule.e_term,[])(*8.6.03*), (Selem.e_istate, ctxt), (v,s))) |
|
1065 | Napp _ => (Tac.Empty_Tac_, (Selem.e_istate, ctxt), (Rule.e_term, Selem.Sundef)) (*helpless*) |
|
1066 | Appy (m', scrst as (_,_,_,v,_,_)) => |
|
1067 (m', (Selem.ScrState scrst, ctxt), (v, Selem.Sundef))) (*next stac*) |
|
1068 | next_tac _ _ _ (is, _) = error ("next_tac: not impl for " ^ (Selem.istate2str is)); |
|
1069 |
886 |
1070 (* create the initial interpreter state |
887 (* create the initial interpreter state |
1071 from the items of the guard and the formal arguments of the partial_function. |
888 from the items of the guard and the formal arguments of the partial_function. |
1072 Note on progression from (a) type fmz_ \<longrightarrow> (e) arguments of the partial_function: |
889 Note on progression from (a) type fmz_ \<longrightarrow> (e) arguments of the partial_function: |
1073 (a) fmz is given by a math author |
890 (a) fmz is given by a math author |