FlexibleInterpreter.agda 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. -- Andreas and James, Nov 2011 and Oct 2012
  2. -- function with flexible arity
  3. -- {-# OPTIONS -v tc.cover:20 #-}
  4. module FlexibleInterpreter where
  5. open import Common.Equality
  6. open import Common.IO
  7. open import Common.Nat renaming (zero to Z; suc to S) hiding (pred)
  8. data Ty : Set where
  9. nat : Ty
  10. arr : Ty -> Ty -> Ty
  11. data Exp : Ty -> Set where
  12. zero : Exp nat
  13. suc : Exp (arr nat nat)
  14. pred : Exp (arr nat nat)
  15. app : {a b : Ty} -> Exp (arr a b) -> Exp a -> Exp b
  16. Sem : Ty -> Set
  17. Sem nat = Nat
  18. Sem (arr a b) = Sem a -> Sem b
  19. module Flex where
  20. -- Haskell does not seem to handle this
  21. eval' : (a : Ty) -> Exp a -> Sem a
  22. eval' ._ zero = Z
  23. eval' ._ suc = S
  24. eval' b (app f e) = eval' _ f (eval' _ e)
  25. eval' .(arr nat nat) pred Z = Z
  26. eval' ._ pred (S n) = n
  27. eval : {a : Ty} -> Exp a -> Sem a
  28. eval zero = Z
  29. eval suc = S
  30. eval pred Z = Z
  31. eval pred (S n) = n
  32. eval (app f e) = eval f (eval e)
  33. testEvalSuc : ∀ {n} → eval suc n ≡ S n
  34. testEvalSuc = refl
  35. testEvalPredZero : eval pred Z ≡ Z
  36. testEvalPredZero = refl
  37. testEvalPredSuc : ∀ {n} → eval pred (S n) ≡ n
  38. testEvalPredSuc = refl
  39. module Rigid where
  40. -- This executes fine
  41. eval : {a : Ty} -> Exp a -> Sem a
  42. eval zero = Z
  43. eval suc = S
  44. eval pred = λ { Z -> Z
  45. ; (S n) -> n
  46. }
  47. eval (app f e) = eval f (eval e)
  48. open Flex
  49. expr = (app pred (app suc (app suc zero)))
  50. test = eval expr
  51. main = printNat test
  52. -- should print 1