src/HOL/ex/Term_Of_Syntax.thy
author huffman
Mon, 16 Feb 2009 13:08:21 -0800
changeset 29943 922b931fd2eb
parent 28965 1de908189869
child 31139 0b615ac7eeaf
permissions -rw-r--r--
datatype num = One | Dig0 num | Dig1 num
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28258
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/ex/Term_Of_Syntax.thy
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
     2
    ID:         $Id$
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
     4
*)
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
     5
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
     6
header {* Input syntax for term_of functions *}
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
     7
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
     8
theory Term_Of_Syntax
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
     9
imports Code_Eval
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    10
begin
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    11
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    12
setup {*
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28335
diff changeset
    13
  Sign.declare_const [] ((Binding.name "rterm_of", @{typ "'a \<Rightarrow> 'b"}), NoSyn)
28258
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    14
  #> snd
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    15
*}
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    16
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    17
notation (output)
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    18
  rterm_of ("\<guillemotleft>_\<guillemotright>")
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    19
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    20
locale rterm_syntax =
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    21
  fixes rterm_of_syntax :: "'a \<Rightarrow> 'b" ("\<guillemotleft>_\<guillemotright>")
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    22
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    23
parse_translation {*
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    24
let
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    25
  fun rterm_of_tr [t] = Lexicon.const @{const_name rterm_of} $ t
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    26
    | rterm_of_tr ts = raise TERM ("rterm_of_tr", ts);
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    27
in
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    28
  [(Syntax.fixedN ^ "rterm_of_syntax", rterm_of_tr)]
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    29
end
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    30
*}
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    31
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    32
setup {*
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    33
let
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    34
  val subst_rterm_of = Eval.mk_term
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    35
    (fn (v, _) => error ("illegal free variable in term quotation: " ^ quote v))
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28258
diff changeset
    36
    (Typerep.mk (fn (v, sort) => Typerep.typerep (TFree (v, sort))));
28258
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    37
  fun subst_rterm_of' (Const (@{const_name rterm_of}, _), [t]) = subst_rterm_of t
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    38
    | subst_rterm_of' (Const (@{const_name rterm_of}, _), _) =
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    39
        error ("illegal number of arguments for " ^ quote @{const_name rterm_of})
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    40
    | subst_rterm_of' (t, ts) = list_comb (t, map (subst_rterm_of' o strip_comb) ts);
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    41
  fun subst_rterm_of'' t = 
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    42
    let
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    43
      val t' = subst_rterm_of' (strip_comb t);
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    44
    in if t aconv t'
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    45
      then NONE
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    46
      else SOME t'
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    47
    end;
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    48
  fun check_rterm_of ts ctxt =
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    49
    let
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    50
      val ts' = map subst_rterm_of'' ts;
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    51
    in if exists is_some ts'
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    52
      then SOME (map2 the_default ts ts', ctxt)
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    53
      else NONE
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    54
    end;
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    55
in
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    56
  Context.theory_map (Syntax.add_term_check 0 "rterm_of" check_rterm_of)
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    57
end;
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    58
*}
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    59
4bf450d50c32 moved term_of syntax to separate theory
haftmann
parents:
diff changeset
    60
end