src/HOL/ex/Multiquote.thy
author wenzelm
Thu Sep 27 15:42:08 2001 +0200 (2001-09-27)
changeset 11586 d8a7f6318457
parent 11020 646c929b6293
child 11823 5a3fcd84e55e
permissions -rw-r--r--
tuned;
wenzelm@8569
     1
(*  Title:      HOL/ex/Multiquote.thy
wenzelm@8569
     2
    ID:         $Id$
wenzelm@8569
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@9297
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@8569
     5
*)
wenzelm@8569
     6
wenzelm@10357
     7
header {* Multiple nested quotations and anti-quotations *}
wenzelm@10357
     8
wenzelm@9297
     9
theory Multiquote = Main:
wenzelm@8569
    10
wenzelm@11586
    11
text {*
wenzelm@11586
    12
  Multiple nested quotations and anti-quotations -- basically a
wenzelm@11586
    13
  generalized version of de-Bruijn representation.
wenzelm@11586
    14
*}
wenzelm@11586
    15
wenzelm@8569
    16
syntax
wenzelm@8578
    17
  "_quote" :: "'b => ('a => 'b)"	     (".'(_')." [0] 1000)
wenzelm@10838
    18
  "_antiquote" :: "('a => 'b) => 'b"         ("\<acute>_" [1000] 1000)
wenzelm@8569
    19
wenzelm@8569
    20
parse_translation {*
wenzelm@8569
    21
  let
wenzelm@8569
    22
    fun antiquote_tr i (Const ("_antiquote", _) $ (t as Const ("_antiquote", _) $ _)) =
wenzelm@8569
    23
          skip_antiquote_tr i t
wenzelm@8569
    24
      | antiquote_tr i (Const ("_antiquote", _) $ t) =
wenzelm@8569
    25
          antiquote_tr i t $ Bound i
wenzelm@8569
    26
      | antiquote_tr i (t $ u) = antiquote_tr i t $ antiquote_tr i u
wenzelm@8569
    27
      | antiquote_tr i (Abs (x, T, t)) = Abs (x, T, antiquote_tr (i + 1) t)
wenzelm@8569
    28
      | antiquote_tr _ a = a
wenzelm@8569
    29
    and skip_antiquote_tr i ((c as Const ("_antiquote", _)) $ t) =
wenzelm@8569
    30
          c $ skip_antiquote_tr i t
wenzelm@8569
    31
      | skip_antiquote_tr i t = antiquote_tr i t;
wenzelm@8569
    32
wenzelm@8578
    33
    fun quote_tr [t] = Abs ("s", dummyT, antiquote_tr 0 (Term.incr_boundvars 1 t))
wenzelm@8569
    34
      | quote_tr ts = raise TERM ("quote_tr", ts);
wenzelm@8569
    35
  in [("_quote", quote_tr)] end
wenzelm@9297
    36
*}
wenzelm@8569
    37
wenzelm@9297
    38
text {* basic examples *}
wenzelm@9297
    39
term ".(a + b + c)."
wenzelm@10838
    40
term ".(a + b + c + \<acute>x + \<acute>y + 1)."
wenzelm@10838
    41
term ".(\<acute>(f w) + \<acute>x)."
wenzelm@10838
    42
term ".(f \<acute>x \<acute>y z)."
wenzelm@8569
    43
wenzelm@9297
    44
text {* advanced examples *}
wenzelm@10838
    45
term ".(.(\<acute>\<acute>x + \<acute>y).)."
wenzelm@11020
    46
term ".(.(\<acute>\<acute>x + \<acute>y). \<circ> \<acute>f)."
wenzelm@11020
    47
term ".(\<acute>(f \<circ> \<acute>g))."
wenzelm@11020
    48
term ".(.( \<acute>\<acute>(f \<circ> \<acute>g) ).)."
wenzelm@8569
    49
wenzelm@9297
    50
end