src/HOLCF/Tools/domain/domain_axioms.ML
author huffman
Tue Jan 29 18:00:12 2008 +0100 (2008-01-29)
changeset 26012 f6917792f8a4
parent 24712 64ed05609568
child 27691 ce171cbd4b93
permissions -rw-r--r--
new term-building combinators
wenzelm@23152
     1
(*  Title:      HOLCF/Tools/domain/domain_axioms.ML
wenzelm@23152
     2
    ID:         $Id$
wenzelm@23152
     3
    Author:     David von Oheimb
wenzelm@23152
     4
wenzelm@23152
     5
Syntax generator for domain command.
wenzelm@23152
     6
*)
wenzelm@23152
     7
wenzelm@23152
     8
structure Domain_Axioms = struct
wenzelm@23152
     9
wenzelm@23152
    10
local
wenzelm@23152
    11
wenzelm@23152
    12
open Domain_Library;
wenzelm@23152
    13
infixr 0 ===>;infixr 0 ==>;infix 0 == ; 
wenzelm@23152
    14
infix 1 ===; infix 1 ~= ; infix 1 <<; infix 1 ~<<;
wenzelm@23152
    15
infix 9 `   ; infix 9 `% ; infix 9 `%%; infixr 9 oo;
wenzelm@23152
    16
wenzelm@23152
    17
fun calc_axioms comp_dname (eqs : eq list) n (((dname,_),cons) : eq)=
wenzelm@23152
    18
let
wenzelm@23152
    19
wenzelm@23152
    20
(* ----- axioms and definitions concerning the isomorphism ------------------ *)
wenzelm@23152
    21
wenzelm@23152
    22
  val dc_abs = %%:(dname^"_abs");
wenzelm@23152
    23
  val dc_rep = %%:(dname^"_rep");
wenzelm@23152
    24
  val x_name'= "x";
wenzelm@23152
    25
  val x_name = idx_name eqs x_name' (n+1);
wenzelm@23152
    26
  val dnam = Sign.base_name dname;
wenzelm@23152
    27
wenzelm@23152
    28
  val abs_iso_ax = ("abs_iso", mk_trp(dc_rep`(dc_abs`%x_name') === %:x_name'));
wenzelm@23152
    29
  val rep_iso_ax = ("rep_iso", mk_trp(dc_abs`(dc_rep`%x_name') === %:x_name'));
wenzelm@23152
    30
wenzelm@23152
    31
  val when_def = ("when_def",%%:(dname^"_when") == 
wenzelm@23152
    32
     foldr (uncurry /\ ) (/\x_name'((when_body cons (fn (x,y) =>
wenzelm@23152
    33
				Bound(1+length cons+x-y)))`(dc_rep`Bound 0))) (when_funs cons));
wenzelm@23152
    34
  
wenzelm@23152
    35
  val copy_def = let
wenzelm@23152
    36
    fun idxs z x arg = if is_rec arg
wenzelm@23152
    37
			 then (cproj (Bound z) eqs (rec_of arg))`Bound(z-x)
wenzelm@23152
    38
			 else Bound(z-x);
wenzelm@23152
    39
    fun one_con (con,args) =
wenzelm@23152
    40
        foldr /\# (list_ccomb (%%:con, mapn (idxs (length args)) 1 args)) args;
wenzelm@23152
    41
  in ("copy_def", %%:(dname^"_copy") ==
wenzelm@23152
    42
       /\"f" (list_ccomb (%%:(dname^"_when"), map one_con cons))) end;
wenzelm@23152
    43
wenzelm@23152
    44
(* -- definitions concerning the constructors, discriminators and selectors - *)
wenzelm@23152
    45
wenzelm@23152
    46
  fun con_def m n (_,args) = let
huffman@26012
    47
    fun idxs z x arg = (if is_lazy arg then mk_up else I) (Bound(z-x));
wenzelm@23152
    48
    fun parms vs = mk_stuple (mapn (idxs(length vs)) 1 vs);
wenzelm@23152
    49
    fun inj y 1 _ = y
huffman@26012
    50
    |   inj y _ 0 = mk_sinl y
huffman@26012
    51
    |   inj y i j = mk_sinr (inj y (i-1) (j-1));
wenzelm@23152
    52
  in foldr /\# (dc_abs`(inj (parms args) m n)) args end;
wenzelm@23152
    53
  
wenzelm@23152
    54
  val con_defs = mapn (fn n => fn (con,args) =>
wenzelm@23152
    55
    (extern_name con ^"_def", %%:con == con_def (length cons) n (con,args))) 0 cons;
wenzelm@23152
    56
  
wenzelm@23152
    57
  val dis_defs = let
wenzelm@23152
    58
	fun ddef (con,_) = (dis_name con ^"_def",%%:(dis_name con) == 
wenzelm@23152
    59
		 list_ccomb(%%:(dname^"_when"),map 
wenzelm@23152
    60
			(fn (con',args) => (foldr /\#
huffman@26012
    61
			   (if con'=con then TT else FF) args)) cons))
wenzelm@23152
    62
	in map ddef cons end;
wenzelm@23152
    63
wenzelm@23152
    64
  val mat_defs = let
wenzelm@23152
    65
	fun mdef (con,_) = (mat_name con ^"_def",%%:(mat_name con) == 
wenzelm@23152
    66
		 list_ccomb(%%:(dname^"_when"),map 
wenzelm@23152
    67
			(fn (con',args) => (foldr /\#
wenzelm@23152
    68
			   (if con'=con
huffman@26012
    69
                               then mk_return (mk_ctuple (map (bound_arg args) args))
huffman@26012
    70
                               else mk_fail) args)) cons))
wenzelm@23152
    71
	in map mdef cons end;
wenzelm@23152
    72
wenzelm@23152
    73
  val pat_defs =
wenzelm@23152
    74
    let
wenzelm@23152
    75
      fun pdef (con,args) =
wenzelm@23152
    76
        let
wenzelm@23152
    77
          val ps = mapn (fn n => fn _ => %:("pat" ^ string_of_int n)) 1 args;
wenzelm@23152
    78
          val xs = map (bound_arg args) args;
wenzelm@23152
    79
          val r = Bound (length args);
huffman@26012
    80
          val rhs = case args of [] => mk_return HOLogic.unit
huffman@26012
    81
                                | _ => mk_ctuple_pat ps ` mk_ctuple xs;
huffman@26012
    82
          fun one_con (con',args') = foldr /\# (if con'=con then rhs else mk_fail) args';
wenzelm@23152
    83
        in (pat_name con ^"_def", list_comb (%%:(pat_name con), ps) == 
wenzelm@23152
    84
               list_ccomb(%%:(dname^"_when"), map one_con cons))
wenzelm@23152
    85
        end
wenzelm@23152
    86
    in map pdef cons end;
wenzelm@23152
    87
wenzelm@23152
    88
  val sel_defs = let
wenzelm@23152
    89
	fun sdef con n arg = Option.map (fn sel => (sel^"_def",%%:sel == 
wenzelm@23152
    90
		 list_ccomb(%%:(dname^"_when"),map 
wenzelm@23152
    91
			(fn (con',args) => if con'<>con then UU else
wenzelm@23152
    92
			 foldr /\# (Bound (length args - n)) args) cons))) (sel_of arg);
wenzelm@23152
    93
	in List.mapPartial I (List.concat(map (fn (con,args) => mapn (sdef con) 1 args) cons)) end;
wenzelm@23152
    94
wenzelm@23152
    95
wenzelm@23152
    96
(* ----- axiom and definitions concerning induction ------------------------- *)
wenzelm@23152
    97
huffman@26012
    98
  val reach_ax = ("reach", mk_trp(cproj (mk_fix (%%:(comp_dname^"_copy"))) eqs n
wenzelm@23152
    99
					`%x_name === %:x_name));
wenzelm@23152
   100
  val take_def = ("take_def",%%:(dname^"_take") == mk_lam("n",cproj
huffman@26012
   101
	     (mk_iterate (Bound 0, %%:(comp_dname^"_copy"), UU)) eqs n));
wenzelm@23152
   102
  val finite_def = ("finite_def",%%:(dname^"_finite") == mk_lam(x_name,
wenzelm@23152
   103
	mk_ex("n",(%%:(dname^"_take") $ Bound 0)`Bound 1 === Bound 1)));
wenzelm@23152
   104
wenzelm@23152
   105
in (dnam,
wenzelm@23152
   106
    [abs_iso_ax, rep_iso_ax, reach_ax],
wenzelm@23152
   107
    [when_def, copy_def] @
wenzelm@23152
   108
     con_defs @ dis_defs @ mat_defs @ pat_defs @ sel_defs @
wenzelm@23152
   109
    [take_def, finite_def])
wenzelm@23152
   110
end; (* let *)
wenzelm@23152
   111
wenzelm@23152
   112
fun infer_props thy = map (apsnd (FixrecPackage.legacy_infer_prop thy));
wenzelm@23152
   113
wenzelm@23152
   114
fun add_axioms_i x = snd o PureThy.add_axioms_i (map Thm.no_attributes x);
wenzelm@23152
   115
fun add_axioms_infer axms thy = add_axioms_i (infer_props thy axms) thy;
wenzelm@23152
   116
wenzelm@23152
   117
fun add_defs_i x = snd o (PureThy.add_defs_i false) (map Thm.no_attributes x);
wenzelm@23152
   118
fun add_defs_infer defs thy = add_defs_i (infer_props thy defs) thy;
wenzelm@23152
   119
wenzelm@23152
   120
in (* local *)
wenzelm@23152
   121
wenzelm@23152
   122
fun add_axioms (comp_dnam, eqs : eq list) thy' = let
wenzelm@23152
   123
  val comp_dname = Sign.full_name thy' comp_dnam;
wenzelm@23152
   124
  val dnames = map (fst o fst) eqs;
wenzelm@23152
   125
  val x_name = idx_name dnames "x"; 
wenzelm@23152
   126
  fun copy_app dname = %%:(dname^"_copy")`Bound 0;
wenzelm@23152
   127
  val copy_def = ("copy_def" , %%:(comp_dname^"_copy") ==
huffman@26012
   128
				    /\"f"(mk_ctuple (map copy_app dnames)));
wenzelm@23152
   129
  val bisim_def = ("bisim_def",%%:(comp_dname^"_bisim")==mk_lam("R",
wenzelm@23152
   130
    let
wenzelm@23152
   131
      fun one_con (con,args) = let
wenzelm@23152
   132
	val nonrec_args = filter_out is_rec args;
wenzelm@23152
   133
	val    rec_args = List.filter     is_rec args;
wenzelm@23152
   134
	val    recs_cnt = length rec_args;
wenzelm@23152
   135
	val allargs     = nonrec_args @ rec_args
wenzelm@23152
   136
				      @ map (upd_vname (fn s=> s^"'")) rec_args;
wenzelm@23152
   137
	val allvns      = map vname allargs;
wenzelm@23152
   138
	fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
wenzelm@23152
   139
	val vns1        = map (vname_arg "" ) args;
wenzelm@23152
   140
	val vns2        = map (vname_arg "'") args;
wenzelm@23152
   141
	val allargs_cnt = length nonrec_args + 2*recs_cnt;
wenzelm@23152
   142
	val rec_idxs    = (recs_cnt-1) downto 0;
wenzelm@23152
   143
	val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
wenzelm@23152
   144
					 (allargs~~((allargs_cnt-1) downto 0)));
wenzelm@23152
   145
	fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
wenzelm@23152
   146
			   Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
wenzelm@23152
   147
	val capps = foldr mk_conj (mk_conj(
wenzelm@23152
   148
	   Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1),
wenzelm@23152
   149
	   Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2)))
wenzelm@23152
   150
           (mapn rel_app 1 rec_args);
wenzelm@23152
   151
        in foldr mk_ex (Library.foldr mk_conj 
wenzelm@23152
   152
			      (map (defined o Bound) nonlazy_idxs,capps)) allvns end;
wenzelm@23152
   153
      fun one_comp n (_,cons) =mk_all(x_name(n+1),mk_all(x_name(n+1)^"'",mk_imp(
wenzelm@23152
   154
	 		proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
wenzelm@23152
   155
         		foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
wenzelm@23152
   156
					::map one_con cons))));
wenzelm@23152
   157
    in foldr1 mk_conj (mapn one_comp 0 eqs)end ));
wenzelm@23152
   158
  fun add_one (thy,(dnam,axs,dfs)) = thy
wenzelm@24712
   159
	|> Sign.add_path dnam
wenzelm@23152
   160
	|> add_defs_infer dfs
wenzelm@23152
   161
	|> add_axioms_infer axs
wenzelm@24712
   162
	|> Sign.parent_path;
wenzelm@23152
   163
  val thy = Library.foldl add_one (thy', mapn (calc_axioms comp_dname eqs) 0 eqs);
wenzelm@24712
   164
in thy |> Sign.add_path comp_dnam  
wenzelm@23152
   165
       |> add_defs_infer (bisim_def::(if length eqs>1 then [copy_def] else []))
wenzelm@24712
   166
       |> Sign.parent_path
wenzelm@23152
   167
end;
wenzelm@23152
   168
wenzelm@23152
   169
end; (* local *)
wenzelm@23152
   170
end; (* struct *)