author | haftmann |
Sat, 08 Oct 2016 14:09:53 +0200 | |
changeset 64113 | 86efd3d4dc98 |
parent 63762 | 6920b1885eff |
child 69593 | 3dda49e08b9d |
permissions | -rw-r--r-- |
63762 | 1 |
(* Title: HOL/Library/Type_Length.thy |
2 |
Author: John Matthews, Galois Connections, Inc., Copyright 2006 |
|
24465 | 3 |
*) |
25262
d0928156e326
Added reference to Jeremy Dawson's paper on the word library.
kleing
parents:
24465
diff
changeset
|
4 |
|
63762 | 5 |
section \<open>Assigning lengths to types by type classes\<close> |
25262
d0928156e326
Added reference to Jeremy Dawson's paper on the word library.
kleing
parents:
24465
diff
changeset
|
6 |
|
37655 | 7 |
theory Type_Length |
63762 | 8 |
imports Numeral_Type |
24465 | 9 |
begin |
10 |
||
61799 | 11 |
text \<open> |
24465 | 12 |
The aim of this is to allow any type as index type, but to provide a |
13 |
default instantiation for numeral types. This independence requires |
|
63762 | 14 |
some duplication with the definitions in \<^file>\<open>Numeral_Type.thy\<close>. |
61799 | 15 |
\<close> |
24465 | 16 |
|
29608 | 17 |
class len0 = |
26514 | 18 |
fixes len_of :: "'a itself \<Rightarrow> nat" |
24465 | 19 |
|
64113 | 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 |
||
63762 | 32 |
text \<open>Some theorems are only true on words with length greater 0.\<close> |
26514 | 33 |
|
34 |
class len = len0 + |
|
64113 | 35 |
assumes len_gt_0 [iff]: "0 < LENGTH('a)" |
26514 | 36 |
|
37 |
instantiation num0 and num1 :: len0 |
|
38 |
begin |
|
39 |
||
63762 | 40 |
definition len_num0: "len_of (_ :: num0 itself) = 0" |
41 |
definition len_num1: "len_of (_ :: num1 itself) = 1" |
|
26514 | 42 |
|
43 |
instance .. |
|
24465 | 44 |
|
26514 | 45 |
end |
46 |
||
47 |
instantiation bit0 and bit1 :: (len0) len0 |
|
48 |
begin |
|
24465 | 49 |
|
64113 | 50 |
definition len_bit0: "len_of (_ :: 'a::len0 bit0 itself) = 2 * LENGTH('a)" |
51 |
definition len_bit1: "len_of (_ :: 'a::len0 bit1 itself) = 2 * LENGTH('a) + 1" |
|
26514 | 52 |
|
53 |
instance .. |
|
54 |
||
55 |
end |
|
24465 | 56 |
|
57 |
lemmas len_of_numeral_defs [simp] = len_num0 len_num1 len_bit0 len_bit1 |
|
58 |
||
63762 | 59 |
instance num1 :: len |
60 |
by standard simp |
|
61 |
instance bit0 :: (len) len |
|
62 |
by standard simp |
|
63 |
instance bit1 :: (len0) len |
|
64 |
by standard simp |
|
24465 | 65 |
|
66 |
end |