src/HOL/ex/Coercion_Examples.thy
changeset 40282 329cd9dd5949
parent 40281 3c6198fd0937
child 40283 805ce1d64af0
equal deleted inserted replaced
40281:3c6198fd0937 40282:329cd9dd5949
       
     1 (*  Title:      HOL/ex/Coercion_Examples.thy
       
     2     Author:     Dmitriy Traytel, TU Muenchen
       
     3 
       
     4 Examples for coercive subtyping via subtype constraints.
       
     5 *)
       
     6 
     1 theory Coercion_Examples
     7 theory Coercion_Examples
     2 imports Main
     8 imports Main
     3 uses "~~/src/Tools/subtyping.ML"
     9 uses "~~/src/Tools/subtyping.ML"
     4 begin
    10 begin
     5 
    11 
     6 (* Coercion/type maps definitions*) 
    12 (* Coercion/type maps definitions*)
     7 
    13 
     8 consts func :: "(nat \<Rightarrow> int) \<Rightarrow> nat"
    14 consts func :: "(nat \<Rightarrow> int) \<Rightarrow> nat"
     9 consts arg :: "int \<Rightarrow> nat"
    15 consts arg :: "int \<Rightarrow> nat"
    10 (* Invariant arguments
    16 (* Invariant arguments
    11 term "func arg"
    17 term "func arg"
    38 declare [[coercion int]]
    44 declare [[coercion int]]
    39 
    45 
    40 declare [[map_function map]]
    46 declare [[map_function map]]
    41 
    47 
    42 definition map_fun :: "('a \<Rightarrow> 'b) \<Rightarrow> ('c \<Rightarrow> 'd) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'd)" where
    48 definition map_fun :: "('a \<Rightarrow> 'b) \<Rightarrow> ('c \<Rightarrow> 'd) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'd)" where
    43  "map_fun f g h = g o h o f" 
    49  "map_fun f g h = g o h o f"
    44 
    50 
    45 declare [[map_function "\<lambda> f g h . g o h o f"]]
    51 declare [[map_function "\<lambda> f g h . g o h o f"]]
    46 
    52 
    47 primrec map_pair :: "('a \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a * 'b) \<Rightarrow> ('c * 'd)" where
    53 primrec map_pair :: "('a \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a * 'b) \<Rightarrow> ('c * 'd)" where
    48  "map_pair f g (x,y) = (f x, g y)"
    54  "map_pair f g (x,y) = (f x, g y)"
   119 term "map (flip funfun True)"
   125 term "map (flip funfun True)"
   120 
   126 
   121 term "map (flip funfun True) [id,cnat,cint,cbool]"
   127 term "map (flip funfun True) [id,cnat,cint,cbool]"
   122 
   128 
   123 consts ii :: "int \<Rightarrow> int"
   129 consts ii :: "int \<Rightarrow> int"
   124 consts aaa :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" 
   130 consts aaa :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   125 consts nlist :: "nat list"
   131 consts nlist :: "nat list"
   126 consts ilil :: "int list \<Rightarrow> int list"
   132 consts ilil :: "int list \<Rightarrow> int list"
   127 
   133 
   128 term "ii (aaa (1::nat) True)"
   134 term "ii (aaa (1::nat) True)"
   129 
   135 
   133 
   139 
   134 (***************************************************)
   140 (***************************************************)
   135 
   141 
   136 (* Other examples *)
   142 (* Other examples *)
   137 
   143 
   138 definition xs :: "bool list" where "xs = [True]" 
   144 definition xs :: "bool list" where "xs = [True]"
   139 
   145 
   140 term "(xs::nat list)"
   146 term "(xs::nat list)"
   141 
   147 
   142 term "(1::nat) = True"
   148 term "(1::nat) = True"
   143 
   149