doc-src/IsarAdvanced/Codegen/Thy/Codegen.thy
changeset 22479 de15ea8fb348
parent 22473 753123c89d72
child 22550 c5039bee2602
equal deleted inserted replaced
22478:110f7f6f8a5d 22479:de15ea8fb348
    78     framework setup.    
    78     framework setup.    
    79   \end{warn}
    79   \end{warn}
    80 *}
    80 *}
    81 
    81 
    82 
    82 
       
    83 subsection {* An exmaple: a simple theory of search trees *}
       
    84 
       
    85 datatype ('a, 'b) searchtree = Leaf "'a\<Colon>linorder" 'b
       
    86   | Branch "('a, 'b) searchtree" "'a" "('a, 'b) searchtree"
       
    87 
       
    88 fun
       
    89   find :: "('a\<Colon>linorder, 'b) searchtree \<Rightarrow> 'a \<Rightarrow> 'b option" where
       
    90   "find (Leaf key val) it = (if it = key then Some val else None)"
       
    91   | "find (Branch t1 key t2) it = (if it \<le> key then find t1 it else find t2 it)"
       
    92 
       
    93 fun
       
    94   update :: "'a\<Colon>linorder \<times> 'b \<Rightarrow> ('a, 'b) searchtree \<Rightarrow> ('a, 'b) searchtree" where
       
    95   "update (it, entry) (Leaf key val) = (
       
    96     if it = key then Leaf key entry
       
    97       else if it \<le> key
       
    98       then Branch (Leaf it entry) it (Leaf key val)
       
    99       else Branch (Leaf key val) it (Leaf it entry)
       
   100    )"
       
   101   | "update (it, entry) (Branch t1 key t2) = (
       
   102     if it \<le> key
       
   103       then (Branch (update (it, entry) t1) key t2)
       
   104       else (Branch t1 key (update (it, entry) t2))
       
   105    )"
       
   106 
       
   107 fun
       
   108   example :: "nat \<Rightarrow> (nat, string) searchtree" where
       
   109   "example n = update (n, ''bar'') (Leaf 0 ''foo'')"
       
   110 
       
   111 code_gen example (*<*)(SML #)(*>*)(SML "examples/tree.ML")
       
   112 
       
   113 text {*
       
   114   \lstsml{Thy/examples/tree.ML}
       
   115 *}
       
   116 
    83 subsection {* Code generation process *}
   117 subsection {* Code generation process *}
    84 
   118 
    85 text {*
   119 text {*
    86   \begin{figure}[h]
   120   \begin{figure}[h]
    87   \centering
   121   \centering
   113       (\qn{preprocessing}).  Their purpose is to turn theorems
   147       (\qn{preprocessing}).  Their purpose is to turn theorems
   114       representing non- or badly executable
   148       representing non- or badly executable
   115       specifications into equivalent but executable counterparts.
   149       specifications into equivalent but executable counterparts.
   116       The result is a structured collection of \qn{code theorems}.
   150       The result is a structured collection of \qn{code theorems}.
   117 
   151 
   118     \item These \qn{code theorems} then are extracted
   152     \item These \qn{code theorems} then are \qn{translated}
   119       into an Haskell-like intermediate
   153       into an Haskell-like intermediate
   120       language.
   154       language.
   121 
   155 
   122     \item Finally, out of the intermediate language the final
   156     \item Finally, out of the intermediate language the final
   123       code in the desired \qn{target language} is \qn{serialized}.
   157       code in the desired \qn{target language} is \qn{serialized}.
  1173   @{index_ML CodegenData.add_preproc: "string * (theory -> thm list -> thm list)
  1207   @{index_ML CodegenData.add_preproc: "string * (theory -> thm list -> thm list)
  1174     -> theory -> theory"} \\
  1208     -> theory -> theory"} \\
  1175   @{index_ML CodegenData.del_preproc: "string -> theory -> theory"} \\
  1209   @{index_ML CodegenData.del_preproc: "string -> theory -> theory"} \\
  1176   @{index_ML CodegenData.add_datatype: "string * ((string * sort) list * (string * typ list) list)
  1210   @{index_ML CodegenData.add_datatype: "string * ((string * sort) list * (string * typ list) list)
  1177     -> theory -> theory"} \\
  1211     -> theory -> theory"} \\
  1178   @{index_ML CodegenData.del_datatype: "string -> theory -> theory"} \\
       
  1179   @{index_ML CodegenData.get_datatype: "theory -> string
  1212   @{index_ML CodegenData.get_datatype: "theory -> string
  1180     -> ((string * sort) list * (string * typ list) list) option"} \\
  1213     -> (string * sort) list * (string * typ list) list"} \\
  1181   @{index_ML CodegenData.get_datatype_of_constr: "theory -> CodegenConsts.const -> string option"}
  1214   @{index_ML CodegenData.get_datatype_of_constr: "theory -> CodegenConsts.const -> string option"}
  1182   \end{mldecls}
  1215   \end{mldecls}
  1183 
  1216 
  1184   \begin{description}
  1217   \begin{description}
  1185 
  1218 
  1222      a datatype to executable content, with type constructor
  1255      a datatype to executable content, with type constructor
  1223      @{text name} and specification @{text spec}; @{text spec} is
  1256      @{text name} and specification @{text spec}; @{text spec} is
  1224      a pair consisting of a list of type variable with sort
  1257      a pair consisting of a list of type variable with sort
  1225      constraints and a list of constructors with name
  1258      constraints and a list of constructors with name
  1226      and types of arguments.
  1259      and types of arguments.
  1227 
       
  1228   \item @{ML CodegenData.del_datatype}~@{text "name"}~@{text "thy"}
       
  1229      remove a datatype from executable content, if present.
       
  1230 
  1260 
  1231   \item @{ML CodegenData.get_datatype_of_constr}~@{text "thy"}~@{text "const"}
  1261   \item @{ML CodegenData.get_datatype_of_constr}~@{text "thy"}~@{text "const"}
  1232      returns type constructor corresponding to
  1262      returns type constructor corresponding to
  1233      constructor @{text const}; returns @{text NONE}
  1263      constructor @{text const}; returns @{text NONE}
  1234      if @{text const} is no constructor.
  1264      if @{text const} is no constructor.