equal
deleted
inserted
replaced
10 section {* Universal error monad *} |
10 section {* Universal error monad *} |
11 |
11 |
12 datatype 'a maybe = Ok 'a | Fail |
12 datatype 'a maybe = Ok 'a | Fail |
13 |
13 |
14 definition |
14 definition |
15 bind :: "'a maybe \<Rightarrow> ('a \<Rightarrow> 'b maybe) \<Rightarrow> 'b maybe" (infixl "\<bind>" 60) |
15 bind :: "'a maybe \<Rightarrow> ('a \<Rightarrow> 'b maybe) \<Rightarrow> 'b maybe" (infixl "\<bind>" 60) where |
16 "m \<bind> f = (case m of Ok r \<Rightarrow> f r | Fail \<Rightarrow> Fail)" |
16 "m \<bind> f = (case m of Ok r \<Rightarrow> f r | Fail \<Rightarrow> Fail)" |
17 |
17 |
18 syntax |
18 syntax |
19 "_bind" :: "patterns \<Rightarrow> 'a maybe \<Rightarrow> 'b \<Rightarrow> 'c" ("(_ := _;//_)" 0) |
19 "_bind" :: "patterns \<Rightarrow> 'a maybe \<Rightarrow> 'b \<Rightarrow> 'c" ("(_ := _;//_)" 0) |
20 translations |
20 translations |
83 primrec (free_tv_list) |
83 primrec (free_tv_list) |
84 "free_tv [] = {}" |
84 "free_tv [] = {}" |
85 "free_tv (x # xs) = free_tv x \<union> free_tv xs" |
85 "free_tv (x # xs) = free_tv x \<union> free_tv xs" |
86 |
86 |
87 definition |
87 definition |
88 dom :: "subst \<Rightarrow> nat set" |
88 dom :: "subst \<Rightarrow> nat set" where |
89 "dom s = {n. s n \<noteq> TVar n}" |
89 "dom s = {n. s n \<noteq> TVar n}" |
90 -- {* domain of a substitution *} |
90 -- {* domain of a substitution *} |
91 |
91 |
92 cod :: "subst \<Rightarrow> nat set" |
92 definition |
|
93 cod :: "subst \<Rightarrow> nat set" where |
93 "cod s = (\<Union>m \<in> dom s. free_tv (s m))" |
94 "cod s = (\<Union>m \<in> dom s. free_tv (s m))" |
94 -- {* codomain of a substitutions: the introduced variables *} |
95 -- {* codomain of a substitutions: the introduced variables *} |
95 |
96 |
96 defs (overloaded) |
97 defs (overloaded) |
97 free_tv_subst: "free_tv s \<equiv> dom s \<union> cod s" |
98 free_tv_subst: "free_tv s \<equiv> dom s \<union> cod s" |
101 wrt.\ a type structure @{text s}, i.e.\ whether @{text n} is greater |
102 wrt.\ a type structure @{text s}, i.e.\ whether @{text n} is greater |
102 than any type variable occuring in the type structure. |
103 than any type variable occuring in the type structure. |
103 *} |
104 *} |
104 |
105 |
105 definition |
106 definition |
106 new_tv :: "nat \<Rightarrow> 'a::type_struct \<Rightarrow> bool" |
107 new_tv :: "nat \<Rightarrow> 'a::type_struct \<Rightarrow> bool" where |
107 "new_tv n ts = (\<forall>m. m \<in> free_tv ts \<longrightarrow> m < n)" |
108 "new_tv n ts = (\<forall>m. m \<in> free_tv ts \<longrightarrow> m < n)" |
108 |
109 |
109 |
110 |
110 subsubsection {* Identity substitution *} |
111 subsubsection {* Identity substitution *} |
111 |
112 |
112 definition |
113 definition |
113 id_subst :: subst |
114 id_subst :: subst where |
114 "id_subst = (\<lambda>n. TVar n)" |
115 "id_subst = (\<lambda>n. TVar n)" |
115 |
116 |
116 lemma app_subst_id_te [simp]: |
117 lemma app_subst_id_te [simp]: |
117 "$id_subst = (\<lambda>t::typ. t)" |
118 "$id_subst = (\<lambda>t::typ. t)" |
118 -- {* application of @{text id_subst} does not change type expression *} |
119 -- {* application of @{text id_subst} does not change type expression *} |
382 |
383 |
383 consts |
384 consts |
384 has_type :: "(typ list \<times> expr \<times> typ) set" |
385 has_type :: "(typ list \<times> expr \<times> typ) set" |
385 |
386 |
386 abbreviation |
387 abbreviation |
387 has_type_rel ("((_) |-/ (_) :: (_))" [60, 0, 60] 60) |
388 has_type_rel ("((_) |-/ (_) :: (_))" [60, 0, 60] 60) where |
388 "a |- e :: t == (a, e, t) \<in> has_type" |
389 "a |- e :: t == (a, e, t) \<in> has_type" |
389 |
390 |
390 inductive has_type |
391 inductive has_type |
391 intros |
392 intros |
392 Var: "n < length a \<Longrightarrow> a |- Var n :: a ! n" |
393 Var: "n < length a \<Longrightarrow> a |- Var n :: a ! n" |