src/HOLCF/domain/axioms.ML
author wenzelm
Thu Jul 14 19:28:24 2005 +0200 (2005-07-14)
changeset 16842 5979c46853d1
parent 16778 2162c0de4673
child 17811 10ebcd7032c1
permissions -rw-r--r--
tuned;
oheimb@2453
     1
(*  Title:      HOLCF/domain/axioms.ML
oheimb@2453
     2
    ID:         $Id$
wenzelm@12030
     3
    Author:     David von Oheimb
oheimb@2453
     4
wenzelm@12030
     5
Syntax generator for domain section.
regensbu@1274
     6
*)
regensbu@1274
     7
regensbu@1274
     8
structure Domain_Axioms = struct
regensbu@1274
     9
regensbu@1274
    10
local
regensbu@1274
    11
regensbu@1274
    12
open Domain_Library;
regensbu@1274
    13
infixr 0 ===>;infixr 0 ==>;infix 0 == ; 
regensbu@1274
    14
infix 1 ===; infix 1 ~= ; infix 1 <<; infix 1 ~<<;
regensbu@1274
    15
infix 9 `   ; infix 9 `% ; infix 9 `%%; infixr 9 oo;
regensbu@1274
    16
regensbu@1274
    17
fun infer_types thy' = map (inferT_axm (sign_of thy'));
regensbu@1274
    18
regensbu@1274
    19
fun calc_axioms comp_dname (eqs : eq list) n (((dname,_),cons) : eq)=
regensbu@1274
    20
let
regensbu@1274
    21
oheimb@1637
    22
(* ----- axioms and definitions concerning the isomorphism ------------------ *)
regensbu@1274
    23
berghofe@11531
    24
  val dc_abs = %%:(dname^"_abs");
berghofe@11531
    25
  val dc_rep = %%:(dname^"_rep");
regensbu@1274
    26
  val x_name'= "x";
regensbu@1274
    27
  val x_name = idx_name eqs x_name' (n+1);
oheimb@4008
    28
  val dnam = Sign.base_name dname;
regensbu@1274
    29
berghofe@11531
    30
 val abs_iso_ax = ("abs_iso" ,mk_trp(dc_rep`(dc_abs`%x_name')=== %:x_name'));
berghofe@11531
    31
 val rep_iso_ax = ("rep_iso" ,mk_trp(dc_abs`(dc_rep`%x_name')=== %:x_name'));
regensbu@1274
    32
berghofe@11531
    33
  val when_def = ("when_def",%%:(dname^"_when") == 
skalberg@15574
    34
     foldr (uncurry /\ ) (/\x_name'((when_body cons (fn (x,y) =>
skalberg@15574
    35
				Bound(1+length cons+x-y)))`(dc_rep`Bound 0))) (when_funs cons));
regensbu@1274
    36
oheimb@1637
    37
  fun con_def outer recu m n (_,args) = let
wenzelm@16842
    38
     fun idxs z x arg = (if is_lazy arg then fn t => %%:upN`t else I)
oheimb@4755
    39
			(if recu andalso is_rec arg then (cproj (Bound z) eqs
oheimb@4755
    40
				  (rec_of arg))`Bound(z-x) else Bound(z-x));
huffman@16778
    41
     fun parms [] = %%:ONE_N
huffman@16778
    42
     |   parms vs = foldr'(fn(x,t)=> %%:spairN`x`t)(mapn (idxs(length vs))1 vs);
oheimb@1637
    43
     fun inj y 1 _ = y
huffman@16778
    44
     |   inj y _ 0 = %%:sinlN`y
huffman@16778
    45
     |   inj y i j = %%:sinrN`(inj y (i-1) (j-1));
skalberg@15574
    46
  in foldr /\# (outer (inj (parms args) m n)) args end;
regensbu@1274
    47
berghofe@11531
    48
  val copy_def = ("copy_def", %%:(dname^"_copy") == /\"f" (dc_abs oo 
skalberg@15570
    49
	Library.foldl (op `) (%%:(dname^"_when") , 
wenzelm@16842
    50
	              mapn (con_def I true (length cons)) 0 cons)));
regensbu@1274
    51
oheimb@1637
    52
(* -- definitions concerning the constructors, discriminators and selectors - *)
oheimb@1637
    53
oheimb@4043
    54
  val con_defs = mapn (fn n => fn (con,args) => (extern_name con ^"_def",  
berghofe@11531
    55
  %%:con == con_def (fn t => dc_abs`t) false (length cons) n (con,args))) 0 cons;
regensbu@1274
    56
oheimb@4043
    57
  val dis_defs = let
berghofe@11531
    58
	fun ddef (con,_) = (dis_name con ^"_def",%%:(dis_name con) == 
berghofe@11531
    59
		 mk_cRep_CFun(%%:(dname^"_when"),map 
skalberg@15574
    60
			(fn (con',args) => (foldr /\#
huffman@16778
    61
			   (if con'=con then %%:TT_N else %%:FF_N) args)) cons))
oheimb@1637
    62
	in map ddef cons end;
regensbu@1274
    63
huffman@16224
    64
  val mat_defs = let
huffman@16224
    65
	fun mdef (con,_) = (mat_name con ^"_def",%%:(mat_name con) == 
huffman@16224
    66
		 mk_cRep_CFun(%%:(dname^"_when"),map 
huffman@16224
    67
			(fn (con',args) => (foldr /\#
huffman@16778
    68
			   (if con'=con
huffman@16778
    69
                               then %%:returnN`(mk_ctuple (map (bound_arg args) args))
huffman@16778
    70
                               else %%:failN) args)) cons))
huffman@16224
    71
	in map mdef cons end;
huffman@16224
    72
oheimb@4043
    73
  val sel_defs = let
huffman@16394
    74
	fun sdef con n arg = Option.map (fn sel => (sel^"_def",%%:sel == 
berghofe@11531
    75
		 mk_cRep_CFun(%%:(dname^"_when"),map 
huffman@16778
    76
			(fn (con',args) => if con'<>con then UU else
huffman@16394
    77
			 foldr /\# (Bound (length args - n)) args) cons))) (sel_of arg);
wenzelm@16842
    78
	in List.mapPartial I (List.concat(map (fn (con,args) => mapn (sdef con) 1 args) cons)) end;
regensbu@1274
    79
regensbu@1274
    80
oheimb@1637
    81
(* ----- axiom and definitions concerning induction ------------------------- *)
regensbu@1274
    82
huffman@16778
    83
  val reach_ax = ("reach", mk_trp(cproj (%%:fixN`%%(comp_dname^"_copy")) eqs n
berghofe@11531
    84
					`%x_name === %:x_name));
berghofe@11531
    85
  val take_def = ("take_def",%%:(dname^"_take") == mk_lam("n",cproj' 
huffman@16778
    86
	     (%%:iterateN $ Bound 0 $ %%:(comp_dname^"_copy") $ UU) eqs n));
berghofe@11531
    87
  val finite_def = ("finite_def",%%:(dname^"_finite") == mk_lam(x_name,
berghofe@11531
    88
	mk_ex("n",(%%:(dname^"_take") $ Bound 0)`Bound 1 === Bound 1)));
regensbu@1274
    89
oheimb@4043
    90
in (dnam,
oheimb@4043
    91
    [abs_iso_ax, rep_iso_ax, reach_ax],
oheimb@4043
    92
    [when_def, copy_def] @
huffman@16224
    93
     con_defs @ dis_defs @ mat_defs @ sel_defs @
oheimb@4043
    94
    [take_def, finite_def])
oheimb@1637
    95
end; (* let *)
regensbu@1274
    96
wenzelm@8438
    97
fun add_axioms_i x = #1 o PureThy.add_axioms_i (map Thm.no_attributes x);
wenzelm@16332
    98
fun add_axioms_infer axms thy = add_axioms_i (infer_types thy axms) thy;
regensbu@1274
    99
regensbu@1274
   100
in (* local *)
regensbu@1274
   101
oheimb@4008
   102
fun add_axioms (comp_dnam, eqs : eq list) thy' = let
oheimb@4008
   103
  val comp_dname = Sign.full_name (sign_of thy') comp_dnam;
regensbu@1274
   104
  val dnames = map (fst o fst) eqs;
regensbu@1274
   105
  val x_name = idx_name dnames "x"; 
berghofe@11531
   106
  fun copy_app dname = %%:(dname^"_copy")`Bound 0;
berghofe@11531
   107
  val copy_def = ("copy_def" , %%:(comp_dname^"_copy") ==
oheimb@1637
   108
				    /\"f"(foldr' cpair (map copy_app dnames)));
berghofe@11531
   109
  val bisim_def = ("bisim_def",%%:(comp_dname^"_bisim")==mk_lam("R",
regensbu@1274
   110
    let
regensbu@1274
   111
      fun one_con (con,args) = let
oheimb@1637
   112
	val nonrec_args = filter_out is_rec args;
skalberg@15570
   113
	val    rec_args = List.filter     is_rec args;
oheimb@1637
   114
	val    recs_cnt = length rec_args;
oheimb@1637
   115
	val allargs     = nonrec_args @ rec_args
oheimb@1637
   116
				      @ map (upd_vname (fn s=> s^"'")) rec_args;
oheimb@1637
   117
	val allvns      = map vname allargs;
oheimb@1637
   118
	fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
oheimb@1637
   119
	val vns1        = map (vname_arg "" ) args;
oheimb@1637
   120
	val vns2        = map (vname_arg "'") args;
oheimb@1637
   121
	val allargs_cnt = length nonrec_args + 2*recs_cnt;
oheimb@1637
   122
	val rec_idxs    = (recs_cnt-1) downto 0;
oheimb@1637
   123
	val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
oheimb@1637
   124
					 (allargs~~((allargs_cnt-1) downto 0)));
oheimb@4755
   125
	fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
oheimb@1637
   126
			   Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
skalberg@15574
   127
	val capps = foldr mk_conj (mk_conj(
berghofe@11531
   128
	   Bound(allargs_cnt+1)===mk_cRep_CFun(%%:con,map (bound_arg allvns) vns1),
skalberg@15574
   129
	   Bound(allargs_cnt+0)===mk_cRep_CFun(%%:con,map (bound_arg allvns) vns2)))
skalberg@15574
   130
           (mapn rel_app 1 rec_args);
skalberg@15574
   131
        in foldr mk_ex (Library.foldr mk_conj 
skalberg@15574
   132
			      (map (defined o Bound) nonlazy_idxs,capps)) allvns end;
oheimb@1637
   133
      fun one_comp n (_,cons) =mk_all(x_name(n+1),mk_all(x_name(n+1)^"'",mk_imp(
oheimb@4755
   134
	 		proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
oheimb@1637
   135
         		foldr' mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
oheimb@1637
   136
					::map one_con cons))));
regensbu@1274
   137
    in foldr' mk_conj (mapn one_comp 0 eqs)end ));
wenzelm@16332
   138
  fun add_one (thy,(dnam,axs,dfs)) = thy
oheimb@4043
   139
	|> Theory.add_path dnam
wenzelm@16332
   140
	|> add_axioms_infer dfs(*add_defs_i*)
wenzelm@16332
   141
	|> add_axioms_infer axs
wenzelm@4862
   142
	|> Theory.parent_path;
skalberg@15570
   143
  val thy = Library.foldl add_one (thy', mapn (calc_axioms comp_dname eqs) 0 eqs);
oheimb@4043
   144
in thy |> Theory.add_path comp_dnam  
wenzelm@16332
   145
       |> add_axioms_infer (bisim_def::(if length eqs>1 then [copy_def] else []))
wenzelm@4862
   146
       |> Theory.parent_path
oheimb@4043
   147
end;
regensbu@1274
   148
regensbu@1274
   149
end; (* local *)
regensbu@1274
   150
end; (* struct *)