src/HOL/Library/Type_Length.thy
changeset 64113 86efd3d4dc98
parent 63762 6920b1885eff
child 69593 3dda49e08b9d
equal deleted inserted replaced
64112:84c1ae86b9af 64113:86efd3d4dc98
    15 \<close>
    15 \<close>
    16 
    16 
    17 class len0 =
    17 class len0 =
    18   fixes len_of :: "'a itself \<Rightarrow> nat"
    18   fixes len_of :: "'a itself \<Rightarrow> nat"
    19 
    19 
       
    20 syntax "_type_length" :: "type \<Rightarrow> nat" ("(1LENGTH/(1'(_')))")
       
    21 
       
    22 translations "LENGTH('a)" \<rightharpoonup>
       
    23   "CONST len_of (CONST Pure.type :: 'a itself)"
       
    24 
       
    25 print_translation \<open>
       
    26   let
       
    27     fun len_of_itself_tr' ctxt [Const (@{const_syntax Pure.type}, Type (_, [T]))] =
       
    28       Syntax.const @{syntax_const "_type_length"} $ Syntax_Phases.term_of_typ ctxt T
       
    29   in [(@{const_syntax len_of}, len_of_itself_tr')] end
       
    30 \<close>
       
    31 
    20 text \<open>Some theorems are only true on words with length greater 0.\<close>
    32 text \<open>Some theorems are only true on words with length greater 0.\<close>
    21 
    33 
    22 class len = len0 +
    34 class len = len0 +
    23   assumes len_gt_0 [iff]: "0 < len_of TYPE ('a)"
    35   assumes len_gt_0 [iff]: "0 < LENGTH('a)"
    24 
    36 
    25 instantiation num0 and num1 :: len0
    37 instantiation num0 and num1 :: len0
    26 begin
    38 begin
    27 
    39 
    28 definition len_num0: "len_of (_ :: num0 itself) = 0"
    40 definition len_num0: "len_of (_ :: num0 itself) = 0"
    33 end
    45 end
    34 
    46 
    35 instantiation bit0 and bit1 :: (len0) len0
    47 instantiation bit0 and bit1 :: (len0) len0
    36 begin
    48 begin
    37 
    49 
    38 definition len_bit0: "len_of (_ :: 'a::len0 bit0 itself) = 2 * len_of TYPE('a)"
    50 definition len_bit0: "len_of (_ :: 'a::len0 bit0 itself) = 2 * LENGTH('a)"
    39 definition len_bit1: "len_of (_ :: 'a::len0 bit1 itself) = 2 * len_of TYPE('a) + 1"
    51 definition len_bit1: "len_of (_ :: 'a::len0 bit1 itself) = 2 * LENGTH('a) + 1"
    40 
    52 
    41 instance ..
    53 instance ..
    42 
    54 
    43 end
    55 end
    44 
    56