src/Tools/Code/code_thingol.ML
changeset 55150 0940309ed8f1
parent 55147 bce3dbc11f95
child 55188 7ca0204ece66
equal deleted inserted replaced
55149:626d8f08d479 55150:0940309ed8f1
    21       Dict_Const of (string * class) * dict list list
    21       Dict_Const of (string * class) * dict list list
    22     | Dict_Var of vname * (int * int);
    22     | Dict_Var of vname * (int * int);
    23   datatype itype =
    23   datatype itype =
    24       `%% of string * itype list
    24       `%% of string * itype list
    25     | ITyVar of vname;
    25     | ITyVar of vname;
    26   type const = { sym: Code_Symbol.symbol, typargs: itype list, dicts: dict list list,
    26   type const = { sym: Code_Symbol.T, typargs: itype list, dicts: dict list list,
    27     dom: itype list, range: itype, annotate: bool };
    27     dom: itype list, range: itype, annotate: bool };
    28   datatype iterm =
    28   datatype iterm =
    29       IConst of const
    29       IConst of const
    30     | IVar of vname option
    30     | IVar of vname option
    31     | `$ of iterm * iterm
    31     | `$ of iterm * iterm
    53   val unfold_const_app: iterm -> (const * iterm list) option
    53   val unfold_const_app: iterm -> (const * iterm list) option
    54   val is_IVar: iterm -> bool
    54   val is_IVar: iterm -> bool
    55   val is_IAbs: iterm -> bool
    55   val is_IAbs: iterm -> bool
    56   val eta_expand: int -> const * iterm list -> iterm
    56   val eta_expand: int -> const * iterm list -> iterm
    57   val contains_dict_var: iterm -> bool
    57   val contains_dict_var: iterm -> bool
    58   val add_constsyms: iterm -> Code_Symbol.symbol list -> Code_Symbol.symbol list
    58   val add_constsyms: iterm -> Code_Symbol.T list -> Code_Symbol.T list
    59   val add_tyconames: iterm -> string list -> string list
    59   val add_tyconames: iterm -> string list -> string list
    60   val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
    60   val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
    61 
    61 
    62   datatype stmt =
    62   datatype stmt =
    63       NoStmt
    63       NoStmt
    75   type program = stmt Code_Symbol.Graph.T
    75   type program = stmt Code_Symbol.Graph.T
    76   val unimplemented: program -> string list
    76   val unimplemented: program -> string list
    77   val implemented_deps: program -> string list
    77   val implemented_deps: program -> string list
    78   val map_terms_bottom_up: (iterm -> iterm) -> iterm -> iterm
    78   val map_terms_bottom_up: (iterm -> iterm) -> iterm -> iterm
    79   val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt
    79   val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt
    80   val is_constr: program -> Code_Symbol.symbol -> bool
    80   val is_constr: program -> Code_Symbol.T -> bool
    81   val is_case: stmt -> bool
    81   val is_case: stmt -> bool
    82   val group_stmts: theory -> program
    82   val group_stmts: theory -> program
    83     -> ((Code_Symbol.symbol * stmt) list * (Code_Symbol.symbol * stmt) list
    83     -> ((Code_Symbol.T * stmt) list * (Code_Symbol.T * stmt) list
    84       * ((Code_Symbol.symbol * stmt) list * (Code_Symbol.symbol * stmt) list)) list
    84       * ((Code_Symbol.T * stmt) list * (Code_Symbol.T * stmt) list)) list
    85 
    85 
    86   val read_const_exprs: theory -> string list -> string list * string list
    86   val read_const_exprs: theory -> string list -> string list * string list
    87   val consts_program: theory -> bool -> string list -> program
    87   val consts_program: theory -> bool -> string list -> program
    88   val dynamic_conv: theory -> (program
    88   val dynamic_conv: theory -> (program
    89     -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.symbol list -> conv)
    89     -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.T list -> conv)
    90     -> conv
    90     -> conv
    91   val dynamic_value: theory -> ((term -> term) -> 'a -> 'a) -> (program
    91   val dynamic_value: theory -> ((term -> term) -> 'a -> 'a) -> (program
    92     -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.symbol list -> 'a)
    92     -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.T list -> 'a)
    93     -> term -> 'a
    93     -> term -> 'a
    94   val static_conv: theory -> string list -> (program -> string list
    94   val static_conv: theory -> string list -> (program -> string list
    95     -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.symbol list -> conv)
    95     -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.T list -> conv)
    96     -> conv
    96     -> conv
    97   val static_conv_simple: theory -> string list
    97   val static_conv_simple: theory -> string list
    98     -> (program -> (string * sort) list -> term -> conv) -> conv
    98     -> (program -> (string * sort) list -> term -> conv) -> conv
    99   val static_value: theory -> ((term -> term) -> 'a -> 'a) -> string list ->
    99   val static_value: theory -> ((term -> term) -> 'a -> 'a) -> string list ->
   100     (program -> string list
   100     (program -> string list
   101       -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.symbol list -> 'a)
   101       -> ((string * sort) list * typscheme) * iterm -> Code_Symbol.T list -> 'a)
   102     -> term -> 'a
   102     -> term -> 'a
   103 end;
   103 end;
   104 
   104 
   105 structure Code_Thingol: CODE_THINGOL =
   105 structure Code_Thingol: CODE_THINGOL =
   106 struct
   106 struct
       
   107 
       
   108 open Basic_Code_Symbol;
   107 
   109 
   108 (** auxiliary **)
   110 (** auxiliary **)
   109 
   111 
   110 fun unfoldl dest x =
   112 fun unfoldl dest x =
   111   case dest x
   113   case dest x
   145     val (tys1, ty1) = unfold_fun ty;
   147     val (tys1, ty1) = unfold_fun ty;
   146     val (tys3, tys2) = chop n tys1;
   148     val (tys3, tys2) = chop n tys1;
   147     val ty3 = Library.foldr (op `->) (tys2, ty1);
   149     val ty3 = Library.foldr (op `->) (tys2, ty1);
   148   in (tys3, ty3) end;
   150   in (tys3, ty3) end;
   149 
   151 
   150 type const = { sym: Code_Symbol.symbol, typargs: itype list, dicts: dict list list,
   152 type const = { sym: Code_Symbol.T, typargs: itype list, dicts: dict list list,
   151   dom: itype list, range: itype, annotate: bool };
   153   dom: itype list, range: itype, annotate: bool };
   152 
   154 
   153 datatype iterm =
   155 datatype iterm =
   154     IConst of const
   156     IConst of const
   155   | IVar of vname option
   157   | IVar of vname option
   284       superinst_params: ((string * (const * int)) * (thm * bool)) list };
   286       superinst_params: ((string * (const * int)) * (thm * bool)) list };
   285 
   287 
   286 type program = stmt Code_Symbol.Graph.T;
   288 type program = stmt Code_Symbol.Graph.T;
   287 
   289 
   288 fun unimplemented program =
   290 fun unimplemented program =
   289   Code_Symbol.Graph.fold (fn (Code_Symbol.Constant c, (NoStmt, _)) => cons c | _ => I) program [];
   291   Code_Symbol.Graph.fold (fn (Constant c, (NoStmt, _)) => cons c | _ => I) program [];
   290 
   292 
   291 fun implemented_deps program =
   293 fun implemented_deps program =
   292   Code_Symbol.Graph.keys program
   294   Code_Symbol.Graph.keys program
   293   |> subtract (op =) (Code_Symbol.Graph.all_preds program (map Code_Symbol.Constant (unimplemented program)))
   295   |> subtract (op =) (Code_Symbol.Graph.all_preds program (map Constant (unimplemented program)))
   294   |> map_filter (fn Code_Symbol.Constant c => SOME c | _ => NONE);
   296   |> map_filter (fn Constant c => SOME c | _ => NONE);
   295 
   297 
   296 fun map_terms_bottom_up f (t as IConst _) = f t
   298 fun map_terms_bottom_up f (t as IConst _) = f t
   297   | map_terms_bottom_up f (t as IVar _) = f t
   299   | map_terms_bottom_up f (t as IVar _) = f t
   298   | map_terms_bottom_up f (t1 `$ t2) = f
   300   | map_terms_bottom_up f (t1 `$ t2) = f
   299       (map_terms_bottom_up f t1 `$ map_terms_bottom_up f t2)
   301       (map_terms_bottom_up f t1 `$ map_terms_bottom_up f t2)
   464       ##>> fold_map (fn (c, (vs, tys)) =>
   466       ##>> fold_map (fn (c, (vs, tys)) =>
   465         ensure_const thy algbr eqngr permissive c
   467         ensure_const thy algbr eqngr permissive c
   466         ##>> pair (map (unprefix "'" o fst) vs)
   468         ##>> pair (map (unprefix "'" o fst) vs)
   467         ##>> fold_map (translate_typ thy algbr eqngr permissive) tys) cos
   469         ##>> fold_map (translate_typ thy algbr eqngr permissive) tys) cos
   468       #>> Datatype;
   470       #>> Datatype;
   469   in ensure_stmt Code_Symbol.Type_Constructor stmt_datatype tyco end
   471   in ensure_stmt Type_Constructor stmt_datatype tyco end
   470 and ensure_const thy algbr eqngr permissive c =
   472 and ensure_const thy algbr eqngr permissive c =
   471   let
   473   let
   472     fun stmt_datatypecons tyco =
   474     fun stmt_datatypecons tyco =
   473       ensure_tyco thy algbr eqngr permissive tyco
   475       ensure_tyco thy algbr eqngr permissive tyco
   474       #>> Datatypecons;
   476       #>> Datatypecons;
   492     val stmt_const = case Code.get_type_of_constr_or_abstr thy c
   494     val stmt_const = case Code.get_type_of_constr_or_abstr thy c
   493      of SOME (tyco, _) => stmt_datatypecons tyco
   495      of SOME (tyco, _) => stmt_datatypecons tyco
   494       | NONE => (case Axclass.class_of_param thy c
   496       | NONE => (case Axclass.class_of_param thy c
   495          of SOME class => stmt_classparam class
   497          of SOME class => stmt_classparam class
   496           | NONE => stmt_fun (Code_Preproc.cert eqngr c))
   498           | NONE => stmt_fun (Code_Preproc.cert eqngr c))
   497   in ensure_stmt Code_Symbol.Constant stmt_const c end
   499   in ensure_stmt Constant stmt_const c end
   498 and ensure_class thy (algbr as (_, algebra)) eqngr permissive class =
   500 and ensure_class thy (algbr as (_, algebra)) eqngr permissive class =
   499   let
   501   let
   500     val super_classes = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class;
   502     val super_classes = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class;
   501     val cs = #params (Axclass.get_info thy class);
   503     val cs = #params (Axclass.get_info thy class);
   502     val stmt_class =
   504     val stmt_class =
   503       fold_map (fn super_class =>
   505       fold_map (fn super_class =>
   504         ensure_classrel thy algbr eqngr permissive (class, super_class)) super_classes
   506         ensure_classrel thy algbr eqngr permissive (class, super_class)) super_classes
   505       ##>> fold_map (fn (c, ty) => ensure_const thy algbr eqngr permissive c
   507       ##>> fold_map (fn (c, ty) => ensure_const thy algbr eqngr permissive c
   506         ##>> translate_typ thy algbr eqngr permissive ty) cs
   508         ##>> translate_typ thy algbr eqngr permissive ty) cs
   507       #>> (fn info => Class (unprefix "'" Name.aT, info))
   509       #>> (fn info => Class (unprefix "'" Name.aT, info))
   508   in ensure_stmt Code_Symbol.Type_Class stmt_class class end
   510   in ensure_stmt Type_Class stmt_class class end
   509 and ensure_classrel thy algbr eqngr permissive (sub_class, super_class) =
   511 and ensure_classrel thy algbr eqngr permissive (sub_class, super_class) =
   510   let
   512   let
   511     val stmt_classrel =
   513     val stmt_classrel =
   512       ensure_class thy algbr eqngr permissive sub_class
   514       ensure_class thy algbr eqngr permissive sub_class
   513       ##>> ensure_class thy algbr eqngr permissive super_class
   515       ##>> ensure_class thy algbr eqngr permissive super_class
   514       #>> Classrel;
   516       #>> Classrel;
   515   in ensure_stmt Code_Symbol.Class_Relation stmt_classrel (sub_class, super_class) end
   517   in ensure_stmt Class_Relation stmt_classrel (sub_class, super_class) end
   516 and ensure_inst thy (algbr as (_, algebra)) eqngr permissive (tyco, class) =
   518 and ensure_inst thy (algbr as (_, algebra)) eqngr permissive (tyco, class) =
   517   let
   519   let
   518     val super_classes = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class;
   520     val super_classes = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class;
   519     val these_class_params = these o try (#params o Axclass.get_info thy);
   521     val these_class_params = these o try (#params o Axclass.get_info thy);
   520     val class_params = these_class_params class;
   522     val class_params = these_class_params class;
   550       ##>> fold_map translate_classparam_instance class_params
   552       ##>> fold_map translate_classparam_instance class_params
   551       ##>> fold_map translate_classparam_instance superclass_params
   553       ##>> fold_map translate_classparam_instance superclass_params
   552       #>> (fn (((((class, tyco), vs), superinsts), inst_params), superinst_params) =>
   554       #>> (fn (((((class, tyco), vs), superinsts), inst_params), superinst_params) =>
   553           Classinst { class = class, tyco = tyco, vs = vs,
   555           Classinst { class = class, tyco = tyco, vs = vs,
   554             superinsts = superinsts, inst_params = inst_params, superinst_params = superinst_params });
   556             superinsts = superinsts, inst_params = inst_params, superinst_params = superinst_params });
   555   in ensure_stmt Code_Symbol.Class_Instance stmt_inst (tyco, class) end
   557   in ensure_stmt Class_Instance stmt_inst (tyco, class) end
   556 and translate_typ thy algbr eqngr permissive (TFree (v, _)) =
   558 and translate_typ thy algbr eqngr permissive (TFree (v, _)) =
   557       pair (ITyVar (unprefix "'" v))
   559       pair (ITyVar (unprefix "'" v))
   558   | translate_typ thy algbr eqngr permissive (Type (tyco, tys)) =
   560   | translate_typ thy algbr eqngr permissive (Type (tyco, tys)) =
   559       ensure_tyco thy algbr eqngr permissive tyco
   561       ensure_tyco thy algbr eqngr permissive tyco
   560       ##>> fold_map (translate_typ thy algbr eqngr permissive) tys
   562       ##>> fold_map (translate_typ thy algbr eqngr permissive) tys
   602     ensure_const thy algbr eqngr permissive c
   604     ensure_const thy algbr eqngr permissive c
   603     ##>> fold_map (translate_typ thy algbr eqngr permissive) typargs
   605     ##>> fold_map (translate_typ thy algbr eqngr permissive) typargs
   604     ##>> fold_map (translate_dicts thy algbr eqngr permissive some_thm) (typargs ~~ sorts)
   606     ##>> fold_map (translate_dicts thy algbr eqngr permissive some_thm) (typargs ~~ sorts)
   605     ##>> fold_map (translate_typ thy algbr eqngr permissive) (range :: dom)
   607     ##>> fold_map (translate_typ thy algbr eqngr permissive) (range :: dom)
   606     #>> (fn (((c, typargs), dss), range :: dom) =>
   608     #>> (fn (((c, typargs), dss), range :: dom) =>
   607       IConst { sym = Code_Symbol.Constant c, typargs = typargs, dicts = dss,
   609       IConst { sym = Constant c, typargs = typargs, dicts = dss,
   608         dom = dom, range = range, annotate = annotate })
   610         dom = dom, range = range, annotate = annotate })
   609   end
   611   end
   610 and translate_app_const thy algbr eqngr permissive some_thm ((c_ty, ts), some_abs) =
   612 and translate_app_const thy algbr eqngr permissive some_thm ((c_ty, ts), some_abs) =
   611   translate_const thy algbr eqngr permissive some_thm (c_ty, some_abs)
   613   translate_const thy algbr eqngr permissive some_thm (c_ty, some_abs)
   612   ##>> fold_map (translate_term thy algbr eqngr permissive some_thm o rpair NONE) ts
   614   ##>> fold_map (translate_term thy algbr eqngr permissive some_thm o rpair NONE) ts
   627       else map_filter I (map2 mk_constr case_pats (nth_drop t_pos ts));
   629       else map_filter I (map2 mk_constr case_pats (nth_drop t_pos ts));
   628     fun casify constrs ty t_app ts =
   630     fun casify constrs ty t_app ts =
   629       let
   631       let
   630         fun collapse_clause vs_map ts body =
   632         fun collapse_clause vs_map ts body =
   631           case body
   633           case body
   632            of IConst { sym = Code_Symbol.Constant c, ... } => if member (op =) undefineds c
   634            of IConst { sym = Constant c, ... } => if member (op =) undefineds c
   633                 then []
   635                 then []
   634                 else [(ts, body)]
   636                 else [(ts, body)]
   635             | ICase { term = IVar (SOME v), clauses = clauses, ... } =>
   637             | ICase { term = IVar (SOME v), clauses = clauses, ... } =>
   636                 if forall (fn (pat', body') => exists_var pat' v
   638                 if forall (fn (pat', body') => exists_var pat' v
   637                   orelse not (exists_var body' v)) clauses
   639                   orelse not (exists_var body' v)) clauses
   745   let
   747   let
   746     fun project program =
   748     fun project program =
   747       if permissive then program
   749       if permissive then program
   748       else Code_Symbol.Graph.restrict
   750       else Code_Symbol.Graph.restrict
   749         (member (op =) (Code_Symbol.Graph.all_succs program
   751         (member (op =) (Code_Symbol.Graph.all_succs program
   750           (map Code_Symbol.Constant consts))) program;
   752           (map Constant consts))) program;
   751     fun generate_consts thy algebra eqngr =
   753     fun generate_consts thy algebra eqngr =
   752       fold_map (ensure_const thy algebra eqngr permissive);
   754       fold_map (ensure_const thy algebra eqngr permissive);
   753   in
   755   in
   754     invoke_generation permissive thy (Code_Preproc.obtain false thy consts [])
   756     invoke_generation permissive thy (Code_Preproc.obtain false thy consts [])
   755       generate_consts consts
   757       generate_consts consts
   764   let
   766   let
   765     val ty = fastype_of t;
   767     val ty = fastype_of t;
   766     val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
   768     val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
   767       o dest_TFree))) t [];
   769       o dest_TFree))) t [];
   768     val t' = annotate thy algbr eqngr (@{const_name dummy_pattern}, ty) [] t;
   770     val t' = annotate thy algbr eqngr (@{const_name dummy_pattern}, ty) [] t;
   769     val dummy_constant = Code_Symbol.Constant @{const_name dummy_pattern};
   771     val dummy_constant = Constant @{const_name dummy_pattern};
   770     val stmt_value =
   772     val stmt_value =
   771       fold_map (translate_tyvar_sort thy algbr eqngr false) vs
   773       fold_map (translate_tyvar_sort thy algbr eqngr false) vs
   772       ##>> translate_typ thy algbr eqngr false ty
   774       ##>> translate_typ thy algbr eqngr false ty
   773       ##>> translate_term thy algbr eqngr false NONE (t', NONE)
   775       ##>> translate_term thy algbr eqngr false NONE (t', NONE)
   774       #>> (fn ((vs, ty), t) => Fun
   776       #>> (fn ((vs, ty), t) => Fun
   781         val program2 = Code_Symbol.Graph.del_node dummy_constant program1;
   783         val program2 = Code_Symbol.Graph.del_node dummy_constant program1;
   782         val deps_all = Code_Symbol.Graph.all_succs program2 deps;
   784         val deps_all = Code_Symbol.Graph.all_succs program2 deps;
   783         val program3 = Code_Symbol.Graph.restrict (member (op =) deps_all) program2;
   785         val program3 = Code_Symbol.Graph.restrict (member (op =) deps_all) program2;
   784       in ((program3, ((vs_ty, t), deps)), (dep, program2)) end;
   786       in ((program3, ((vs_ty, t), deps)), (dep, program2)) end;
   785   in
   787   in
   786     ensure_stmt Code_Symbol.Constant stmt_value @{const_name dummy_pattern}
   788     ensure_stmt Constant stmt_value @{const_name dummy_pattern}
   787     #> snd
   789     #> snd
   788     #> term_value
   790     #> term_value
   789   end;
   791   end;
   790 
   792 
   791 fun original_sorts vs =
   793 fun original_sorts vs =