# Theory Derive_Examples

section Examples
theory Derive_Examples
imports
Derive
"Comparator_Generator/Compare_Order_Instances"
"Equality_Generator/Equality_Instances"
HOL.Rat
begin
subsection "Rational Numbers"
text ‹The rational numbers are not a datatype, so it will not be possible to derive
corresponding instances of comparators, hashcodes, etc. via the generators. But we can and should
still register the existing instances, so that later datatypes are supported
which use rational numbers.›
text ‹Use the linear order on rationals to define the @{class compare_order}-instance.›
derive (linorder) compare_order rat
text ‹Use @{term "(=) :: rat => rat => bool"} as equality function.›
derive (eq) equality rat
text ‹First manually define a hashcode function.›
instantiation rat :: hashable
begin
definition "def_hashmap_size = (λ_ :: rat itself. 10)"
definition "hashcode (r :: rat) = hashcode (quotient_of r)"
instance
by (intro_classes)(simp_all add: def_hashmap_size_rat_def)
end
text ‹And then register it at the generator.›
derive (hashcode) hash_code rat
subsection "A Datatype Without Nested Recursion"