packages feed

clafer-0.5.8: src/Language/Clafer.hs

{-# LANGUAGE NamedFieldPuns     #-}
{-
 Copyright (C) 2012-2017 Kacper Bak, Jimmy Liang, Michal Antkiewicz <http://gsd.uwaterloo.ca>

 Permission is hereby granted, free of charge, to any person obtaining a copy of
 this software and associated documentation files (the "Software"), to deal in
 the Software without restriction, including without limitation the rights to
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 of the Software, and to permit persons to whom the Software is furnished to do
 so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
-}
{- | Top-level interface to the Clafer compiler

Normal usage:

> t :: InputModel -> InputModel -> Either [ClaferErr] [String]
> t a b =
>   runClafer defaultClaferArgs $
>     do
>       addModuleFragment a
>       addModuleFragment b
>       parse
>       iModule <- desugar ""
>       compile iModule
>       generate

Example of compiling a model consisting of one fragment:

> compileOneFragment :: ClaferArgs -> InputModel -> Either ClaferErr CompilerResult
> compileOneFragment args model =
>   runClafer args $
>     do
>       addModuleFragment model
>       parse
>       iModule <- desugar "http://mydomain.org/mymodel.cfr"
>       compile iModule
>       generate

> compileTwoFragments :: ClaferArgs -> InputModel -> InputModel -> Either ClaferErr [String]
> compileTwoFragments args frag1 frag2 =
>   runClafer args $
>    do
>      addModuleFragment frag1
>      addModuleFragment frag2
>      parse
>      iModule <- desugar ""
>      compile iModule
>      generate

Use "throwErr" to halt execution:

> runClafer args $
>   when (notValid args) $ throwErr (ClaferErr "Invalid arguments.")

Use "catchErrs" to catch the errors.
-}
module Language.Clafer
  ( runCompiler
  , addModuleFragment
  , compile
  , parse
  , desugar
  , generate
  , generateHtml
  , runClaferT
  , runClafer
  , ClaferErr
  , getEnv
  , putEnv
  , CompilerResult(..)
  , InputModel
  , Token
  , Module
  , GEnv
  , IModule
  , ClaferEnv(..)
  , getIr
  , getAst
  , makeEnv
  , Pos(..)
  , IrTrace(..)
  , module Language.Clafer.ClaferArgs
  , module Language.Clafer.Front.ErrM
  ) where

import           Control.Lens.Plated
import           Control.Monad
import           Control.Monad.State
import           Data.Aeson
import qualified Data.Aeson.Key as Key
import qualified Data.Aeson.KeyMap as KeyMap
import           Data.Data.Lens
import           Data.Either
import           Data.List
import qualified Data.Map as Map
import           Data.Maybe
import           Data.String.Conversions
import           System.Exit
import           System.FilePath (dropExtension, takeBaseName)
import           System.IO
import           System.Process (readProcessWithExitCode, system)
import           System.Environment (getExecutablePath)

import           Language.Clafer.ClaferArgs hiding (Clafer)
import qualified Language.Clafer.ClaferArgs  as Mode (ClaferMode (Clafer))
import           Language.Clafer.Comments
import           Language.Clafer.Common
import qualified Language.Clafer.Css as Css
import           Language.Clafer.Front.AbsClafer
import           Language.Clafer.Front.ErrM
import           Language.Clafer.Front.LayoutResolver
import           Language.Clafer.Front.LexClafer
import           Language.Clafer.Front.ParClafer
import           Language.Clafer.Front.PrintClafer
import           Language.Clafer.Generator.Alloy
import           Language.Clafer.Generator.AlloyLtl
import           Language.Clafer.Generator.AlloyNames (alloyNames, toAlloyNames)
import           Language.Clafer.Generator.Choco
import           Language.Clafer.Generator.Concat
import           Language.Clafer.Generator.Explicit
import           Language.Clafer.Generator.Graph
import           Language.Clafer.Generator.Html
import           Language.Clafer.Generator.Stats
import           Language.Clafer.Import
import           Language.Clafer.Intermediate.Desugarer
import           Language.Clafer.Intermediate.Intclafer
import           Language.Clafer.Intermediate.Resolver
import           Language.Clafer.Intermediate.ResolverType (redundantExps)
import           Language.Clafer.Intermediate.ScopeAnalysis
import           Language.Clafer.Intermediate.StringAnalyzer
import           Language.Clafer.Intermediate.Tracing
import           Language.Clafer.Intermediate.Transformer
import           Language.Clafer.JSONMetaData
import           Language.Clafer.Optimizer.Optimizer
import           Language.Clafer.QNameUID
import           Language.ClaferT

type InputModel = String

-- | Run the Clafer compiler on the files of a model, as read by 'readModel': the imported
-- files and the model, compiled together.
-- mURL = Nothing means compile the top-level module
-- mURL = Just url means compile an imported module from the given url
runCompiler :: Maybe URL -> ClaferArgs -> [ModelFile] -> IO ()
runCompiler    mURL         args'         modelFiles =
  do
    hSetBuffering stdout LineBuffering
    result <- runClaferT args' $
      do
        forM_ (fragments inputModel) addModuleFragment
        modifyEnv $ \env -> env{inputFiles = modelFiles}
        parse
        iModule <- desugar mURL
        {- need to runCompiler on imports
        liftIO $ do
          forM_ (_mModules iModule) $ \url -> do
            -- use the same args just change the file name
            importedModel <- retrieveModelFromURL url
            runCompiler (Just url) (args' { file = getFileName url }) importedModel
        -}
        compile iModule

        fs <- save args'

        when (validate args') $ liftIO $ do
            putStrLn $ "[clafer]              Validating " ++ file args'

        when (validate args') $ liftIO $ do
          failed <- concat <$> forM fs (runValidate args')
          putStrLn "\n"
          -- an output that its tool rejects is an error, as a compile error is
          unless (null failed) $ do
            putStrLn $ "[clafer]              Validation failed: " ++ intercalate ", " failed
            exitFailure
    when (Html `elem` mode args') $
      htmlCatch result args' inputModel
    result `cth` handleErrs
  where
  inputModel = modelText modelFiles
  cth (Left err) f = f err
  cth (Right r)  _ = return r
  fragments model = map unlines $ fragments' $ lines model
  fragments' []                  = []
  fragments' ("//# FRAGMENT":xs) = fragments' $ " " : xs
  fragments' model               = takeWhile (/= "//# FRAGMENT") model : fragments' (dropWhile (/= "//# FRAGMENT") model)
--  htmlCatch :: Either ClaferErr CompilerResult -> ClaferArgs -> String -> IO(CompilerResult)
  htmlCatch (Right r) _ _ = return r
  htmlCatch (Left err) args'' model =
    do let f = dropExtension (file args'') ++ ".html"
       let result = (if (self_contained args'')
                     then Css.header ++ "<style>" ++ Css.css ++ "</style>" ++ "</head>\n<body>\n<pre>\n"
                     else "")
                     ++ highlightErrorsIn modelFiles model err ++
                     (if (self_contained args'')
                      then "\n</pre>\n</html>"
                      else "")
       liftIO $ if console_output args'' then putStrLn result else writeFile f result

  handleErrs = mapM_ handleErr

  handleErr (ClaferErr mesg) =
    do
      putStrLn "\nError...\n"
      putStrLn mesg
      exitFailure
  -- We only use one fragment. Fragment id and position is not useful to us. We
  -- only care about the position relative to
  handleErr (ParseErr ErrPos{modelPos = Pos l c} mesg) =
    do
      putStrLn $ "\nParse failed at " ++ errorPosition l c ++ "..."
      putStrLn mesg
      exitFailure
  handleErr (SemanticErr ErrPos{modelPos = Pos l c} mesg) =
    do
      putStrLn $ "\nCompile error at " ++ errorPosition l c ++ "..."
      putStrLn mesg
      exitFailure
  -- the file and the position in it: "file.cfr:3:5"
  errorPosition l c = let (path, l') = modelPosition modelFiles l in path ++ ":" ++ show l' ++ ":" ++ show c

save :: MonadIO m => ClaferArgs -> ClaferT m [ String ]
save args'=
  do
    resultsMap <- generate
    let results = snd $ unzip $ Map.toList resultsMap
    -- print stats only once
    unless (no_stats args') $ liftIO $ printStats results
    -- save the outputs
    (iModule, genv, _) <- getIr
    forM results $ saveResult (iModule, genv) resultsMap
  where
    -- saveResult :: MonadIO m => CompilerResult -> IModule -> (Map.Map ClaferMode CompilerResult) -> ClaferT m String
    saveResult (iModule', genv') resultsMap' result@CompilerResult { extension, scopesList } = do
      result' <- if (add_graph args') && (Html `elem` (mode args') && ("dot" `isSuffixOf` (extension)))
            then do
                   ast' <- getAst
                   (_, graph, _) <- liftIO $ readProcessWithExitCode "dot"  ["-Tsvg"] $ genSimpleGraph ast' iModule' (takeBaseName $ file args') (show_references args')
                   return $ summary graph result
            else return result
      let f = dropExtension $ file args'
      let f' = f ++ "." ++ if extension == "tmp.als" then "als" else extension
      liftIO $ if console_output args' then putStrLn (outputCode result') else writeFile f' (outputCode result')
      liftIO $ when (alloy_mapping args') $ writeFile (f ++ ".map") $ show (mappingToAlloy result')
      let
        qNameMaps :: QNameMaps
        qNameMaps = deriveQNameMaps iModule'
      liftIO $ when (meta_data args') $ writeFile (f ++ ".cfr-map") $ generateJSONnameUIDMap qNameMaps
      liftIO $ when (meta_data args' && inScopeModes) $ writeFile (f ++ ".cfr-scope") $ generateJSONScopes qNameMaps $ getScopesList resultsMap'
      -- the explicit form's scope file: every scope chocosolver solves it with
      liftIO $ when (meta_data args' && extension == "des.cfr") $ writeFile (f ++ ".des.cfr-scope") $
        snd $ genExplicitModule (iModule', genv') scopesList (minint args', maxint args') (decimal_digits args') Nothing
      return f'
    saveResult _ _ NoCompilerResult { reason } = do
      liftIO $ putStrLn reason
      return ""
    printStats :: [CompilerResult] -> IO ()
    printStats []         = putStrLn "No compiler output."
    printStats (r:rs) = case r of
      CompilerResult { statistics } -> putStrLn statistics
      (NoCompilerResult _) -> printStats rs

    inScopeModes :: Bool
    inScopeModes =
      Alloy `elem` mode args' ||
      AlloyLtl `elem` mode args' ||
      Choco `elem` mode args'

    getScopesList :: Map.Map ClaferMode CompilerResult -> [(UID, Integer)]
    getScopesList    resultsMap =
        let
           alloyResult = Map.lookup Alloy resultsMap
           chocoResult = Map.lookup Choco resultsMap
        in
           if (isNothing alloyResult)
           then if (isNothing chocoResult)
                then []
                else scopesList $ fromJust chocoResult
           else scopesList $ fromJust alloyResult

summary :: String -> CompilerResult -> CompilerResult
summary graph result@CompilerResult{} = result{outputCode=unlines $ summary' graph ("<pre>" ++ statistics result ++ "</pre>") (lines $ outputCode result)}
summary _     noResult = noResult

summary' :: String -> String -> [String] -> [String]
summary' _ _ [] = []
summary' graph stats ("<!-- # SUMMARY /-->":xs) = graph:stats:summary' graph stats xs
summary' graph stats ("<!-- # STATS /-->":xs) = stats:summary' graph stats xs
summary' graph stats ("<!-- # GRAPH /-->":xs) = graph:summary' graph stats xs
summary' graph stats ("<!-- # CVLGRAPH /-->":xs) = graph:summary' graph stats xs
summary' graph stats (x:xs) = x:summary' graph stats xs

-- | Validates an output with its tool; returns the output if the tool rejects it. A counterexample
--   is not a failure: the tool exits with 0.
runValidate :: ClaferArgs -> String -> IO [String]
runValidate args' fo
  | Alloy `elem` modes && ".als" `isSuffixOf` fo = do
    putStrLn ("=========== Running the commands of " ++ fo ++ " =============")
    run $ validateAlloy path ++ fo
  | Choco `elem` modes && ".js" `isSuffixOf` fo =
    run $ validateChoco path ++ fo
  | Graph `elem` modes && ".dot" `isSuffixOf` fo = do
    putStrLn ("=========== Parsing+Generating   " ++ fo ++ " =============")
    run $ validateGraph ++ fo
  | Mode.Clafer `elem` modes && ".des.cfr" `isSuffixOf` fo = do
    putStrLn ("=========== Compiling the explicit model " ++ fo ++ " =============")
    validateClafer fo
  | otherwise = return []
  where
  path = tooldir args' ++ "/"
  modes = mode args'
  run command = do
    code <- system command
    return [ fo | code /= ExitSuccess ]

-- | The explicit model compiles to itself: clafer -m clafer on it prints it (its first line,
--   //# OPTIONS --explicit, gives the options).
validateClafer :: FilePath -> IO [String]
validateClafer fo = do
  clafer' <- getExecutablePath
  explicit <- readFile fo
  (code, out, err) <- readProcessWithExitCode clafer' ["-s", "-m", "clafer", "-o", fo] ""
  case code of
    -- the console output ends with one more newline
    ExitSuccess | dropWhileEnd (== '\n') out == dropWhileEnd (== '\n') explicit -> return []
                | otherwise       -> do
                    putStrLn $ "The explicit model does not compile to itself: " ++ fo
                    return [fo]
    _ -> do
      putStrLn $ out ++ err
      return [fo]

-- | Runs all commands (the default run command if there are none) and prints the instances and counterexamples.
validateAlloy :: String -> String
validateAlloy path = "java --enable-native-access=ALL-UNNAMED -jar \"" ++ path ++ "alloy6.2.0.jar\" exec -c \"*\" -t table -o - "

validateChoco :: String -> String
validateChoco path = "java -jar \"" ++ path ++ "chocosolver.jar\" -v --file "

validateGraph :: String
validateGraph = "dot -Tsvg -O "



-- | Add a new fragment to the model. Fragments should be added in order.
addModuleFragment :: Monad m => InputModel -> ClaferT m ()
addModuleFragment i =
  do
    env <- getEnv
    let modelFrags' = modelFrags env ++ [i]
    let frags' = frags env ++ [endPos $ concat modelFrags']
    putEnv env{ modelFrags = modelFrags', frags = frags' }
  where
  endPos "" = Pos 1 1
  endPos model =
    Pos line' column'
    where
    input' = lines' model
    line' = toInteger $ length input'
    column' = 1 + toInteger (length $ last input')
    -- Can't use the builtin lines because it ignores the last empty lines (as of base 4.5).
    lines' "" = [""]
    lines' input'' =
      line'' : rest'
      where
      (line'', rest) = break (== '\n') input''
      rest' =
        case rest of
          "" -> []
          ('\n' : r) -> lines' r
          x -> error $ "linesing " ++ x -- How can it be nonempty and not start with a newline after the break? Should never happen.

-- | Converts the Err monads (created by the BNFC parser generator) to ClaferT
liftParseErrs :: Monad m => [Err a] -> ClaferT m [a]
liftParseErrs e =
  do
    result <- zipWithM extract [0..] e
    case partitionEithers result of
      ([], ok) -> return ok
      (e',  _) -> throwErrs e'
  where
  extract _ (Ok m)  = return $ Right m
  extract frgId (Bad p s) =
    do
      -- Bad maps to ParseErr
      return $ Left $ ParseErr (ErrFragPos frgId p) s

-- | Converts one Err. liftParseErrs is better if you want to report multiple errors.
-- | This method will only report one before ceasing execution.
liftParseErr :: Monad m => Err a -> ClaferT m a
liftParseErr e = head `liftM` liftParseErrs [e]


-- | Parses the model into AST. Adding more fragments beyond this point will have no effect.
parse :: Monad m => ClaferT m ()
parse =
  do
    env <- getEnv
    let completeModel = concat $ modelFrags env

    (astErr, otherTokens') <- (parseFrag $ args env) completeModel
    ast <- liftParseErr astErr
    case sameLineClafers ast of
      (span', name') : _ -> throwErr (ParseErr span' $ "clafer " ++ name' ++ " must start on a new line" :: CErr Span)
      []                 -> return ()

    putEnv env{ cAst = Just ast, astModuleTrace = traceAstModule ast, otherTokens = otherTokens' }
  where
  -- Elements have no separator in the grammar, so `b : B A 3` parses as two
  -- sibling clafers, b : B and A 3. Returns the clafers which start on the same
  -- line as their previous sibling clafer.
  sameLineClafers :: Module -> [(Span, String)]
  sameLineClafers (Module _ decls') = concatMap sameLine $ elementLists [ e | ElementDecl _ e <- decls' ]
    where
    elementLists es = es : concatMap elementLists [ es' | ElementsList _ es' <- map nested es ]
    nested (Subclafer _ (Clafer _ _ _ _ _ _ _ _ _ _ elements')) = elements'
    nested (ClaferUse _ _ _ elements') = elements'
    nested e = ElementsEmpty $ getSpan e
    sameLine es = [ (s2, n2) | ((Span (Pos l1 _) _, _), (s2@(Span (Pos l2 _) _), n2)) <- zip clafers' (drop 1 clafers'), l1 == l2 ]
      where
      clafers' = mapMaybe claferStart es
    claferStart (Subclafer s (Clafer _ _ _ _ (PosIdent (_, n)) _ _ _ _ _ _)) = Just (s, n)
    claferStart (ClaferUse s name' _ _) = Just (s, "`" ++ printTree name')
    claferStart _ = Nothing
  parseFrag :: (Monad m) => ClaferArgs -> String    -> ClaferT m (Err Module, [Token])
  parseFrag                 args'         inputModel = do
    let model' = if (not $ no_layout args') && (new_layout args') then resLayout inputModel else inputModel
    let allTokens = myLexer model'
    let (parseableTokens, otherTokens') = partition isParseable allTokens
    tokens2 <- if not ((new_layout args') || (no_layout args')) then resolveLayout otherTokens' parseableTokens else return parseableTokens
    return (pModule tokens2, otherTokens')
    where
      isParseable (PT _ (T_PosLineComment _))  = False
      isParseable (PT _ (T_PosBlockComment _)) = False
      isParseable (PT _ (T_PosAlloy _))        = False
      isParseable (PT _ (T_PosChoco _))        = False
      isParseable _                            = True

desugar :: Monad m => Maybe URL -> ClaferT m IModule
desugar mURL = do
  ast' <- getAst
  return $ desugarModule mURL ast'

-- | Compiles the AST into IR.
compile :: Monad m => IModule -> ClaferT m ()
compile desugaredMod = do
    env <- getEnv
    let clafersWithKeyWords = foldMapIR isKeyWord desugaredMod
    when (""/=clafersWithKeyWords) $ throwErr (ClaferErr $ ("The model contains clafers with keywords as names in the following places:\n"++) clafersWithKeyWords :: CErr Span)
    ir <- analyze (args env) desugaredMod
    let (imodule, _, _) = ir

    let spanList = foldMapIR gt1 imodule
    when (afm (args env) && spanList/="") $ throwErr (ClaferErr $ ("The model is not an attributed feature model.\nThe following places contain cardinality larger than 1:\n"++) spanList :: CErr Span)
    let unsatisfiableGroups' = unsatisfiableGroups imodule
    unless (null unsatisfiableGroups') $ throwErrs unsatisfiableGroups'
    putEnv $ env{ cIr = Just ir }
    where
      isKeyWord :: Ir -> String
      isKeyWord (IRClafer IClafer{_cinPos = (Span (Pos l c) _) ,_ident=i}) =
        if i `elem` keywordIdents
        then "Line " ++ show l ++ " column " ++ show c ++ "\n"
        else ""
      isKeyWord _ = ""
      gt1 :: Ir -> String
      gt1 (IRClafer (IClafer (Span (Pos l c) _) IClaferModifiers{_abstract=False} _ _ _ _ _ _ (Just (_, m)) _ _)) = if m > 1 || m < 0 then "Line " ++ show l ++ " column " ++ show c ++ "\n" else ""
      gt1 _ = ""


-- | The spans of the JSON output in the files of the model:
--   @{"file": "lib.cfr", "start": {"line": 3, "column": 5}, "end": ...}@, with the lines in the file.
--   Unchanged if the files are not known.
spansInFiles :: [ModelFile] -> Value -> Value
spansInFiles [] value = value
spansInFiles files value = inFiles value
  where
  inFiles (Object o) = case (KeyMap.lookup (Key.fromString "start") o, KeyMap.lookup (Key.fromString "end") o) of
    (Just (Object start'), Just (Object end'))
      | KeyMap.size o == 2
      , Just (Number line1) <- KeyMap.lookup (Key.fromString "line") start'
      , Just (Number line2) <- KeyMap.lookup (Key.fromString "line") end' ->
        let (path, line1') = modelPosition files (round line1)
            -- the end is in the same file, relative to the start
            line2' = round line2 - round line1 + line1'
        in object [ Key.fromString "file" .= path
                  , Key.fromString "start" .= KeyMap.insert (Key.fromString "line") (toJSON line1') start'
                  , Key.fromString "end" .= KeyMap.insert (Key.fromString "line") (toJSON line2') end' ]
    _ -> Object $ fmap inFiles o
  inFiles (Array a) = Array $ fmap inFiles a
  inFiles other = other

-- | Splits the AST into their fragments, and generates the output for each fragment; each file
--   of the model starts with its name.
generateHtml :: ClaferEnv -> String
generateHtml env =
  case cAst env of
    Nothing -> ""
    (Just (Module _ decls')) ->
      let
          cargs = args env
          irMap = irModuleTrace env
          comments = if add_comments cargs then getComments $ unlines $ modelFrags env else [];
          -- the fragment markers and the names of the files, where they start
          markers = sortOn fst $ map (\frg -> (frg, fragmentMarker)) (frags env)
                                 ++ [ (Pos line' 1, printFileHeader path) | (line', path) <- fileStarts (inputFiles env) ]
      in (if self_contained cargs then Css.header ++ "<style>" ++ Css.css ++ "</style></head>\n<body>\n" else "")
        ++ unlines (genFragments decls' markers irMap comments) ++
        (if self_contained cargs then "</body>\n</html>" else "")
  where
    lne :: Declaration -> Pos
    lne (ElementDecl (Span p _) _) = p
    lne (EnumDecl (Span p _) _  _) = p
    genFragments :: [Declaration] -> [(Pos, String)] -> Map.Map Span [Ir] -> [(Span, String)] -> [String]
    genFragments []           markers _ comments = printRest markers comments
    genFragments (decl:decls') ((pos, marker):markers) irMap comments
      | pos <= lne decl = let (comments', c) = printPreComment (Span pos pos) comments in
                            [ c | not (null c) ] ++ marker : genFragments (decl:decls') markers irMap comments'
    genFragments (decl:decls') markers irMap comments = let (comments', c) = printPreComment (getSpan decl) comments in
                                                          c : (cleanOutput $ revertLayout $ printDeclaration decl 0 irMap True $ inDecl decl comments') : (genFragments decls' markers irMap $ afterDecl decl comments')
    -- the comments and the names of the files after the last declaration, in the order of the
    -- text; the fragment markers end with the last declaration
    printRest [] comments = printComments comments
    printRest ((pos, marker):markers) comments
      | marker == fragmentMarker = printRest markers comments
      | otherwise = let (before, after) = span (\(Span p _, _) -> p < pos) comments in
                      printComments before ++ marker : printRest markers after
    fragmentMarker = "<!-- # FRAGMENT /-->"
    inDecl :: Declaration -> [(Span, String)] -> [(Span, String)]
    inDecl decl comments = let s = getSpan decl in dropWhile (\x -> fst x <= s) comments
    afterDecl :: Declaration -> [(Span, String)] -> [(Span, String)]
    afterDecl decl comments = let (Span _ (Pos line' _)) = getSpan decl in dropWhile (\(x, _) -> let (Span _ (Pos line'' _)) = x in line'' <= line') comments
    printComments [] = []
    printComments ((s, comment):cs') = (snd (printComment s [(s, comment)]) ++ "<br>\n"):printComments cs'

-- | Whether the model has no decimal literals, no real or double literals, no product
--   operator and no temporal operators.
iExpBasedChecks :: IModule -> (Bool, Bool, Bool, Bool)
iExpBasedChecks iModule = (null decimalLiterals, null realLiterals, null productOperators, null tempOperators)
  where
    iexps :: [ IExp ]
    iexps = universeOn biplate iModule
    decimalLiterals = [ e | e@(IDecimal _) <- iexps ]
    realLiterals = [ e | e <- iexps, isRealOrDouble e ]
    isRealOrDouble (IReal _)   = True
    isRealOrDouble (IDouble _) = True
    isRealOrDouble _           = False
    productOperators = filter isProductOperator iexps
    tempOperators = filter isTempOperator iexps
    isProductOperator (IFunExp op' _) = op' == iProdSet
    isProductOperator _               = False

-- | Whether the model has no references to decimals, no references to reals or doubles, and
--   no temporal modifiers.
iClaferBasedChecks :: IModule -> (Bool, Bool, Bool)
iClaferBasedChecks iModule = ( not (any (hasReferenceTo [ decimalType ]) iClafers)
                             , not (any (hasReferenceTo [ realType, doubleType ]) iClafers)
                             , null tempModifiers)
  where
    iClafers :: [ IClafer ]
    iClafers = universeOn biplate iModule
    hasReferenceTo types IClafer{_reference=(Just IReference{_ref=pexp'})} = any (`elem` types) $ getRefIds pexp'
    hasReferenceTo _     _                                                 = False
    tempModifiers = filter hasTempModifier iClafers

-- | Errors for the groups of concrete clafers whose children, including the inherited ones,
--   cannot have as many instances as the lower bound of the group cardinality requires,
--   e.g., @xor@ over no children. The group cardinality may be inherited or redefined.
unsatisfiableGroups :: IModule -> [CErr Span]
unsatisfiableGroups iModule = mapMaybe groupErr iClafers
  where
    iClafers :: [ IClafer ]
    iClafers = universeOn biplate iModule
    uidIClaferMap' = createUidIClaferMap iModule
    groupErr c@IClafer{_cinPos, _ident, _gcard = Just IGCard{_interval = gcard'@(gMin, _)}}
      | not (_isAbstract c) && gMin > 0 && maybe False (< gMin) maxInstances =
          Just $ SemanticErr _cinPos $ "The group cardinality " ++ showInterval gcard' ++ " of " ++ _ident
            ++ " cannot be satisfied: its children, including the inherited ones, can have at most "
            ++ show (fromJust maxInstances) ++ " instances"
      | otherwise = Nothing
      where
        children' = getSubclafers $ concatMap _elements $ findHierarchy getSuper uidIClaferMap' c
        -- Nothing if a child has no upper bound
        maxInstances = foldM addUpper 0 children'
        addUpper n child = case _card child of
          Just (_, m) | m >= 0 -> Just (n + m)
          _                    -> Nothing
    groupErr _ = Nothing

-- | Generates outputs for the given IR.
generate :: Monad m => ClaferT m (Map.Map ClaferMode CompilerResult)
generate =
  do
    env <- getEnv
    ast' <- getAst
    (iModule, genv, au) <- getIr
    let
      (hasNoDecimalLiterals, hasNoRealLiterals, hasNoProductOperator, hasNoTempOperators) = iExpBasedChecks iModule
      (hasNoReferenceToDecimal, hasNoReferenceToReal, hasNoTempModifiers) = iClaferBasedChecks iModule
      staticClaferSubset = hasNoTempOperators && hasNoTempModifiers
      cargs = args env
      otherTokens' = otherTokens env
      modes = mode cargs
      scopes = getScopeStrategy (scope_strategy cargs) iModule
      -- the scopes are used by the Alloy and Choco outputs only
      scopeWarnings = if any (`elem` modes) [Alloy, AlloyLtl, Choco]
                      then showLargeScopes (inputFiles env) $ largeReferenceScopes iModule scopes
                      else ""
      stats = showStats au (statsModule iModule) ++ scopeWarnings ++ showRedundantExps (inputFiles env) (redundantExps iModule)
              ++ showNoClaferNavs (inputFiles env) (noClaferNavs ast')
              ++ (if skip_resolver cargs then "" else showDoubleUnderscoreNames (inputFiles env) iModule)
              ++ showFixedInitialValues (inputFiles env) (fixedInitialValues ast' iModule)

    return $ Map.fromList (
        -- result for Alloy
        (if Alloy `elem` modes
         then if hasNoDecimalLiterals && hasNoRealLiterals && hasNoReferenceToDecimal && hasNoReferenceToReal && hasNoProductOperator
              then
                 let
                    (imod,strMap) = astrModule iModule
                    useAlloyLtlGen = not staticClaferSubset || AlloyLtl `elem` modes
                    (genAlloyCode, alloyModes, fileExt) = if useAlloyLtlGen then (genAlloyLtlModule, [Alloy, AlloyLtl], "tmp.als") else (genModule, [Alloy], "als")
                    -- the clafers whose UIDs cannot be Alloy names get names of their own
                    names = alloyNames imod
                    imodA = toAlloyNames names imod
                    genvA = if Map.null names then genv else genv{uidClaferMap = createUidIClaferMap imodA}
                    scopesA = [ (Map.findWithDefault u u names, s) | (u, s) <- scopes ]
                    alloyCode = genAlloyCode cargs{mode = alloyModes} (imodA, genvA) scopesA otherTokens'
                    addCommentStats = if no_stats cargs then const else addStats
                 in
                    [ (Alloy,
                      CompilerResult {
                       extension = fileExt,
                       outputCode = addCommentStats (fst alloyCode) stats,
                       statistics = stats,
                       claferEnv  = env,
                       mappingToAlloy = fromMaybe [] (Just $ snd alloyCode),
                       stringMap = strMap,
                       scopesList = scopes
                      })
                    ]
              else [ (Alloy,
                      NoCompilerResult {
                         reason = "Alloy output unavailable because the model contains: "
                                ++ (if hasNoDecimalLiterals then "" else "a decimal literal, ")
                                ++ (if hasNoRealLiterals then "" else "a real or double literal, ")
                                ++ (if hasNoReferenceToDecimal then "" else "a reference to a decimal, ")
                                ++ (if hasNoReferenceToReal then "" else "a reference to a real or double, ")
                                ++ (if hasNoProductOperator then "" else "the product operator, ")
                        })
                     ]
          else []
        )
        -- result for JSON
        ++ (if (JSON `elem` modes)
            then [(JSON,
                  CompilerResult {
                   extension = "json",
                   outputCode = concat
                     [ "{"
                     , case gatherObjectivesAndAttributes iModule $ astModuleTrace env of
                          Nothing         -> ""
                          Just objectives -> "\"objectives\":" ++ convertString (encode $ spansInFiles (inputFiles env) $ toJSON objectives) ++ ","
                     , "\"iModule\":"
                     , convertString $ encode $ spansInFiles (inputFiles env) $ toJSON iModule
                     , "}"
                     ],
                   statistics = stats,
                   claferEnv  = env,
                   mappingToAlloy = [],
                   stringMap = Map.empty,
                   scopesList = []
                  }) ]
          else []
        )
        -- result for Clafer: the explicit form of the model, static or temporal; chocosolver
        -- solves the static ones
        ++ (if Mode.Clafer `elem` modes
          then if hasNoRealLiterals && hasNoReferenceToReal
               then [ (Mode.Clafer,
                  CompilerResult {
                   extension = "des.cfr",
                   outputCode = fst $ genExplicitModule (iModule, genv) scopes (minint cargs, maxint cargs) (decimal_digits cargs)
                                  (if staticClaferSubset then Nothing else Just (trace_len cargs)),
                   statistics = stats,
                   claferEnv  = env,
                   mappingToAlloy = [],
                   stringMap = Map.empty,
                   scopesList = scopes
                  }) ]
               else [ (Mode.Clafer,
                  NoCompilerResult {
                    reason = "Clafer output unavailable because the model contains: "
                           ++ (if hasNoRealLiterals then "" else "a real or double literal, ")
                           ++ (if hasNoReferenceToReal then "" else "a reference to a real or double. ")
                  }) ]
          else []
        )
        -- result for Html
        ++ (if Html `elem` modes
          then [ (Html,
                  CompilerResult {
                   extension = "html",
                   outputCode = generateHtml env,
                   statistics = stats,
                   claferEnv  = env,
                   mappingToAlloy = [],
                   stringMap = Map.empty,
                   scopesList = []
                  }) ]
          else []
        )
        ++ (if Graph `elem` modes
          then [ (Graph,
                  CompilerResult {
                     extension = "dot",
                     outputCode = genSimpleGraph ast' iModule (takeBaseName $ file cargs) (show_references cargs),
                     statistics = stats,
                     claferEnv  = env,
                     mappingToAlloy = [],
                     stringMap = Map.empty,
                     scopesList = []
                  }) ]
          else []
        )
        ++ (if CVLGraph `elem` modes
          then [ (CVLGraph,
                  CompilerResult {
                       extension = "cvl.dot",
                       outputCode = genCVLGraph ast' iModule (takeBaseName $ file cargs),
                       statistics = stats,
                       claferEnv  = env,
                       mappingToAlloy = [],
                       stringMap = Map.empty,
                       scopesList = []
                  }) ]
          else []
        )
        -- result for Choco
        ++ (if Choco `elem` modes
            then if hasNoRealLiterals && hasNoReferenceToReal && staticClaferSubset
                 then [(Choco,
                        CompilerResult {
                          extension = "js",
                          outputCode = genCModule (iModule, genv) scopes (minint cargs, maxint cargs) (decimal_digits cargs) otherTokens',
                          statistics = stats,
                          claferEnv  = env,
                          mappingToAlloy = [],
                          stringMap = Map.empty,
                          scopesList = scopes
                         })
                      ]
                 else [(Choco,
                        NoCompilerResult {
                          reason = if not staticClaferSubset
                            then "Choco output unavailable because the model contains temporal operators"
                            else "Choco output unavailable because the model contains: "
                                 ++ (if hasNoRealLiterals then "" else "a real or double literal, ")
                                 ++ (if hasNoReferenceToReal then "" else "a reference to a real or double. ")
                        })
                      ]
          else []
        ))

-- | Result of generation for a given mode
data CompilerResult
  = CompilerResult
    { extension      :: String                -- ^ output file extension
    , outputCode     :: String                -- ^ output text
    , statistics     :: String                -- ^ the final environment of the compiler
    , claferEnv      :: ClaferEnv             -- ^ Maps source constraint spans in Alloy to the spans in the IR
    , mappingToAlloy :: [(Span, IrTrace)]     -- ^ Map back from Ints used to represent Strings
    , stringMap      :: (Map.Map Int String)  -- ^ scopes generated by scope inference
    , scopesList     :: [(UID, Integer)]
    }
  | NoCompilerResult
    { reason :: String
    }
  deriving Show

liftError :: (Monad m, Language.ClaferT.Throwable t) => Either t a -> ClaferT m a
liftError = either throwErr return

analyze :: Monad m => ClaferArgs -> IModule -> ClaferT m (IModule, GEnv, Bool)
analyze args' iModule = do
  _ <-liftError $ findDupModule args' iModule
  let
    au = allUnique iModule
  -- With -f, the names are the UIDs, as in an explicit model: a name that cannot be a UID is an error.
  case nonUidNames iModule of
    (c : _) | skip_resolver args' -> throwErr (SemanticErr (_cinPos c) $
      "With -f (--skip-resolver), the names of the clafers are their UIDs, so they must be unique and without apostrophes, but "
      ++ _ident c ++ " is not" :: ClaferSErr)
    _ -> return ()
  (rTree, genv) <- liftError $ resolveModule args' iModule
  let tTree = transModule rTree
  return (optimizeModule args' (tTree, genv), genv, au)

addStats :: String -> String -> String
addStats code stats = "/*\n" ++ stats ++ "*/\n" ++ code

showStats :: Bool -> Stats -> String
showStats au (Stats na nr nc nconst ngoals sgl) =
  unlines [ "All clafers: " ++ (show (na + nc)) ++ " | Abstract: " ++ (show na) ++ " | Concrete: " ++ (show nc) ++ " | Reference: " ++ (show nr)
          , "Constraints: " ++ show nconst
          , "Goals: " ++ show ngoals
          , "Global scope: " ++ showInterval sgl
          , "Can skip name resolver: " ++ if au then "yes" else "no" ]

-- | Warnings about the large scopes of references with an unbounded multiplicity, at their positions in the files
showLargeScopes :: [ModelFile] -> [(IClafer, Integer, IClafer, Integer)] -> String
showLargeScopes files = unlines . map showLargeScope
  where
  showLargeScope (clafer, scope', target, targetScope) =
    let
      Span (Pos l c) _ = _cinPos clafer
      (path, l') = modelPosition files l
      name = _ident clafer
      reference = name ++ " -> " ++ _ident target
      card' = fromMaybe (0, -1) $ _card clafer
    in
      "Warning: " ++ path ++ ":" ++ show l' ++ ":" ++ show c ++ ": the scope of " ++ reference ++ " " ++ showInterval card'
      ++ " is " ++ show scope' ++ ", since " ++ name ++ " may point to all " ++ show targetScope ++ " " ++ _ident target ++ " in each of its parents."
      ++ " If fewer suffice, give it an upper bound, e.g., " ++ reference ++ " " ++ show (fst card') ++ "..10, to make the instance generation faster."

-- | Warnings about the expressions compared with, subtracted from or intersected with themselves, at their positions in the files
showRedundantExps :: [ModelFile] -> [PExp] -> String
showRedundantExps files = unlines . map showRedundantExp
  where
  showRedundantExp PExp{_inPos = Span (Pos l c) _, _exp = IFunExp fun _} =
    let
      (path, l') = modelPosition files l
    in
      "Warning: " ++ path ++ ":" ++ show l' ++ ":" ++ show c ++ ": '" ++ fun ++ "' is redundant because the two subexpressions are always equivalent, so it is " ++ meaning fun ++ "."
  showRedundantExp _ = ""
  meaning fun
    | fun == iEq         = "always true"
    | fun == iNeq        = "always false"
    | fun == iDifference = "always empty"
    | otherwise          = "the same as either subexpression"

-- | The navigations from @noclafer@, e.g., @noclafer.d@, as written: the desugarer and the name
--   resolver replace them with @noclafer@, since a navigation from the empty set is empty
noClaferNavs :: Module -> [Span]
noClaferNavs ast' =
  [ s | EJoin s (ClaferId _ (Path _ [ModIdIdent _ (PosIdent (_, n))])) _ <- universeOnOf biplate uniplate ast'
      , n == noClaferIdent ]

-- | Warnings about the navigations from @noclafer@, at their positions in the files
showNoClaferNavs :: [ModelFile] -> [Span] -> String
showNoClaferNavs files = unlines . map showNoClaferNav
  where
  showNoClaferNav (Span (Pos l c) _) =
    let
      (path, l') = modelPosition files l
    in
      "Warning: " ++ path ++ ":" ++ show l' ++ ":" ++ show c ++ ": a navigation from '" ++ noClaferIdent ++ "' is redundant, since it is always empty."

-- | Warnings about the names with @__@, which the compiler uses between the names of a UID
--   (e.g., @x__size@ for the @size@ in @x@): in a full model, such a name may be the UID of
--   another clafer, and a lookup by name, e.g., in a scope file, splits it. An explicit model
--   (@-f@, @--explicit@) has them by design.
showDoubleUnderscoreNames :: [ModelFile] -> IModule -> String
showDoubleUnderscoreNames files iModule = unlines
  [ "Warning: " ++ path ++ ":" ++ show l' ++ ":" ++ show c ++ ": the name '" ++ _ident claf
    ++ "' contains '__', which the compiler uses in the unique names of clafers (e.g., x__size for the size in x)."
  | claf@IClafer{_cinPos = Span (Pos l c) _} <- universeOn biplate iModule :: [IClafer]
  , "__" `isInfixOf` _ident claf
  , let (path, l') = modelPosition files l ]

-- | The clafers with an initializer @:=@ under an inherited constant initializer @=@, e.g.,
--   @v : Val := 2@ under @abstract Val -> integer = 2@, each with the nearest such super clafer.
--   Inheritance is pure strengthening (kbak/clafer-semantics#164): the own initializer is added
--   to the inherited one and does not redefine it. Since @=@ fixes the value in every state, the
--   @:=@ repeats it or conflicts with it; the warning does not decide which (gsdlab/clafer#124).
--   The kinds of the initializers come from the AST, since the desugarer turns them into
--   constraints; a reference to a constant, e.g., @-> 2@, is a constant initializer.
fixedInitialValues :: Module -> IModule -> [(IClafer, IClafer)]
fixedInitialValues ast' iModule =
  [ (c, s)
  | c <- universeOn biplate iModule :: [IClafer]
  , Map.lookup (_cinPos c) initKinds == Just False
  , s : _ <- [ [ s | s <- drop 1 $ findHierarchy getSuper uidIClaferMap' c
                   , _uid s /= _uid c
                   , Map.lookup (_cinPos s) initKinds == Just True ] ] ]
  where
    uidIClaferMap' = createUidIClaferMap iModule
    -- for the span of each clafer with an initializer, whether the initializer is constant
    initKinds :: Map.Map Span Bool
    initKinds = Map.fromList $ mapMaybe initKind (universeOnOf biplate uniplate ast' :: [Clafer])
    initKind (Clafer s _ _ _ _ _ reference' _ init' _ _) = case init' of
      InitSome _ (InitConstant _) _ -> Just (s, True)
      InitSome _ (InitDefault _) _  -> Just (s, False)
      InitEmpty _ | isJust (referenceToConstant reference') -> Just (s, True)
      _ -> Nothing

-- | Warnings about the initializers @:=@ that redefine an inherited @=@, at their positions in the files
showFixedInitialValues :: [ModelFile] -> [(IClafer, IClafer)] -> String
showFixedInitialValues files = unlines . map showFixedInitialValue
  where
  showFixedInitialValue (claf@IClafer{_cinPos = Span (Pos l c) _}, super') =
    let
      (path, l') = modelPosition files l
    in
      "Warning: " ++ path ++ ":" ++ show l' ++ ":" ++ show c ++ ": the initializer ':=' of " ++ _ident claf
      ++ " does not redefine the initializer '=' of " ++ _ident super' ++ ", which " ++ _ident claf
      ++ " inherits: both hold. Since '=' fixes the value in every state, the ':=' repeats an equal value,"
      ++ " and a different value leaves no instance of " ++ _ident claf ++ "."

showInterval :: (Integer, Integer) -> String
showInterval (n, -1) = show n ++ "..*"
showInterval (n, m) = show n ++ ".." ++ show m

gatherObjectivesAndAttributes :: IModule -> Map.Map Span [Ast] -> Maybe ObjectivesAndAttributes
gatherObjectivesAndAttributes    iModule    astModuleTrace'      = let
    objectives = (foldl' gatherObjectives [] $ _mDecls iModule)
  in if null objectives
     then Nothing
     else Just $ ObjectivesAndAttributes objectives (map _uid $ filter isIntClafer iClafers)
  where
    gatherObjectives :: [String] -> IElement         -> [String]
    gatherObjectives    objs        (IEGoal _ cpexp') =
      printCPexp (Map.lookup (_inPos cpexp') astModuleTrace')
      :objs
    gatherObjectives    objs        _                 = objs

    printCPexp :: Maybe [Ast] -> String
    printCPexp (Just [_,_,(AstGoal g)]) = case g of
      GoalMinimize      _ [e] -> "min " ++ (printTree e)
      GoalMaximize      _ [e] -> "max " ++ (printTree e)
      GoalMinDeprecated _ [e] -> "min " ++ (printTree e)
      GoalMaxDeprecated _ [e] -> "max " ++ (printTree e)
      g' -> "[BUG]: unexpected goal found!" ++ show g'
    printCPexp e          = "[BUG]: expression not found!" ++ show e

    iClafers :: [ IClafer ]
    iClafers = universeOn biplate iModule

    isIntClafer (IClafer{_reference=(Just IReference{_ref=pexp'})}) = any (`elem` ["integer", "int"]) $ getRefIds pexp'
    isIntClafer _                                                   = False