-- 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.4.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] mkTypeInfo :: Typeable t => Proxy t -> TypeInfo Haskell mkTypeInfo' :: TypeRep -> TypeInfo Haskell data Language Haskell :: Language PureScript :: Language typePackage :: Lens' (TypeInfo lang_a6Dw) Text typeModule :: Lens' (TypeInfo lang_a6Dw) Text typeName :: Lens' (TypeInfo lang_a6Dw) Text typeParameters :: Lens (TypeInfo lang_a6Dw) (TypeInfo lang_aaNO) [TypeInfo lang_a6Dw] [TypeInfo lang_aaNO] -- | Types that have a lens for accessing a 'TypeInfo Haskell'. class HasHaskType t haskType :: HasHaskType t => Lens' t (TypeInfo Haskell) -- | 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.TypeInfo 'Language.PureScript.Bridge.TypeInfo.Haskell) 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 :: (Generic t, Typeable t, GDataConstructor (Rep t)) => Proxy t -> SumType Haskell data DataConstructor (lang :: Language) DataConstructor :: !Text -> !(Either [TypeInfo lang] [RecordEntry lang]) -> DataConstructor [_sigConstructor] :: DataConstructor -> !Text [_sigValues] :: DataConstructor -> !(Either [TypeInfo lang] [RecordEntry lang]) data RecordEntry (lang :: Language) RecordEntry :: !Text -> !(TypeInfo lang) -> RecordEntry [_recLabel] :: RecordEntry -> !Text [_recValue] :: RecordEntry -> !(TypeInfo lang) getUsedTypes :: SumType lang -> [TypeInfo lang] constructorToType :: DataConstructor lang -> [TypeInfo lang] -> [TypeInfo lang] sigConstructor :: Lens' (DataConstructor lang_aftf) Text sigValues :: Lens (DataConstructor lang_aftf) (DataConstructor lang_agAd) (Either [TypeInfo lang_aftf] [RecordEntry lang_aftf]) (Either [TypeInfo lang_agAd] [RecordEntry lang_agAd]) -- | 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 :: Lens' (RecordEntry lang_afte) Text recValue :: Lens (RecordEntry lang_afte) (RecordEntry lang_agBt) (TypeInfo lang_afte) (TypeInfo lang_agBt) instance GHC.Show.Show (Language.PureScript.Bridge.SumType.SumType lang) instance GHC.Show.Show (Language.PureScript.Bridge.SumType.DataConstructor 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 -> TypeInfo PureScript -> Text sumTypesToModules :: Modules -> [SumType PureScript] -> Modules sumTypeToModule :: SumType PureScript -> Modules -> Modules typesToImportLines :: ImportLines -> [TypeInfo PureScript] -> ImportLines typeToImportLines :: TypeInfo PureScript -> 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 Haskell TypeInfo with -- ^== or doCheck, you have choice (<|>), you -- can fail (empty) and you can return a translated PureScript -- TypeInfo (return). The Haskell TypeInfo can be -- accessed with: -- --
-- view haskType ---- -- Find usage examples in Language.PureScript.Bridge.Primitives -- and Language.PureScript.Bridge.PSTypes module Language.PureScript.Bridge.Builder type FullBridge = TypeInfo Haskell -> TypeInfo PureScript -- | Bridges to use when a BridgePart returns Nothing. type FixUpBridge = FullBridge data BridgeData fullBridge :: Lens' BridgeData FullBridge data BridgeBuilder a type BridgePart = BridgeBuilder (TypeInfo PureScript) -- | 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. In this case -- it is highly recommended that you build your custom FixUpBridge -- from BridgePart with buildBridgeWithCustomFixUp too, -- with FixUpBridge being finally errorFixUp. This way you -- get all the builder convenience and proper bridging of -- typeParameters. For an example have a look at the -- implementation of clearPackageFixup. clearPackageFixUp :: FixUpBridge -- | A FixUpBridge which calles error when used. Usage: -- --
-- buildBridgeWithCustomFixUp errorFixUp yourBridge --errorFixUp :: FixUpBridge -- | Build a bridge. -- -- This is a convenience wrapper for buildBridgeWithCustomFixUp. -- -- Definition: -- --
-- buildBridgeWithCustomFixUp clearPackageFixUp --buildBridge :: BridgePart -> FullBridge -- | Takes a constructed BridgePart and makes it a total function -- (FullBridge) by using the supplied FixUpBridge when -- BridgePart returns Nothing. -- -- The supplied BridgePart also gets passed through -- fixTypeParameters in order to support translation of type -- constructors. buildBridgeWithCustomFixUp :: FixUpBridge -> BridgePart -> FullBridge -- | Do some check on properties of haskType. doCheck :: Getter (TypeInfo Haskell) a -> (a -> Bool) -> BridgeBuilder () -- | 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 (TypeInfo Haskell) a -> a -> 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 :: BridgeBuilder [TypeInfo PureScript] -- | Translate types that come from any module named -- Something.TypeParameters to lower case: -- -- Also drop the 1 at the end if present. This method gets called by -- buildBridge and buildBridgeWithCustomFixUp for you - you should -- not need to call it. -- -- It enables you to even bridge type constructor definitions, see -- Language.PureScript.Bridge.TypeParameters for more details. fixTypeParameters :: BridgePart -> BridgePart 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 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 :: BridgePart psBool :: TypeInfo PureScript -- | Uses type parameters from haskType (bridged). psEither :: BridgePart psInt :: TypeInfo PureScript -- | Uses type parameters from haskType (bridged). psMaybe :: BridgePart psString :: TypeInfo PureScript -- | Uses type parameters from haskType (bridged). psTuple :: BridgePart psUnit :: TypeInfo PureScript module Language.PureScript.Bridge.Primitives boolBridge :: BridgePart eitherBridge :: BridgePart 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 :: TypeInfo Haskell -> 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 (buildBridge defaultBridge) "path/to/your/purescript/project" myTypes ---- -- You can define your own type bridges based on defaultBridge: -- --
-- myBridge = defaultBridge <|> mySpecialTypeBridge ---- -- and use it with writePSTypes: -- --
-- writePSTypes (buildBridge myBridge) "path/to/your/purescript/project" myTypes ---- -- Find examples for implementing your own bridges in: -- Language.PureScript.Bridge.Primitives. -- --