src/HOL/HOLCF/Tools/Domain/domain.ML
changeset 44080 53d95b52954c
parent 44005 421f8bc19ce4
child 46947 b8c7eb0c2f89
equal deleted inserted replaced
44079:bcc60791b7b9 44080:53d95b52954c
    33 
    33 
    34 open HOLCF_Library
    34 open HOLCF_Library
    35 
    35 
    36 fun first  (x,_,_) = x
    36 fun first  (x,_,_) = x
    37 fun second (_,x,_) = x
    37 fun second (_,x,_) = x
    38 fun third  (_,_,x) = x
       
    39 
    38 
    40 (* ----- calls for building new thy and thms -------------------------------- *)
    39 (* ----- calls for building new thy and thms -------------------------------- *)
    41 
    40 
    42 type info =
    41 type info =
    43      Domain_Take_Proofs.iso_info list * Domain_Take_Proofs.take_induct_info
    42      Domain_Take_Proofs.iso_info list * Domain_Take_Proofs.take_induct_info
    76         map (fn (_,dbind,_,_) => dbind) raw_specs
    75         map (fn (_,dbind,_,_) => dbind) raw_specs
    77     val raw_rhss :
    76     val raw_rhss :
    78         (binding * (bool * binding option * 'b) list * mixfix) list list =
    77         (binding * (bool * binding option * 'b) list * mixfix) list list =
    79         map (fn (_,_,_,cons) => cons) raw_specs
    78         map (fn (_,_,_,cons) => cons) raw_specs
    80     val dtnvs' : (string * typ list) list =
    79     val dtnvs' : (string * typ list) list =
    81         map (fn (dbind, vs, mx) => (Sign.full_name thy dbind, vs)) dtnvs
    80         map (fn (dbind, vs, _) => (Sign.full_name thy dbind, vs)) dtnvs
    82 
    81 
    83     val all_cons = map (Binding.name_of o first) (flat raw_rhss)
    82     val all_cons = map (Binding.name_of o first) (flat raw_rhss)
    84     val test_dupl_cons =
    83     val _ =
    85       case duplicates (op =) all_cons of 
    84       case duplicates (op =) all_cons of 
    86         [] => false | dups => error ("Duplicate constructors: " 
    85         [] => false | dups => error ("Duplicate constructors: " 
    87                                       ^ commas_quote dups)
    86                                       ^ commas_quote dups)
    88     val all_sels =
    87     val all_sels =
    89       (map Binding.name_of o map_filter second o maps second) (flat raw_rhss)
    88       (map Binding.name_of o map_filter second o maps second) (flat raw_rhss)
    90     val test_dupl_sels =
    89     val _ =
    91       case duplicates (op =) all_sels of
    90       case duplicates (op =) all_sels of
    92         [] => false | dups => error("Duplicate selectors: "^commas_quote dups)
    91         [] => false | dups => error("Duplicate selectors: "^commas_quote dups)
    93 
    92 
    94     fun test_dupl_tvars s =
    93     fun test_dupl_tvars s =
    95       case duplicates (op =) (map(fst o dest_TFree)s) of
    94       case duplicates (op =) (map(fst o dest_TFree)s) of
    96         [] => false | dups => error("Duplicate type arguments: " 
    95         [] => false | dups => error("Duplicate type arguments: " 
    97                                     ^commas_quote dups)
    96                                     ^commas_quote dups)
    98     val test_dupl_tvars' = exists test_dupl_tvars (map snd dtnvs')
    97     val _ = exists test_dupl_tvars (map snd dtnvs')
    99 
    98 
   100     val sorts : (string * sort) list =
    99     val sorts : (string * sort) list =
   101       let val all_sorts = map (map dest_TFree o snd) dtnvs'
   100       let val all_sorts = map (map dest_TFree o snd) dtnvs'
   102       in
   101       in
   103         case distinct (eq_set (op =)) all_sorts of
   102         case distinct (eq_set (op =)) all_sorts of
   117 
   116 
   118     (* test for free type variables, illegal sort constraints on rhs,
   117     (* test for free type variables, illegal sort constraints on rhs,
   119        non-pcpo-types and invalid use of recursive type
   118        non-pcpo-types and invalid use of recursive type
   120        replace sorts in type variables on rhs *)
   119        replace sorts in type variables on rhs *)
   121     val rec_tab = Domain_Take_Proofs.get_rec_tab thy
   120     val rec_tab = Domain_Take_Proofs.get_rec_tab thy
   122     fun check_rec no_rec (T as TFree (v,_))  =
   121     fun check_rec _ (T as TFree (v,_))  =
   123         if AList.defined (op =) sorts v then T
   122         if AList.defined (op =) sorts v then T
   124         else error ("Free type variable " ^ quote v ^ " on rhs.")
   123         else error ("Free type variable " ^ quote v ^ " on rhs.")
   125       | check_rec no_rec (T as Type (s, Ts)) =
   124       | check_rec no_rec (T as Type (s, Ts)) =
   126         (case AList.lookup (op =) dtnvs' s of
   125         (case AList.lookup (op =) dtnvs' s of
   127           NONE =>
   126           NONE =>
   139                   NONE => T
   138                   NONE => T
   140                 | SOME c =>
   139                 | SOME c =>
   141                   error ("Illegal indirect recursion of type " ^ 
   140                   error ("Illegal indirect recursion of type " ^ 
   142                          quote (Syntax.string_of_typ_global tmp_thy T) ^
   141                          quote (Syntax.string_of_typ_global tmp_thy T) ^
   143                          " under type constructor " ^ quote c)))
   142                          " under type constructor " ^ quote c)))
   144       | check_rec no_rec (TVar _) = error "extender:check_rec"
   143       | check_rec _ (TVar _) = error "extender:check_rec"
   145 
   144 
   146     fun prep_arg (lazy, sel, raw_T) =
   145     fun prep_arg (lazy, sel, raw_T) =
   147       let
   146       let
   148         val T = prep_typ tmp_thy sorts raw_T
   147         val T = prep_typ tmp_thy sorts raw_T
   149         val _ = check_rec NONE T
   148         val _ = check_rec NONE T
   152     fun prep_con (b, args, mx) = (b, map prep_arg args, mx)
   151     fun prep_con (b, args, mx) = (b, map prep_arg args, mx)
   153     fun prep_rhs cons = map prep_con cons
   152     fun prep_rhs cons = map prep_con cons
   154     val rhss : (binding * (bool * binding option * typ) list * mixfix) list list =
   153     val rhss : (binding * (bool * binding option * typ) list * mixfix) list list =
   155         map prep_rhs raw_rhss
   154         map prep_rhs raw_rhss
   156 
   155 
   157     fun mk_arg_typ (lazy, dest_opt, T) = if lazy then mk_upT T else T
   156     fun mk_arg_typ (lazy, _, T) = if lazy then mk_upT T else T
   158     fun mk_con_typ (bind, args, mx) =
   157     fun mk_con_typ (_, args, _) =
   159         if null args then oneT else foldr1 mk_sprodT (map mk_arg_typ args)
   158         if null args then oneT else foldr1 mk_sprodT (map mk_arg_typ args)
   160     fun mk_rhs_typ cons = foldr1 mk_ssumT (map mk_con_typ cons)
   159     fun mk_rhs_typ cons = foldr1 mk_ssumT (map mk_con_typ cons)
   161 
   160 
   162     val absTs : typ list = map Type dtnvs'
   161     val absTs : typ list = map Type dtnvs'
   163     val repTs : typ list = map mk_rhs_typ rhss
   162     val repTs : typ list = map mk_rhs_typ rhss
   172         thy
   171         thy
   173           |> fold_map (fn ((dbind, cons), info) =>
   172           |> fold_map (fn ((dbind, cons), info) =>
   174                 Domain_Constructors.add_domain_constructors dbind cons info)
   173                 Domain_Constructors.add_domain_constructors dbind cons info)
   175              (dbinds ~~ rhss ~~ iso_infos)
   174              (dbinds ~~ rhss ~~ iso_infos)
   176 
   175 
   177     val (take_rews, thy) =
   176     val (_, thy) =
   178         Domain_Induction.comp_theorems
   177         Domain_Induction.comp_theorems
   179           dbinds take_info constr_infos thy
   178           dbinds take_info constr_infos thy
   180   in
   179   in
   181     thy
   180     thy
   182   end
   181   end
   183 
   182 
   184 fun define_isos (spec : (binding * mixfix * (typ * typ)) list) =
   183 fun define_isos (spec : (binding * mixfix * (typ * typ)) list) =
   185   let
   184   let
   186     fun prep (dbind, mx, (lhsT, rhsT)) =
   185     fun prep (dbind, mx, (lhsT, rhsT)) =
   187       let val (dname, vs) = dest_Type lhsT
   186       let val (_, vs) = dest_Type lhsT
   188       in (map (fst o dest_TFree) vs, dbind, mx, rhsT, NONE) end
   187       in (map (fst o dest_TFree) vs, dbind, mx, rhsT, NONE) end
   189   in
   188   in
   190     Domain_Isomorphism.domain_isomorphism (map prep spec)
   189     Domain_Isomorphism.domain_isomorphism (map prep spec)
   191   end
   190   end
   192 
   191