Printf.agda 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. module Printf 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 : String -> List Format
  24. format s = format' (toList s)
  25. where
  26. format' : List Char -> List Format
  27. format' ('%' :: 's' :: fmt) = stringArg :: format' fmt
  28. format' ('%' :: 'n' :: fmt) = natArg :: format' fmt
  29. format' ('%' :: 'd' :: fmt) = intArg :: format' fmt
  30. format' ('%' :: 'f' :: fmt) = floatArg :: format' fmt
  31. format' ('%' :: 'c' :: fmt) = charArg :: format' fmt
  32. format' ('%' :: '%' :: fmt) = litChar '%' :: format' fmt
  33. format' ('%' :: c :: fmt) = badFormat c :: format' fmt
  34. format' (c :: fmt) = litChar c :: format' fmt
  35. format' [] = []
  36. Printf' : List Format -> Set
  37. Printf' (stringArg :: fmt) = String × Printf' fmt
  38. Printf' (natArg :: fmt) = Nat × Printf' fmt
  39. Printf' (intArg :: fmt) = Int × Printf' fmt
  40. Printf' (floatArg :: fmt) = Float × Printf' fmt
  41. Printf' (charArg :: fmt) = Char × Printf' fmt
  42. Printf' (badFormat c :: fmt) = BadFormat c
  43. Printf' (litChar _ :: fmt) = Printf' fmt
  44. Printf' [] = Unit
  45. Printf : String -> Set
  46. Printf fmt = Printf' (format fmt)
  47. {-
  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 = ""
  57. -}
  58. printf : (fmt : String) -> Printf fmt -> String
  59. printf fmt = printf' (format fmt)
  60. where
  61. printf' : (fmt : List Format) -> Printf' fmt -> String
  62. printf' (stringArg :: fmt) < s , args > = s ++ printf' fmt args
  63. printf' (natArg :: fmt) < n , args > = showNat n ++ printf' fmt args
  64. printf' (intArg :: fmt) < n , args > = showInt n ++ printf' fmt args
  65. printf' (floatArg :: fmt) < x , args > = showFloat x ++ printf' fmt args
  66. printf' (charArg :: fmt) < c , args > = showChar c ++ printf' fmt args
  67. printf' (litChar c :: fmt) args = fromList (c :: []) ++ printf' fmt args
  68. printf' (badFormat _ :: fmt) ()
  69. printf' [] unit = ""
  70. mainS : String
  71. -- mainS = printf "pi = %f" < 3.14 , unit >
  72. mainS = printf "Answer is %n, pi = %f %% %s"
  73. < 42 , < 3.14159 , < "Alonzo" , unit > > >