-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Generate PureScript data types from Haskell data types -- -- Generate PureScript data types from Haskell data types @package purescript-bridge @version 0.8.0.0 -- | As we translate types and not type constructors, we have to pass dummy -- types to any type constructor. -- -- buildBridge will translate all parameter types which come -- from a module TypeParameters (e.g. this one) to lower case. -- -- For translating something like Maybe: -- --
-- data Maybe' a = Nothing' | Just' a -- ---- -- you would use: -- --
-- import Language.PureScript.Bridge -- import Language.PureScript.Bridge.TypeParameters -- -- st = mkSumType (Proxy :: Proxy (Maybe' A)) -- Note that we use "Maybe' A" instead of just Maybe - which would not work. -- --module Language.PureScript.Bridge.TypeParameters data A data B data C data D data E data F data G data H data I data J data K data L data M data N data O data P data Q data R data S data T data U data V data W data X data Y data Z -- | You can use those if your type parameters are actually type -- constructors as well: st = mkSumType (Proxy :: Proxy -- (ReaderT R M1 A)) data A1 a data B1 a data C1 a data D1 a data E1 a data F1 a data G1 a data H1 a data I1 a data J1 a data K1 a data L1 a data M1 a data N1 a data O1 a data P1 a data Q1 a data R1 a data S1 a data T1 a data U1 a data V1 a data W1 a data X1 a data Y1 a data Z1 a module Language.PureScript.Bridge.TypeInfo -- | Basic info about a data type: data TypeInfo (lang :: Language) TypeInfo :: !Text -> !Text -> !Text -> ![TypeInfo lang] -> TypeInfo -- | Hackage package [_typePackage] :: TypeInfo -> !Text -- | Full Module path [_typeModule] :: TypeInfo -> !Text [_typeName] :: TypeInfo -> !Text [_typeParameters] :: TypeInfo -> ![TypeInfo lang] -- | For convenience: type PSType = TypeInfo PureScript -- | For convenience: type HaskellType = TypeInfo Haskell mkTypeInfo :: Typeable t => Proxy t -> HaskellType mkTypeInfo' :: TypeRep -> HaskellType data Language Haskell :: Language PureScript :: Language typePackage :: forall lang_a7Jq. Lens' (TypeInfo lang_a7Jq) Text typeModule :: forall lang_a7Jq. Lens' (TypeInfo lang_a7Jq) Text typeName :: forall lang_a7Jq. Lens' (TypeInfo lang_a7Jq) Text typeParameters :: forall lang_a7Jq lang_abHw. Lens (TypeInfo lang_a7Jq) (TypeInfo lang_abHw) [TypeInfo lang_a7Jq] [TypeInfo lang_abHw] -- | Types that have a lens for accessing a 'TypeInfo Haskell'. class HasHaskType t haskType :: HasHaskType t => Lens' t HaskellType haskType :: HasHaskType t => Lens' t HaskellType -- | Put the TypeInfo in a list together with all its -- _typeParameters (recursively) flattenTypeInfo :: TypeInfo lang -> [TypeInfo lang] instance Language.PureScript.Bridge.TypeInfo.HasHaskType Language.PureScript.Bridge.TypeInfo.HaskellType instance GHC.Show.Show (Language.PureScript.Bridge.TypeInfo.TypeInfo lang) instance GHC.Classes.Ord (Language.PureScript.Bridge.TypeInfo.TypeInfo lang) instance GHC.Classes.Eq (Language.PureScript.Bridge.TypeInfo.TypeInfo lang) module Language.PureScript.Bridge.SumType -- | Generic representation of your Haskell types. data SumType (lang :: Language) SumType :: (TypeInfo lang) -> [DataConstructor lang] -> SumType -- | Create a representation of your sum (and product) types, for doing -- type translations and writing it out to your PureScript modules. In -- order to get the type information we use a dummy variable of type -- Proxy (YourType). mkSumType :: forall t. (Generic t, Typeable t, GDataConstructor (Rep t)) => Proxy t -> SumType Haskell data DataConstructor (lang :: Language) DataConstructor :: !Text -> !(Either [TypeInfo lang] [RecordEntry lang]) -> DataConstructor -- | e.g. Left/Right for Either [_sigConstructor] :: DataConstructor -> !Text [_sigValues] :: DataConstructor -> !(Either [TypeInfo lang] [RecordEntry lang]) data RecordEntry (lang :: Language) RecordEntry :: !Text -> !(TypeInfo lang) -> RecordEntry -- | e.g. runState for State [_recLabel] :: RecordEntry -> !Text [_recValue] :: RecordEntry -> !(TypeInfo lang) -- | Get all used types in a sum type. -- -- This includes all types found at the right hand side of a sum type -- definition, not the type parameters of the sum type itself getUsedTypes :: SumType lang -> Set (TypeInfo lang) constructorToTypes :: DataConstructor lang -> Set (TypeInfo lang) -> Set (TypeInfo lang) sigConstructor :: forall lang_aeNB. Lens' (DataConstructor lang_aeNB) Text sigValues :: forall lang_aeNB lang_afN3. Lens (DataConstructor lang_aeNB) (DataConstructor lang_afN3) (Either [TypeInfo lang_aeNB] [RecordEntry lang_aeNB]) (Either [TypeInfo lang_afN3] [RecordEntry lang_afN3]) -- | TypInfo lens for SumType. sumTypeInfo :: Functor f => (TypeInfo lang -> f (TypeInfo lang)) -> SumType lang -> f (SumType lang) -- | DataConstructor lens for SumType. sumTypeConstructors :: Functor f => ([DataConstructor lang] -> f [DataConstructor lang]) -> SumType lang -> f (SumType lang) recLabel :: forall lang_aeNA. Lens' (RecordEntry lang_aeNA) Text recValue :: forall lang_aeNA lang_afO7. Lens (RecordEntry lang_aeNA) (RecordEntry lang_afO7) (TypeInfo lang_aeNA) (TypeInfo lang_afO7) instance GHC.Classes.Eq (Language.PureScript.Bridge.SumType.SumType lang) instance GHC.Show.Show (Language.PureScript.Bridge.SumType.SumType lang) instance GHC.Classes.Eq (Language.PureScript.Bridge.SumType.DataConstructor lang) instance GHC.Show.Show (Language.PureScript.Bridge.SumType.DataConstructor lang) instance GHC.Classes.Eq (Language.PureScript.Bridge.SumType.RecordEntry lang) instance GHC.Show.Show (Language.PureScript.Bridge.SumType.RecordEntry lang) instance (GHC.Generics.Datatype a, Language.PureScript.Bridge.SumType.GDataConstructor c) => Language.PureScript.Bridge.SumType.GDataConstructor (GHC.Generics.D1 a c) instance (Language.PureScript.Bridge.SumType.GDataConstructor a, Language.PureScript.Bridge.SumType.GDataConstructor b) => Language.PureScript.Bridge.SumType.GDataConstructor (a GHC.Generics.:+: b) instance (GHC.Generics.Constructor a, Language.PureScript.Bridge.SumType.GRecordEntry b) => Language.PureScript.Bridge.SumType.GDataConstructor (GHC.Generics.C1 a b) instance (Language.PureScript.Bridge.SumType.GRecordEntry a, Language.PureScript.Bridge.SumType.GRecordEntry b) => Language.PureScript.Bridge.SumType.GRecordEntry (a GHC.Generics.:*: b) instance Language.PureScript.Bridge.SumType.GRecordEntry GHC.Generics.U1 instance (GHC.Generics.Selector a, Data.Typeable.Internal.Typeable t) => Language.PureScript.Bridge.SumType.GRecordEntry (GHC.Generics.S1 a (GHC.Generics.K1 GHC.Generics.R t)) module Language.PureScript.Bridge.Printer data Module (lang :: Language) PSModule :: !Text -> !(Map Text ImportLine) -> ![SumType lang] -> Module [psModuleName] :: Module -> !Text [psImportLines] :: Module -> !(Map Text ImportLine) [psTypes] :: Module -> ![SumType lang] type PSModule = Module PureScript data ImportLine ImportLine :: !Text -> !(Set Text) -> ImportLine [importModule] :: ImportLine -> !Text [importTypes] :: ImportLine -> !(Set Text) type Modules = Map Text PSModule type ImportLines = Map Text ImportLine printModule :: FilePath -> PSModule -> IO () sumTypesToNeededPackages :: [SumType lang] -> Set Text sumTypeToNeededPackages :: SumType lang -> Set Text moduleToText :: Module PureScript -> Text importLineToText :: ImportLine -> Text sumTypeToText :: SumType PureScript -> Text constructorToText :: Int -> DataConstructor PureScript -> Text recordEntryToText :: RecordEntry PureScript -> Text typeInfoToText :: Bool -> PSType -> Text sumTypesToModules :: Modules -> [SumType PureScript] -> Modules sumTypeToModule :: SumType PureScript -> Modules -> Modules typesToImportLines :: ImportLines -> Set PSType -> ImportLines typeToImportLines :: PSType -> ImportLines -> ImportLines importsFromList :: [ImportLine] -> Map Text ImportLine mergeImportLines :: ImportLines -> ImportLines -> ImportLines unlessM :: Monad m => m Bool -> m () -> m () instance GHC.Show.Show (Language.PureScript.Bridge.Printer.Module lang) instance GHC.Show.Show Language.PureScript.Bridge.Printer.ImportLine -- | A bridge builder DSL, powered by Monad, Alternative and -- lens. -- -- Bridges can be built within the BridgeBuilder monad. You can -- check properties of the to-be-bridged HaskellType with -- ^== or doCheck, you have choice (<|>), you -- can fail (empty) and you can return a translated PSType -- (return). The HaskellType can be accessed with: -- --
-- view haskType ---- -- Find usage examples in Language.PureScript.Bridge.Primitives -- and Language.PureScript.Bridge.PSTypes module Language.PureScript.Bridge.Builder data BridgeBuilder a type BridgePart = BridgeBuilder PSType -- | Bridges to use when a BridgePart returns Nothing (See -- buildBridgeWithCustomFixUp). -- -- It is similar to BridgeBuilder but does not offer choice or failure. -- It is used for constructing fallbacks if a BridgePart evaluates -- to Nothing. -- -- For type definitions you should use the more generic -- (MonadReader BridgeData m) constraint. This way your -- code will work in both FixUpBuilder and BridgeBuilder: -- --
-- {-# LANGUAGE FlexibleContexts #-}
--
-- import Control.Monad.Reader.Class
-- import Language.PureScript.Bridge.TypeInfo
--
data FixUpBuilder a
type FixUpBridge = FixUpBuilder PSType
data BridgeData
-- | Lens for access to the complete bridge from within our Reader monad.
--
-- This is used for example for implementing psTypeParameters.
fullBridge :: Lens' BridgeData FullBridge
-- | Check parts of haskType for equality:
--
-- -- textBridge :: BridgePart -- textBridge = do -- typeName ^== "Text" -- typeModule ^== "Data.Text.Internal" <|> typeModule ^== "Data.Text.Internal.Lazy" -- return psString --(^==) :: Eq a => Getter HaskellType a -> a -> BridgeBuilder () infix 4 ^== -- | Do some check on properties of haskType. doCheck :: Getter HaskellType a -> (a -> Bool) -> BridgeBuilder () -- | An associative binary operation (<|>) :: Alternative f => forall a. f a -> f a -> f a -- | Bridge haskType typeParameters over to PureScript types. -- -- To be used for bridging type constructors. psTypeParameters :: MonadReader BridgeData m => m [PSType] type FullBridge = HaskellType -> PSType -- | Build a bridge. -- -- This is a convenience wrapper for buildBridgeWithCustomFixUp -- and should normally be sufficient. -- -- Definition: -- --
-- buildBridgeWithCustomFixUp clearPackageFixUp --buildBridge :: BridgePart -> FullBridge -- | Bridge to PureScript by simply clearing out the _typePackage -- field. This bridge is used by default as FixUpBridge by -- buildBridge: -- --
-- buildBridge = buildBridgeWithCustomFixUp clearPackageFixUp ---- -- Thus, if no bridge matches a type, it gets optimistically translated -- to a PureScript type which is idential to the Haskell type. Only the -- _typePackage field gets cleared, as it is very unlikely that -- the PureScript package is called the same as the Haskell package. -- -- Alternatively, if you are not that optimistic, you can use errorFixUp -- - which simply calls error when used. -- --
-- buildBridgeWithCustomFixUp errorFixUp yourBridge ---- -- Of course you can also write your own FixUpBridge. It works the -- same as for BridgePart, but you can not have choice -- (<|>) or failure (empty). clearPackageFixUp :: MonadReader BridgeData m => m PSType -- | A FixUpBridge which calles error when used. Usage: -- --
-- buildBridgeWithCustomFixUp errorFixUp yourBridge --errorFixUp :: MonadReader BridgeData m => m PSType -- | Takes a constructed BridgePart and makes it a total function -- (FullBridge) by using the supplied FixUpBridge when -- BridgePart returns Nothing. buildBridgeWithCustomFixUp :: FixUpBridge -> BridgePart -> FullBridge instance Control.Monad.Reader.Class.MonadReader Language.PureScript.Bridge.Builder.BridgeData Language.PureScript.Bridge.Builder.BridgeBuilder instance GHC.Base.Monad Language.PureScript.Bridge.Builder.BridgeBuilder instance GHC.Base.Applicative Language.PureScript.Bridge.Builder.BridgeBuilder instance GHC.Base.Functor Language.PureScript.Bridge.Builder.BridgeBuilder instance Control.Monad.Reader.Class.MonadReader Language.PureScript.Bridge.Builder.BridgeData Language.PureScript.Bridge.Builder.FixUpBuilder instance GHC.Base.Monad Language.PureScript.Bridge.Builder.FixUpBuilder instance GHC.Base.Applicative Language.PureScript.Bridge.Builder.FixUpBuilder instance GHC.Base.Functor Language.PureScript.Bridge.Builder.FixUpBuilder instance Language.PureScript.Bridge.TypeInfo.HasHaskType Language.PureScript.Bridge.Builder.BridgeData instance GHC.Base.Alternative Language.PureScript.Bridge.Builder.BridgeBuilder instance GHC.Base.MonadPlus Language.PureScript.Bridge.Builder.BridgeBuilder -- | PureScript types to be used for bridges, e.g. in -- Language.PureScript.Bridge.Primitives. module Language.PureScript.Bridge.PSTypes -- | Uses type parameters from haskType (bridged). psArray :: MonadReader BridgeData m => m PSType psBool :: PSType -- | Uses type parameters from haskType (bridged). psEither :: MonadReader BridgeData m => m PSType psInt :: PSType -- | Uses type parameters from haskType (bridged). psMaybe :: MonadReader BridgeData m => m PSType psString :: PSType -- | Uses type parameters from haskType (bridged). psTuple :: MonadReader BridgeData m => m PSType psUnit :: PSType module Language.PureScript.Bridge.Primitives boolBridge :: BridgePart eitherBridge :: BridgePart -- | Dummy bridge, translates every type with clearPackageFixUp dummyBridge :: MonadReader BridgeData m => m PSType intBridge :: BridgePart listBridge :: BridgePart maybeBridge :: BridgePart stringBridge :: BridgePart textBridge :: BridgePart unitBridge :: BridgePart module Language.PureScript.Bridge.Tuple tupleBridge :: BridgePart data TupleParserState Start :: TupleParserState OpenFound :: TupleParserState ColonFound :: TupleParserState Tuple :: TupleParserState NoTuple :: TupleParserState step :: TupleParserState -> Char -> TupleParserState isTuple :: HaskellType -> Bool instance GHC.Show.Show Language.PureScript.Bridge.Tuple.TupleParserState instance GHC.Classes.Eq Language.PureScript.Bridge.Tuple.TupleParserState module Language.PureScript.Bridge -- | Translate all TypeInfo values in a SumType to PureScript -- types. -- -- Example usage, with defaultBridge: -- --
-- data Foo = Foo | Bar Int | FooBar Int Text deriving (Generic, Typeable, Show) ---- --
-- bridgeSumType (buildBridge defaultBridge) (mkSumType (Proxy :: Proxy Foo)) --bridgeSumType :: FullBridge -> SumType Haskell -> SumType PureScript -- | Default bridge for mapping primitive/common types: You can append your -- own bridges like this: -- --
-- defaultBridge <|> myBridge1 <|> myBridge2 ---- -- Find examples for bridge definitions in -- Language.PureScript.Bridge.Primitives and -- Language.PureScript.Bridge.Tuple. defaultBridge :: BridgePart -- | Your entry point to this library and quite likely all you will need. -- Make sure all your types derive Generic and Typeable. Typeable is not -- needed from ghc-7.10 on. -- -- Then list all your types you want to use in PureScript and call -- writePSTypes: -- --
-- let myTypes = [ -- mkSumType (Proxy :: Proxy MyType1) -- , mkSumType (Proxy :: Proxy MyType2) -- ] -- -- writePSTypes "path/to/your/purescript/project" (buildBridge defaultBridge) myTypes ---- -- You can define your own type bridges based on defaultBridge: -- --
-- myBridge = defaultBridge <|> mySpecialTypeBridge ---- -- and use it with writePSTypes: -- --
-- writePSTypes "path/to/your/purescript/project" (buildBridge myBridge) myTypes ---- -- Find examples for implementing your own bridges in: -- Language.PureScript.Bridge.Primitives. -- --