convertible-1.1.1.0/0000755000000000000000000000000012515141523012351 5ustar0000000000000000convertible-1.1.1.0/convertible.cabal0000644000000000000000000000602212515141523015651 0ustar0000000000000000Name: convertible Version: 1.1.1.0 License: BSD3 Maintainer: Erik Hesselink Author: John Goerzen Copyright: Copyright (c) 2009-2011 John Goerzen license-file: LICENSE extra-source-files: LICENSE, utils/genCinstances.hs, Makefile, testsrc/TestInfrastructure.hs, testsrc/TestMap.hs, testsrc/TestNum.hs, testsrc/TestTime.hs homepage: http://hackage.haskell.org/package/convertible bug-reports: https://github.com/hdbc/convertible/issues Category: Data synopsis: Typeclasses and instances for converting between types Description: convertible provides a typeclass with a single function that is designed to help convert between different types: numeric values, dates and times, and the like. The conversions perform bounds checking and return a pure Either value. This means that you need not remember which specific function performs the conversion you desire. . Also included in the package are optional instances that provide conversion for various numeric and time types, as well as utilities for writing your own instances. . Finally, there is a function that will raise an exception on bounds-checking violation, or return a bare value otherwise, implemented in terms of the safer function described above. . Convertible is also used by HDBC 2.0 for handling marshalling of data to and from databases. . Convertible is backed by an extensive test suite and passes tests on GHC and Hugs. Stability: Stable Build-Type: Simple Cabal-Version: >=1.6 flag buildtests description: Build the executable to run unit tests default: False source-repository head type: git location: git://github.com/hdbc/convertible.git library Build-Depends: base>=3 && <5, old-time, time>=1.1.3, bytestring >= 0.10.2, containers, old-locale, mtl, text >= 0.8 GHC-Options: -O2 -Wall -fno-warn-orphans Exposed-Modules: Data.Convertible, Data.Convertible.Base, Data.Convertible.Utils, Data.Convertible.Instances, Data.Convertible.Instances.C, Data.Convertible.Instances.Map, Data.Convertible.Instances.Num, Data.Convertible.Instances.Text, Data.Convertible.Instances.Time Extensions: ExistentialQuantification, MultiParamTypeClasses, UndecidableInstances, FlexibleInstances, FlexibleContexts, TypeSynonymInstances, CPP Executable runtests if flag(buildtests) Buildable: True Build-Depends: HUnit, QuickCheck >= 2.0, testpack >= 2.0, time>=1.1.3 else Buildable: False Main-Is: runtests.hs Hs-Source-Dirs: ., testsrc GHC-Options: -O2 Extensions: ExistentialQuantification, MultiParamTypeClasses, UndecidableInstances, FlexibleInstances, FlexibleContexts, TypeSynonymInstances, CPP convertible-1.1.1.0/LICENSE0000644000000000000000000000272012515141523013357 0ustar0000000000000000Copyright (c) 2009 - 2011 John Goerzen All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of John Goerzen nor the names of the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. convertible-1.1.1.0/Makefile0000644000000000000000000000112612515141523014011 0ustar0000000000000000all: setup @echo "Please use Cabal to build this package; not make." ./setup configure ./setup build setup: Setup.lhs ghc --make -o setup Setup.lhs install: setup ./setup install clean: -./setup clean -runghc Setup.lhs clean .PHONY: test test: test-ghc test-hugs @echo "" @echo "All tests pass." test-hugs: @echo " ****** Running hugs tests" runhugs -98 +o -P$(PWD):$(PWD)/testsrc: testsrc/runtests.hs test-ghc: @echo " ****** Building GHC tests" runghc Setup.lhs configure -f buildtests runghc Setup.lhs build @echo " ****** Running GHC tests" ./dist/build/runtests/runtests convertible-1.1.1.0/Setup.lhs0000644000000000000000000000011712515141523014160 0ustar0000000000000000#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain convertible-1.1.1.0/Data/0000755000000000000000000000000012515141523013222 5ustar0000000000000000convertible-1.1.1.0/Data/Convertible.hs0000644000000000000000000000217012515141523016032 0ustar0000000000000000{- Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} {- | Module : Data.Convertible.Base Copyright : Copyright (C) 2009-2011 John Goerzen License : BSD3 Maintainer : John Goerzen Stability : provisional Portability: portable This is a library to provide a uniform interface for safe conversions between different types of data. To get started reading about it, consult: "Data.Convertible.Base" for information about the conversions themselves "Data.Convertible.Utils" for helpful tools for people writing 'Convertible' instances "Data.Convertible.Instances" for a large collection of ready-built 'Convertible' instances You can import these modules individually, or this module will export the entire library for you. -} module Data.Convertible ( module Data.Convertible.Base, module Data.Convertible.Utils ) where import Data.Convertible.Base import Data.Convertible.Utils import Data.Convertible.Instances () convertible-1.1.1.0/Data/Convertible/0000755000000000000000000000000012515141523015476 5ustar0000000000000000convertible-1.1.1.0/Data/Convertible/Base.hs0000644000000000000000000000711412515141523016707 0ustar0000000000000000{- Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} {- | Module : Data.Convertible.Base Copyright : Copyright (C) 2009-2011 John Goerzen License : BSD3 Maintainer : John Goerzen Stability : provisional Portability: portable -} module Data.Convertible.Base( -- * The conversion process convert, Convertible(..), -- * Handling the results ConvertResult, ConvertError(..), convError, prettyConvertError ) where import Control.Monad.Error import Data.Typeable {- | The result of a safe conversion via 'safeConvert'. -} type ConvertResult a = Either ConvertError a ---------------------------------------------------------------------- -- Conversions ---------------------------------------------------------------------- {- | A typeclass that represents something that can be converted. A @Convertible a b@ instance represents an @a@ that can be converted to a @b@. -} class Convertible a b where {- | Convert @a@ to @b@, returning Right on success and Left on error. For a simpler interface, see 'convert'. -} safeConvert :: a -> ConvertResult b {- {- | Any type can be converted to itself. -} instance Convertible a a where safeConvert x = return x -} {- {- | Lists of any convertible type can be converted. -} instance Convertible a b => Convertible [a] [b] where safeConvert = mapM safeConvert -} {- | Convert from one type of data to another. Raises an exception if there is an error with the conversion. For a function that does not raise an exception in that case, see 'safeConvert'. -} convert :: Convertible a b => a -> b convert x = case safeConvert x of Left e -> error (prettyConvertError e) Right r -> r {- instance Convertible Int Double where safeConvert = return . fromIntegral instance Convertible Double Int where safeConvert = return . truncate -- could do bounds checking here instance Convertible Integer Double where safeConvert = return . fromIntegral instance Convertible Double Integer where safeConvert = return . truncate -} ---------------------------------------------------------------------- -- Error Handling ---------------------------------------------------------------------- {- | How we indicate that there was an error. -} data ConvertError = ConvertError { convSourceValue :: String, convSourceType :: String, convDestType :: String, convErrorMessage :: String} deriving (Eq, Read, Show) instance Error ConvertError where strMsg x = ConvertError "(unknown)" "(unknown)" "(unknown)" x convError' :: (Show a, Typeable a, Typeable b) => String -> a -> b -> ConvertResult b convError' msg inpval retval = Left $ ConvertError { convSourceValue = show inpval, convSourceType = show . typeOf $ inpval, convDestType = show . typeOf $ retval, convErrorMessage = msg} convError :: (Show a, Typeable a, Typeable b) => String -> a -> ConvertResult b convError msg inpval = convError' msg inpval undefined prettyConvertError :: ConvertError -> String prettyConvertError (ConvertError sv st dt em) = "Convertible: error converting source data " ++ sv ++ " of type " ++ st ++ " to type " ++ dt ++ ": " ++ em convertible-1.1.1.0/Data/Convertible/Instances.hs0000644000000000000000000000173412515141523017766 0ustar0000000000000000{- Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} {- | Module : Data.Convertible.Instances Copyright : Copyright (C) 2009-2011 John Goerzen License : BSD3 Maintainer : John Goerzen Stability : provisional Portability: portable Collection of ready-made 'Data.Convertible.Convertible' instances. See each individual module for more docs: "Data.Convertible.Instances.C" "Data.Convertible.Instances.Map" "Data.Convertible.Instances.Num" "Data.Convertible.Instances.Time" You can find a list of these instances at 'Data.Convertible.Base.Convertible'. -} module Data.Convertible.Instances( ) where import Data.Convertible.Instances.C() import Data.Convertible.Instances.Map() import Data.Convertible.Instances.Num() import Data.Convertible.Instances.Text() import Data.Convertible.Instances.Time() convertible-1.1.1.0/Data/Convertible/Utils.hs0000644000000000000000000000500612515141523017133 0ustar0000000000000000{- Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} {- | Module : Data.Convertible.Utils Copyright : Copyright (C) 2009-2011 John Goerzen License : BSD3 Maintainer : John Goerzen Stability : provisional Portability: portable -} module Data.Convertible.Utils(boundedConversion, convertVia ) where import Data.Convertible.Base import Data.Typeable {- | Utility function to perform bounds checking as part of a conversion. Does this be examining the bounds of the destination type, converting to the type of the source via 'safeConvert', comparing to the source value. Results in an error if the conversion is out of bounds. -} boundedConversion :: (Ord a, Bounded b, Show a, Show b, Convertible a Integer, Convertible b Integer, Typeable a, Typeable b) => (a -> ConvertResult b) -- ^ Function to do the conversion -> a -- ^ Input data -> ConvertResult b -- ^ Result boundedConversion func inp = do result <- func inp let smallest = asTypeOf minBound result let biggest = asTypeOf maxBound result let smallest' = (convert smallest)::Integer let biggest' = (convert biggest)::Integer let inp' = (convert inp)::Integer if inp' < smallest' || inp' > biggest' then convError ("Input value outside of bounds: " ++ show (smallest, biggest)) inp else return result {- | Useful for defining conversions that are implemented in terms of other conversions via an intermediary type. Instead of: >instance Convertible CalendarTime POSIXTime where > safeConvert a = do r <- safeConvert a > safeConvert (r :: ClockTime) we can now write: >instance Convertible CalendarTime POSIXTime where > safeConvert = convertVia (undefined::ClockTime) which does the same thing -- converts a CalendarTime to a ClockTime, then a ClockTime to a POSIXTime, both using existing 'Convertible' instances. -} convertVia :: (Convertible a b, Convertible b c) => b -- ^ Dummy data to establish intermediate type - can be undefined -> a -- ^ Input value -> ConvertResult c -- ^ Result convertVia dummy inp = do r1 <- safeConvert inp safeConvert (asTypeOf r1 dummy) convertible-1.1.1.0/Data/Convertible/Instances/0000755000000000000000000000000012515141523017425 5ustar0000000000000000convertible-1.1.1.0/Data/Convertible/Instances/C.hs0000644000000000000000000014735112515141523020156 0ustar0000000000000000{- | Module : Data.Convertible.Instances.C Copyright : Copyright (C) 2009-2011 John Goerzen License : BSD3 Maintainer : John Goerzen Stability : provisional Portability: portable Numeric instances for Convertible for C types. See comments in "Data.Convertible.Instances.Num". Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} module Data.Convertible.Instances.C() where import Data.Convertible.Base import Data.Convertible.Utils import Data.Convertible.Instances.Num() import Data.Int import Data.Word import Foreign.C.Types -- remainder of this file generated by utils/genCinstances.hs -- Section 1 instance Convertible CFloat Int where safeConvert = boundedConversion (return . truncate) instance Convertible Int CFloat where safeConvert = return . fromIntegral instance Convertible CFloat Int8 where safeConvert = boundedConversion (return . truncate) instance Convertible Int8 CFloat where safeConvert = return . fromIntegral instance Convertible CFloat Int16 where safeConvert = boundedConversion (return . truncate) instance Convertible Int16 CFloat where safeConvert = return . fromIntegral instance Convertible CFloat Int32 where safeConvert = boundedConversion (return . truncate) instance Convertible Int32 CFloat where safeConvert = return . fromIntegral instance Convertible CFloat Int64 where safeConvert = boundedConversion (return . truncate) instance Convertible Int64 CFloat where safeConvert = return . fromIntegral instance Convertible CFloat Word where safeConvert = boundedConversion (return . truncate) instance Convertible Word CFloat where safeConvert = return . fromIntegral instance Convertible CFloat Word8 where safeConvert = boundedConversion (return . truncate) instance Convertible Word8 CFloat where safeConvert = return . fromIntegral instance Convertible CFloat Word16 where safeConvert = boundedConversion (return . truncate) instance Convertible Word16 CFloat where safeConvert = return . fromIntegral instance Convertible CFloat Word32 where safeConvert = boundedConversion (return . truncate) instance Convertible Word32 CFloat where safeConvert = return . fromIntegral instance Convertible CFloat Word64 where safeConvert = boundedConversion (return . truncate) instance Convertible Word64 CFloat where safeConvert = return . fromIntegral instance Convertible CDouble Int where safeConvert = boundedConversion (return . truncate) instance Convertible Int CDouble where safeConvert = return . fromIntegral instance Convertible CDouble Int8 where safeConvert = boundedConversion (return . truncate) instance Convertible Int8 CDouble where safeConvert = return . fromIntegral instance Convertible CDouble Int16 where safeConvert = boundedConversion (return . truncate) instance Convertible Int16 CDouble where safeConvert = return . fromIntegral instance Convertible CDouble Int32 where safeConvert = boundedConversion (return . truncate) instance Convertible Int32 CDouble where safeConvert = return . fromIntegral instance Convertible CDouble Int64 where safeConvert = boundedConversion (return . truncate) instance Convertible Int64 CDouble where safeConvert = return . fromIntegral instance Convertible CDouble Word where safeConvert = boundedConversion (return . truncate) instance Convertible Word CDouble where safeConvert = return . fromIntegral instance Convertible CDouble Word8 where safeConvert = boundedConversion (return . truncate) instance Convertible Word8 CDouble where safeConvert = return . fromIntegral instance Convertible CDouble Word16 where safeConvert = boundedConversion (return . truncate) instance Convertible Word16 CDouble where safeConvert = return . fromIntegral instance Convertible CDouble Word32 where safeConvert = boundedConversion (return . truncate) instance Convertible Word32 CDouble where safeConvert = return . fromIntegral instance Convertible CDouble Word64 where safeConvert = boundedConversion (return . truncate) instance Convertible Word64 CDouble where safeConvert = return . fromIntegral -- Section 2 instance Convertible CFloat Double where safeConvert = return . realToFrac instance Convertible Double CFloat where safeConvert = return . realToFrac instance Convertible CFloat Float where safeConvert = return . realToFrac instance Convertible Float CFloat where safeConvert = return . realToFrac instance Convertible CFloat Rational where safeConvert = return . realToFrac instance Convertible Rational CFloat where safeConvert = return . realToFrac instance Convertible CDouble Double where safeConvert = return . realToFrac instance Convertible Double CDouble where safeConvert = return . realToFrac instance Convertible CDouble Float where safeConvert = return . realToFrac instance Convertible Float CDouble where safeConvert = return . realToFrac instance Convertible CDouble Rational where safeConvert = return . realToFrac instance Convertible Rational CDouble where safeConvert = return . realToFrac -- Section 3 instance Convertible CChar Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 CULLong where safeConvert = boundedConversion (return . fromIntegral) -- Section 4 instance Convertible CChar CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CChar CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong CULLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong CLLong where safeConvert = boundedConversion (return . fromIntegral) -- Section 5 instance Convertible CFloat CDouble where safeConvert = return . realToFrac instance Convertible CDouble CFloat where safeConvert = return . realToFrac -- Section 6 instance Convertible CFloat Integer where safeConvert = return . truncate instance Convertible Integer CFloat where safeConvert = return . fromIntegral instance Convertible CDouble Integer where safeConvert = return . truncate instance Convertible Integer CDouble where safeConvert = return . fromIntegral -- Section 7 instance Convertible CChar Integer where safeConvert = return . fromIntegral instance Convertible Integer CChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSChar Integer where safeConvert = return . fromIntegral instance Convertible Integer CSChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUChar Integer where safeConvert = return . fromIntegral instance Convertible Integer CUChar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CShort Integer where safeConvert = return . fromIntegral instance Convertible Integer CShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUShort Integer where safeConvert = return . fromIntegral instance Convertible Integer CUShort where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CInt Integer where safeConvert = return . fromIntegral instance Convertible Integer CInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CUInt Integer where safeConvert = return . fromIntegral instance Convertible Integer CUInt where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLong Integer where safeConvert = return . fromIntegral instance Convertible Integer CLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULong Integer where safeConvert = return . fromIntegral instance Convertible Integer CULong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CSize Integer where safeConvert = return . fromIntegral instance Convertible Integer CSize where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CWchar Integer where safeConvert = return . fromIntegral instance Convertible Integer CWchar where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CLLong Integer where safeConvert = return . fromIntegral instance Convertible Integer CLLong where safeConvert = boundedConversion (return . fromIntegral) instance Convertible CULLong Integer where safeConvert = return . fromIntegral instance Convertible Integer CULLong where safeConvert = boundedConversion (return . fromIntegral) -- Section 8o instance Convertible CChar Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CChar where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CSChar Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CSChar where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CUChar Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CUChar where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CShort Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CShort where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CUShort Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CUShort where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CInt Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CInt where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CUInt Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CUInt where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CLong Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CLong where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CULong Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CULong where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CSize Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CSize where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CWchar Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CWchar where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CLLong Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CLLong where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible CULLong Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char CULLong where safeConvert = boundedConversion (return . fromIntegral . fromEnum) convertible-1.1.1.0/Data/Convertible/Instances/Map.hs0000644000000000000000000000134112515141523020475 0ustar0000000000000000{- | Module : Data.Convertible.Instances.Map Copyright : Copyright (C) 2009-2011 John Goerzen License : BSD3 Maintainer : John Goerzen Stability : provisional Portability: portable Instances to convert between Map and association list. Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} module Data.Convertible.Instances.Map() where import Data.Convertible.Base import qualified Data.Map as Map instance Ord k => Convertible [(k, a)] (Map.Map k a) where safeConvert = return . Map.fromList instance Convertible (Map.Map k a) [(k, a)] where safeConvert = return . Map.toList convertible-1.1.1.0/Data/Convertible/Instances/Num.hs0000644000000000000000000005540012515141523020524 0ustar0000000000000000{- | Module : Data.Convertible.Instances.Num Copyright : Copyright (C) 2009-2011 John Goerzen License : BSD3 Maintainer : John Goerzen Stability : provisional Portability: portable Numeric instances for Convertible. Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE These instances perform conversion between numeric types such as Double, Int, Integer, Rational, and the like. Here are some notes about the conversion process: Conversions from floating-point types such as Double to integral types are done via the 'truncate' function. This is a somewhat arbitrary decision; if you need different behavior, you will have to write your own instance or manually perform the conversion. All conversions perform bounds checking. If a value is too large for its destination type, you will get a 'ConvertError' informing you of this. Note that this behavior differs from functions in the Haskell standard libraries, which will perform the conversion without error, but give you garbage in the end. Conversions do not perform precision checking; loss of precision is implied with certain conversions (for instance, Double to Float) and this is not an error. -} module Data.Convertible.Instances.Num() where import Data.Convertible.Base import Data.Convertible.Utils import Data.Int import Data.Word ------------------------------------------------------------ {- The following instances generated by this code: fp = ["Double", "Float", "Rational"] int = ["Int", "Int8", "Int16", "Int32", "Int64", "Word", "Word8", "Word16", "Word32", "Word64"] allItems l1 l2 = concatMap (\x -> map (\y -> (x, y)) int) fp work = allItems fp int printIt (f, i) = "instance Convertible " ++ f ++ " " ++ i ++ " where \n\ \ safeConvert = boundedConversion (return . truncate)\n\ \instance Convertible " ++ i ++ " " ++ f ++ " where \n\ \ safeConvert = return . fromIntegral\n" main = mapM_ (putStrLn . printIt) work -} instance Convertible Double Int where safeConvert = boundedConversion (return . truncate) instance Convertible Int Double where safeConvert = return . fromIntegral instance Convertible Double Int8 where safeConvert = boundedConversion (return . truncate) instance Convertible Int8 Double where safeConvert = return . fromIntegral instance Convertible Double Int16 where safeConvert = boundedConversion (return . truncate) instance Convertible Int16 Double where safeConvert = return . fromIntegral instance Convertible Double Int32 where safeConvert = boundedConversion (return . truncate) instance Convertible Int32 Double where safeConvert = return . fromIntegral instance Convertible Double Int64 where safeConvert = boundedConversion (return . truncate) instance Convertible Int64 Double where safeConvert = return . fromIntegral instance Convertible Double Word where safeConvert = boundedConversion (return . truncate) instance Convertible Word Double where safeConvert = return . fromIntegral instance Convertible Double Word8 where safeConvert = boundedConversion (return . truncate) instance Convertible Word8 Double where safeConvert = return . fromIntegral instance Convertible Double Word16 where safeConvert = boundedConversion (return . truncate) instance Convertible Word16 Double where safeConvert = return . fromIntegral instance Convertible Double Word32 where safeConvert = boundedConversion (return . truncate) instance Convertible Word32 Double where safeConvert = return . fromIntegral instance Convertible Double Word64 where safeConvert = boundedConversion (return . truncate) instance Convertible Word64 Double where safeConvert = return . fromIntegral instance Convertible Float Int where safeConvert = boundedConversion (return . truncate) instance Convertible Int Float where safeConvert = return . fromIntegral instance Convertible Float Int8 where safeConvert = boundedConversion (return . truncate) instance Convertible Int8 Float where safeConvert = return . fromIntegral instance Convertible Float Int16 where safeConvert = boundedConversion (return . truncate) instance Convertible Int16 Float where safeConvert = return . fromIntegral instance Convertible Float Int32 where safeConvert = boundedConversion (return . truncate) instance Convertible Int32 Float where safeConvert = return . fromIntegral instance Convertible Float Int64 where safeConvert = boundedConversion (return . truncate) instance Convertible Int64 Float where safeConvert = return . fromIntegral instance Convertible Float Word where safeConvert = boundedConversion (return . truncate) instance Convertible Word Float where safeConvert = return . fromIntegral instance Convertible Float Word8 where safeConvert = boundedConversion (return . truncate) instance Convertible Word8 Float where safeConvert = return . fromIntegral instance Convertible Float Word16 where safeConvert = boundedConversion (return . truncate) instance Convertible Word16 Float where safeConvert = return . fromIntegral instance Convertible Float Word32 where safeConvert = boundedConversion (return . truncate) instance Convertible Word32 Float where safeConvert = return . fromIntegral instance Convertible Float Word64 where safeConvert = boundedConversion (return . truncate) instance Convertible Word64 Float where safeConvert = return . fromIntegral instance Convertible Rational Int where safeConvert = boundedConversion (return . truncate) instance Convertible Int Rational where safeConvert = return . fromIntegral instance Convertible Rational Int8 where safeConvert = boundedConversion (return . truncate) instance Convertible Int8 Rational where safeConvert = return . fromIntegral instance Convertible Rational Int16 where safeConvert = boundedConversion (return . truncate) instance Convertible Int16 Rational where safeConvert = return . fromIntegral instance Convertible Rational Int32 where safeConvert = boundedConversion (return . truncate) instance Convertible Int32 Rational where safeConvert = return . fromIntegral instance Convertible Rational Int64 where safeConvert = boundedConversion (return . truncate) instance Convertible Int64 Rational where safeConvert = return . fromIntegral instance Convertible Rational Word where safeConvert = boundedConversion (return . truncate) instance Convertible Word Rational where safeConvert = return . fromIntegral instance Convertible Rational Word8 where safeConvert = boundedConversion (return . truncate) instance Convertible Word8 Rational where safeConvert = return . fromIntegral instance Convertible Rational Word16 where safeConvert = boundedConversion (return . truncate) instance Convertible Word16 Rational where safeConvert = return . fromIntegral instance Convertible Rational Word32 where safeConvert = boundedConversion (return . truncate) instance Convertible Word32 Rational where safeConvert = return . fromIntegral instance Convertible Rational Word64 where safeConvert = boundedConversion (return . truncate) instance Convertible Word64 Rational where safeConvert = return . fromIntegral ------------------------------------------------------------ {- The following instances generated by this code: int = ["Int", "Int8", "Int16", "Int32", "Int64", "Word", "Word8", "Word16", "Word32", "Word64"] allItems l1 l2 = concatMap (\x -> map (\y -> (x, y)) l1) l2 work = filter (\(a, b) -> a /= b) (allItems int int) printIt (f, i) = "instance Convertible " ++ f ++ " " ++ i ++ " where \n\ \ safeConvert = boundedConversion (return . fromIntegral)\n" printInteger i = "instance Convertible Integer " ++ i ++ " where \n\ \ safeConvert = boundedConversion (return . fromIntegral)\n\ \instance Convertible " ++ i ++ " Integer where \n\ \ safeConvert = return . fromIntegral\n\n" main = do mapM_ (putStrLn . printIt) work mapM_ (putStrLn . printInteger) int -} instance Convertible Int Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Integer Int where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int Integer where safeConvert = return . fromIntegral instance Convertible Integer Int8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int8 Integer where safeConvert = return . fromIntegral instance Convertible Integer Int16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int16 Integer where safeConvert = return . fromIntegral instance Convertible Integer Int32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int32 Integer where safeConvert = return . fromIntegral instance Convertible Integer Int64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Int64 Integer where safeConvert = return . fromIntegral instance Convertible Integer Word where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word Integer where safeConvert = return . fromIntegral instance Convertible Integer Word8 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word8 Integer where safeConvert = return . fromIntegral instance Convertible Integer Word16 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word16 Integer where safeConvert = return . fromIntegral instance Convertible Integer Word32 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word32 Integer where safeConvert = return . fromIntegral instance Convertible Integer Word64 where safeConvert = boundedConversion (return . fromIntegral) instance Convertible Word64 Integer where safeConvert = return . fromIntegral ------------------------------------------------------------ instance Convertible Integer Double where safeConvert = return . fromIntegral instance Convertible Integer Float where safeConvert = return . fromIntegral instance Convertible Integer Rational where safeConvert = return . fromIntegral instance Convertible Double Integer where safeConvert = return . truncate instance Convertible Float Integer where safeConvert = return . truncate instance Convertible Rational Integer where safeConvert = return . truncate instance Convertible Float Double where safeConvert = return . realToFrac instance Convertible Double Float where safeConvert = return . realToFrac instance Convertible Float Rational where safeConvert = return . toRational instance Convertible Rational Float where safeConvert = return . fromRational instance Convertible Double Rational where safeConvert = return . toRational instance Convertible Rational Double where safeConvert = return . fromRational ------------------------------------------------------------ instance Convertible Char Integer where safeConvert = return . fromIntegral . fromEnum instance Convertible Integer Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) ------------------------------------------------------------ {- These instances generated by: int = ["Int", "Int8", "Int16", "Int32", "Int64", "Word", "Word8", "Word16", "Word32", "Word64"] printIt i = "instance Convertible Char " ++ i ++ " where \n\ \ safeConvert = boundedConversion (return . fromIntegral . fromEnum)\n\ \instance Convertible " ++ i ++ " Char where \n\ \ safeConvert = boundedConversion (return . toEnum . fromIntegral)\n\n" main = do mapM_ (putStrLn . printIt) int -} instance Convertible Char Int where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Int Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char Int8 where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Int8 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char Int16 where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Int16 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char Int32 where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Int32 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char Int64 where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Int64 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char Word where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Word Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char Word8 where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Word8 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char Word16 where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Word16 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char Word32 where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Word32 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Char Word64 where safeConvert = boundedConversion (return . fromIntegral . fromEnum) instance Convertible Word64 Char where safeConvert = boundedConversion (return . toEnum . fromIntegral) instance Convertible Integer Integer where safeConvert = return . id convertible-1.1.1.0/Data/Convertible/Instances/Text.hs0000644000000000000000000001447712515141523020722 0ustar0000000000000000{-# LANGUAGE CPP #-} {- | Module : Data.Convertible.Instances.Text Copyright : Copyright (C) 2011 MailRank, Inc. License : LGPL Maintainer : John Goerzen Stability : provisional Portability: portable Text instances for Convertible. Copyright (C) 2011 MailRank, Inc. All rights reserved. For license and copyright information, see the file COPYRIGHT These instances perform conversion between text-like types such as Text, ByteString, and the like. The instances do /not/ include conversions between ByteString and Text or String, since such conversions cannot safely be performed without knowing the encoding of the ByteString. -} module Data.Convertible.Instances.Text() where import Data.Convertible.Base import qualified Data.Text as TS import qualified Data.Text.Encoding as TE import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Builder as TLB import qualified Data.Text.Lazy.Encoding as TLE import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Builder as BB import Data.Word (Word8) import Data.Foldable instance Convertible TS.Text [Char] where {-# INLINE safeConvert #-} safeConvert = Right . TS.unpack instance Convertible TS.Text TL.Text where {-# INLINE safeConvert #-} safeConvert = Right . TL.fromStrict instance Convertible TS.Text TLB.Builder where {-# INLINE safeConvert #-} safeConvert = Right . TLB.fromText instance Convertible TS.Text BS.ByteString where {-# INLINE safeConvert #-} safeConvert = Right . TE.encodeUtf8 instance Convertible TS.Text BL.ByteString where {-# INLINE safeConvert #-} safeConvert = Right . BL.fromStrict . TE.encodeUtf8 instance Convertible TS.Text BB.Builder where {-# INLINE safeConvert #-} #if MIN_VERSION_text(1,2,0) safeConvert = Right . TE.encodeUtf8Builder #else safeConvert = safeConvert . TE.encodeUtf8 #endif instance Convertible TL.Text [Char] where {-# INLINE safeConvert #-} safeConvert = Right . TL.unpack instance Convertible TL.Text TS.Text where {-# INLINE safeConvert #-} safeConvert = Right . TL.toStrict instance Convertible TL.Text TLB.Builder where {-# INLINE safeConvert #-} safeConvert = Right . TLB.fromLazyText instance Convertible TL.Text BS.ByteString where {-# INLINE safeConvert #-} safeConvert = safeConvert . TLE.encodeUtf8 instance Convertible TL.Text BL.ByteString where {-# INLINE safeConvert #-} safeConvert = Right . TLE.encodeUtf8 instance Convertible TL.Text BB.Builder where {-# INLINE safeConvert #-} #if MIN_VERSION_text(1,2,0) safeConvert = Right . TLE.encodeUtf8Builder #else safeConvert = safeConvert . TLE.encodeUtf8 #endif instance Convertible TLB.Builder [Char] where {-# INLINE safeConvert #-} safeConvert = safeConvert . TLB.toLazyText instance Convertible TLB.Builder TS.Text where {-# INLINE safeConvert #-} safeConvert = safeConvert . TLB.toLazyText instance Convertible TLB.Builder TL.Text where {-# INLINE safeConvert #-} safeConvert = Right . TLB.toLazyText instance Convertible TLB.Builder BS.ByteString where {-# INLINE safeConvert #-} safeConvert = safeConvert . TLB.toLazyText instance Convertible TLB.Builder BL.ByteString where {-# INLINE safeConvert #-} safeConvert = safeConvert . TLB.toLazyText instance Convertible TLB.Builder BB.Builder where {-# INLINE safeConvert #-} safeConvert = safeConvert . TLB.toLazyText instance Convertible BS.ByteString [Word8] where {-# INLINE safeConvert #-} safeConvert = Right . BS.unpack instance Convertible BS.ByteString TS.Text where {-# INLINE safeConvert #-} safeConvert = Right . TE.decodeUtf8 instance Convertible BS.ByteString TL.Text where {-# INLINE safeConvert #-} safeConvert = fmap TL.fromStrict . safeConvert instance Convertible BS.ByteString TLB.Builder where {-# INLINE safeConvert #-} safeConvert = fmap TLB.fromText . safeConvert instance Convertible BS.ByteString BL.ByteString where {-# INLINE safeConvert #-} safeConvert = Right . BL.fromStrict instance Convertible BS.ByteString BB.Builder where {-# INLINE safeConvert #-} safeConvert = Right . BB.byteString instance Convertible BL.ByteString [Word8] where {-# INLINE safeConvert #-} safeConvert = Right . BL.unpack instance Convertible BL.ByteString TS.Text where {-# INLINE safeConvert #-} safeConvert = fmap TL.toStrict . safeConvert instance Convertible BL.ByteString TL.Text where {-# INLINE safeConvert #-} safeConvert = Right . TLE.decodeUtf8 instance Convertible BL.ByteString TLB.Builder where {-# INLINE safeConvert #-} safeConvert = fmap TLB.fromLazyText . safeConvert instance Convertible BL.ByteString BS.ByteString where {-# INLINE safeConvert #-} safeConvert = Right . BL.toStrict instance Convertible BL.ByteString BB.Builder where {-# INLINE safeConvert #-} safeConvert = Right . BB.lazyByteString instance Convertible [Char] TS.Text where {-# INLINE safeConvert #-} safeConvert = Right . TS.pack instance Convertible [Char] TL.Text where {-# INLINE safeConvert #-} safeConvert = Right . TL.pack instance Convertible [Char] TLB.Builder where {-# INLINE safeConvert #-} safeConvert = Right . TLB.fromString instance Convertible [Word8] BS.ByteString where {-# INLINE safeConvert #-} safeConvert = Right . BS.pack instance Convertible [Word8] BL.ByteString where {-# INLINE safeConvert #-} safeConvert = Right . BL.pack instance Convertible [Word8] BB.Builder where {-# INLINE safeConvert #-} safeConvert = Right . foldMap BB.word8 instance Convertible Char TS.Text where {-# INLINE safeConvert #-} safeConvert = Right . TS.singleton instance Convertible Char TL.Text where {-# INLINE safeConvert #-} safeConvert = Right . TL.singleton instance Convertible Char TLB.Builder where {-# INLINE safeConvert #-} safeConvert = Right . TLB.singleton instance Convertible Word8 BS.ByteString where {-# INLINE safeConvert #-} safeConvert = Right . BS.singleton instance Convertible Word8 BL.ByteString where {-# INLINE safeConvert #-} safeConvert = Right . BL.singleton instance Convertible Word8 BB.Builder where {-# INLINE safeConvert #-} safeConvert = Right . BB.word8 convertible-1.1.1.0/Data/Convertible/Instances/Time.hs0000644000000000000000000002420512515141523020662 0ustar0000000000000000{- | Module : Data.Convertible.Instances.Time Copyright : Copyright (C) 2009-2011 John Goerzen License : BSD3 Maintainer : John Goerzen Stability : provisional Portability: portable Instances to convert between various time structures, both old- and new-style. At present, this module does not do full input validation. That is, it is possible to get an exception rather than a Left result from these functions if your input is invalid, particularly when converting from the old-style System.Time structures. Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} module Data.Convertible.Instances.Time() where import Data.Convertible.Base import Data.Convertible.Utils import Data.Convertible.Instances.Num() import qualified System.Time as ST import Data.Time import Data.Time.Clock.POSIX import Data.Time.Calendar.OrdinalDate import Data.Ratio import Foreign.C.Types ---------------------------------------------------------------------- -- Intra-System.Time stuff ---------------------------------------------------------------------- instance Convertible ST.ClockTime ST.CalendarTime where safeConvert = return . ST.toUTCTime instance Convertible ST.CalendarTime ST.ClockTime where safeConvert = return . ST.toClockTime instance Convertible ST.ClockTime Integer where safeConvert (ST.TOD x _) = return x instance Convertible Integer ST.ClockTime where safeConvert x = return $ ST.TOD x 0 ---------------------------------------------------------------------- -- Intra-Data.Time stuff ---------------------------------------------------------------------- ------------------------------ POSIX and UTC times {- Covered under Real a instance Convertible Rational POSIXTime where safeConvert = return . fromRational -} instance Convertible Rational POSIXTime where safeConvert = return . fromRational instance Convertible Integer POSIXTime where safeConvert = return . fromInteger instance Convertible Int POSIXTime where safeConvert = return . fromIntegral instance Convertible Double POSIXTime where safeConvert = return . realToFrac instance Convertible POSIXTime Integer where safeConvert = return . truncate instance Convertible POSIXTime Rational where safeConvert = return . toRational instance Convertible POSIXTime Double where safeConvert = return . realToFrac instance Convertible POSIXTime Int where safeConvert = boundedConversion (return . truncate) instance Convertible POSIXTime UTCTime where safeConvert = return . posixSecondsToUTCTime instance Convertible UTCTime POSIXTime where safeConvert = return . utcTimeToPOSIXSeconds instance Convertible Rational UTCTime where safeConvert a = safeConvert a >>= return . posixSecondsToUTCTime instance Convertible Integer UTCTime where safeConvert a = safeConvert a >>= return . posixSecondsToUTCTime instance Convertible Int UTCTime where safeConvert a = safeConvert a >>= return . posixSecondsToUTCTime instance Convertible Double UTCTime where safeConvert a = safeConvert a >>= return . posixSecondsToUTCTime instance Convertible UTCTime Rational where safeConvert = safeConvert . utcTimeToPOSIXSeconds instance Convertible UTCTime Integer where safeConvert = safeConvert . utcTimeToPOSIXSeconds instance Convertible UTCTime Double where safeConvert = safeConvert . utcTimeToPOSIXSeconds instance Convertible UTCTime Int where safeConvert = boundedConversion (safeConvert . utcTimeToPOSIXSeconds) ------------------------------ LocalTime stuff instance Convertible UTCTime ZonedTime where safeConvert = return . utcToZonedTime utc instance Convertible POSIXTime ZonedTime where safeConvert = return . utcToZonedTime utc . posixSecondsToUTCTime instance Convertible ZonedTime UTCTime where safeConvert = return . zonedTimeToUTC instance Convertible ZonedTime POSIXTime where safeConvert = return . utcTimeToPOSIXSeconds . zonedTimeToUTC {- Too obvious? instance Convertible LocalTime Day where safeConvert = return . localDay instance Convertible LocalTime TimeOfDay where safeConvert = return . localTimeOfDay -} ---------------------------------------------------------------------- -- Conversions between old and new time ---------------------------------------------------------------------- instance Convertible ST.CalendarTime ZonedTime where safeConvert ct = return $ ZonedTime { zonedTimeToLocalTime = LocalTime { localDay = fromGregorian (fromIntegral $ ST.ctYear ct) (1 + (fromEnum $ ST.ctMonth ct)) (ST.ctDay ct), localTimeOfDay = TimeOfDay { todHour = ST.ctHour ct, todMin = ST.ctMin ct, todSec = (fromIntegral $ ST.ctSec ct) + fromRational (ST.ctPicosec ct % 1000000000000) } }, zonedTimeZone = TimeZone { timeZoneMinutes = ST.ctTZ ct `div` 60, timeZoneSummerOnly = ST.ctIsDST ct, timeZoneName = ST.ctTZName ct} } instance Convertible ST.CalendarTime POSIXTime where safeConvert = convertVia (undefined::ST.ClockTime) instance Convertible ST.CalendarTime UTCTime where safeConvert = convertVia (undefined::POSIXTime) instance Convertible ST.ClockTime POSIXTime where safeConvert (ST.TOD x y) = return $ fromRational $ fromInteger x + fromRational (y % 1000000000000) instance Convertible ST.ClockTime UTCTime where safeConvert = convertVia (undefined::POSIXTime) instance Convertible ST.ClockTime ZonedTime where safeConvert = convertVia (undefined::UTCTime) instance Convertible ZonedTime ST.ClockTime where safeConvert = convertVia (undefined::POSIXTime) instance Convertible POSIXTime ST.ClockTime where safeConvert x = return $ ST.TOD rsecs rpico where rsecs = floor x rpico = truncate $ abs $ 1000000000000 * (x - (fromIntegral rsecs)) instance Convertible UTCTime ST.ClockTime where safeConvert = safeConvert . utcTimeToPOSIXSeconds instance Convertible ZonedTime ST.CalendarTime where safeConvert zt = return $ ST.CalendarTime { ST.ctYear = fromIntegral year, ST.ctMonth = toEnum (month - 1), ST.ctDay = day, ST.ctHour = todHour ltod, ST.ctMin = todMin ltod, ST.ctSec = secs, ST.ctPicosec = pico, ST.ctWDay = toEnum . snd . sundayStartWeek . localDay . zonedTimeToLocalTime $ zt, ST.ctYDay = (snd . toOrdinalDate . localDay . zonedTimeToLocalTime $ zt) - 1, ST.ctTZName = timeZoneName . zonedTimeZone $ zt, ST.ctTZ = (timeZoneMinutes . zonedTimeZone $ zt) * 60, ST.ctIsDST = timeZoneSummerOnly . zonedTimeZone $ zt } where (year, month, day) = toGregorian . localDay . zonedTimeToLocalTime $ zt ltod = localTimeOfDay . zonedTimeToLocalTime $ zt secs = (truncate . todSec $ ltod)::Int picoRational = toRational (todSec ltod) - toRational secs pico = truncate (picoRational * 1000000000000) instance Convertible POSIXTime ST.CalendarTime where safeConvert = convertVia (undefined::ZonedTime) instance Convertible UTCTime ST.CalendarTime where safeConvert = safeConvert . utcTimeToPOSIXSeconds instance Convertible ST.TimeDiff NominalDiffTime where {- This is a clever hack. We convert the TimeDiff to a ClockTime, applying it as a diff vs. the epoch. Converting this ClockTime to a POSIXTime yiels the NominalDiffTime we want, since a POSIXTime is a NominalDiffTime vs. the epoch. -} safeConvert td = safeConvert clockTime where clockTime = ST.addToClockTime td (ST.TOD 0 0) instance Convertible NominalDiffTime ST.TimeDiff where {- Similar clever hack as above. -} safeConvert ndt = do clockt <- safeConvert ndt return (ST.diffClockTimes clockt (ST.TOD 0 0)) instance Convertible Integer ST.TimeDiff where safeConvert = convertVia (undefined::NominalDiffTime) instance Convertible Double ST.TimeDiff where safeConvert = convertVia (undefined::NominalDiffTime) instance Convertible ST.TimeDiff Integer where safeConvert = convertVia (undefined :: NominalDiffTime) instance Convertible ST.TimeDiff Rational where safeConvert = convertVia (undefined :: NominalDiffTime) instance Convertible ST.TimeDiff Double where safeConvert = convertVia (undefined :: NominalDiffTime) ---------------------------------------------------------------------- -- Foreign.C Types ---------------------------------------------------------------------- instance Convertible CTime POSIXTime where safeConvert = return . realToFrac instance Convertible POSIXTime CTime where safeConvert = return . fromInteger . truncate instance Convertible CTime Integer where safeConvert = return . truncate . toRational instance Convertible Integer CTime where safeConvert = return . fromInteger instance Convertible CTime Double where safeConvert = return . realToFrac instance Convertible Double CTime where safeConvert = return . fromInteger . truncate instance Convertible CTime Int where safeConvert x = do r1 <- safeConvert x boundedConversion (return . fromInteger) r1 instance Convertible Int CTime where safeConvert = safeConvert . toInteger instance Convertible CTime UTCTime where safeConvert = convertVia (undefined :: POSIXTime) instance Convertible UTCTime CTime where safeConvert = convertVia (undefined :: POSIXTime) instance Convertible CTime ST.ClockTime where safeConvert = convertVia (undefined :: POSIXTime) instance Convertible ST.ClockTime CTime where safeConvert = convertVia (undefined :: POSIXTime) instance Convertible CTime ST.CalendarTime where safeConvert = convertVia (undefined::POSIXTime) instance Convertible ST.CalendarTime CTime where safeConvert = convertVia (undefined::POSIXTime) instance Convertible CTime ZonedTime where safeConvert = convertVia (undefined::POSIXTime) instance Convertible ZonedTime CTime where safeConvert = convertVia (undefined::POSIXTime) convertible-1.1.1.0/testsrc/0000755000000000000000000000000012515141523014040 5ustar0000000000000000convertible-1.1.1.0/testsrc/runtests.hs0000644000000000000000000000064612515141523016271 0ustar0000000000000000module Main where import qualified Test.HUnit as HU import Test.HUnit.Tools import qualified TestNum import qualified TestMap import qualified TestTime test1 = HU.TestCase ((HU.@=?) "x" "x") alltests = [HU.TestLabel "test1" test1, tl "TestNum" TestNum.allt, tl "TestMap" TestMap.allt, tl "TestTime" TestTime.allt] main = do runVerboseTests (HU.TestList alltests) return ()convertible-1.1.1.0/testsrc/TestInfrastructure.hs0000644000000000000000000000051712515141523020257 0ustar0000000000000000{- Copyright (C) 2009 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} module TestInfrastructure where import qualified Test.QuickCheck as QC import qualified Test.HUnit as HU import Test.HUnit.Tools q :: QC.Testable a => String -> a -> HU.Test q = qc2hu 250 convertible-1.1.1.0/testsrc/TestMap.hs0000644000000000000000000000124312515141523015751 0ustar0000000000000000{- Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} module TestMap where import TestInfrastructure import Data.Convertible import Test.QuickCheck import Test.QuickCheck.Tools import Test.QuickCheck.Instances import qualified Test.QuickCheck.Property as P import qualified Data.Map as Map propListMap :: [(Int, Int)] -> P.Result propListMap x = safeConvert x @?= Right (Map.fromList x) propMapList :: Map.Map Int Int -> P.Result propMapList x = safeConvert x @?= Right (Map.toList x) allt = [q "[(Int, Int)] -> Map" propListMap, q "Map -> [(Int, Int)]" propMapList] convertible-1.1.1.0/testsrc/TestNum.hs0000644000000000000000000001032512515141523015774 0ustar0000000000000000{- Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} module TestNum where import TestInfrastructure import Data.Convertible import Test.QuickCheck import Test.QuickCheck.Tools import qualified Test.QuickCheck.Property as P import Data.Word prop_int_to_integer :: Int -> P.Result prop_int_to_integer x = safeConvert x @?= Right ((fromIntegral x)::Integer) prop_integer_to_int_pass :: Integer -> Property prop_integer_to_int_pass x = (x <= fromIntegral (maxBound :: Int)) && (x >= fromIntegral (minBound :: Int)) ==> safeConvert x @?= Right ((fromIntegral x)::Int) prop_integer_to_word8 :: Integer -> P.Result prop_integer_to_word8 x = safeConvert x @?= if x >= fromIntegral (minBound :: Word8) && x <= fromIntegral (maxBound :: Word8) then Right ((fromIntegral x)::Word8) else Left $ ConvertError (show x) "Integer" "Word8" "Input value outside of bounds: (0,255)" prop_integer_to_word8_safe :: Integer -> Property prop_integer_to_word8_safe x = x <= fromIntegral (maxBound :: Word8) && x >= fromIntegral (minBound :: Word8) ==> safeConvert x @?= Right ((fromIntegral x)::Word8) prop_integer_to_word8_unsafe :: Integer -> Property prop_integer_to_word8_unsafe x = x < fromIntegral (minBound :: Word8) || x > fromIntegral (maxBound :: Word8) ==> ((safeConvert x)::ConvertResult Word8) @?= (Left $ ConvertError (show x) "Integer" "Word8" "Input value outside of bounds: (0,255)") prop_double_to_word8 :: Double -> P.Result prop_double_to_word8 x = safeConvert x @?= if truncate x >= toInteger (minBound :: Word8) && truncate x <= toInteger (maxBound :: Word8) then Right ((truncate x)::Word8) else Left $ ConvertError (show x) "Double" "Word8" "Input value outside of bounds: (0,255)" prop_double_to_word8_safe :: Double -> Property prop_double_to_word8_safe x = x <= fromIntegral (maxBound :: Word8) && x >= fromIntegral (minBound :: Word8) ==> safeConvert x @?= Right ((truncate x)::Word8) prop_double_to_word8_unsafe :: Double -> Property prop_double_to_word8_unsafe x = truncate x < toInteger (minBound :: Word8) || truncate x > toInteger (maxBound :: Word8) ==> ((safeConvert x)::ConvertResult Word8) @?= (Left $ ConvertError (show x) "Double" "Word8" "Input value outside of bounds: (0,255)") propIntDouble :: Int -> P.Result propIntDouble x = safeConvert x @?= Right ((fromIntegral x)::Double) propIntChar :: Int -> P.Result propIntChar x = safeConvert x @?= if x >= fromEnum (minBound :: Char) && x <= fromEnum (maxBound :: Char) then Right ((toEnum x)::Char) else Left $ ConvertError (show x) "Int" "Char" "Input value outside of bounds: ('\\NUL','\\1114111')" propCharInt :: Char -> P.Result propCharInt c = safeConvert c @?= Right ((fromEnum c)::Int) where x = fromEnum c propIntIntegerInt :: Int -> P.Result propIntIntegerInt x = Right x @=? do r1 <- ((safeConvert x)::ConvertResult Integer) ((safeConvert r1)::ConvertResult Int) propDoubleRationalDouble :: Double -> P.Result propDoubleRationalDouble x = Right x @=? do r1 <- ((safeConvert x)::ConvertResult Rational) ((safeConvert r1)::ConvertResult Double) allt = [q "Int -> Integer" prop_int_to_integer, q "Integer -> Int (safe bounds)" prop_integer_to_int_pass, q "Integer -> Word8 (general)" prop_integer_to_word8, q "Integer -> Word8 (safe bounds)" prop_integer_to_word8_safe, q "Integer -> Word8 (unsafe bounds)" prop_integer_to_word8_unsafe, q "Double -> Word8 (general)" prop_double_to_word8, q "Double -> Word8 (safe bounds)" prop_double_to_word8_safe, q "Double -> Word8 (unsafe bounds)" prop_double_to_word8_unsafe, q "Int -> Double" propIntDouble, q "Int -> Char" propIntChar, q "Char -> Int" propCharInt, q "identity Int -> Integer -> Int" propIntIntegerInt, q "identity Double -> Rational -> Double" propDoubleRationalDouble ] convertible-1.1.1.0/testsrc/TestTime.hs0000644000000000000000000001635112515141523016140 0ustar0000000000000000{- Copyright (C) 2009-2011 John Goerzen All rights reserved. For license and copyright information, see the file LICENSE -} module TestTime where import TestInfrastructure import Data.Convertible import Test.QuickCheck import Test.QuickCheck.Tools import Test.QuickCheck.Instances import qualified Test.QuickCheck.Property as P import qualified System.Time as ST import Data.Time import Data.Time.Clock.POSIX import Data.Ratio import Foreign.C.Types instance Arbitrary ST.ClockTime where arbitrary = do r1 <- arbitrary r2 <- sized $ \n -> choose (0, 1000000000000 - 1) return (ST.TOD r1 r2) -- coarbitrary (ST.TOD a b) = coarbitrary a . coarbitrary b instance Arbitrary ST.CalendarTime where arbitrary = do r <- arbitrary return $ convert (r::POSIXTime) instance Arbitrary NominalDiffTime where arbitrary = do r <- arbitrary return $ convert (r::ST.ClockTime) instance Arbitrary UTCTime where arbitrary = do r <- arbitrary return $ convert (r::POSIXTime) instance Arbitrary ZonedTime where arbitrary = do r <- arbitrary return $ convert (r::POSIXTime) instance Eq ZonedTime where a == b = zonedTimeToUTC a == zonedTimeToUTC b propCltCalt :: ST.ClockTime -> P.Result propCltCalt x = safeConvert x @?= Right (ST.toUTCTime x) propCltCaltClt :: ST.ClockTime -> P.Result propCltCaltClt x = Right x @=? do r1 <- ((safeConvert x)::ConvertResult ST.CalendarTime) safeConvert r1 propCltPT :: ST.ClockTime -> P.Result propCltPT x@(ST.TOD y z) = safeConvert x @?= Right (r::POSIXTime) where r = fromRational $ fromInteger y + fromRational (z % 1000000000000) propPTClt :: POSIXTime -> P.Result propPTClt x = safeConvert x @?= Right (r::ST.ClockTime) where r = ST.TOD rsecs rpico rsecs = floor x rpico = truncate $ abs $ 1000000000000 * (x - (fromIntegral rsecs)) propCaltPT :: ST.CalendarTime -> P.Result propCaltPT x = safeConvert x @?= expected where expected = do r <- safeConvert x (safeConvert (r :: ST.ClockTime))::ConvertResult POSIXTime propCltPTClt :: ST.ClockTime -> P.Result propCltPTClt x = Right (toTOD x) @=? case do r1 <- (safeConvert x)::ConvertResult POSIXTime safeConvert r1 of Left x -> Left x Right y -> Right $ toTOD y where toTOD (ST.TOD x y) = (x, y) {- Right x @=? do r1 <- (safeConvert x)::ConvertResult POSIXTime safeConvert r1 -} propPTZTPT :: POSIXTime -> P.Result propPTZTPT x = Right x @=? do r1 <- safeConvert x safeConvert (r1 :: ZonedTime) propPTCltPT :: POSIXTime -> P.Result propPTCltPT x = Right x @=? do r1 <- (safeConvert x)::ConvertResult ST.ClockTime safeConvert r1 propPTCalPT :: POSIXTime -> P.Result propPTCalPT x = Right x @=? do r1 <- safeConvert x safeConvert (r1::ST.CalendarTime) propUTCCaltUTC :: UTCTime -> P.Result propUTCCaltUTC x = Right x @=? do r1 <- safeConvert x safeConvert (r1::ST.CalendarTime) propPTUTC :: POSIXTime -> P.Result propPTUTC x = safeConvert x @?= Right (posixSecondsToUTCTime x) propUTCPT :: UTCTime -> P.Result propUTCPT x = safeConvert x @?= Right (utcTimeToPOSIXSeconds x) propCltUTC :: ST.ClockTime -> P.Result propCltUTC x = safeConvert x @?= Right (posixSecondsToUTCTime . convert $ x) propZTCTeqZTCaltCt :: ZonedTime -> P.Result propZTCTeqZTCaltCt x = route1 @=? route2 where route1 = (safeConvert x)::ConvertResult ST.ClockTime route2 = do calt <- safeConvert x safeConvert (calt :: ST.CalendarTime) propCaltZTCalt :: ST.ClockTime -> P.Result propCaltZTCalt x = Right x @=? do zt <- ((safeConvert calt)::ConvertResult ZonedTime) calt' <- ((safeConvert zt)::ConvertResult ST.CalendarTime) return (ST.toClockTime calt') where calt = ST.toUTCTime x propCaltZTCalt2 :: ST.CalendarTime -> P.Result propCaltZTCalt2 x = Right x @=? do zt <- safeConvert x safeConvert (zt :: ZonedTime) propZTCaltCtZT :: ZonedTime -> P.Result propZTCaltCtZT x = Right x @=? do calt <- safeConvert x ct <- safeConvert (calt :: ST.CalendarTime) safeConvert (ct :: ST.ClockTime) propZTCtCaltZT :: ZonedTime -> P.Result propZTCtCaltZT x = Right x @=? do ct <- safeConvert x calt <- safeConvert (ct :: ST.ClockTime) safeConvert (calt :: ST.CalendarTime) propZTCaltZT :: ZonedTime -> P.Result propZTCaltZT x = Right x @=? do calt <- safeConvert x safeConvert (calt :: ST.CalendarTime) propZTCtCaltCtZT :: ZonedTime -> P.Result propZTCtCaltCtZT x = Right x @=? do ct <- safeConvert x calt <- safeConvert (ct :: ST.ClockTime) ct' <- safeConvert (calt :: ST.CalendarTime) safeConvert (ct' :: ST.ClockTime) propUTCZT :: UTCTime -> Bool propUTCZT x = x == zonedTimeToUTC (convert x) propUTCZTUTC :: UTCTime -> P.Result propUTCZTUTC x = Right x @=? do r1 <- ((safeConvert x)::ConvertResult ZonedTime) safeConvert r1 propNdtTdNdt :: NominalDiffTime -> P.Result propNdtTdNdt x = Right x @=? do r1 <- ((safeConvert x)::ConvertResult ST.TimeDiff) safeConvert r1 propPTCPT :: POSIXTime -> P.Result propPTCPT x = Right testval @=? do r1 <- safeConvert testval safeConvert (r1 :: CTime) where testval = (convert ((truncate x)::Integer))::POSIXTime -- CTime doesn't support picosecs allt = [q "ClockTime -> CalendarTime" propCltCalt, q "ClockTime -> CalendarTime -> ClockTime" propCltCaltClt, q "ClockTime -> POSIXTime" propCltPT, q "POSIXTime -> ClockTime" propPTClt, q "CalendarTime -> POSIXTime" propCaltPT, q "identity ClockTime -> POSIXTime -> ClockTime" propCltPTClt, q "identity POSIXTime -> ClockTime -> POSIXTime" propPTCltPT, q "identity POSIXTime -> ZonedTime -> POSIXTime" propPTZTPT, q "identity POSIXTime -> CalendarTime -> POSIXTime" propPTCalPT, q "identity UTCTime -> CalendarTime -> UTCTime" propUTCCaltUTC, q "POSIXTime -> UTCTime" propPTUTC, q "UTCTime -> POSIXTime" propUTCPT, q "ClockTime -> UTCTime" propCltUTC, q "ZonedTime -> ClockTime == ZonedTime -> CalendarTime -> ClockTime" propZTCTeqZTCaltCt, q "identity CalendarTime -> ZonedTime -> CalendarTime" propCaltZTCalt, q "identity CalendarTime -> ZonedTime -> CalenderTime, test 2" propCaltZTCalt2, q "identity ZonedTime -> CalendarTime -> ZonedTime" propZTCaltZT, q "ZonedTime -> CalendarTime -> ClockTime -> ZonedTime" propZTCaltCtZT, q "ZonedTime -> ClockTime -> CalendarTime -> ZonedTime" propZTCtCaltZT, q "ZonedTime -> ColckTime -> CalendarTime -> ClockTime -> ZonedTime" propZTCtCaltCtZT, q "UTCTime -> ZonedTime" propUTCZT, q "UTCTime -> ZonedTime -> UTCTime" propUTCZTUTC, q "identity NominalDiffTime -> TimeDiff -> NominalDiffTime" propNdtTdNdt, q "identity POSIXTime -> CTime -> POSIXTime" propPTCPT ] convertible-1.1.1.0/utils/0000755000000000000000000000000012515141523013511 5ustar0000000000000000convertible-1.1.1.0/utils/genCinstances.hs0000644000000000000000000000522612515141523016636 0ustar0000000000000000cint = ["CChar", "CSChar", "CUChar", "CShort", "CUShort", "CInt", "CUInt", "CLong", "CULong", "CSize", "CWchar", "CLLong", "CULLong"] cfloat = ["CFloat", "CDouble", "CLDouble"] hsint = ["Int", "Int8", "Int16", "Int32", "Int64", "Word", "Word8", "Word16", "Word32", "Word64"] hsfloat = ["Double", "Float", "Rational"] printFP (f, i) = "instance Convertible " ++ f ++ " " ++ i ++ " where \n\ \ safeConvert = boundedConversion (return . truncate)\n\ \instance Convertible " ++ i ++ " " ++ f ++ " where \n\ \ safeConvert = return . fromIntegral\n" printIntegerF f = "instance Convertible " ++ f ++ " Integer where\n\ \ safeConvert = return . truncate\n\ \instance Convertible Integer " ++ f ++ " where\n\ \ safeConvert = return . fromIntegral\n" printIntegerI i = "instance Convertible " ++ i ++ " Integer where\n\ \ safeConvert = return . fromIntegral\n\ \instance Convertible Integer " ++ i ++ " where\n\ \ safeConvert = boundedConversion (return . fromIntegral)\n" printCharI i = "instance Convertible " ++ i ++ " Char where\n\ \ safeConvert = boundedConversion (return . toEnum . fromIntegral)\n\ \instance Convertible Char " ++ i ++ " where\n\ \ safeConvert = boundedConversion (return . fromIntegral . fromEnum)\n" printFP1 (f1, f2) = "instance Convertible " ++ f1 ++ " " ++ f2 ++ " where\n\ \ safeConvert = return . realToFrac\n" printFPFP (f1, f2) = printFP1 (f1, f2) ++ printFP1 (f2, f1) printInt (i1, i2) = "instance Convertible " ++ i1 ++ " " ++ i2 ++ " where\n\ \ safeConvert = boundedConversion (return . fromIntegral)\n" printIntInt (i1, i2) = printInt (i1, i2) ++ printInt (i2, i1) main = do putStrLn "-- Section 1" mapM_ (putStrLn . printFP) (concatMap (\x -> map (\y -> (x, y)) hsint) cfloat) putStrLn "-- Section 2" mapM_ (putStrLn . printFPFP) (concatMap (\x -> map (\y -> (x, y)) hsfloat) cfloat) putStrLn "-- Section 3" mapM_ (putStrLn . printIntInt) (concatMap (\x -> map (\y -> (x, y)) hsint) cint) putStrLn "-- Section 4" mapM_ (putStrLn . printInt) . filter (\(a, b) -> a /= b) $ (concatMap (\x -> map (\y -> (x, y)) cint) cint) putStrLn "-- Section 5" mapM_ (putStrLn . printFP1) . filter (\(a, b) -> a /= b) $ (concatMap (\x -> map (\y -> (x, y)) cfloat) cfloat) putStrLn "-- Section 6" mapM_ (putStrLn . printIntegerF) cfloat putStrLn "-- Section 7" mapM_ (putStrLn . printIntegerI) cint putStrLn "-- Section 8o" mapM_ (putStrLn . printCharI) cint