605 \lstsml{Thy/examples/collect_duplicates.ML} |
606 \lstsml{Thy/examples/collect_duplicates.ML} |
606 *} |
607 *} |
607 |
608 |
608 text {* |
609 text {* |
609 Obviously, polymorphic equality is implemented the Haskell |
610 Obviously, polymorphic equality is implemented the Haskell |
610 way using a type class. How is this achieved? By an |
611 way using a type class. How is this achieved? HOL introduces |
611 almost trivial definition in the HOL setup: |
612 an explicit class @{text eq} with a corresponding operation |
612 *} |
613 @{const eq} such that @{thm eq [no_vars]}. |
613 (*<*) |
614 The preprocessing framework does the rest. |
614 setup {* Sign.add_path "foo" *} |
|
615 consts "op =" :: "'a" |
|
616 (*>*) |
|
617 class eq (attach "op =") = type |
|
618 |
|
619 text {* |
|
620 This merely introduces a class @{text eq} with corresponding |
|
621 operation @{text "op ="}; |
|
622 the preprocessing framework does the rest. |
|
623 For datatypes, instances of @{text eq} are implicitly derived |
615 For datatypes, instances of @{text eq} are implicitly derived |
624 when possible. |
616 when possible. For other types, you may instantiate @{text eq} |
|
617 manually like any other type class. |
625 |
618 |
626 Though this @{text eq} class is designed to get rarely in |
619 Though this @{text eq} class is designed to get rarely in |
627 the way, a subtlety |
620 the way, a subtlety |
628 enters the stage when definitions of overloaded constants |
621 enters the stage when definitions of overloaded constants |
629 are dependent on operational equality. For example, let |
622 are dependent on operational equality. For example, let |
630 us define a lexicographic ordering on tuples: |
623 us define a lexicographic ordering on tuples: |
631 *} |
624 *} |
632 (*<*) |
625 |
633 hide (open) "class" eq |
|
634 hide (open) const "op =" |
|
635 setup {* Sign.parent_path *} |
|
636 (*>*) |
|
637 instantiation * :: (ord, ord) ord |
626 instantiation * :: (ord, ord) ord |
638 begin |
627 begin |
639 |
628 |
640 definition |
629 definition |
641 [code func del]: "p1 < p2 \<longleftrightarrow> (let (x1, y1) = p1; (x2, y2) = p2 in |
630 [code func del]: "p1 < p2 \<longleftrightarrow> (let (x1, y1) = p1; (x2, y2) = p2 in |