199 syntax |
201 syntax |
200 "_pat" :: "'a" |
202 "_pat" :: "'a" |
201 "_var" :: "'a" |
203 "_var" :: "'a" |
202 |
204 |
203 translations |
205 translations |
204 "_Case1 p r" => "branch (_pat p)\<cdot>(_var p r)" |
206 "_Case1 p r" => "CONST branch (_pat p)\<cdot>(_var p r)" |
205 "_var (_args x y) r" => "csplit\<cdot>(_var x (_var y r))" |
207 "_var (_args x y) r" => "CONST csplit\<cdot>(_var x (_var y r))" |
206 "_var () r" => "unit_when\<cdot>r" |
208 "_var () r" => "CONST unit_when\<cdot>r" |
207 |
209 |
208 parse_translation {* |
210 parse_translation {* |
209 (* rewrites (_pat x) => (return) *) |
211 (* rewrites (_pat x) => (return) *) |
210 (* rewrites (_var x t) => (Abs_CFun (%x. t)) *) |
212 (* rewrites (_var x t) => (Abs_CFun (%x. t)) *) |
211 [("_pat", K (Syntax.const "Fixrec.return")), |
213 [("_pat", K (Syntax.const "Fixrec.return")), |
212 mk_binder_tr ("_var", "Abs_CFun")]; |
214 mk_binder_tr ("_var", @{const_syntax Abs_CFun})]; |
213 *} |
215 *} |
214 |
216 |
215 text {* Printing Case expressions *} |
217 text {* Printing Case expressions *} |
216 |
218 |
217 syntax |
219 syntax |
218 "_match" :: "'a" |
220 "_match" :: "'a" |
219 |
221 |
220 print_translation {* |
222 print_translation {* |
221 let |
223 let |
222 fun dest_LAM (Const ("Rep_CFun",_) $ Const ("unit_when",_) $ t) = |
224 fun dest_LAM (Const (@{const_syntax Rep_CFun},_) $ Const (@{const_syntax unit_when},_) $ t) = |
223 (Syntax.const "Unity", t) |
225 (Syntax.const @{const_syntax Unity}, t) |
224 | dest_LAM (Const ("Rep_CFun",_) $ Const ("csplit",_) $ t) = |
226 | dest_LAM (Const (@{const_syntax Rep_CFun},_) $ Const (@{const_syntax csplit},_) $ t) = |
225 let |
227 let |
226 val (v1, t1) = dest_LAM t; |
228 val (v1, t1) = dest_LAM t; |
227 val (v2, t2) = dest_LAM t1; |
229 val (v2, t2) = dest_LAM t1; |
228 in (Syntax.const "_args" $ v1 $ v2, t2) end |
230 in (Syntax.const "_args" $ v1 $ v2, t2) end |
229 | dest_LAM (Const ("Abs_CFun",_) $ t) = |
231 | dest_LAM (Const (@{const_syntax Abs_CFun},_) $ t) = |
230 let |
232 let |
231 val abs = case t of Abs abs => abs |
233 val abs = case t of Abs abs => abs |
232 | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0); |
234 | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0); |
233 val (x, t') = atomic_abs_tr' abs; |
235 val (x, t') = atomic_abs_tr' abs; |
234 in (Syntax.const "_var" $ x, t') end |
236 in (Syntax.const "_var" $ x, t') end |
235 | dest_LAM _ = raise Match; (* too few vars: abort translation *) |
237 | dest_LAM _ = raise Match; (* too few vars: abort translation *) |
236 |
238 |
237 fun Case1_tr' [Const("branch",_) $ p, r] = |
239 fun Case1_tr' [Const(@{const_syntax branch},_) $ p, r] = |
238 let val (v, t) = dest_LAM r; |
240 let val (v, t) = dest_LAM r; |
239 in Syntax.const "_Case1" $ (Syntax.const "_match" $ p $ v) $ t end; |
241 in Syntax.const "_Case1" $ (Syntax.const "_match" $ p $ v) $ t end; |
240 |
242 |
241 in [("Rep_CFun", Case1_tr')] end; |
243 in [(@{const_syntax Rep_CFun}, Case1_tr')] end; |
242 *} |
244 *} |
243 |
245 |
244 translations |
246 translations |
245 "x" <= "_match Fixrec.return (_var x)" |
247 "x" <= "_match Fixrec.return (_var x)" |
246 |
248 |
247 |
249 |
248 subsection {* Pattern combinators for data constructors *} |
250 subsection {* Pattern combinators for data constructors *} |
249 |
251 |
250 types ('a, 'b) pat = "'a \<rightarrow> 'b maybe" |
252 types ('a, 'b) pat = "'a \<rightarrow> 'b maybe" |
251 |
253 |
252 constdefs |
254 definition |
253 cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat" |
255 cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat" where |
254 "cpair_pat p1 p2 \<equiv> \<Lambda>\<langle>x, y\<rangle>. |
256 "cpair_pat p1 p2 = (\<Lambda>\<langle>x, y\<rangle>. |
255 bind\<cdot>(p1\<cdot>x)\<cdot>(\<Lambda> a. bind\<cdot>(p2\<cdot>y)\<cdot>(\<Lambda> b. return\<cdot>\<langle>a, b\<rangle>))" |
257 bind\<cdot>(p1\<cdot>x)\<cdot>(\<Lambda> a. bind\<cdot>(p2\<cdot>y)\<cdot>(\<Lambda> b. return\<cdot>\<langle>a, b\<rangle>)))" |
256 |
258 |
|
259 definition |
257 spair_pat :: |
260 spair_pat :: |
258 "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat" |
261 "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat" where |
259 "spair_pat p1 p2 \<equiv> \<Lambda>(:x, y:). cpair_pat p1 p2\<cdot>\<langle>x, y\<rangle>" |
262 "spair_pat p1 p2 = (\<Lambda>(:x, y:). cpair_pat p1 p2\<cdot>\<langle>x, y\<rangle>)" |
260 |
263 |
261 sinl_pat :: "('a, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" |
264 definition |
262 "sinl_pat p \<equiv> sscase\<cdot>p\<cdot>(\<Lambda> x. fail)" |
265 sinl_pat :: "('a, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where |
263 |
266 "sinl_pat p = sscase\<cdot>p\<cdot>(\<Lambda> x. fail)" |
264 sinr_pat :: "('b, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" |
267 |
265 "sinr_pat p \<equiv> sscase\<cdot>(\<Lambda> x. fail)\<cdot>p" |
268 definition |
266 |
269 sinr_pat :: "('b, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where |
267 up_pat :: "('a, 'b) pat \<Rightarrow> ('a u, 'b) pat" |
270 "sinr_pat p = sscase\<cdot>(\<Lambda> x. fail)\<cdot>p" |
268 "up_pat p \<equiv> fup\<cdot>p" |
271 |
269 |
272 definition |
270 TT_pat :: "(tr, unit) pat" |
273 up_pat :: "('a, 'b) pat \<Rightarrow> ('a u, 'b) pat" where |
271 "TT_pat \<equiv> \<Lambda> b. If b then return\<cdot>() else fail fi" |
274 "up_pat p = fup\<cdot>p" |
272 |
275 |
273 FF_pat :: "(tr, unit) pat" |
276 definition |
274 "FF_pat \<equiv> \<Lambda> b. If b then fail else return\<cdot>() fi" |
277 TT_pat :: "(tr, unit) pat" where |
275 |
278 "TT_pat = (\<Lambda> b. If b then return\<cdot>() else fail fi)" |
276 ONE_pat :: "(one, unit) pat" |
279 |
277 "ONE_pat \<equiv> \<Lambda> ONE. return\<cdot>()" |
280 definition |
|
281 FF_pat :: "(tr, unit) pat" where |
|
282 "FF_pat = (\<Lambda> b. If b then fail else return\<cdot>() fi)" |
|
283 |
|
284 definition |
|
285 ONE_pat :: "(one, unit) pat" where |
|
286 "ONE_pat = (\<Lambda> ONE. return\<cdot>())" |
278 |
287 |
279 text {* Parse translations (patterns) *} |
288 text {* Parse translations (patterns) *} |
280 translations |
289 translations |
281 "_pat (cpair\<cdot>x\<cdot>y)" => "cpair_pat (_pat x) (_pat y)" |
290 "_pat (CONST cpair\<cdot>x\<cdot>y)" => "CONST cpair_pat (_pat x) (_pat y)" |
282 "_pat (spair\<cdot>x\<cdot>y)" => "spair_pat (_pat x) (_pat y)" |
291 "_pat (CONST spair\<cdot>x\<cdot>y)" => "CONST spair_pat (_pat x) (_pat y)" |
283 "_pat (sinl\<cdot>x)" => "sinl_pat (_pat x)" |
292 "_pat (CONST sinl\<cdot>x)" => "CONST sinl_pat (_pat x)" |
284 "_pat (sinr\<cdot>x)" => "sinr_pat (_pat x)" |
293 "_pat (CONST sinr\<cdot>x)" => "CONST sinr_pat (_pat x)" |
285 "_pat (up\<cdot>x)" => "up_pat (_pat x)" |
294 "_pat (CONST up\<cdot>x)" => "CONST up_pat (_pat x)" |
286 "_pat TT" => "TT_pat" |
295 "_pat (CONST TT)" => "CONST TT_pat" |
287 "_pat FF" => "FF_pat" |
296 "_pat (CONST FF)" => "CONST FF_pat" |
288 "_pat ONE" => "ONE_pat" |
297 "_pat (CONST ONE)" => "CONST ONE_pat" |
289 |
298 |
290 text {* Parse translations (variables) *} |
299 text {* Parse translations (variables) *} |
291 translations |
300 translations |
292 "_var (cpair\<cdot>x\<cdot>y) r" => "_var (_args x y) r" |
301 "_var (CONST cpair\<cdot>x\<cdot>y) r" => "_var (_args x y) r" |
293 "_var (spair\<cdot>x\<cdot>y) r" => "_var (_args x y) r" |
302 "_var (CONST spair\<cdot>x\<cdot>y) r" => "_var (_args x y) r" |
294 "_var (sinl\<cdot>x) r" => "_var x r" |
303 "_var (CONST sinl\<cdot>x) r" => "_var x r" |
295 "_var (sinr\<cdot>x) r" => "_var x r" |
304 "_var (CONST sinr\<cdot>x) r" => "_var x r" |
296 "_var (up\<cdot>x) r" => "_var x r" |
305 "_var (CONST up\<cdot>x) r" => "_var x r" |
297 "_var TT r" => "_var () r" |
306 "_var (CONST TT) r" => "_var () r" |
298 "_var FF r" => "_var () r" |
307 "_var (CONST FF) r" => "_var () r" |
299 "_var ONE r" => "_var () r" |
308 "_var (CONST ONE) r" => "_var () r" |
300 |
309 |
301 text {* Print translations *} |
310 text {* Print translations *} |
302 translations |
311 translations |
303 "cpair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)" |
312 "CONST cpair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)" |
304 <= "_match (cpair_pat p1 p2) (_args v1 v2)" |
313 <= "_match (CONST cpair_pat p1 p2) (_args v1 v2)" |
305 "spair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)" |
314 "CONST spair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)" |
306 <= "_match (spair_pat p1 p2) (_args v1 v2)" |
315 <= "_match (CONST spair_pat p1 p2) (_args v1 v2)" |
307 "sinl\<cdot>(_match p1 v1)" <= "_match (sinl_pat p1) v1" |
316 "CONST sinl\<cdot>(_match p1 v1)" <= "_match (CONST sinl_pat p1) v1" |
308 "sinr\<cdot>(_match p1 v1)" <= "_match (sinr_pat p1) v1" |
317 "CONST sinr\<cdot>(_match p1 v1)" <= "_match (CONST sinr_pat p1) v1" |
309 "up\<cdot>(_match p1 v1)" <= "_match (up_pat p1) v1" |
318 "CONST up\<cdot>(_match p1 v1)" <= "_match (CONST up_pat p1) v1" |
310 "TT" <= "_match TT_pat ()" |
319 "CONST TT" <= "_match (CONST TT_pat) ()" |
311 "FF" <= "_match FF_pat ()" |
320 "CONST FF" <= "_match (CONST FF_pat) ()" |
312 "ONE" <= "_match ONE_pat ()" |
321 "CONST ONE" <= "_match (CONST ONE_pat) ()" |
313 |
322 |
314 lemma cpair_pat1: |
323 lemma cpair_pat1: |
315 "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = \<bottom>" |
324 "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = \<bottom>" |
316 apply (simp add: branch_def cpair_pat_def) |
325 apply (simp add: branch_def cpair_pat_def) |
317 apply (rule_tac p="p\<cdot>x" in maybeE, simp_all) |
326 apply (rule_tac p="p\<cdot>x" in maybeE, simp_all) |
380 |
389 |
381 syntax |
390 syntax |
382 "_as_pat" :: "[idt, 'a] \<Rightarrow> 'a" (infixr "\<as>" 10) |
391 "_as_pat" :: "[idt, 'a] \<Rightarrow> 'a" (infixr "\<as>" 10) |
383 "_lazy_pat" :: "'a \<Rightarrow> 'a" ("\<lazy> _" [1000] 1000) |
392 "_lazy_pat" :: "'a \<Rightarrow> 'a" ("\<lazy> _" [1000] 1000) |
384 |
393 |
385 constdefs |
394 definition |
386 wild_pat :: "'a \<rightarrow> unit maybe" |
395 wild_pat :: "'a \<rightarrow> unit maybe" where |
387 "wild_pat \<equiv> \<Lambda> x. return\<cdot>()" |
396 "wild_pat = (\<Lambda> x. return\<cdot>())" |
388 |
397 |
389 as_pat :: "('a \<rightarrow> 'b maybe) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) maybe" |
398 definition |
390 "as_pat p \<equiv> \<Lambda> x. bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> a. return\<cdot>\<langle>x, a\<rangle>)" |
399 as_pat :: "('a \<rightarrow> 'b maybe) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) maybe" where |
391 |
400 "as_pat p = (\<Lambda> x. bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> a. return\<cdot>\<langle>x, a\<rangle>))" |
392 lazy_pat :: "('a \<rightarrow> 'b::pcpo maybe) \<Rightarrow> ('a \<rightarrow> 'b maybe)" |
401 |
393 "lazy_pat p \<equiv> \<Lambda> x. return\<cdot>(run\<cdot>(p\<cdot>x))" |
402 definition |
|
403 lazy_pat :: "('a \<rightarrow> 'b::pcpo maybe) \<Rightarrow> ('a \<rightarrow> 'b maybe)" where |
|
404 "lazy_pat p = (\<Lambda> x. return\<cdot>(run\<cdot>(p\<cdot>x)))" |
394 |
405 |
395 text {* Parse translations (patterns) *} |
406 text {* Parse translations (patterns) *} |
396 translations |
407 translations |
397 "_pat _" => "wild_pat" |
408 "_pat _" => "CONST wild_pat" |
398 "_pat (_as_pat x y)" => "as_pat (_pat y)" |
409 "_pat (_as_pat x y)" => "CONST as_pat (_pat y)" |
399 "_pat (_lazy_pat x)" => "lazy_pat (_pat x)" |
410 "_pat (_lazy_pat x)" => "CONST lazy_pat (_pat x)" |
400 |
411 |
401 text {* Parse translations (variables) *} |
412 text {* Parse translations (variables) *} |
402 translations |
413 translations |
403 "_var _ r" => "_var () r" |
414 "_var _ r" => "_var () r" |
404 "_var (_as_pat x y) r" => "_var (_args x y) r" |
415 "_var (_as_pat x y) r" => "_var (_args x y) r" |
405 "_var (_lazy_pat x) r" => "_var x r" |
416 "_var (_lazy_pat x) r" => "_var x r" |
406 |
417 |
407 text {* Print translations *} |
418 text {* Print translations *} |
408 translations |
419 translations |
409 "_" <= "_match wild_pat ()" |
420 "_" <= "_match (CONST wild_pat) ()" |
410 "_as_pat x (_match p v)" <= "_match (as_pat p) (_args (_var x) v)" |
421 "_as_pat x (_match p v)" <= "_match (CONST as_pat p) (_args (_var x) v)" |
411 "_lazy_pat (_match p v)" <= "_match (lazy_pat p) v" |
422 "_lazy_pat (_match p v)" <= "_match (CONST lazy_pat p) v" |
412 |
423 |
413 text {* Lazy patterns in lambda abstractions *} |
424 text {* Lazy patterns in lambda abstractions *} |
414 translations |
425 translations |
415 "_cabs (_lazy_pat p) r" == "run oo (_Case1 (_lazy_pat p) r)" |
426 "_cabs (_lazy_pat p) r" == "CONST run oo (_Case1 (_lazy_pat p) r)" |
416 |
427 |
417 lemma wild_pat [simp]: "branch wild_pat\<cdot>(unit_when\<cdot>r)\<cdot>x = return\<cdot>r" |
428 lemma wild_pat [simp]: "branch wild_pat\<cdot>(unit_when\<cdot>r)\<cdot>x = return\<cdot>r" |
418 by (simp add: branch_def wild_pat_def) |
429 by (simp add: branch_def wild_pat_def) |
419 |
430 |
420 lemma as_pat [simp]: |
431 lemma as_pat [simp]: |
434 |
445 |
435 subsection {* Match functions for built-in types *} |
446 subsection {* Match functions for built-in types *} |
436 |
447 |
437 defaultsort pcpo |
448 defaultsort pcpo |
438 |
449 |
439 constdefs |
450 definition |
440 match_UU :: "'a \<rightarrow> unit maybe" |
451 match_UU :: "'a \<rightarrow> unit maybe" where |
441 "match_UU \<equiv> \<Lambda> x. fail" |
452 "match_UU = (\<Lambda> x. fail)" |
442 |
453 |
443 match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe" |
454 definition |
444 "match_cpair \<equiv> csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)" |
455 match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe" where |
445 |
456 "match_cpair = csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)" |
446 match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe" |
457 |
447 "match_spair \<equiv> ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)" |
458 definition |
448 |
459 match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe" where |
449 match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe" |
460 "match_spair = ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)" |
450 "match_sinl \<equiv> sscase\<cdot>return\<cdot>(\<Lambda> y. fail)" |
461 |
451 |
462 definition |
452 match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe" |
463 match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe" where |
453 "match_sinr \<equiv> sscase\<cdot>(\<Lambda> x. fail)\<cdot>return" |
464 "match_sinl = sscase\<cdot>return\<cdot>(\<Lambda> y. fail)" |
454 |
465 |
455 match_up :: "'a::cpo u \<rightarrow> 'a maybe" |
466 definition |
456 "match_up \<equiv> fup\<cdot>return" |
467 match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe" where |
457 |
468 "match_sinr = sscase\<cdot>(\<Lambda> x. fail)\<cdot>return" |
458 match_ONE :: "one \<rightarrow> unit maybe" |
469 |
459 "match_ONE \<equiv> \<Lambda> ONE. return\<cdot>()" |
470 definition |
|
471 match_up :: "'a::cpo u \<rightarrow> 'a maybe" where |
|
472 "match_up = fup\<cdot>return" |
|
473 |
|
474 definition |
|
475 match_ONE :: "one \<rightarrow> unit maybe" where |
|
476 "match_ONE = (\<Lambda> ONE. return\<cdot>())" |
460 |
477 |
461 match_TT :: "tr \<rightarrow> unit maybe" |
478 definition |
462 "match_TT \<equiv> \<Lambda> b. If b then return\<cdot>() else fail fi" |
479 match_TT :: "tr \<rightarrow> unit maybe" where |
|
480 "match_TT = (\<Lambda> b. If b then return\<cdot>() else fail fi)" |
463 |
481 |
464 match_FF :: "tr \<rightarrow> unit maybe" |
482 definition |
465 "match_FF \<equiv> \<Lambda> b. If b then fail else return\<cdot>() fi" |
483 match_FF :: "tr \<rightarrow> unit maybe" where |
|
484 "match_FF = (\<Lambda> b. If b then fail else return\<cdot>() fi)" |
466 |
485 |
467 lemma match_UU_simps [simp]: |
486 lemma match_UU_simps [simp]: |
468 "match_UU\<cdot>x = fail" |
487 "match_UU\<cdot>x = fail" |
469 by (simp add: match_UU_def) |
488 by (simp add: match_UU_def) |
470 |
489 |