BadPrintf2.agda 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. module Printf2 where
  2. import AlonzoPrelude
  3. import PreludeList
  4. import PreludeShow
  5. import PreludeString
  6. import PreludeNat
  7. open AlonzoPrelude
  8. open PreludeList, hiding (_++_)
  9. open PreludeShow
  10. open PreludeString
  11. open PreludeNat
  12. data Unit : Set where
  13. unit : Unit
  14. data Format : Set where
  15. stringArg : Format
  16. natArg : Format
  17. intArg : Format
  18. floatArg : Format
  19. charArg : Format
  20. litChar : Char -> Format
  21. badFormat : Char -> Format
  22. data BadFormat (c:Char) : Set where
  23. format' : List Char -> List Format
  24. format' ('%' :: 's' :: fmt) = stringArg :: format' fmt
  25. format' ('%' :: 'n' :: fmt) = natArg :: format' fmt
  26. -- format' ('%' :: 'd' :: fmt) = intArg :: format' fmt
  27. format' ('%' :: 'f' :: fmt) = floatArg :: format' fmt
  28. format' ('%' :: 'c' :: fmt) = charArg :: format' fmt
  29. format' ('%' :: '%' :: fmt) = litChar '%' :: format' fmt
  30. format' ('%' :: c :: fmt) = badFormat c :: format' fmt
  31. format' (c :: fmt) = litChar c :: format' fmt
  32. format' [] = []
  33. format : String -> List Format
  34. format s = format' (toList s)
  35. -- format : String -> List Format
  36. -- format = format' ∘ toList
  37. Printf' : List Format -> Set
  38. Printf' (stringArg :: fmt) = String × Printf' fmt
  39. Printf' (natArg :: fmt) = Nat × Printf' fmt
  40. Printf' (intArg :: fmt) = Int × Printf' fmt
  41. Printf' (floatArg :: fmt) = Float × Printf' fmt
  42. Printf' (charArg :: fmt) = Char × Printf' fmt
  43. Printf' (badFormat c :: fmt) = BadFormat c
  44. Printf' (litChar _ :: fmt) = Printf' fmt
  45. Printf' [] = Unit × Unit
  46. Printf : String -> Set
  47. Printf fmt = Printf' (format fmt)
  48. printf' : (fmt : List Format) -> Printf' fmt -> String
  49. printf' (stringArg :: fmt) < s | args > = s ++ printf' fmt args
  50. printf' (natArg :: fmt) < n | args > = showNat n ++ printf' fmt args
  51. printf' (intArg :: fmt) < n | args > = showInt n ++ printf' fmt args
  52. printf' (floatArg :: fmt) < x | args > = showFloat x ++ printf' fmt args
  53. printf' (charArg :: fmt) < c | args > = showChar c ++ printf' fmt args
  54. printf' (litChar c :: fmt) args = fromList (c :: []) ++ printf' fmt args
  55. printf' (badFormat _ :: fmt) ()
  56. printf' [] < unit | unit > = ""
  57. -- printf' nil unit = ""
  58. printf : (fmt : String) -> Printf fmt -> String
  59. printf fmt = printf' (format fmt)
  60. testFormat : List Format
  61. testFormat = format ""
  62. testArgs : Printf' testFormat
  63. testArgs = < unit | unit >
  64. mainS : String
  65. mainS = printf "%s" < 42 | unit >