PrintFloat.agda 2.5 KB

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