theory Uint16 imports
Uint_Common
Code_Target_Word begin
text‹
Restriction for ML code generation:
This theory assumes that the ML system provides a Word16
implementation (mlton does, but PolyML 5.5 does not).
Therefore, the code setup lives in the target ‹SML_word›
rather than ‹SML›. This ensures that code generation still
works as long as ‹uint16› is not involved.
For the target ‹SML› itself, no special code generation
for this type is set up. Nevertheless, it should work by emulation via 🍋‹16 word›.
Restriction for OCaml code generation:
OCaml does not provide an int16 type, so no special code generation
for this type is set up. ›
section‹Type definition and primitive operations›
typedef uint16 = ‹UNIV :: 16 word set› ..
global_interpretation uint16: word_type_copy Abs_uint16 Rep_uint16 using type_definition_uint16 by (rule word_type_copy.intro)
setup_lifting type_definition_uint16
declare uint16.of_word_of [code abstype]
declare Quotient_uint16 [transfer_rule]
instantiation uint16 :: ‹{comm_ring_1, semiring_modulo, equal, linorder, order_bot, order_top}› begin
uint16: word_type_copy_ring Abs_uint16 Rep_uint16
by standard (fact zero_uint16.rep_eq one_uint16.rep_eq
plus_uint16.rep_eq uminus_uint16.rep_eq minus_uint16.rep_eq
times_uint16.rep_eq divide_uint16.rep_eq modulo_uint16.rep_eq
equal_uint16.rep_eq less_eq_uint16.rep_eq less_uint16.rep_eq
bot_uint16.rep_eq top_uint16.rep_eq)+
proof -
show ‹OFCLASS(uint16, comm_ring_1_class)›
by (rule uint16.of_class_comm_ring_1)
show ‹OFCLASS(uint16, semiring_modulo_class)›
by (fact uint16.of_class_semiring_modulo)
show ‹OFCLASS(uint16, equal_class)›
by (fact uint16.of_class_equal)
show ‹OFCLASS(uint16, linorder_class)›
by (fact uint16.of_class_linorder)
show ‹OFCLASS(uint16, order_bot_class)›
by (fact uint16.of_class_order_bot)
show ‹OFCLASS(uint16, order_top_class)›
by (fact uint16.of_class_order_top)
uint16 :: ‹{interval_bot, interval_top}›
by (fact uint16.of_class_interval_bot uint16.of_class_interval_top)+
uint16 :: ring_bit_operations
bit_uint16 :: ‹uint16 ==> nat ==> bool› is bit .
not_uint16 :: ‹uint16 ==> uint16› is ‹Bit_Operations.not› .
and_uint16 :: ‹uint16 ==> uint16 ==> uint16› is ‹Bit_Operations.and› .
or_uint16 :: ‹uint16 ==> uint16 ==> uint16› is ‹Bit_Operations.or› .
xor_uint16 :: ‹uint16 ==> uint16 ==> uint16› is ‹Bit_Operations.xor› .
mask_uint16 :: ‹nat ==> uint16› is mask .
push_bit_uint16 :: ‹nat ==> uint16 ==> uint16› is push_bit .
drop_bit_uint16 :: ‹nat ==> uint16 ==> uint16› is drop_bit .
signed_drop_bit_uint16 :: ‹nat ==> uint16 ==> uint16› is signed_drop_bit .
take_bit_uint16 :: ‹nat ==> uint16 ==> uint16› is take_bit .
set_bit_uint16 :: ‹nat ==> uint16 ==> uint16› is Bit_Operations.set_bit .
unset_bit_uint16 :: ‹nat ==> uint16 ==> uint16› is unset_bit .
flip_bit_uint16 :: ‹nat ==> uint16 ==> uint16› is flip_bit .
using uint16.of_class_bit_comprehension
by simp_all standard
‹Code setup›
code_module Uint16 ⇀ (SML_word)
\<open>(* Test that words can handle numbers between 0 and 15 *)
val _ = if4 <= Word.wordSize then () else raise (Fail ("wordSize less than 4"));
structure Uint16 : sig
val shiftl : Word16.word -> IntInf.int -> Word16.word
val shiftr : Word16.word -> IntInf.int -> Word16.word
val shiftr_signed : Word16.word -> IntInf.int -> Word16.word
val test_bit : Word16.word -> IntInf.int -> bool end = struct
fun shiftl x n =
Word16.<< (x, Word.fromLargeInt (IntInf.toLarge n))
fun shiftr x n =
Word16.>> (x, Word.fromLargeInt (IntInf.toLarge n))
fun shiftr_signed x n =
Word16.~>> (x, Word.fromLargeInt (IntInf.toLarge n))
fun test_bit x n =
Word16.andb (x, Word16.<< (0wx1, Word.fromLargeInt (IntInf.toLarge n))) <> Word16.fromInt 0
text‹
Avoid @{term Abs_uint16} in generated code, use @{term Rep_uint16'} instead.
The symbolic implementations for code\_simp use @{term Rep_uint16}.
The new destructor @{term Rep_uint16'} is executable.
As the simplifier is given the [code abstract] equations literally,
we cannot implement @{term Rep_uint16} directly, because that makes code\_simp loop.
If code generation raises Match, some equation probably contains @{term Rep_uint16}
([code abstract] equations for @{typ uint16} may use @{term Rep_uint16} because
these instances will be folded away.)
To convert @{typ "16 word"} values into @{typ uint16}, use @{term "Abs_uint16'"}. ›
definition Rep_uint16' where [simp]: "Rep_uint16' = Rep_uint16"
definition uint16_div :: "uint16 ==> uint16 ==> uint16" where"uint16_div x y = (if y = 0 then undefined ((div) :: uint16 ==> _) x (0 :: uint16) else x div y)"
definition uint16_mod :: "uint16 ==> uint16 ==> uint16" where"uint16_mod x y = (if y = 0 then undefined ((mod) :: uint16 ==> _) x (0 :: uint16) else x mod y)"
contextincludes undefined_transfer begin
lemma div_uint16_code [code]: "x div y = (if y = 0 then 0 else uint16_div x y)" unfolding uint16_div_def by transfer (simp add: word_div_def)
lemma mod_uint16_code [code]: "x mod y = (if y = 0 then x else uint16_mod x y)" unfolding uint16_mod_def by transfer (simp add: word_mod_def)
lemma uint16_div_code [code]: "Rep_uint16 (uint16_div x y) = (if y = 0 then Rep_uint16 (undefined ((div) :: uint16 ==> _) x (0 :: uint16)) else Rep_uint16 x div Rep_uint16 y)" unfolding uint16_div_def by transfer simp
lemma uint16_mod_code [code]: "Rep_uint16 (uint16_mod x y) = (if y = 0 then Rep_uint16 (undefined ((mod) :: uint16 ==> _) x (0 :: uint16)) else Rep_uint16 x mod Rep_uint16 y)" unfolding uint16_mod_def by transfer simp
lemma uint16_msb_test_bit: "msb x ⟷ bit (x :: uint16) 15" by transfer (simp add: msb_word_iff_bit)
lemma msb_uint16_code [code]: "msb x ⟷ uint16_test_bit x 15" by (simp add: uint16.test_bit_def uint16_msb_test_bit)
lemma uint16_of_int_code [code]: "uint16_of_int i = Uint16 (integer_of_int i)"
including integer.lifting by transfer simp
lemma int_of_uint16_code [code]: "int_of_uint16 x = int_of_integer (integer_of_uint16 x)" by (simp add: int_of_uint16.rep_eq integer_of_uint16_def)
lemma uint16_of_nat_code [code]: "uint16_of_nat = uint16_of_int ∘ int" by transfer (simp add: fun_eq_iff)
lemma nat_of_uint16_code [code]: "nat_of_uint16 x = nat_of_integer (integer_of_uint16 x)" unfolding integer_of_uint16_def including integer.lifting by transfer simp
lemma integer_of_uint16_code [code]: "integer_of_uint16 n = integer_of_int (uint (Rep_uint16' n))" unfolding integer_of_uint16_def by transfer auto
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.3Bemerkung:
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.