doc-src/IsarAdvanced/Codegen/Thy/Codegen.thy
changeset 26513 6f306c8c2c54
parent 25870 a6a21adf3b55
child 26732 6ea9de67e576
equal deleted inserted replaced
26512:682dfb674df3 26513:6f306c8c2c54
       
     1 
     1 (* $Id$ *)
     2 (* $Id$ *)
     2 
     3 
     3 (*<*)
     4 (*<*)
     4 theory Codegen
     5 theory Codegen
     5 imports Main
     6 imports Main
   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
   940   of Haskell @{text Eq}:
   929   of Haskell @{text Eq}:
   941 *}
   930 *}
   942 
   931 
   943 typedecl bar
   932 typedecl bar
   944 
   933 
   945 instance bar :: eq ..
   934 instantiation bar :: eq
       
   935 begin
       
   936 
       
   937 definition "eq (x\<Colon>bar) y \<longleftrightarrow> x = y"
       
   938 
       
   939 instance by default (simp add: eq_bar_def)
       
   940 
       
   941 end
   946 
   942 
   947 code_type %tt bar
   943 code_type %tt bar
   948   (Haskell "Integer")
   944   (Haskell "Integer")
   949 
   945 
   950 text {*
   946 text {*