src/HOL/ex/Multiquote.thy
author huffman
Wed, 09 Nov 2011 11:44:42 +0100
changeset 45436 62bc9474d04b
parent 35113 1a0c129bb2e0
child 52143 36ffe23b25f8
permissions -rw-r--r--
use simproc_setup for some nat_numeral simprocs; add simproc tests
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
10357
wenzelm
parents: 9297
diff changeset
     5
header {* Multiple nested quotations and anti-quotations *}
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
11586
wenzelm
parents: 11020
diff changeset
    11
text {*
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.
wenzelm
parents: 11020
diff changeset
    14
*}
wenzelm
parents: 11020
diff changeset
    15
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    16
syntax
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
    17
  "_quote" :: "'b => ('a => 'b)"    ("\<guillemotleft>_\<guillemotright>" [0] 1000)
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
    18
  "_antiquote" :: "('a => 'b) => 'b"    ("\<acute>_" [1000] 1000)
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    19
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    20
parse_translation {*
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    21
  let
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
    22
    fun antiquote_tr i (Const (@{syntax_const "_antiquote"}, _) $
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
    23
          (t as Const (@{syntax_const "_antiquote"}, _) $ _)) = skip_antiquote_tr i t
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
    24
      | antiquote_tr i (Const (@{syntax_const "_antiquote"}, _) $ 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
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
    29
    and skip_antiquote_tr i ((c as Const (@{syntax_const "_antiquote"}, _)) $ 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);
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 32960
diff changeset
    35
  in [(@{syntax_const "_quote"}, quote_tr)] end
9297
wenzelm
parents: 8627
diff changeset
    36
*}
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    37
9297
wenzelm
parents: 8627
diff changeset
    38
text {* basic examples *}
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
9297
wenzelm
parents: 8627
diff changeset
    44
text {* advanced examples *}
11823
5a3fcd84e55e guillemot syntax;
wenzelm
parents: 11586
diff changeset
    45
term "\<guillemotleft>\<guillemotleft>\<acute>\<acute>x + \<acute>y\<guillemotright>\<guillemotright>"
5a3fcd84e55e guillemot syntax;
wenzelm
parents: 11586
diff changeset
    46
term "\<guillemotleft>\<guillemotleft>\<acute>\<acute>x + \<acute>y\<guillemotright> o \<acute>f\<guillemotright>"
5a3fcd84e55e guillemot syntax;
wenzelm
parents: 11586
diff changeset
    47
term "\<guillemotleft>\<acute>(f o \<acute>g)\<guillemotright>"
5a3fcd84e55e guillemot syntax;
wenzelm
parents: 11586
diff changeset
    48
term "\<guillemotleft>\<guillemotleft>\<acute>\<acute>(f o \<acute>g)\<guillemotright>\<guillemotright>"
8569
748a9699f28d added HOL/ex/Multiquote.thy;
wenzelm
parents:
diff changeset
    49
9297
wenzelm
parents: 8627
diff changeset
    50
end