src/HOL/Tools/BNF/Tools/bnf_gfp_util.ML
author blanchet
Mon Jan 20 18:24:56 2014 +0100 (2014-01-20)
changeset 55058 4e700eb471d4
parent 54841 src/HOL/BNF/Tools/bnf_gfp_util.ML@af71b753c459
permissions -rw-r--r--
moved BNF files to 'HOL'
     1 (*  Title:      HOL/BNF/Tools/bnf_gfp_util.ML
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Copyright   2012
     4 
     5 Library for the codatatype construction.
     6 *)
     7 
     8 signature BNF_GFP_UTIL =
     9 sig
    10   val mk_rec_simps: int -> thm -> thm list -> thm list list
    11 
    12   val dest_listT: typ -> typ
    13 
    14   val mk_Cons: term -> term -> term
    15   val mk_Shift: term -> term -> term
    16   val mk_Succ: term -> term -> term
    17   val mk_Times: term * term -> term
    18   val mk_append: term * term -> term
    19   val mk_congruent: term -> term -> term
    20   val mk_clists: term -> term
    21   val mk_Id_on: term -> term
    22   val mk_in_rel: term -> term
    23   val mk_equiv: term -> term -> term
    24   val mk_fromCard: term -> term -> term
    25   val mk_list_rec: term -> term -> term
    26   val mk_nat_rec: term -> term -> term
    27   val mk_prefCl: term -> term
    28   val mk_prefixeq: term -> term -> term
    29   val mk_proj: term -> term
    30   val mk_quotient: term -> term -> term
    31   val mk_shift: term -> term -> term
    32   val mk_size: term -> term
    33   val mk_toCard: term -> term -> term
    34   val mk_undefined: typ -> term
    35   val mk_univ: term -> term
    36 
    37   val mk_specN: int -> thm -> thm
    38 
    39   val mk_InN_Field: int -> int -> thm
    40   val mk_InN_inject: int -> int -> thm
    41   val mk_InN_not_InM: int -> int -> thm
    42 end;
    43 
    44 structure BNF_GFP_Util : BNF_GFP_UTIL =
    45 struct
    46 
    47 open BNF_Util
    48 
    49 val mk_append = HOLogic.mk_binop @{const_name append};
    50 
    51 fun mk_equiv B R =
    52   Const (@{const_name equiv}, fastype_of B --> fastype_of R --> HOLogic.boolT) $ B $ R;
    53 
    54 fun mk_Sigma (A, B) =
    55   let
    56     val AT = fastype_of A;
    57     val BT = fastype_of B;
    58     val ABT = mk_relT (HOLogic.dest_setT AT, HOLogic.dest_setT (range_type BT));
    59   in Const (@{const_name Sigma}, AT --> BT --> ABT) $ A $ B end;
    60 
    61 fun mk_Id_on A =
    62   let
    63     val AT = fastype_of A;
    64     val AAT = mk_relT (HOLogic.dest_setT AT, HOLogic.dest_setT AT);
    65   in Const (@{const_name Id_on}, AT --> AAT) $ A end;
    66 
    67 fun mk_in_rel R =
    68   let
    69     val ((A, B), RT) = `dest_relT (fastype_of R);
    70   in Const (@{const_name in_rel}, RT --> mk_pred2T A B) $ R end;
    71 
    72 fun mk_Times (A, B) =
    73   let val AT = HOLogic.dest_setT (fastype_of A);
    74   in mk_Sigma (A, Term.absdummy AT B) end;
    75 
    76 fun dest_listT (Type (@{type_name list}, [T])) = T
    77   | dest_listT T = raise TYPE ("dest_setT: set type expected", [T], []);
    78 
    79 fun mk_Succ Kl kl =
    80   let val T = fastype_of kl;
    81   in
    82     Const (@{const_name Succ},
    83       HOLogic.mk_setT T --> T --> HOLogic.mk_setT (dest_listT T)) $ Kl $ kl
    84   end;
    85 
    86 fun mk_Shift Kl k =
    87   let val T = fastype_of Kl;
    88   in
    89     Const (@{const_name Shift}, T --> dest_listT (HOLogic.dest_setT T) --> T) $ Kl $ k
    90   end;
    91 
    92 fun mk_shift lab k =
    93   let val T = fastype_of lab;
    94   in
    95     Const (@{const_name shift}, T --> dest_listT (Term.domain_type T) --> T) $ lab $ k
    96   end;
    97 
    98 fun mk_prefCl A =
    99   Const (@{const_name prefCl}, fastype_of A --> HOLogic.boolT) $ A;
   100 
   101 fun mk_prefixeq xs ys =
   102   Const (@{const_name prefixeq}, fastype_of xs --> fastype_of ys --> HOLogic.boolT) $ xs $ ys;
   103 
   104 fun mk_clists r =
   105   let val T = fastype_of r;
   106   in Const (@{const_name clists}, T --> mk_relT (`I (HOLogic.listT (fst (dest_relT T))))) $ r end;
   107 
   108 fun mk_toCard A r =
   109   let
   110     val AT = fastype_of A;
   111     val rT = fastype_of r;
   112   in
   113     Const (@{const_name toCard},
   114       AT --> rT --> HOLogic.dest_setT AT --> fst (dest_relT rT)) $ A $ r
   115   end;
   116 
   117 fun mk_fromCard A r =
   118   let
   119     val AT = fastype_of A;
   120     val rT = fastype_of r;
   121   in
   122     Const (@{const_name fromCard},
   123       AT --> rT --> fst (dest_relT rT) --> HOLogic.dest_setT AT) $ A $ r
   124   end;
   125 
   126 fun mk_Cons x xs =
   127   let val T = fastype_of xs;
   128   in Const (@{const_name Cons}, dest_listT T --> T --> T) $ x $ xs end;
   129 
   130 fun mk_size t = HOLogic.size_const (fastype_of t) $ t;
   131 
   132 fun mk_quotient A R =
   133   let val T = fastype_of A;
   134   in Const (@{const_name quotient}, T --> fastype_of R --> HOLogic.mk_setT T) $ A $ R end;
   135 
   136 fun mk_proj R =
   137   let val ((AT, BT), T) = `dest_relT (fastype_of R);
   138   in Const (@{const_name proj}, T --> AT --> HOLogic.mk_setT BT) $ R end;
   139 
   140 fun mk_univ f =
   141   let val ((AT, BT), T) = `dest_funT (fastype_of f);
   142   in Const (@{const_name univ}, T --> HOLogic.mk_setT AT --> BT) $ f end;
   143 
   144 fun mk_congruent R f =
   145   Const (@{const_name congruent}, fastype_of R --> fastype_of f --> HOLogic.boolT) $ R $ f;
   146 
   147 fun mk_undefined T = Const (@{const_name undefined}, T);
   148 
   149 fun mk_nat_rec Zero Suc =
   150   let val T = fastype_of Zero;
   151   in Const (@{const_name nat_rec}, T --> fastype_of Suc --> HOLogic.natT --> T) $ Zero $ Suc end;
   152 
   153 fun mk_list_rec Nil Cons =
   154   let
   155     val T = fastype_of Nil;
   156     val (U, consT) = `(Term.domain_type) (fastype_of Cons);
   157   in
   158     Const (@{const_name list_rec}, T --> consT --> HOLogic.listT U --> T) $ Nil $ Cons
   159   end;
   160 
   161 fun mk_InN_not_InM 1 _ = @{thm Inl_not_Inr}
   162   | mk_InN_not_InM n m =
   163     if n > m then mk_InN_not_InM m n RS @{thm not_sym}
   164     else mk_InN_not_InM (n - 1) (m - 1) RS @{thm not_arg_cong_Inr};
   165 
   166 fun mk_InN_Field 1 1 = @{thm TrueE[OF TrueI]}
   167   | mk_InN_Field _ 1 = @{thm Inl_Field_csum}
   168   | mk_InN_Field 2 2 = @{thm Inr_Field_csum}
   169   | mk_InN_Field n m = mk_InN_Field (n - 1) (m - 1) RS @{thm Inr_Field_csum};
   170 
   171 fun mk_InN_inject 1 _ = @{thm TrueE[OF TrueI]}
   172   | mk_InN_inject _ 1 = @{thm Inl_inject}
   173   | mk_InN_inject 2 2 = @{thm Inr_inject}
   174   | mk_InN_inject n m = @{thm Inr_inject} RS mk_InN_inject (n - 1) (m - 1);
   175 
   176 fun mk_specN 0 thm = thm
   177   | mk_specN n thm = mk_specN (n - 1) (thm RS spec);
   178 
   179 fun mk_rec_simps n rec_thm defs = map (fn i =>
   180   map (fn def => def RS rec_thm RS mk_nthI n i RS fun_cong) defs) (1 upto n);
   181 
   182 end;