@@ -23,15 +23,13 @@ module Language.PureScript.CodeGen.JS (
2323 identNeedsEscaping
2424) where
2525
26−import Data.Maybe (catMaybes, fromJust, fromMaybe)
26+import Data.Maybe (catMaybes, fromJust)
2727import Data.Function (on)
2828import Data.List (nub, (\\), delete, sortBy)
2929
3030import Control.Monad (replicateM, forM)
3131import Control.Applicative
3232
33−import qualified Data.Map as M
34−
3533import Language.PureScript.Names
3634import Language.PureScript.Declarations
3735import Language.PureScript.Options
@@ -407,49 +405,3 @@ binderToJs m e varName done (NamedBinder ident binder) = do
407405 return (JSVariableIntroduction (identToJs ident) (Just (JSVar varName)) : js)
408406binderToJs m e varName done (PositionedBinder _ binder) =
409407 binderToJs m e varName done binder
410−
411−-- |
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.
415−--
416−lookupConstructor :: Environment -> Qualified ProperName -> (DataDeclType, ProperName, Type)
417−lookupConstructor e ctor = fromMaybe (error "Data constructor not found") $ ctor `M.lookup` dataConstructors e
418−
419−-- |
420−-- Checks whether a data constructor is the only constructor for that type, used
421−-- to simplify the check when generating code for binders.
422−--
423−isOnlyConstructor :: Environment -> Qualified ProperName -> Bool
424−isOnlyConstructor e ctor = numConstructors (ctor, lookupConstructor e ctor) == 1
425− where
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"
431−
432−-- |
433−-- Checks whether a data constructor is for a newtype.
434−--
435−isNewtypeConstructor :: Environment -> Qualified ProperName -> Bool
436−isNewtypeConstructor e ctor = case lookupConstructor e ctor of
437− (Newtype, _, _) -> True
438− (Data, _, _) -> False
439−
440−-- |
441−-- Checks the number of arguments a data constructor accepts.
442−--
443−getConstructorArity :: Environment -> Qualified ProperName -> Int
444−getConstructorArity e = go . (\(_, _, ctors) -> ctors) . lookupConstructor e
445− where
446− go :: Type -> Int
447− go (TypeApp (TypeApp f _) t) | f == tyFunction = go t + 1
448− go (ForAll _ ty _) = go ty
449− go _ = 0
450−
451−-- |
452−-- Checks whether a data constructor has no arguments, for example, `Nothing`.
453−--
454−isNullaryConstructor :: Environment -> Qualified ProperName -> Bool
455−isNullaryConstructor e = (== 0) . getConstructorArity e