src/HOL/ex/Multiquote.thy
author wenzelm
Mon Oct 30 18:24:42 2000 +0100 (2000-10-30)
changeset 10357 0d0cac129618
parent 9297 bafe45732b10
child 10834 a7897aebbffc
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
Multiple nested quotations and anti-quotations -- basically a
wenzelm@8569
     7
generalized version of de-Bruijn representation.
wenzelm@8569
     8
*)
wenzelm@8569
     9
wenzelm@10357
    10
header {* Multiple nested quotations and anti-quotations *}
wenzelm@10357
    11
wenzelm@9297
    12
theory Multiquote = Main:
wenzelm@8569
    13
wenzelm@8569
    14
syntax
wenzelm@8578
    15
  "_quote" :: "'b => ('a => 'b)"	     (".'(_')." [0] 1000)
wenzelm@9297
    16
  "_antiquote" :: "('a => 'b) => 'b"         ("`_" [1000] 1000)
wenzelm@8569
    17
wenzelm@8569
    18
parse_translation {*
wenzelm@8569
    19
  let
wenzelm@8569
    20
    fun antiquote_tr i (Const ("_antiquote", _) $ (t as Const ("_antiquote", _) $ _)) =
wenzelm@8569
    21
          skip_antiquote_tr i t
wenzelm@8569
    22
      | antiquote_tr i (Const ("_antiquote", _) $ t) =
wenzelm@8569
    23
          antiquote_tr i t $ Bound i
wenzelm@8569
    24
      | antiquote_tr i (t $ u) = antiquote_tr i t $ antiquote_tr i u
wenzelm@8569
    25
      | antiquote_tr i (Abs (x, T, t)) = Abs (x, T, antiquote_tr (i + 1) t)
wenzelm@8569
    26
      | antiquote_tr _ a = a
wenzelm@8569
    27
    and skip_antiquote_tr i ((c as Const ("_antiquote", _)) $ t) =
wenzelm@8569
    28
          c $ skip_antiquote_tr i t
wenzelm@8569
    29
      | skip_antiquote_tr i t = antiquote_tr i t;
wenzelm@8569
    30
wenzelm@8578
    31
    fun quote_tr [t] = Abs ("s", dummyT, antiquote_tr 0 (Term.incr_boundvars 1 t))
wenzelm@8569
    32
      | quote_tr ts = raise TERM ("quote_tr", ts);
wenzelm@8569
    33
  in [("_quote", quote_tr)] end
wenzelm@9297
    34
*}
wenzelm@8569
    35
wenzelm@9297
    36
text {* basic examples *}
wenzelm@9297
    37
term ".(a + b + c)."
wenzelm@9297
    38
term ".(a + b + c + `x + `y + 1)."
wenzelm@9297
    39
term ".(`(f w) + `x)."
wenzelm@9297
    40
term ".(f `x `y z)."
wenzelm@8569
    41
wenzelm@9297
    42
text {* advanced examples *}
wenzelm@9297
    43
term ".(.(` `x + `y).)."
wenzelm@9297
    44
term ".(.(` `x + `y). o `f)."
wenzelm@9297
    45
term ".(`(f o `g))."
wenzelm@9297
    46
term ".(.( ` `(f o `g) ).)."
wenzelm@8569
    47
wenzelm@9297
    48
end