src/Pure/codegen.ML
changeset 21056 2cfe839e8d58
parent 21002 c879f0150db9
child 21098 d0d8a48ae4e6
equal deleted inserted replaced
21055:e053811d680e 21056:2cfe839e8d58
   559 (**** retrieve definition of constant ****)
   559 (**** retrieve definition of constant ****)
   560 
   560 
   561 fun is_instance thy T1 T2 =
   561 fun is_instance thy T1 T2 =
   562   Sign.typ_instance thy (T1, Logic.legacy_varifyT T2);
   562   Sign.typ_instance thy (T1, Logic.legacy_varifyT T2);
   563 
   563 
   564 fun get_assoc_code thy s T = Option.map snd (Library.find_first (fn ((s', T'), _) =>
   564 fun get_assoc_code thy s T = Option.map snd (find_first (fn ((s', T'), _) =>
   565   s = s' andalso is_instance thy T T') (#consts (CodegenData.get thy)));
   565   s = s' andalso is_instance thy T T') (#consts (CodegenData.get thy)));
   566 
   566 
   567 fun get_aux_code xs = map_filter (fn (m, code) =>
   567 fun get_aux_code xs = map_filter (fn (m, code) =>
   568   if m = "" orelse member (op =) (!mode) m then SOME code else NONE) xs;
   568   if m = "" orelse member (op =) (!mode) m then SOME code else NONE) xs;
   569 
   569 
   579         val (lhs, rhs) = Logic.dest_equals t;
   579         val (lhs, rhs) = Logic.dest_equals t;
   580         val (c, args) = strip_comb lhs;
   580         val (c, args) = strip_comb lhs;
   581         val (s, T) = dest_Const c
   581         val (s, T) = dest_Const c
   582       in if forall is_Var args then SOME (s, (T, (args, rhs))) else NONE
   582       in if forall is_Var args then SOME (s, (T, (args, rhs))) else NONE
   583       end handle TERM _ => NONE;
   583       end handle TERM _ => NONE;
   584     fun add_def thyname (defs, (name, t)) = (case dest t of
   584     fun add_def thyname (name, t) = (case dest t of
   585         NONE => defs
   585         NONE => I
   586       | SOME _ => (case dest (prep_def (Thm.get_axiom thy name)) of
   586       | SOME _ => (case dest (prep_def (Thm.get_axiom thy name)) of
   587           NONE => defs
   587           NONE => I
   588         | SOME (s, (T, (args, rhs))) => Symtab.update
   588         | SOME (s, (T, (args, rhs))) => Symtab.map_default (s, [])
   589             (s, (T, (thyname, split_last (rename_terms (args @ [rhs])))) ::
   589             (cons (T, (thyname, split_last (rename_terms (args @ [rhs])))))))
   590             the_default [] (Symtab.lookup defs s)) defs))
       
   591   in
   590   in
   592     foldl (fn ((thyname, axms), defs) =>
   591     fold (fn (thyname, axms) => Symtab.fold (add_def thyname) axms) axmss Symtab.empty
   593       Symtab.foldl (add_def thyname) (defs, axms)) Symtab.empty axmss
       
   594   end;
   592   end;
   595 
   593 
   596 fun get_defn thy defs s T = (case Symtab.lookup defs s of
   594 fun get_defn thy defs s T = (case Symtab.lookup defs s of
   597     NONE => NONE
   595     NONE => NONE
   598   | SOME ds =>
   596   | SOME ds =>
   846       in if module = "" orelse module = module' then SOME (s, c) else NONE end)
   844       in if module = "" orelse module = module' then SOME (s, c) else NONE end)
   847         (rev (Graph.all_preds gr xs));
   845         (rev (Graph.all_preds gr xs));
   848     fun string_of_cycle (a :: b :: cs) =
   846     fun string_of_cycle (a :: b :: cs) =
   849           let val SOME (x, y) = get_first (fn (x, (_, a', _)) =>
   847           let val SOME (x, y) = get_first (fn (x, (_, a', _)) =>
   850             if a = a' then Option.map (pair x)
   848             if a = a' then Option.map (pair x)
   851               (Library.find_first (equal b o #2 o Graph.get_node gr)
   849               (find_first (equal b o #2 o Graph.get_node gr)
   852                 (Graph.imm_succs gr x))
   850                 (Graph.imm_succs gr x))
   853             else NONE) code
   851             else NONE) code
   854           in x ^ " called by " ^ y ^ "\n" ^ string_of_cycle (b :: cs) end
   852           in x ^ " called by " ^ y ^ "\n" ^ string_of_cycle (b :: cs) end
   855       | string_of_cycle _ = ""
   853       | string_of_cycle _ = ""
   856   in
   854   in
   985               Pretty.brk 1, Pretty.str "then NONE",
   983               Pretty.brk 1, Pretty.str "then NONE",
   986               Pretty.brk 1, Pretty.str "else ",
   984               Pretty.brk 1, Pretty.str "else ",
   987               Pretty.block [Pretty.str "SOME ", Pretty.block (Pretty.str "[" ::
   985               Pretty.block [Pretty.str "SOME ", Pretty.block (Pretty.str "[" ::
   988                 flat (separate [Pretty.str ",", Pretty.brk 1]
   986                 flat (separate [Pretty.str ",", Pretty.brk 1]
   989                   (map (fn ((s, T), s') => [Pretty.block
   987                   (map (fn ((s, T), s') => [Pretty.block
   990                     [Pretty.str ("(" ^ Library.quote (Symbol.escape s) ^ ","), Pretty.brk 1,
   988                     [Pretty.str ("(" ^ quote (Symbol.escape s) ^ ","), Pretty.brk 1,
   991                      mk_app false (mk_term_of gr "Generated" false T)
   989                      mk_app false (mk_term_of gr "Generated" false T)
   992                        [Pretty.str s'], Pretty.str ")"]]) frees')) @
   990                        [Pretty.str s'], Pretty.str ")"]]) frees')) @
   993                   [Pretty.str "]"])]],
   991                   [Pretty.str "]"])]],
   994             Pretty.brk 1, Pretty.str "end"]), Pretty.str ");"]) ^
   992             Pretty.brk 1, Pretty.str "end"]), Pretty.str ");"]) ^
   995       "\n\nend;\n") ();
   993       "\n\nend;\n") ();