@@ -23,15 +23,13 @@ module Language.PureScript.CodeGen.JS (
23 identNeedsEscaping
23 identNeedsEscaping
26−import Data.Maybe (catMaybes, fromJust, fromMaybe)
26+import Data.Maybe (catMaybes, fromJust)
27import Data.Function (on)
27import Data.Function (on)
28import Data.List (nub, (\\), delete, sortBy)
28import Data.List (nub, (\\), delete, sortBy)
30import Control.Monad (replicateM, forM)
30import Control.Monad (replicateM, forM)
31import Control.Applicative
31import Control.Applicative
33−import qualified Data.Map as M
35import Language.PureScript.Names
33import Language.PureScript.Names
36import Language.PureScript.Declarations
34import Language.PureScript.Declarations
37import Language.PureScript.Options
35import Language.PureScript.Options
@@ -407,49 +405,3 @@ binderToJs m e varName done (NamedBinder ident binder) = do
407 return (JSVariableIntroduction (identToJs ident) (Just (JSVar varName)) : js)
405 return (JSVariableIntroduction (identToJs ident) (Just (JSVar varName)) : js)
408binderToJs m e varName done (PositionedBinder _ binder) =
406binderToJs m e varName done (PositionedBinder _ binder) =
409 binderToJs m e varName done binder
407 binderToJs m e varName done binder
412−-- Finds the value stored for a data constructor in the current environment.
413−-- This is a partial function, but if an invalid type has reached this far then
414−-- something has gone wrong in typechecking.
416−lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type)
417−lookupConstructor e ctor = fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors e
420−-- Checks whether a data constructor is the only constructor for that type, used
421−-- to simplify the check when generating code for binders.
423−isOnlyConstructor :: Environment -> Qualified ProperName -> Bool
424−isOnlyConstructor e ctor = numConstructors (ctor, lookupConstructor e ctor) == 1
426− numConstructors :: (Qualified ProperName, (DataDeclType, ProperName, Type)) -> Int
427− numConstructors ty = length $ filter (((==) `on` typeConstructor) ty) $ M.toList $ dataConstructors e
428− typeConstructor :: (Qualified ProperName, (DataDeclType, ProperName, Type)) -> (ModuleName, ProperName)
429− typeConstructor (Qualified (Just moduleName) _, (_, tyCtor, _)) = (moduleName, tyCtor)
430− typeConstructor _ = error "Invalid argument to isOnlyConstructor"
433−-- Checks whether a data constructor is for a newtype.
435−isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
436−isNewtypeConstructor e ctor = case lookupConstructor e ctor of
437− (Newtype, _, _) -> True
438− (Data, _, _) -> False
441−-- Checks the number of arguments a data constructor accepts.
443−getConstructorArity :: Environment -> Qualified ProperName -> Int
444−getConstructorArity e = go . (\(_, _, ctors) -> ctors) . lookupConstructor e
447− go (TypeApp (TypeApp f _) t) | f == tyFunction = go t + 1
448− go (ForAll _ ty _) = go ty
452−-- Checks whether a data constructor has no arguments, for example, `Nothing`.
454−isNullaryConstructor :: Environment -> Qualified ProperName -> Bool
455−isNullaryConstructor e = (== 0) . getConstructorArity e