src/HOL/ex/Multiquote.thy
author wenzelm
Mon, 25 Mar 2019 17:21:26 +0100
changeset 69981 3dced198b9ec
parent 69597 ff784d5a5bfb
permissions -rw-r--r--
more strict AFP properties;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/ex/Multiquote.thy
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
     3
*)
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
     4
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
     5
section \<open>Multiple nested quotations and anti-quotations\<close>
10357
wenzelm
parents: 9297
diff changeset
     6
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
     7
theory Multiquote
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
     8
imports Main
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
     9
begin
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    10
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    11
text \<open>
11586
wenzelm
parents: 11020
diff changeset
    12
  Multiple nested quotations and anti-quotations -- basically a
wenzelm
parents: 11020
diff changeset
    13
  generalized version of de-Bruijn representation.
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    14
\<close>
11586
wenzelm
parents: 11020
diff changeset
    15
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    16
syntax
56233
797060c19f5c more symbols;
wenzelm
parents: 52143
diff changeset
    17
  "_quote" :: "'b \<Rightarrow> ('a \<Rightarrow> 'b)"    ("\<guillemotleft>_\<guillemotright>" [0] 1000)
797060c19f5c more symbols;
wenzelm
parents: 52143
diff changeset
    18
  "_antiquote" :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b"    ("\<acute>_" [1000] 1000)
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    19
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    20
parse_translation \<open>
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    21
  let
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59031
diff changeset
    22
    fun antiquote_tr i (Const (\<^syntax_const>\<open>_antiquote\<close>, _) $
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59031
diff changeset
    23
          (t as Const (\<^syntax_const>\<open>_antiquote\<close>, _) $ _)) = skip_antiquote_tr i t
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59031
diff changeset
    24
      | antiquote_tr i (Const (\<^syntax_const>\<open>_antiquote\<close>, _) $ t) =
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    25
          antiquote_tr i t $ Bound i
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    26
      | antiquote_tr i (t $ u) = antiquote_tr i t $ antiquote_tr i u
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    27
      | antiquote_tr i (Abs (x, T, t)) = Abs (x, T, antiquote_tr (i + 1) t)
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    28
      | antiquote_tr _ a = a
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59031
diff changeset
    29
    and skip_antiquote_tr i ((c as Const (\<^syntax_const>\<open>_antiquote\<close>, _)) $ t) =
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    30
          c $ skip_antiquote_tr i t
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    31
      | skip_antiquote_tr i t = antiquote_tr i t;
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    32
8578
wenzelm
parents: 8572
diff changeset
    33
    fun quote_tr [t] = Abs ("s", dummyT, antiquote_tr 0 (Term.incr_boundvars 1 t))
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    34
      | quote_tr ts = raise TERM ("quote_tr", ts);
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 59031
diff changeset
    35
  in [(\<^syntax_const>\<open>_quote\<close>, K quote_tr)] end
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    36
\<close>
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    37
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    38
text \<open>basic examples\<close>
11823
5a3fcd84e55e guillemot syntax;
wenzelm
parents: 11586
diff changeset
    39
term "\<guillemotleft>a + b + c\<guillemotright>"
5a3fcd84e55e guillemot syntax;
wenzelm
parents: 11586
diff changeset
    40
term "\<guillemotleft>a + b + c + \<acute>x + \<acute>y + 1\<guillemotright>"
5a3fcd84e55e guillemot syntax;
wenzelm
parents: 11586
diff changeset
    41
term "\<guillemotleft>\<acute>(f w) + \<acute>x\<guillemotright>"
5a3fcd84e55e guillemot syntax;
wenzelm
parents: 11586
diff changeset
    42
term "\<guillemotleft>f \<acute>x \<acute>y z\<guillemotright>"
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    43
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    44
text \<open>advanced examples\<close>
11823
5a3fcd84e55e guillemot syntax;
wenzelm
parents: 11586
diff changeset
    45
term "\<guillemotleft>\<guillemotleft>\<acute>\<acute>x + \<acute>y\<guillemotright>\<guillemotright>"
56233
797060c19f5c more symbols;
wenzelm
parents: 52143
diff changeset
    46
term "\<guillemotleft>\<guillemotleft>\<acute>\<acute>x + \<acute>y\<guillemotright> \<circ> \<acute>f\<guillemotright>"
797060c19f5c more symbols;
wenzelm
parents: 52143
diff changeset
    47
term "\<guillemotleft>\<acute>(f \<circ> \<acute>g)\<guillemotright>"
797060c19f5c more symbols;
wenzelm
parents: 52143
diff changeset
    48
term "\<guillemotleft>\<guillemotleft>\<acute>\<acute>(f \<circ> \<acute>g)\<guillemotright>\<guillemotright>"
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    49
9297
wenzelm
parents: 8627
diff changeset
    50
end