packages feed

claferIG-0.5.8: src/Language/Clafer/IG/CommandLine.hs

{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use camelCase" #-}

{-
 Copyright (C) 2012-2017 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.
-}

module Language.Clafer.IG.CommandLine (runCommandLine, printError, findNecessaryBitwidth, intToFloat, pickLargerScope, allowedMaxInt, requiredBitwidth) where

import Language.Clafer
import Language.ClaferT
import Language.Clafer.Intermediate.Intclafer
import Language.Clafer.IG.ClaferIG
import Language.Clafer.Import (modelPosition)
import Language.Clafer.IG.ClaferModel
import Language.Clafer.IG.CommandLineParser
import Language.Clafer.IG.Constraints
import Language.Clafer.IG.SolutionView
import Language.Clafer.Comments
import Language.Clafer.JSONMetaData
import Language.Clafer.QNameUID
import qualified Language.Clafer.IG.AlloyIGInterface as AlloyIG
import Control.Monad
import Control.Monad.Trans
import Control.Monad.IO.Class
import Control.Monad.Except
import Data.Char
import Data.IORef
import Data.List
import Data.Monoid
import Data.Foldable hiding (mapM_, maximum, find, elem, notElem, foldr, any, concat)
import qualified Data.Map as Map
import Data.Maybe
import System.Console.Haskeline
import System.IO
import GHC.Float
import qualified Text.Parsec.Error as E
import Prelude

data AutoComplete
    = Auto_Command
    | Auto_Clafer
    | Auto_ClaferInstance
    | Auto_UnsatCoreMinimization
    | Auto_Space
    | Auto_Digit
    | No_Auto deriving Show

data AutoCompleteContext = AutoCompleteContext {clafers::IORef [String], claferInstances::IORef [String]}

-- | The solutions shown so far, the most recent first, and whether they are shown as the changes
--   from the previous snapshot
data Context = Context {currentAlloyInstance::Maybe String, saved::[ClaferSolution], unsaved::[ClaferSolution], autoCompleteContext::AutoCompleteContext, diffView::Bool}

-- | Interactive session interface
runCommandLine :: ClaferIGT IO ()
runCommandLine =
    do
        solve
        clafers <- getClafers

        clafersRef <- liftIO $ newIORef clafers
        claferInstancesRef <- liftIO $ newIORef []

        let autoCompleteContext = AutoCompleteContext clafersRef claferInstancesRef
        diffView' <- diff <$> getClaferIGArgs
        runInputT Settings {
            complete = completeFunc autoCompleteContext,
            historyFile = Nothing,
            autoAddHistory = True
        } $ loop Next (Context Nothing [] [] autoCompleteContext diffView')
    where


    loop :: Command -> Context -> InputT (ClaferIGT IO) ()

    loop Quit _ = return ()

    loop Next context =
        do
            solution <- lift next
            case solution of
                Instance claferSolution xml -> showNew claferSolution xml context
                UnsatCore core counterexample -> do
                    liftIO $ hPutStrLn stderr "No instances found."
                    liftIO $ hPutStrLn stderr "The following set of constraints cannot be satisfied in the current scope."
                    liftIO $ hPutStrLn stderr "Consider removing one of the constraints from the model."
                    liftIO $ hPutStrLn stderr "Use the setUnsatCoreMinimization command to minimize the set of constraints below."
                    liftIO $ hPutStrLn stderr "Try increasing the scopes to get an instance (type 'h' for help)."

                    printConstraints core
                    case counterexample of
                        Just (Counterexample removed claferSolution _) -> do
                            liftIO $ hPutStrLn stderr "Altering the following constraints produced the following near-miss example:"
                            printTransformations removed
                            outputStrLn $ solutionText claferSolution
                        Nothing -> return ()
                    nextLoop context
                NoInstance -> do
                    dynamic <- lift isDynamicModel
                    liftIO $ hPutStrLn stderr $ if dynamic
                        then "No more traces found. Try increasing the scopes or the maximum trace length ('tl') to get more traces (type 'h' for help)."
                        else "No more instances found. Try increasing the scopes to get more instances (type 'h' for help)."
                    nextLoop context
            where
            printTransformation :: Constraint -> [Constraint] -> (String, [Constraint])
            printTransformation UserConstraint{constraintInfo = info} rest = ("removed " ++ syntax info, rest)
            printTransformation ExactCardinalityConstraint{claferInfo = info} rest =
                (show info ++ " changed to " ++ show (setUpper (setLower info 0) Nothing), rest)
            printTransformation LowerCardinalityConstraint{claferInfo = info} rest =
                case deleteUpper (uniqueId info) rest of
                    -- Deleted the lower and upper constraint
                    Just rest' -> (show info ++ " changed to " ++ show (setUpper (setLower info 0) Nothing), rest')
                    -- Only deleted the lower constraint
                    Nothing    -> (show info ++ " changed to " ++ show (setLower info 0), rest)
            printTransformation UpperCardinalityConstraint{claferInfo = info} rest =
                case deleteLower (uniqueId info) rest of
                    -- Deleted the lower and upper constraint
                    Just rest' -> (show info ++ " changed to " ++ show (setUpper (setLower info 0) Nothing), rest')
                    -- Only deleted the upper constraint
                    Nothing    -> (show info ++ " changed to " ++ show (setUpper info Nothing), rest)

            printConstraint UserConstraint{constraintInfo = info} = show info
            printConstraint constraint = show $ claferInfo constraint

            printConstraints = printConstraints' (1::Integer)
            printConstraints' _ [] = return ()
            printConstraints' i (c:cs) =
                do
                    liftIO $ hPutStrLn stderr $ "  " ++ show i ++ ") " ++ printConstraint c
                    printConstraints' (i + 1) cs

            printTransformations cs = printTransformations' (1::Integer) cs
            printTransformations' _ [] = return ()
            printTransformations' i (c:cs) =
                do
                    let (prnt, rest) = printTransformation c cs
                    liftIO $ hPutStrLn stderr $ "  " ++ show i ++ ") " ++ prnt
                    printTransformations' (i + 1) rest

            setLower info@ClaferInfo{cardinality = c} lower = info{cardinality = c{lower = lower}}

            setUpper info@ClaferInfo{cardinality = c} upper = info{cardinality = c{upper = upper}}

            deleteLower :: String -> [Constraint] -> Maybe [Constraint]
            deleteLower id' ys =
                findAndDelete id' (filter isLowerCardinalityConstraint ys)

            deleteUpper :: String -> [Constraint] -> Maybe [Constraint]
            deleteUpper id' ys =
                findAndDelete id' (filter isUpperCardinalityConstraint ys)

            findAndDelete :: String -> [Constraint] -> Maybe [Constraint]
            findAndDelete _ [] = Nothing
            findAndDelete id' (c:cs)
                | id' == uniqueId (claferInfo c) = Just cs
                | otherwise                     = (c :) `fmap` findAndDelete id' cs


    loop Help context =
        do
            outputStrLn (
                "------------------\n" ++
                "| " ++ claferIGVersion ++ " |\n" ++
                "------------------\n\n" ++
                "You can invoke the following commands as indicated by single quotes:\n" ++
                "[tab]              - print the available commands\n" ++
                "                   - auto-complete command name, a clafer name, or clafer instance name in a given context\n" ++
                "'n'ext, [enter]    - to produce the next solution if available, an instance of a static model or a trace\n" ++
                "                     of a dynamic model, or to output a message that no more solutions exist within\n" ++
                "                     the given scope\n" ++
                "'i'ncrease         - to increase the maximum number of instances of a given clafer or all clafers (scope)\n" ++
                "'s'et              - to set the maximum number of instances of a given clafer or all clafers (scope)\n" ++
                "'m'axint, 'maxint' - to set the bitwidth by providing the largest integer\n" ++
                "'tl'               - to set the maximum length of the traces of a dynamic model\n" ++
                "'d'iff             - to show each next instance or state as its changes from the previous one, or in full\n" ++
                "                     (+ appears, - disappears, ~ changes its value)\n" ++
                "'fork'             - to produce another trace with the same first states as the last trace\n" ++
                "'config'           - to produce a trace of a dynamic model whose clafers that do not change over time\n" ++
                "                     differ from those of the traces so far, or an instance of a static model whose\n" ++
                "                     clafers, without the values of their references, differ from those so far\n" ++
                "'check'            - to check the assertions of the model within the scopes and the maximum trace\n" ++
                "                     length, and to give a counterexample of each one that fails\n" ++
                "sa'v'e             - to save all instances displayed so far or a counterexample to files named \n" ++
                "                     <model file name>.cfr.<instance number>.data, one instance per file\n" ++
                "'dot'              - to save the most recent solution as a Graphviz graph, for a trace its states\n" ++
                "                     and transitions, to <model file name>.cfr.<solution number>.dot\n" ++
                "'q'uit             - to quit the interactive session\n" ++
                "'r'eload           - to reload your clafer model\n" ++
                "'h'elp             - to display this menu options summary\n" ++
                "'scope'            - to print out the values of the global scope and individual Clafer scopes\n" ++
                "'saveScopes'       - to generate a '<model>.cfr-scope' file with the current scopes\n" ++
                "'loadScopes'       - to load scopes from a '<model>.cfr-scope' file\n" ++
                "'setUnsatCoreMinimization' - to choose UnSAT core minimization strategy [fastest | medium | best]. Default: fastest\n" ++
                "'c', 'claferModel' - to print out the original Clafer model verbatim\n" ++
                "'a', 'alloyModel'  - to print out the output of Clafer translator verbatim\n" ++
                "'alloyInstance'    - to print out the Alloy xml document of the most recent solution\n" ++
                "'f'ind             - to print a Clafer with given name found in the most recent solution\n\n" ++
                "Parameterized command usage:\n" ++
                "'i [enter]'         - to increase for all clafers by 1\n" ++
                "'i <name> [enter]'  - to increase for the clafer <name> by 1\n" ++
                "'i <name> <number>' - to increase for the clafer <name> by <number>\n" ++
                "'s <number> [enter]'- to set for the clafers to <number>\n" ++
                "'s <name> <number>' - to set for the clafer <name> to <number>\n" ++
                "'f <name>'          - to display a clafer <name>\n" ++
                "'tl <number>'       - to set the maximum trace length to <number>\n" ++
                "'fork <number>'     - to produce another trace with the states of the last trace before state <number>\n" ++
                "'check <number>'    - to check only the assertions on line <number>, or all of them if it is not positive\n" ++
                "'setUnsatCoreMinimization fastest' - fastest but the worst\n" ++
                "'setUnsatCoreMinimization medium'\n" ++
                "'setUnsatCoreMinimization best' - best but slowest even for modest size cores"
                )

            nextLoop context

    loop Save context@Context{saved=saved, unsaved=unsaved} =
        do
            save unsaved (toInteger $ length saved)
            nextLoop context{saved=unsaved ++ saved, unsaved=[]}

    loop SaveGraph context =
        do
            let solutions = unsaved context ++ saved context
            case solutions of
                [] -> outputStrLn "No solution yet: type 'n' for the first one."
                solution : _ -> do
                    claferModelFile' <- lift getModelFileName
                    let saveName = claferModelFile' ++ "." ++ show (length solutions) ++ ".dot"
                    qNameMaps' <- lift getQNameMaps
                    liftIO $ writeFile saveName $ if diffView context
                        then solutionDiffDot (lpqName qNameMaps') solution
                        else solutionDot solution
                    outputStrLn $ "Saved to " ++ saveName
            nextLoop context

    loop (Check line) context =
        do
            allAssertions <- lift getAssertions
            let assertions = maybe allAssertions (\n -> filter (isOnLine n . snd) allAssertions) line
            dynamic <- lift isDynamicModel
            when (null assertions) $ outputStrLn $ maybe "The model has no assertions." (\n -> "The model has no assertion on line " ++ show n ++ ".") line
            (context', failed) <- foldM checkAssertion' (context, 0 :: Int) assertions
            let bounds = if dynamic then "the scopes and the maximum trace length" else "the scopes"
            unless (null assertions) $ outputStrLn $
                show (length assertions) ++ " assertion(s) checked within " ++ bounds ++ ": " ++ show failed ++ " with a counterexample."
            nextLoop context'
        where
        checkAssertion' (context', failed) (name, info) =
            do
                counterexample <- lift $ checkAssertion name
                case counterexample of
                    Nothing -> outputStrLn ("No counterexample: " ++ show info) >> return (context', failed)
                    Just (claferSolution, xml) -> do
                        outputStrLn $ "Counterexample of: " ++ show info
                        context'' <- display claferSolution xml context'
                        return (context'', failed + 1)

    loop Reload context =
        do
            oldScopes <- lift getScopes
            oldBw <- lift getBitwidth

            _ <- runExceptT $ ExceptT (lift reload) `catchError` (liftIO . mapM_ (hPutStrLn stderr) . printError)

            env <- lift getClaferEnv
            let ir = fst3 $ fromJust $ cIr env
            tempScopes <- lift getScopes
            lift $ mergeScopes oldScopes tempScopes

            newScopes <- lift getScopes
            lift $ setBitwidth $ findNecessaryBitwidth ir oldBw $ snd $ unzip newScopes
            newBw <- lift getBitwidth
            printBitwidthWarning newBw
            lift $ solve
            nextLoop context

    loop (IncreaseGlobalScope inc') context =
        do
            globalScope <- lift getGlobalScope
            bitwidth' <- lift getBitwidth
            let newGlobalScope = max 1 $ globalScope+inc'
            lift $ setGlobalScope newGlobalScope
            when (newGlobalScope > allowedMaxInt bitwidth') $ do
                outputStrLn $ "Warning! Requested global scope is larger than maximum allowed by bitwidth ... increasing bitwidth"
                lift $ setBitwidth $ requiredBitwidth newGlobalScope
                newBw <- lift getBitwidth
                printBitwidthWarning newBw

            -- the clafers without instances keep their scopes, 0
            oldScopes <- lift $ filterM (fmap not . hasNoInstances . fst) =<< getScopes
            mapM_ ( \(sigName', val') -> do
                        claferName' <- lift $ claferNameOfSig sigName'
                        setAlloyScopeAndBitwidth bitwidth' (max 1 $ val'+inc') claferName' sigName') oldScopes
            lift solve
            outputStrLn ("Global scope changed to " ++ show newGlobalScope)
            nextLoop context
    loop (SetGlobalScope newGlobalScope') context =
        do
            let newGlobalScope = max 1 newGlobalScope'
            bitwidth' <- lift getBitwidth
            lift $ setGlobalScope newGlobalScope
            when (newGlobalScope > allowedMaxInt bitwidth') $ do
                outputStrLn $ "Warning! Requested global scope is larger than maximum allowed by bitwidth ... increasing bitwidth"
                lift $ setBitwidth $ requiredBitwidth newGlobalScope
                newBw <- lift getBitwidth
                printBitwidthWarning newBw
            lift solve
            outputStrLn ("Global scope set to " ++ show newGlobalScope)
            nextLoop context

    loop (IncreaseScope fqName inc') context =
        do
            try $ do
                qNameMaps' <- lift $ lift $ getQNameMaps
                let uids = getUIDs qNameMaps' fqName
                sigs <- lift $ lift $ mapM sigOfUID uids
                bitwidth' <- lift $ lift getBitwidth

                lift $ mapM_ (incAlloyScopeAndBitwidth bitwidth' inc' fqName) sigs

                lift $ lift $ solve
            nextLoop context

    loop (SetScope qName val') context =
        do
            try $ do
                qNameMaps' <- lift $ lift $ getQNameMaps
                let uids = getUIDs qNameMaps' qName
                sigs <- lift $ lift $ mapM sigOfUID uids
                bitwidth' <- lift $ lift getBitwidth

                lift $ mapM_ (setAlloyScopeAndBitwidth bitwidth' val' qName) sigs

                lift $ lift $ solve
            nextLoop context

    loop (SetBitwidth newBitwidth) context =
        do
            when (newBitwidth >=4) $ do
                lift $ setBitwidth newBitwidth
                printBitwidthWarning newBitwidth
                lift solve
                printBitwidthIntRange newBitwidth
            nextLoop context

    loop (SetMaxTraceLength traceLength) context =
        do
            dynamic <- lift isDynamicModel
            if not dynamic
            then outputStrLn "The model is static: its instances are not traces."
            else if traceLength < 1
            then outputStrLn "The trace length must be at least 1."
            else do
                lift $ setMaxTraceLength traceLength
                lift solve
                outputStrLn $ "The maximum trace length is " ++ show traceLength ++ "."
            nextLoop context

    loop (Fork state) context =
        do
            dynamic <- lift isDynamicModel
            if not dynamic
            then outputStrLn "The model is static: its instances are not traces." >> nextLoop context
            else otherSolution (fork state) ("No other trace has the states of the last trace before state " ++ show state ++ ".") context

    loop NextConfiguration context = otherSolution nextConfiguration "No solution has another configuration." context

    loop ToggleDiff context =
        do
            outputStrLn $ if diffView context
                then "The instances and states are shown in full."
                else "Each next instance or state is shown as its changes from the previous one."
            nextLoop context{diffView = not $ diffView context}

    loop (SetMaxInt newMaxInt) context =
        do
            when (newMaxInt >= 7) $ do
                let newBitwidth = requiredBitwidth newMaxInt
                lift $ setBitwidth newBitwidth
                printBitwidthWarning newBitwidth
                lift solve
                printBitwidthIntRange newBitwidth
            nextLoop context

    loop ShowScopes context =
        do
            globalScope <- lift getGlobalScope
            outputStrLn $ "Global scope = " ++ show globalScope
            originalScopes <- lift getScopes
            -- by UID
            mapM_ (\(sigName, value) -> lift (uidOfSig sigName) >>= \uid' -> printScope (uid', value)) originalScopes
            nextLoop context

            where
            printScope (uid', value)  =
                outputStrLn $ "  " ++ uid' ++ " scope = " ++ show value

    loop SaveScopes context =
        do
            globalScope <- lift getGlobalScope
            originalScopes <- lift getScopes
            modelFileName <- lift getModelFileName
            qNameMaps' <- lift getQNameMaps
            -- by UID
            sigUids <- lift $ mapM (uidOfSig . fst) originalScopes
            let
                globalScopeTuple = ("", globalScope)
                uidScopes = globalScopeTuple : zip sigUids (map snd originalScopes)
            liftIO $ writeCfrScopeFile uidScopes qNameMaps' modelFileName
            outputStrLn "Scopes saved to the `.cfr-scope` file."
            nextLoop context

    loop LoadScopes context =
        do
            modelFileName <- lift getModelFileName
            qNameMaps' <- lift getQNameMaps
            maybeUidScopes <- liftIO $ readCfrScopeFile qNameMaps' modelFileName
            case maybeUidScopes of
                Nothing -> do
                    outputStrLn "The `.cfr-scope` file does not exist. Use the command `saveScopes` to create one."
                    nextLoop context
                Just uidScopes -> do
                    let
                        (globalScopes, normalScopes) = partition (\(uid', _) -> null uid') uidScopes
                        -- from the globalScopes, take the maximum
                        globalScopeVals = map snd globalScopes
                    -- the sigs of the clafers
                    normalSigs <- lift $ mapM (sigOfUID . fst) normalScopes
                    let normalScopesAlloy = zip normalSigs (map snd normalScopes)
                    -- a file may list only some clafers, without the global scope; then it stays
                    unless (null globalScopeVals) $ lift $ setGlobalScope $ maximum globalScopeVals
                    lift $ mapM_ (\(uid', val) -> setAlloyScope val uid') normalScopesAlloy
                    outputStrLn "Scopes loaded from the `.cfr-scope` file."
                    loop ShowScopes context
    loop (Find name) context =
        do
            case (unsaved context ++ saved context) of
                solution:_ ->
                    case find ((name ==) . c_name) $ concatMap traverseModel $ solutionModels solution of
                        Just clafer' -> outputStrLn $ show clafer'
                        Nothing -> outputStrLn $ "\"" ++ name ++ "\" not found in the model."
                []  -> outputStrLn $ "No instance"
            nextLoop context

    loop ShowClaferModel context =
        do
            bitwidth' <- lift getBitwidth
            printBitwidthIntRange bitwidth'
            originalScopes <- lift getScopes
            -- remove the "this/" prefix
            let
                scopes = map ( \ (uid', val') -> (drop 5 uid', val') ) originalScopes
                scopesMap = Map.fromList scopes

            globalScope' <- lift getGlobalScope
            outputStrLn $ "Global scope  = " ++ show globalScope' ++ "\n"

            env <- lift getClaferEnv
            constraints' <- lift getConstraints
            AlloyIG.UnsatCore core <- lift $ ClaferIGT $ lift AlloyIG.sendUnsatCoreCommand
            let unSATs = map getConstraintInfo $ catMaybes $ findRemovable env core constraints'

            claferModel <- lift getClaferModel
            modelFiles' <- lift getModelFiles
            let commentLines = getCommentLines claferModel
            lineNumMap <- lift getlineNumMap


            outputStrLn $ editModel claferModel commentLines unSATs lineNumMap scopesMap modelFiles'
            nextLoop context
            where
                -- the lines of the model: each file under its name, with the numbers of its lines
                editModel model cLines unSATs lineNumMap scopesMap' modelFiles' =
                    let
                        claferLines = lines $ removeCommentsAndUnify model
                        maxLineLength = maximum $ map length claferLines
                        shown = [ (modelPosition modelFiles' num, num, l) | (num, l) <- zip [1..] claferLines, not (num `elem` cLines && isEmptyLine l) ]
                        header ((path, _), _, _) previous
                            | fmap (\((p, _), _, _) -> p) previous == Just path = []
                            | otherwise = ["--- " ++ path]
                    in unlines $ concat $ zipWith (\line' previous -> header line' previous ++ [editLine unSATs (numberOfDigits $ length claferLines) maxLineLength scopesMap' lineNumMap line']) shown (Nothing : map Just shown)

                editLine :: [(Maybe Integer, String)] -> Int -> Int -> (Map.Map String Integer) -> (Map.Map Integer String) -> ((FilePath, Integer), Integer, String) -> String
                editLine unSATs m1 m2 scopesMap' lineNumMap ((_, fileNum), num, l) =
                    show fileNum ++ "." ++ (replicate (1 + m1 - (numberOfDigits $ fromIntegral fileNum)) ' ') ++ (if (isUnSAT unSATs l num) then "> " else "| ") ++ l ++ (replicate (3 + m2 - (length l)) ' ') ++ (if (isUnSAT unSATs l num) then "<UnSAT " else "|      ") ++ (addScopeVal scopesMap' (Map.lookup num lineNumMap))

                -- a constraint with a position is on the line where it starts; a cardinality
                -- is on the line with the clafer's name and cardinality
                isUnSAT :: [(Maybe Integer, String)] -> String -> Integer -> Bool
                isUnSAT us l ln = getAny $ foldMap (\u -> Any $ case u of
                    (Just line', _) -> line' == ln
                    (Nothing, u') -> ((safehead $ words u') == (safehead $ words l) && (safehead $ reverse $ words u') == (safehead $ reverse $ words l)) || (u' `isInfixOf` l)) us
                safehead [] = []
                safehead x = head x

                addScopeVal :: (Map.Map String Integer) -> (Maybe String) ->String
                addScopeVal _       Nothing     = ""
                addScopeVal scopesMap' (Just name) = "scope = " ++ (fromJustShow $ Map.lookup name scopesMap')

                getCommentLines :: String -> [Integer]
                getCommentLines = foldr (\((Span (Pos l1 _) (Pos l2 _)), _) acc -> [l1..l2] ++ acc) [] . getComments

                getConstraintInfo :: Constraint -> (Maybe Integer, String)
                getConstraintInfo (UserConstraint _ info@(ConstraintInfo _ (Span (Pos l _) _) _ _ _)) = (Just l, show info)
                getConstraintInfo x = (Nothing, show $ claferInfo x)

    loop ShowAlloyModel context =
        do
            globalScope' <- lift getGlobalScope
            alloyModel <- lift getAlloyModel
            scopes <- lift getScopes

            outputStrLn $ editAlloyModel alloyModel scopes globalScope'
            nextLoop context
            where
                editAlloyModel :: String -> [(String, Integer)] -> Integer      -> String
                editAlloyModel    model     s                      globalScope' =
                    let alloyLines = lines $ removeCommentsAndUnify model
                        splitNum = 1 + (length $ takeWhile (not . isEmptyLine) alloyLines)
                        (alloyInfo, alloyLines') = splitAt splitNum $ alloyLines
                    in unlines $ (("global scope = " ++ show globalScope' ++ "\n") :) $ alloyInfo ++ map (\(num, line) -> show num ++ ('.' : (replicate (1 + (numberOfDigits $ length alloyLines) - (numberOfDigits num)) ' ') ++ ('|' : ' ' : line))) (zip [(1 + splitNum)..] (addScopeVals alloyLines' s (maximum $ map length alloyLines')))
                addScopeVals :: [String] -> [(String, Integer)] -> Int -> [String]
                addScopeVals [] _ _ = []
                addScopeVals (l:ls) ss m =
                    let val = Data.List.lookup (takeWhile (`notElem` [' ','\t','\n']) $ tail $ dropWhile (/='_') l) ss
                    in if ("sig" `notElem` words l) then ((l ++ (replicate (3 + m - (length l)) ' ') ++ " |") : addScopeVals ls ss m)
                        else (l ++ (replicate (3 + m - (length l)) ' ') ++ " | scope = " ++ (fromJustShow val)) : addScopeVals ls ss m

    loop ShowAlloyInstance context =
        do
            case currentAlloyInstance context of
                Just alloyInstance -> outputStrLn alloyInstance
                Nothing -> outputStrLn $ "No instance"
            nextLoop context

    loop (SetUnsatCoreMinimization level) context =
        do
            let level' =
                    case level of
                        Fastest -> 2
                        Medium  -> 1
                        Best    -> 0
            lift $ setUnsatCoreMinimization level' >> solve

            nextLoop context

    -- shows a new solution, the next one
    showNew claferSolution xml context = display claferSolution xml context >>= nextLoop

    -- shows a solution, which becomes the most recent one
    display claferSolution xml context =
        do
            claferIGArgs' <- lift getClaferIGArgs
            uidIClaferMap' <- lift getUIDIClaferMap
            qNameMaps' <- lift getQNameMaps
            liftIO $ writeIORef (claferInstances $ autoCompleteContext context) $ nub $ map c_name $ concatMap traverseModel $ solutionModels claferSolution

            let
                iNumber = 1 + length (unsaved context)
                previous = listToMaybe (unsaved context ++ saved context)
            outputStrLn $ if json claferIGArgs'
                then solutionJSON uidIClaferMap' claferSolution
                else if dot claferIGArgs'
                then (if diffView context then solutionDiffDot (lpqName qNameMaps') claferSolution else solutionDot claferSolution)
                else if diffView context
                    then showSolutionDiff (lpqName qNameMaps') iNumber previous claferSolution
                    else showSolution iNumber claferSolution
            return context{unsaved=claferSolution:(unsaved context), currentAlloyInstance=Just xml}

    -- shows a solution other than the next one: a fork, or another configuration
    otherSolution command none context =
        do
            if null (unsaved context ++ saved context)
            then outputStrLn "No solution yet: type 'n' for the first one." >> nextLoop context
            else do
                solution <- lift command
                case solution of
                    Instance claferSolution xml -> showNew claferSolution xml context
                    _ -> outputStrLn none >> nextLoop context

    nextLoop context =
        do
            minput <- getInputLine "claferIG> "
            case minput of
                Nothing -> loop Quit context
                Just input ->
                    case parseCommandLine input of
                        Left msg    -> outputStrLn (show msg) >> nextLoop context
                        Right command -> loop command context

    save :: MonadIO m => [ClaferSolution] -> Integer -> InputT (ClaferIGT m) ()
    save [] _ = return ()
    save (c:cs) counter = do
        claferModelFile' <- lift getModelFileName
        let
            saveName = claferModelFile' ++ "." ++ (show counter) ++ ".data"
        liftIO $ writeFile saveName (solutionText c)
        outputStrLn $ "Saved to " ++ saveName
        save cs (counter + 1)

try :: MonadIO m => ExceptT String (InputT m) a -> InputT m ()
try e = either outputStrLn (void . return) =<< runExceptT e

incAlloyScopeAndBitwidth :: MonadIO m => Integer -> Integer -> String -> UID -> InputT (ClaferIGT m) ()
incAlloyScopeAndBitwidth                 bitwidth'  inc'       fqName'   sigName' = do
    scopeValue <- lift $ valueOfScope sigName'
    setAlloyScopeAndBitwidth bitwidth' (max 1 $ scopeValue+inc') fqName' sigName'

setAlloyScopeAndBitwidth :: MonadIO m => Integer -> Integer -> String -> UID -> InputT (ClaferIGT m) ()
setAlloyScopeAndBitwidth                 bitwidth'  newValue'   fqName'   sigName' = do
  empty' <- lift $ hasNoInstances sigName'
  if empty'
  then outputStrLn $ "Scope of " ++ fqName' ++ " (" ++ (drop 5 sigName') ++ ") stays 0: it has no instances, since no concrete clafer inherits from it or from the abstract clafer it is in"
  else do
    when (newValue' > allowedMaxInt bitwidth') $ do
        lift $ setBitwidth $ requiredBitwidth $ newValue'
        newBw <- lift $ getBitwidth
        outputStrLn $ "Warning! Requested scope for " ++ fqName' ++ " is larger than maximum allowed by bitwidth ... increasing bitwidth"
        printBitwidthWarning newBw
    _ <- lift $ setAlloyScope newValue' sigName'
    outputStrLn $ "Scope of " ++ fqName' ++ " (" ++ (drop 5 sigName') ++ ") changed to " ++ show newValue'

mergeScopes :: MonadIO m => [(UID, Integer)] -> [(UID, Integer)] -> ClaferIGT m ()
mergeScopes _ [] = return ()
mergeScopes oldScopes newScopes = do
    let mergedScopes = map (pickLargerScope oldScopes) newScopes
    mapM_ (\(uid', val) -> setAlloyScope val uid') mergedScopes

pickLargerScope :: [(String, Integer)] -> (String, Integer) -> (String, Integer)
pickLargerScope    oldScopes              (uid', val)        =
    let
        oldScopesMap = Map.fromList oldScopes
        oldVal = Map.findWithDefault val uid' oldScopesMap
    in (uid', max val oldVal)

-- | bitwidth required to store the given integer
requiredBitwidth :: Integer -> Integer
requiredBitwidth    n    = ceiling $ logBase 2 $ (+1) $ (*2) $ intToFloat n

-- | the largest integer allowed by the given bitwidth
allowedMaxInt :: Integer -> Integer
allowedMaxInt    bw       = (2 ^ (bw - 1)) - 1

-- | the smallest integer allowed by the given bitwidth
allowedMinInt :: Integer -> Integer
allowedMinInt    bw       = -((allowedMaxInt bw) + 1)

printBitwidthIntRange :: MonadIO m => Integer -> InputT m ()
printBitwidthIntRange    bw       = outputStrLn $ concat [
    "Integer range = ",
    show $ allowedMinInt bw,
    "..",
    show $ allowedMaxInt bw,
    " (",
    "bitwidth = ",
    show bw,
    ")" ]

printBitwidthWarning :: MonadIO m => Integer -> InputT m ()
printBitwidthWarning bw = when (bw > 9) $ outputStrLn $ "Warning! This is a very large bitwidth for Alloy, which may cause slow down."

-- i Ali|ce
--     If the cursor is at | then not open. The "ce" prevents autocomplete.
-- i Ali| ce
--     Is open, and autocomplete will kick in.
isOpen :: String -> [Char] -> Bool
isOpen _ [] = True
isOpen _ (x:_) = isSpace x


completeFunc :: MonadIO m => AutoCompleteContext -> CompletionFunc m
completeFunc context (prev, nxt) =
    if isOpen prev nxt then
        liftIO $ evalComplete context prev
    else
        return (prev, [])


completePrefix :: String -> [String] -> [Completion]
completePrefix prefix choices = map simpleCompletion $ filter (prefix `isPrefixOf`) choices


evalComplete :: AutoCompleteContext -> String -> IO (String, [Completion])
evalComplete context prev =
    do
        completion <- autoComplete context word auto
        return (reverseRest, completion)
    where
    input = reverse prev
    (reverseWord, reverseRest) = break isSpace prev
    word = reverse reverseWord
    auto = autoCompleteDetect $ parseCommandLineAutoComplete input


autoComplete :: AutoCompleteContext -> String -> AutoComplete -> IO [Completion]
autoComplete _ word Auto_Command = return $ completePrefix word commandStrings
autoComplete context word Auto_Clafer =
    do
        c <- readIORef $ clafers context
        return $ completePrefix word c
autoComplete context word Auto_ClaferInstance =
    do
        ci <- readIORef $ claferInstances context
        return $ completePrefix word ci
autoComplete _ word Auto_UnsatCoreMinimization = return $ completePrefix word ["fastest", "medium", "best"]
autoComplete _ _ Auto_Digit = return [] -- Don't auto complete numbers.
autoComplete _ word Auto_Space = return [simpleCompletion $ word]
autoComplete _ _ No_Auto = return []

autoCompleteDetect :: E.ParseError -> AutoComplete
autoCompleteDetect error'
    -- An unexpected message means that parsing failed before eof
    | any (not . null) unexpectedMessages = No_Auto
    | any (== "command") expectedMessages = Auto_Command
    | any (== "clafer") expectedMessages = Auto_Clafer
    | any (== "claferInstance") expectedMessages = Auto_ClaferInstance
    | any (== "Unsat core minimization level") expectedMessages = Auto_UnsatCoreMinimization
    | any (== "digit") expectedMessages = Auto_Digit
    | any (== "space") expectedMessages = Auto_Space
    | otherwise = No_Auto
    where
    messages = errorMessages error'
    unexpectedMessages = mapMaybe unexpectedMessage messages
    expectedMessages   = mapMaybe expectedMessage messages


-- | The errors of loading a model, which give the file and the position in it.
printError :: ClaferErrs -> [String]
printError (ClaferErrs errs) =
    map printErr errs
    where
    printErr (ParseErr (ErrPos _ _ (Pos l c)) msg) =
        "Parse failed at line " ++ show l ++ " column " ++ show c ++ "...\n" ++ msg
    printErr (SemanticErr (ErrPos _ _ (Pos l c)) msg) =
        "Compile error at line " ++ show l ++ " column " ++ show c ++ "...\n" ++ msg
    printErr (ClaferErr msg) = msg

-- only remove the contents while preserving the empty lines
removeCommentsAndUnify :: String -> String
removeCommentsAndUnify [] = []
removeCommentsAndUnify model@[_] = model
removeCommentsAndUnify ('\t':model) = ' ' : ' ' : ' ' : removeCommentsAndUnify model
removeCommentsAndUnify ('/':'/':model) = removeCommentsAndUnify $ dropWhile (/='\n') model
removeCommentsAndUnify ('/':'*':model) = removeBlock model
    where
        -- discard everything until "*/" is found or end of text while preserving new lines
        removeBlock :: String -> String
        removeBlock [] = []
        removeBlock [_] = []
        removeBlock ('\n':model') = '\n' : removeBlock model'
        removeBlock ('*':'/':model') = removeCommentsAndUnify model'
        -- discard contents of the block comment
        removeBlock (_:model') = removeBlock model'
removeCommentsAndUnify (c:model) = c : removeCommentsAndUnify model

isEmptyLine :: String -> Bool
isEmptyLine l = filter (`notElem` [' ', '\n', '\t', '\r']) l == []

numberOfDigits :: Int -> Int
numberOfDigits x = length $ show x

findNecessaryBitwidth :: IModule -> Integer -> [ Integer ] -> Integer
findNecessaryBitwidth ir oldBw scopeValues =
    if (newBw < oldBw) then oldBw else newBw
    where
        newBw = ceiling $ logBase 2 $ (+1) $ (*2) $ maxInModel ir
        maxInModel :: IModule -> Float
        maxInModel ir' = intToFloat $ max (maximum scopeValues) $ foldIR getMax 0 ir'
        getMax :: Ir -> Integer -> Integer
        getMax (IRIExp (IInt n)) m = max m $ abs n
        getMax (IRClafer IClafer{_card = Just (_, n)}) m = max m n
        getMax _ m = m

intToFloat :: Integer -> Float
intToFloat = fromInteger . toInteger

fromJustShow :: Maybe Integer -> String
fromJustShow (Just x) = show x
fromJustShow Nothing = "Nothing"