diff -r 0ab2ed1489eb -r 8baf2e8b16e2 src/Doc/Implementation/Logic.thy --- a/src/Doc/Implementation/Logic.thy Fri Dec 03 15:11:16 2021 +0100 +++ b/src/Doc/Implementation/Logic.thy Fri Dec 03 20:11:21 2021 +0100 @@ -168,6 +168,8 @@ @{ML_antiquotation_def "type_abbrev"} & : & \ML_antiquotation\ \\ @{ML_antiquotation_def "nonterminal"} & : & \ML_antiquotation\ \\ @{ML_antiquotation_def "typ"} & : & \ML_antiquotation\ \\ + @{ML_antiquotation_def "Type"} & : & \ML_antiquotation\ \\ + @{ML_antiquotation_def "Type_fn"} & : & \ML_antiquotation\ \\ \end{matharray} \<^rail>\ @@ -180,6 +182,8 @@ @@{ML_antiquotation nonterminal}) embedded ; @@{ML_antiquotation typ} type + ; + (@@{ML_antiquotation Type} | @@{ML_antiquotation Type_fn}) embedded \ \<^descr> \@{class c}\ inlines the internalized class \c\ --- as \<^ML_type>\string\ @@ -199,6 +203,51 @@ \<^descr> \@{typ \}\ inlines the internalized type \\\ --- as constructor term for datatype \<^ML_type>\typ\. + + \<^descr> \@{Type source}\ refers to embedded source text to produce a type + constructor with name (formally checked) and arguments (inlined ML text). + The embedded \source\ follows the syntax category @{syntax type_const} + defined below. + + \<^descr> \@{Type_fn source}\ is similar to \@{Type source}\, but produces a partial + ML function that matches against a type constructor pattern, following + syntax category @{syntax type_const_fn} below. + + + \<^rail>\ + @{syntax_def type_const}: @{syntax name} (@{syntax embedded_ml}*) + ; + @{syntax_def type_const_fn}: @{syntax type_const} @'=>' @{syntax embedded} + ; + @{syntax_def embedded_ml}: @{syntax embedded} | + @{syntax control_symbol} @{syntax embedded} | @'_' + \ + + The text provided as @{syntax embedded_ml} is treated as regular Isabelle/ML + source, possibly with nested antiquotations. ML text that only consists of a + single antiquotation in compact control-cartouche notation, can be written + without an extra level of nesting embedded text (see the last example + below). +\ + +text %mlex \ + Here are some minimal examples for type constructor antiquotations. +\ + +ML \ + \ \type constructor without arguments:\ + val natT = \<^Type>\nat\; + + \ \type constructor with arguments:\ + fun mk_funT (A, B) = \<^Type>\fun A B\; + + \ \type constructor decomposition as partial function:\ + val dest_funT = \<^Type_fn>\fun A B => \(A, B)\\; + + \ \nested type constructors:\ + fun mk_relT A = \<^Type>\fun A \<^Type>\fun A \<^Type>\bool\\\; + + \<^assert> (mk_relT natT = \<^typ>\nat \ nat \ bool\); \ @@ -387,6 +436,9 @@ @{ML_antiquotation_def "const"} & : & \ML_antiquotation\ \\ @{ML_antiquotation_def "term"} & : & \ML_antiquotation\ \\ @{ML_antiquotation_def "prop"} & : & \ML_antiquotation\ \\ + @{ML_antiquotation_def "Const"} & : & \ML_antiquotation\ \\ + @{ML_antiquotation_def "Const_"} & : & \ML_antiquotation\ \\ + @{ML_antiquotation_def "Const_fn"} & : & \ML_antiquotation\ \\ \end{matharray} \<^rail>\ @@ -398,6 +450,9 @@ @@{ML_antiquotation term} term ; @@{ML_antiquotation prop} prop + ; + (@@{ML_antiquotation Const} | @@{ML_antiquotation Const_} | @@{ML_antiquotation Const_fn}) + embedded \ \<^descr> \@{const_name c}\ inlines the internalized logical constant name \c\ --- @@ -414,6 +469,57 @@ \<^descr> \@{prop \}\ inlines the internalized proposition \\\ --- as constructor term for datatype \<^ML_type>\term\. + + \<^descr> \@{Const source}\ refers to embedded source text to produce a term + constructor with name (formally checked), type arguments and term arguments + (inlined ML text). The embedded \source\ follows the syntax category + @{syntax term_const} defined below, using @{syntax embedded_ml} from + antiquotation @{ML_antiquotation Type} (\secref{sec:types}). + + \<^descr> \@{Const_ source}\ is similar to \@{Const source}\ for patterns instead of + closed values. This distinction is required due to redundant type + information within term constants: subtrees with duplicate ML pattern + variable need to be suppressed (replaced by dummy patterns). The embedded \source\ follows + the syntax category @{syntax term_const} defined below. + + \<^descr> \@{Const_fn source}\ is similar to \@{Const_ source}\, but produces a + proper \<^verbatim>\fn\ expression with function body. The embedded \source\ follows + the syntax category @{syntax term_const_fn} defined below. + + + \<^rail>\ + @{syntax_def term_const}: + @{syntax name} (@{syntax embedded_ml}*) (@{syntax for_args})? + ; + @{syntax_def type_const_fn}: @{ syntax term_const} @'=>' @{syntax embedded} + ; + @{syntax_def for_args}: @'for' (@{syntax embedded_ml}*) + \ +\ + +text %mlex \ + Here are some minimal examples for term constant antiquotations. Type + arguments for constants are analogous to type constructors + (\secref{sec:types}). Term arguments are different: a flexible number of + arguments is inserted via curried applications (\<^ML>\op $\). +\ + +ML \ + \ \constant without type argument:\ + fun mk_conj (A, B) = \<^Const>\conj for A B\; + val dest_conj = \<^Const_fn>\conj for A B => \(A, B)\\; + + \ \constant with type argument:\ + fun mk_eq T (t, u) = \<^Const>\HOL.eq T for t u\; + val dest_eq = \<^Const_fn>\HOL.eq T for t u => \(T, (t, u))\\; + + \ \constant with variable number of term arguments:\ + val c = Const (\<^const_name>\conj\, \<^typ>\bool \ bool \ bool\); + val P = \<^term>\P::bool\; + val Q = \<^term>\Q::bool\; + \<^assert> (\<^Const>\conj\ = c); + \<^assert> (\<^Const>\conj for P\ = c $ P); + \<^assert> (\<^Const>\conj for P Q\ = c $ P $ Q); \