src/Pure/Isar/code_unit.ML
changeset 30022 1d8b8fa19074
parent 29288 253bcf2a5854
child 30688 2d1d426e00e4
equal deleted inserted replaced
30021:19c06d4763e0 30022:1d8b8fa19074
    32 
    32 
    33   (*constructor sets*)
    33   (*constructor sets*)
    34   val constrset_of_consts: theory -> (string * typ) list
    34   val constrset_of_consts: theory -> (string * typ) list
    35     -> string * ((string * sort) list * (string * typ list) list)
    35     -> string * ((string * sort) list * (string * typ list) list)
    36 
    36 
    37   (*defining equations*)
    37   (*code equations*)
    38   val assert_eqn: theory -> thm -> thm
    38   val assert_eqn: theory -> thm -> thm
    39   val mk_eqn: theory -> thm -> thm * bool
    39   val mk_eqn: theory -> thm -> thm * bool
    40   val assert_linear: (string -> bool) -> thm * bool -> thm * bool
    40   val assert_linear: (string -> bool) -> thm * bool -> thm * bool
    41   val const_eqn: thm -> string
    41   val const_eqn: thm -> string
    42   val const_typ_eqn: thm -> string * typ
    42   val const_typ_eqn: thm -> string * typ
    74 
    74 
    75 (* utilities *)
    75 (* utilities *)
    76 
    76 
    77 fun typscheme thy (c, ty) =
    77 fun typscheme thy (c, ty) =
    78   let
    78   let
    79     fun dest (TVar ((v, 0), sort)) = (v, sort)
    79     val ty' = Logic.unvarifyT ty;
       
    80     fun dest (TFree (v, sort)) = (v, sort)
    80       | dest ty = error ("Illegal type parameter in type scheme: " ^ Syntax.string_of_typ_global thy ty);
    81       | dest ty = error ("Illegal type parameter in type scheme: " ^ Syntax.string_of_typ_global thy ty);
    81     val vs = map dest (Sign.const_typargs thy (c, ty));
    82     val vs = map dest (Sign.const_typargs thy (c, ty'));
    82   in (vs, ty) end;
    83   in (vs, Type.strip_sorts ty') end;
    83 
    84 
    84 fun inst_thm thy tvars' thm =
    85 fun inst_thm thy tvars' thm =
    85   let
    86   let
    86     val tvars = (Term.add_tvars o Thm.prop_of) thm [];
    87     val tvars = (Term.add_tvars o Thm.prop_of) thm [];
    87     val inter_sort = Sorts.inter_sort (Sign.classes_of thy);
    88     val inter_sort = Sorts.inter_sort (Sign.classes_of thy);
   311       in (c, (fst o strip_type) ty') end;
   312       in (c, (fst o strip_type) ty') end;
   312     val c' :: cs' = map ty_sorts cs;
   313     val c' :: cs' = map ty_sorts cs;
   313     val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
   314     val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
   314     val vs = Name.names Name.context Name.aT sorts;
   315     val vs = Name.names Name.context Name.aT sorts;
   315     val cs''' = map (inst vs) cs'';
   316     val cs''' = map (inst vs) cs'';
   316   in (tyco, (vs, cs''')) end;
   317   in (tyco, (vs, rev cs''')) end;
   317 
   318 
   318 
   319 
   319 (* defining equations *)
   320 (* code equations *)
   320 
   321 
   321 fun assert_eqn thy thm =
   322 fun assert_eqn thy thm =
   322   let
   323   let
   323     val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm
   324     val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm
   324       handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm)
   325       handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm)
   349     fun check _ (Abs _) = bad_thm
   350     fun check _ (Abs _) = bad_thm
   350           ("Abstraction on left hand side of equation\n"
   351           ("Abstraction on left hand side of equation\n"
   351             ^ Display.string_of_thm thm)
   352             ^ Display.string_of_thm thm)
   352       | check 0 (Var _) = ()
   353       | check 0 (Var _) = ()
   353       | check _ (Var _) = bad_thm
   354       | check _ (Var _) = bad_thm
   354           ("Variable with application on left hand side of defining equation\n"
   355           ("Variable with application on left hand side of code equation\n"
   355             ^ Display.string_of_thm thm)
   356             ^ Display.string_of_thm thm)
   356       | check n (t1 $ t2) = (check (n+1) t1; check 0 t2)
   357       | check n (t1 $ t2) = (check (n+1) t1; check 0 t2)
   357       | check n (Const (_, ty)) = if n <> (length o fst o strip_type) ty
   358       | check n (Const (_, ty)) = if n <> (length o fst o strip_type) ty
   358           then bad_thm
   359           then bad_thm
   359             ("Partially applied constant on left hand side of equation\n"
   360             ("Partially applied constant on left hand side of equation\n"
   361           else ();
   362           else ();
   362     val _ = map (check 0) args;
   363     val _ = map (check 0) args;
   363     val ty_decl = Sign.the_const_type thy c;
   364     val ty_decl = Sign.the_const_type thy c;
   364     val _ = if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
   365     val _ = if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
   365       then () else bad_thm ("Type\n" ^ string_of_typ thy ty
   366       then () else bad_thm ("Type\n" ^ string_of_typ thy ty
   366            ^ "\nof defining equation\n"
   367            ^ "\nof code equation\n"
   367            ^ Display.string_of_thm thm
   368            ^ Display.string_of_thm thm
   368            ^ "\nis incompatible with declared function type\n"
   369            ^ "\nis incompatible with declared function type\n"
   369            ^ string_of_typ thy ty_decl)
   370            ^ string_of_typ thy ty_decl)
   370   in thm end;
   371   in thm end;
   371 
   372 
   386   in thm end;
   387   in thm end;
   387 
   388 
   388 fun assert_linear is_cons (thm, false) = (thm, false)
   389 fun assert_linear is_cons (thm, false) = (thm, false)
   389   | assert_linear is_cons (thm, true) = if snd (add_linear (assert_pat is_cons thm)) then (thm, true)
   390   | assert_linear is_cons (thm, true) = if snd (add_linear (assert_pat is_cons thm)) then (thm, true)
   390       else bad_thm
   391       else bad_thm
   391         ("Duplicate variables on left hand side of defining equation:\n"
   392         ("Duplicate variables on left hand side of code equation:\n"
   392           ^ Display.string_of_thm thm);
   393           ^ Display.string_of_thm thm);
   393 
   394 
   394 
   395 
   395 fun mk_eqn thy = add_linear o assert_eqn thy o AxClass.unoverload thy
   396 fun mk_eqn thy = add_linear o assert_eqn thy o AxClass.unoverload thy
   396   o LocalDefs.meta_rewrite_rule (ProofContext.init thy);
   397   o LocalDefs.meta_rewrite_rule (ProofContext.init thy);