src/HOL/ex/Coercion_Examples.thy
changeset 40297 c753e3f8b4d6
parent 40283 805ce1d64af0
child 40839 48e01d16dd17
equal deleted inserted replaced
40296:ac4d75f86d97 40297:c753e3f8b4d6
     9 uses "~~/src/Tools/subtyping.ML"
     9 uses "~~/src/Tools/subtyping.ML"
    10 begin
    10 begin
    11 
    11 
    12 setup Subtyping.setup
    12 setup Subtyping.setup
    13 
    13 
    14 (* Coercion/type maps definitions*)
    14 (* Error messages test *)
    15 
    15 
    16 consts func :: "(nat \<Rightarrow> int) \<Rightarrow> nat"
    16 consts func :: "(nat \<Rightarrow> int) \<Rightarrow> nat"
    17 consts arg :: "int \<Rightarrow> nat"
    17 consts arg :: "int \<Rightarrow> nat"
    18 (* Invariant arguments
    18 (* Invariant arguments
    19 term "func arg"
    19 term "func arg"
    34 *)
    34 *)
    35 (* Different constructors
    35 (* Different constructors
    36 term "[1::int] = func"
    36 term "[1::int] = func"
    37 *)
    37 *)
    38 
    38 
       
    39 (* Coercion/type maps definitions *)
       
    40 
    39 primrec nat_of_bool :: "bool \<Rightarrow> nat"
    41 primrec nat_of_bool :: "bool \<Rightarrow> nat"
    40 where
    42 where
    41   "nat_of_bool False = 0"
    43   "nat_of_bool False = 0"
    42 | "nat_of_bool True = 1"
    44 | "nat_of_bool True = 1"
    43 
    45 
    44 declare [[coercion nat_of_bool]]
    46 declare [[coercion nat_of_bool]]
    45 
    47 
    46 declare [[coercion int]]
    48 declare [[coercion int]]
    47 
    49 
    48 declare [[map_function map]]
    50 declare [[coercion_map map]]
    49 
    51 
    50 definition map_fun :: "('a \<Rightarrow> 'b) \<Rightarrow> ('c \<Rightarrow> 'd) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'd)" where
    52 definition map_fun :: "('a \<Rightarrow> 'b) \<Rightarrow> ('c \<Rightarrow> 'd) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'd)" where
    51  "map_fun f g h = g o h o f"
    53  "map_fun f g h = g o h o f"
    52 
    54 
    53 declare [[map_function "\<lambda> f g h . g o h o f"]]
    55 declare [[coercion_map "\<lambda> f g h . g o h o f"]]
    54 
    56 
    55 primrec map_pair :: "('a \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a * 'b) \<Rightarrow> ('c * 'd)" where
    57 primrec map_pair :: "('a \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a * 'b) \<Rightarrow> ('c * 'd)" where
    56  "map_pair f g (x,y) = (f x, g y)"
    58  "map_pair f g (x,y) = (f x, g y)"
    57 
    59 
    58 declare [[map_function map_pair]]
    60 declare [[coercion_map map_pair]]
    59 
    61 
    60 (* Examples taken from the haskell draft implementation *)
    62 (* Examples taken from the haskell draft implementation *)
    61 
    63 
    62 term "(1::nat) = True"
    64 term "(1::nat) = True"
    63 
    65