src/HOLCF/fixrec_package.ML
changeset 16402 36f41d5e3b3e
parent 16401 57c35ede00b9
child 16461 e6b431cb8e0c
equal deleted inserted replaced
16401:57c35ede00b9 16402:36f41d5e3b3e
     6 *)
     6 *)
     7 
     7 
     8 signature FIXREC_PACKAGE =
     8 signature FIXREC_PACKAGE =
     9 sig
     9 sig
    10   val add_fixrec: string list list -> theory -> theory
    10   val add_fixrec: string list list -> theory -> theory
    11   val add_fixpat: string * string -> theory -> theory
    11   val add_fixpat: ((string * Attrib.src list) * string) list -> theory -> theory
    12 end;
    12 end;
    13 
    13 
    14 structure FixrecPackage: FIXREC_PACKAGE =
    14 structure FixrecPackage: FIXREC_PACKAGE =
    15 struct
    15 struct
    16 
    16 
    40 infix 0 ==;  fun S ==  T = %%:"==" $ S $ T;
    40 infix 0 ==;  fun S ==  T = %%:"==" $ S $ T;
    41 infix 1 ===; fun S === T = %%:"op =" $ S $ T;
    41 infix 1 ===; fun S === T = %%:"op =" $ S $ T;
    42 infix 9 `  ; fun f ` x = %%:"Cfun.Rep_CFun" $ f $ x;
    42 infix 9 `  ; fun f ` x = %%:"Cfun.Rep_CFun" $ f $ x;
    43 
    43 
    44 (* infers the type of a term *)
    44 (* infers the type of a term *)
    45 (* similar to Theory.inferT_axm, but allows any type *)
    45 (* similar to Theory.inferT_axm, but allows any type, not just propT *)
    46 fun infer sg t =
    46 fun infer sg t =
    47   fst (Sign.infer_types (Sign.pp sg) sg (K NONE) (K NONE) [] true ([t],dummyT));
    47   fst (Sign.infer_types (Sign.pp sg) sg (K NONE) (K NONE) [] true ([t],dummyT));
    48 
    48 
    49 (* The next few functions build continuous lambda abstractions *)
    49 (* Similar to Term.lambda, but also allows abstraction over constants *)
    50 
       
    51 (* Similar to Term.lambda, but allows abstraction over constants *)
       
    52 fun lambda' (v as Free (x, T)) t = Abs (x, T, abstract_over (v, t))
    50 fun lambda' (v as Free (x, T)) t = Abs (x, T, abstract_over (v, t))
    53   | lambda' (v as Var ((x, _), T)) t = Abs (x, T, abstract_over (v, t))
    51   | lambda' (v as Var ((x, _), T)) t = Abs (x, T, abstract_over (v, t))
    54   | lambda' (v as Const (x, T)) t = Abs (Sign.base_name x, T, abstract_over (v, t))
    52   | lambda' (v as Const (x, T)) t = Abs (Sign.base_name x, T, abstract_over (v, t))
    55   | lambda' v t = raise TERM ("lambda'", [v, t]);
    53   | lambda' v t = raise TERM ("lambda'", [v, t]);
    56 
    54 
    71 fun mk_ctuple [] = %%:"UU"
    69 fun mk_ctuple [] = %%:"UU"
    72 |   mk_ctuple (t::[]) = t
    70 |   mk_ctuple (t::[]) = t
    73 |   mk_ctuple (t::ts) = %%:"Cprod.cpair"`t`(mk_ctuple ts);
    71 |   mk_ctuple (t::ts) = %%:"Cprod.cpair"`t`(mk_ctuple ts);
    74 
    72 
    75 (*************************************************************************)
    73 (*************************************************************************)
    76 (************************ fixed-point definitions ************************)
    74 (************* fixed-point definitions and unfolding theorems ************)
    77 (*************************************************************************)
    75 (*************************************************************************)
    78 
    76 
    79 fun add_fixdefs eqs thy =
    77 fun add_fixdefs eqs thy =
    80   let
    78   let
    81     val (lhss,rhss) = ListPair.unzip (map dest_eqs eqs);
    79     val (lhss,rhss) = ListPair.unzip (map dest_eqs eqs);
   222     val thmss = ListPair.zip (thm_names, rew_thmss);
   220     val thmss = ListPair.zip (thm_names, rew_thmss);
   223   in
   221   in
   224     (#1 o PureThy.add_thmss (map Thm.no_attributes thmss)) thy
   222     (#1 o PureThy.add_thmss (map Thm.no_attributes thmss)) thy
   225   end;
   223   end;
   226 
   224 
   227 (* this proves the def without fix is a theorem, this uses the fixpoint def *)
       
   228 (*
       
   229 fun make_simp name eqs ct fixdef_thm thy' = 
       
   230   let
       
   231     val ss = simpset_of thy';
       
   232     val eq_thm = fixdef_thm RS fix_eq2;
       
   233     val ind_thm = fixdef_thm RS def_fix_ind;
       
   234     val rew_thms = prove_list thy' unfold_thm eqs;
       
   235     val thmss =
       
   236       [ (basename^"_unfold", [unfold_thm])
       
   237       , (basename^"_ind", [ind_thm])
       
   238       , (basename^"_rews", rew_thms) ]
       
   239   in
       
   240     (#1 o PureThy.add_thmss (map Thm.no_attributes thmss)) thy'
       
   241   end;
       
   242 *)
       
   243 (*************************************************************************)
   225 (*************************************************************************)
   244 (************************* Main fixrec function **************************)
   226 (************************* Main fixrec function **************************)
   245 (*************************************************************************)
   227 (*************************************************************************)
   246 
   228 
   247 (* this calls the main processing function and then returns the new state *)
   229 (* this calls the main processing function and then returns the new state *)
   258 
   240 
   259 (*************************************************************************)
   241 (*************************************************************************)
   260 (******************************** Fixpat *********************************)
   242 (******************************** Fixpat *********************************)
   261 (*************************************************************************)
   243 (*************************************************************************)
   262 
   244 
   263 fun fix_pat name pat thy = 
   245 fun fix_pat thy pat = 
   264   let
   246   let
   265     val sign = sign_of thy;
   247     val sg = sign_of thy;
   266     val t = term_of (Thm.read_cterm sign (pat, dummyT));
   248     val t = term_of (Thm.read_cterm sg (pat, dummyT));
   267     val T = fastype_of t;
   249     val T = fastype_of t;
   268     val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
   250     val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
   269     fun head_const (Const ("Cfun.Rep_CFun",_) $ f $ t) = head_const f
   251     fun head_const (Const ("Cfun.Rep_CFun",_) $ f $ t) = head_const f
   270       | head_const (Const (c,_)) = c
   252       | head_const (Const (c,_)) = c
   271       | head_const _ = sys_error "FIXPAT: function is not declared as constant in theory";
   253       | head_const _ = sys_error "FIXPAT: function is not declared as constant in theory";
   272     val c = head_const t;
   254     val c = head_const t;
   273     val unfold_thm = Goals.get_thm thy (c^"_unfold");
   255     val unfold_thm = Goals.get_thm thy (c^"_unfold");
   274     val thm = prove_goalw_cterm [] (cterm_of sign eq)
   256     val rew = prove_goalw_cterm [] (cterm_of sg eq)
   275           (fn _ => [stac unfold_thm 1, simp_tac (simpset_of thy) 1]);
   257           (fn _ => [stac unfold_thm 1, simp_tac (simpset_of thy) 1]);
   276     val _ = print_thm thm;
   258   in rew end;
   277   in
   259 
   278     (#1 o PureThy.add_thmss [Thm.no_attributes (name, [thm])]) thy
   260 fun add_fixpat pats thy = 
   279   end;
   261   let
   280 
   262     val ((names, srcss), strings) = apfst ListPair.unzip (ListPair.unzip pats);
   281 fun add_fixpat (name,pat) = fix_pat name pat;
   263     val atts = map (map (Attrib.global_attribute thy)) srcss;
       
   264     val rews = map (fix_pat thy) strings;
       
   265     val (thy', _) = PureThy.add_thms ((names ~~ rews) ~~ atts) thy;
       
   266   in thy' end;
   282 
   267 
   283 (*************************************************************************)
   268 (*************************************************************************)
   284 (******************************** Parsers ********************************)
   269 (******************************** Parsers ********************************)
   285 (*************************************************************************)
   270 (*************************************************************************)
   286 
   271 
   289 val fixrec_decl = P.and_list1 (Scan.repeat1 P.prop);
   274 val fixrec_decl = P.and_list1 (Scan.repeat1 P.prop);
   290 
   275 
   291 (* this builds a parser for a new keyword, fixrec, whose functionality 
   276 (* this builds a parser for a new keyword, fixrec, whose functionality 
   292 is defined by add_fixrec *)
   277 is defined by add_fixrec *)
   293 val fixrecP =
   278 val fixrecP =
   294   OuterSyntax.command "fixrec" "parser for fixrec functions" K.thy_decl
   279   OuterSyntax.command "fixrec" "define recursive functions (HOLCF)" K.thy_decl
   295     (fixrec_decl >> (Toplevel.theory o add_fixrec));
   280     (fixrec_decl >> (Toplevel.theory o add_fixrec));
   296 
   281 
   297 (* this adds the parser for fixrec to the syntax *)
   282 (* this adds the parser for fixrec to the syntax *)
   298 val _ = OuterSyntax.add_parsers [fixrecP];
   283 val _ = OuterSyntax.add_parsers [fixrecP];
   299 
   284 
   300 (* fixpat parser *)
   285 (* fixpat parser *)
   301 val fixpat_decl = P.name -- P.prop;
   286 (*val fixpat_decl = P.name -- P.prop;*)
       
   287 val fixpat_decl = Scan.repeat1 (P.thm_name ":" -- P.prop);
   302 
   288 
   303 val fixpatP =
   289 val fixpatP =
   304   OuterSyntax.command "fixpat" "testing out this parser" K.thy_decl
   290   OuterSyntax.command "fixpat" "define rewrites for fixrec functions" K.thy_decl
   305     (fixpat_decl >> (Toplevel.theory o add_fixpat));
   291     (fixpat_decl >> (Toplevel.theory o add_fixpat));
   306 
   292 
   307 val _ = OuterSyntax.add_parsers [fixpatP];
   293 val _ = OuterSyntax.add_parsers [fixpatP];
   308 
   294 
   309 end; (* local structure *)
   295 end; (* local structure *)