CatchAllVarArity.agda 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. -- Mixing CatchAll branches with varying arity can be tricky.
  2. --
  3. -- If the number of arguments a catch all branch expects to be already abstracted over is smaller
  4. -- than the number of arguments present in the failing clause/branch, we need to apply
  5. -- the catch-all branch to the surplus arguments already abstracted over.
  6. module CatchAllVarArity where
  7. open import Common.Nat
  8. open import Common.IO
  9. open import Common.Unit
  10. f : Nat → Nat → Nat
  11. f zero = λ y → y
  12. f (suc zero) (suc y) = suc y
  13. f x y = (suc y)
  14. case_of_ : ∀{A B : Set} → A → (A → B) → B
  15. case a of f = f a
  16. -- case tree variant of f
  17. g : Nat → Nat → Nat
  18. g = λ x →
  19. case x of
  20. λ{ zero → λ y → y
  21. ; (suc x') →
  22. case x' of
  23. λ{ zero → λ y →
  24. case y of
  25. λ{ (suc y') → suc y'
  26. ; _ → fallback (suc x') y
  27. }
  28. ; _ → λ y → fallback x y
  29. }
  30. }
  31. where
  32. fallback = λ x y → suc y
  33. --expected:
  34. -- 10
  35. -- 21
  36. -- 1
  37. -- 0
  38. -- 4
  39. -- 1
  40. main = printNat (f 0 10) ,,
  41. putStrLn "" ,,
  42. printNat (f 10 20) ,,
  43. putStrLn "" ,,
  44. printNat (f 10 0) ,,
  45. putStrLn "" ,,
  46. printNat (f 0 0) ,,
  47. putStrLn "" ,,
  48. printNat (f 1 4) ,,
  49. putStrLn "" ,,
  50. printNat (f 1 0) ,,
  51. putStrLn ""