## root / src / Ganeti / PyValueInstances.hs @ 85bcb1de

History | View | Annotate | Download (2.4 kB)

1 |
{-| PyValueInstances contains instances for the 'PyValue' typeclass. |
---|---|

2 | |

3 |
The typeclass 'PyValue' converts Haskell values to Python values. |

4 |
This module contains instances of this typeclass for several generic |

5 |
types. These instances are used in the Haskell to Python generation |

6 |
of opcodes and constants, for example. |

7 | |

8 |
-} |

9 | |

10 |
{- |

11 | |

12 |
Copyright (C) 2013 Google Inc. |

13 | |

14 |
This program is free software; you can redistribute it and/or modify |

15 |
it under the terms of the GNU General Public License as published by |

16 |
the Free Software Foundation; either version 2 of the License, or |

17 |
(at your option) any later version. |

18 | |

19 |
This program is distributed in the hope that it will be useful, but |

20 |
WITHOUT ANY WARRANTY; without even the implied warranty of |

21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |

22 |
General Public License for more details. |

23 | |

24 |
You should have received a copy of the GNU General Public License |

25 |
along with this program; if not, write to the Free Software |

26 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |

27 |
02110-1301, USA. |

28 | |

29 |
-} |

30 |
{-# LANGUAGE FlexibleInstances, OverlappingInstances, |

31 |
TypeSynonymInstances, IncoherentInstances #-} |

32 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |

33 |
module Ganeti.PyValueInstances where |

34 | |

35 |
import Data.List (intercalate) |

36 |
import Data.Map (Map) |

37 |
import qualified Data.Map as Map |

38 |
import qualified Data.Set as Set (toList) |

39 | |

40 |
import Ganeti.BasicTypes |

41 |
import Ganeti.THH |

42 | |

43 |
instance PyValue Bool where |

44 |
showValue = show |

45 | |

46 |
instance PyValue Int where |

47 |
showValue = show |

48 | |

49 |
instance PyValue Integer where |

50 |
showValue = show |

51 | |

52 |
instance PyValue Double where |

53 |
showValue = show |

54 | |

55 |
instance PyValue Char where |

56 |
showValue = show |

57 | |

58 |
instance (PyValue a, PyValue b) => PyValue (a, b) where |

59 |
showValue (x, y) = "(" ++ showValue x ++ "," ++ showValue y ++ ")" |

60 | |

61 |
instance (PyValue a, PyValue b, PyValue c) => PyValue (a, b, c) where |

62 |
showValue (x, y, z) = |

63 |
"(" ++ |

64 |
showValue x ++ "," ++ |

65 |
showValue y ++ "," ++ |

66 |
showValue z ++ |

67 |
")" |

68 | |

69 |
instance PyValue String where |

70 |
showValue = show |

71 | |

72 |
instance PyValue a => PyValue [a] where |

73 |
showValue xs = "[" ++ intercalate "," (map showValue xs) ++ "]" |

74 | |

75 |
instance (PyValue k, PyValue a) => PyValue (Map k a) where |

76 |
showValue mp = |

77 |
"{" ++ intercalate ", " (map showPair (Map.assocs mp)) ++ "}" |

78 |
where showPair (k, x) = showValue k ++ ":" ++ showValue x |

79 | |

80 |
instance PyValue a => PyValue (ListSet a) where |

81 |
showValue = showValue . Set.toList . unListSet |