packages feed

hs2ats (empty) → 0.1.0.0

raw patch · 10 files changed

+281/−0 lines, 10 filesdep +basedep +casesdep +criterionsetup-changed

Dependencies added: base, cases, criterion, haskell-src-exts, hs2ats, hspec, hspec-dirstream, language-ats, lens, optparse-generic, system-filepath, text

Files

+ LICENSE view
@@ -0,0 +1,11 @@+Copyright Vanessa McHale (c) 2018++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. 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.++3. Neither the name of the copyright holder nor the names of its 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.
+ README.md view
@@ -0,0 +1,4 @@+# hs2ats++This is a tool to convert Haskell types to ATS types. So far it works quite+well, but documentation and error messages are nearly nonexistent.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ app/Main.hs view
@@ -0,0 +1,6 @@+module Main where++import Language.ATS.Generate (exec)++main :: IO ()+main = exec
+ bench/Bench.hs view
@@ -0,0 +1,12 @@+module Main where++import           Criterion.Main+import           Language.ATS.Generate++main :: IO ()+main =+    defaultMain [ env file $ \f ->+                  bgroup "generateATS"+                      [ bench "test/data/SumType.hs" $ nf generateATS f ]+                ]+    where file = readFile "test/data/SumType.hs"
+ cabal.project.local view
@@ -0,0 +1,7 @@+constraints: hs2ats +development+with-compiler: ghc-8.2.2+tests: True+benchmarks: True+documentation: True+haddock-hoogle: True+haddock-internal: True
+ hs2ats.cabal view
@@ -0,0 +1,87 @@+name:                hs2ats+version:             0.1.0.0+synopsis:            Create ATS types from Haskell types+description:         This package enables scanning Haskell source files for data types and then generating [ATS](http://www.ats-lang.org/) types from them.+homepage:            https://github.com/vmchale/hs2ats#readme+license:             BSD3+license-file:        LICENSE+author:              Vanessa McHale+maintainer:          vamchale@gmail.com+copyright:           Copyright: (c) 2018 Vanessa McHale+--category:            Web+build-type:          Simple+extra-doc-files:     README.md+extra-source-files:  stack.yaml+                   , cabal.project.local+cabal-version:       1.18++Flag development {+  Description: Enable `-Werror`+  manual: True+  default: False+}++library+  hs-source-dirs:      src+  exposed-modules:     Language.ATS.Generate+  build-depends:       base >= 4.7 && < 5+                     , haskell-src-exts+                     , language-ats+                     , text+                     , cases+                     , lens+                     , optparse-generic+  default-language:    Haskell2010+  if flag(development)+    ghc-options:       -Werror+  if impl(ghc >= 8.0)+    ghc-options:       -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+  ghc-options:         -Wall++executable hs2ats+  hs-source-dirs:      app+  main-is:             Main.hs+  ghc-options:         -threaded -rtsopts -with-rtsopts=-N+  build-depends:       base+                     , hs2ats+  default-language:    Haskell2010+  if flag(development)+    ghc-options:       -Werror+  if impl(ghc >= 8.0)+    ghc-options:       -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+  ghc-options:         -Wall++test-suite hs2ats-test+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             Spec.hs+  build-depends:       base+                     , hs2ats+                     , hspec+                     , hspec-dirstream+                     , system-filepath+                     , language-ats+  if flag(development)+    ghc-options:       -Werror+  if impl(ghc >= 8.0)+    ghc-options:       -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+  ghc-options:         -threaded -rtsopts -with-rtsopts=-N -Wall+  default-language:    Haskell2010++benchmark hs2ats-bench+  type:                exitcode-stdio-1.0+  hs-source-dirs:      bench+  main-is:             Bench.hs+  build-depends:       base+                     , hs2ats+                     , criterion+  if flag(development)+    ghc-options:       -Werror+  if impl(ghc >= 8.0)+    ghc-options:       -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+  ghc-options:         -Wall+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/vmchale/hs2ats
+ src/Language/ATS/Generate.hs view
@@ -0,0 +1,127 @@+{-# LANGUAGE DataKinds         #-}+{-# LANGUAGE DeriveAnyClass    #-}+{-# LANGUAGE DeriveGeneric     #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE PatternSynonyms   #-}+{-# LANGUAGE TypeOperators     #-}++module Language.ATS.Generate+    ( exec+    , generateATS+    ) where++import           Cases                        (snakify)+import           Control.Lens                 (over, _head)+import           Data.Char                    (toUpper)+import           Data.Maybe                   (fromJust)+import qualified Data.Text                    as T+import           Language.ATS                 as ATS+import           Language.Haskell.Exts.Parser+import           Language.Haskell.Exts.Syntax as HS+import           Options.Generic++data Program = Program { src    :: FilePath <?> "Haskell source file"+                       , target :: FilePath <?> "ATS target"+                       } deriving (Generic, ParseRecord)++convertConventions :: String -> String+convertConventions = T.unpack . snakify . T.pack++pattern QNamed :: l -> l -> String -> QName l+pattern QNamed x y s = UnQual x (Ident y s)++pattern EmptyQualCon :: l -> ConDecl l -> QualConDecl l+pattern EmptyQualCon x cd = QualConDecl x Nothing Nothing cd++qnameToString :: QName a -> String+qnameToString (QNamed _ _ s) = convertConventions s+qnameToString _              = undefined++toStringATS' :: QName a -> ATS.Type+toStringATS' (QNamed _ _ "Int")     = ATS.Int+toStringATS' (QNamed _ _ "Float")   = ATS.Float+toStringATS' (QNamed _ _ "Integer") = Named (Unqualified "Intinf")+toStringATS' (QNamed _ _ "String")  = Named (Unqualified "Strptr0")+toStringATS' (QNamed _ _ "Bool")    = ATS.Bool+toStringATS' _                      = undefined++-- TODO warn on un-banged types+-- TODO built-in constructors (particularly lists)+typeToType :: HS.Type a -> ATS.Type+typeToType (TyCon _ qn)                      = toStringATS' qn+typeToType (TyVar _ n)                       = Named $ Unqualified (toStringATS n)+typeToType (TyApp _ (TyCon _ qn) t'@TyCon{}) = Dependent (Unqualified $ qnameToString qn) [typeToType t']+typeToType (TyApp _ t@TyApp{} t'@TyCon{})    = over typeCallArgs (typeToType t':) $ typeToType t+typeToType (TyParen _ t)                     = typeToType t+typeToType (TyBang _ _ _ t)                  = typeToType t+typeToType (TyList _ t)                      = Dependent (Unqualified "List_vt") [typeToType t]+typeToType _                                 = undefined++-- TODO allow multiple+fieldDeclToType :: FieldDecl a -> (String, ATS.Type)+fieldDeclToType (FieldDecl _ [n] t) = (toStringATS n, typeToType t)+fieldDeclToType _                   = undefined++conDeclToType :: ConDecl a -> (String, Maybe ATS.Type)+conDeclToType (ConDecl _ n [])  = (toStringATS n, Nothing)+conDeclToType (ConDecl _ n [t]) = (toStringATS n, Just $ typeToType t)+conDeclToType (RecDecl _ n fs)  = (toStringATS n, Just $ AnonymousRecord undefined (fieldDeclToType <$> reverse fs))+conDeclToType _                 = undefined++toStringATS :: HS.Name a -> String+toStringATS (Ident _ s) = s+toStringATS _           = undefined++tyvarToArg :: TyVarBind a -> Arg+tyvarToArg (UnkindedVar _ n) = Arg (Both (toStringATS n) (Vt0p Plus))+tyvarToArg _                 = undefined++asATSName :: DeclHead a -> (String, [Arg])+asATSName (DHead _ n)    = (convertConventions $ toStringATS n, [])+asATSName (DHParen _ d)  = (fst $ asATSName d, [])+asATSName (DHApp _ d tb) = (fst $ asATSName d, tyvarToArg tb : snd (asATSName d))+asATSName _              = undefined++qualConDeclToType :: QualConDecl a -> ATS.Type+qualConDeclToType (EmptyQualCon _ cd) = fromJust $ snd $ conDeclToType cd+qualConDeclToType _                   = undefined++qualConDeclToLeaf :: QualConDecl a -> Leaf+qualConDeclToLeaf (EmptyQualCon _ cd) = Leaf [] (over _head toUpper $ convertConventions $ fst $ conDeclToType cd) [] (snd $ conDeclToType cd)+qualConDeclToLeaf _                   = undefined++asATSType :: Decl a -> Declaration+asATSType (DataDecl _ NewType{} _ dh [qcd] _)  = ViewTypeDef undefined (fst $ asATSName dh) (snd $ asATSName dh) (qualConDeclToType qcd)+asATSType (DataDecl _ DataType{} _ dh [qcd] _) = ViewTypeDef undefined (fst $ asATSName dh) (snd $ asATSName dh) (qualConDeclToType qcd)+asATSType (DataDecl _ DataType{} _ dh qcds _)  = SumViewType (fst $ asATSName dh) (snd $ asATSName dh) (qualConDeclToLeaf <$> reverse qcds)+asATSType _                                    = undefined++-- TODO GDataDecl and DataFamDecl+isDataDecl :: Decl a -> Bool+isDataDecl DataDecl{} = True+isDataDecl _          = False++-- TODO imports+filterModule :: Module a -> [Decl a]+filterModule (Module _ _ _ _ ds) = filter isDataDecl ds+filterModule _                   = []++-- #include "contrib/atscntrb-hx-intinf/mylibies.hats"++modulePrint :: Module a -> String+modulePrint = printATS . ATS . reverse . fmap asATSType . filterModule++generateATS :: String -> String+generateATS hsSrc = modulePrint $ case parseModule hsSrc of+    ParseOk x           -> x+    ParseFailed loc msg -> error (show loc ++ "\n" ++ msg)++withFile :: FilePath -> FilePath -> IO ()+withFile p p' = do+    contents <- readFile p+    writeFile p' (generateATS contents)++exec :: IO ()+exec = do+    x <- getRecord "Generate ATS types for Haskell source code" :: IO Program+    withFile (unHelpful . src $ x) (unHelpful . target $ x)
+ stack.yaml view
@@ -0,0 +1,9 @@+---+resolver: lts-10.4+packages:+  - '.'+extra-deps: []+flags:+  hs2ats:+    development: false+extra-package-dbs: []
+ test/Spec.hs view
@@ -0,0 +1,16 @@+{-# LANGUAGE OverloadedStrings #-}++import qualified Filesystem.Path.CurrentOS as F+import           Language.ATS.Generate+import           Test.Hspec+import           Test.Hspec.Dirstream++-- process test/data/Pair.out w/ patscc -dd++isATS :: F.FilePath -> Bool+isATS x = (extension x `elem`) (pure <$> ["hs", "hsig", "hs-boot", "x", "y"])++main :: IO ()+main = hspec $+    describe "generateATS" $ parallel $+        testFiles "test/data" isATS ((pure :: a -> Either String a) . generateATS)