457 text {* |
451 text {* |
458 Partiality usually enters the game by partial patterns, as |
452 Partiality usually enters the game by partial patterns, as |
459 in the following example, again for amortised queues: |
453 in the following example, again for amortised queues: |
460 *} |
454 *} |
461 |
455 |
462 fun %quote strict_dequeue :: "'a queue \<Rightarrow> 'a \<times> 'a queue" where |
456 fun %quote strict_dequeue :: "'a Introduction.queue \<Rightarrow> 'a \<times> 'a Introduction.queue" where |
463 "strict_dequeue (Queue xs (y # ys)) = (y, Queue xs ys)" |
457 "strict_dequeue (Introduction.Queue xs (y # ys)) = (y, Introduction.Queue xs ys)" |
464 | "strict_dequeue (Queue xs []) = |
458 | "strict_dequeue (Introduction.Queue xs []) = |
465 (case rev xs of y # ys \<Rightarrow> (y, Queue [] ys))" |
459 (case rev xs of y # ys \<Rightarrow> (y, Introduction.Queue [] ys))" |
466 |
460 |
467 text {* |
461 text {* |
468 \noindent In the corresponding code, there is no equation |
462 \noindent In the corresponding code, there is no equation |
469 for the pattern @{term "Queue [] []"}: |
463 for the pattern @{term "Introduction.Queue [] []"}: |
470 *} |
464 *} |
471 |
465 |
472 text %quote {*@{code_stmts strict_dequeue (consts) strict_dequeue (Haskell)}*} |
466 text %quote {*@{code_stmts strict_dequeue (consts) strict_dequeue (Haskell)}*} |
473 |
467 |
474 text {* |
468 text {* |
476 pseudo-\qt{partiality} more explicitly, e.g.~as follows: |
470 pseudo-\qt{partiality} more explicitly, e.g.~as follows: |
477 *} |
471 *} |
478 |
472 |
479 axiomatization %quote empty_queue :: 'a |
473 axiomatization %quote empty_queue :: 'a |
480 |
474 |
481 function %quote strict_dequeue' :: "'a queue \<Rightarrow> 'a \<times> 'a queue" where |
475 function %quote strict_dequeue' :: "'a Introduction.queue \<Rightarrow> 'a \<times> 'a Introduction.queue" where |
482 "strict_dequeue' (Queue xs []) = (if xs = [] then empty_queue |
476 "strict_dequeue' (Introduction.Queue xs []) = (if xs = [] then empty_queue |
483 else strict_dequeue' (Queue [] (rev xs)))" |
477 else strict_dequeue' (Introduction.Queue [] (rev xs)))" |
484 | "strict_dequeue' (Queue xs (y # ys)) = |
478 | "strict_dequeue' (Introduction.Queue xs (y # ys)) = |
485 (y, Queue xs ys)" |
479 (y, Introduction.Queue xs ys)" |
486 by pat_completeness auto |
480 by pat_completeness auto |
487 |
481 |
488 termination %quote strict_dequeue' |
482 termination %quote strict_dequeue' |
489 by (relation "measure (\<lambda>q::'a queue. case q of Queue xs ys \<Rightarrow> length xs)") simp_all |
483 by (relation "measure (\<lambda>q::'a Introduction.queue. case q of Introduction.Queue xs ys \<Rightarrow> length xs)") simp_all |
490 |
484 |
491 text {* |
485 text {* |
492 \noindent For technical reasons the definition of |
486 \noindent For technical reasons the definition of |
493 @{const strict_dequeue'} is more involved since it requires |
487 @{const strict_dequeue'} is more involved since it requires |
494 a manual termination proof. Apart from that observe that |
488 a manual termination proof. Apart from that observe that |