Analysis.hs revision 61091743da1a9ed6dfd5e077fdcc972553358962
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke{-# OPTIONS -fallow-undecidable-instances #-}
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckeModule : $Header$
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckeDescription : Instance of class Logic for propositional logic
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckeCopyright : (c) Dominik Luecke, Uni Bremen 2007
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckeLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckeMaintainer : luecke@tzi.de
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckeStability : experimental
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckePortability : portable
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckeBasic and static analysis for propositional logic
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke <http://en.wikipedia.org/wiki/Propositional_logic>
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke basicPropositionalAnalysis
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke ,mkStatSymbItems
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke ,mkStatSymbMapItem
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke ,inducedFromMorphism
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke ,inducedFromToMorphism
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Lueckeimport qualified Propositional.AS_BASIC_Propositional as AS_BASIC
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Lueckeimport qualified Propositional.Sign as Sign
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Lueckeimport qualified Common.GlobalAnnotations as GlobalAnnos
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Lueckeimport qualified Common.AS_Annotation as AS_Anno
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Lueckeimport qualified Common.Result as Result
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Lueckeimport qualified Common.Id as Id
548f3850942936a8c6021185c8391dfcd3b03018Dominik Lueckeimport qualified Data.List as List
548f3850942936a8c6021185c8391dfcd3b03018Dominik Lueckeimport qualified Data.Set as Set
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Lueckeimport qualified Data.Map as Map
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Lueckeimport qualified Propositional.Symbol as Symbol
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Lueckeimport qualified Propositional.Morphism as Morphism
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik Luecke-- | Datatype for formulas with diagnosis data
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Lueckedata DIAG_FORM = DiagForm
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik Luecke-- | Formula annotated with a number
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik Lueckedata NUM_FORM = NumForm
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik Luecke nfformula :: AS_Anno.Annoted (AS_BASIC.FORMULA)
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik Luecke , nfnum :: Integer
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Lueckedata TEST_SIG = TestSig
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , occurence :: Int
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , tdiagnosis :: [Result.Diagnosis]
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke-- | Retrieves the signature out of a basic spec
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke -> Sign.Sign -- Input Signature
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke -> TEST_SIG -- Output Signature
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik LueckemakeSig (AS_BASIC.Basic_spec spec) sig = List.foldl retrieveBasicItem
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke (TestSig{ msign=sig
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , occurence=0
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , tdiagnosis = []
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke-- Helper for makeSig
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckeretrieveBasicItem ::
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke TEST_SIG -- Input Signature
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke -> AS_Anno.Annoted (AS_BASIC.BASIC_ITEMS) -- Input Item
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke -> TEST_SIG -- Output Signature
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik LueckeretrieveBasicItem tsig x =
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke occ = occurence tsig
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke if (occ == 0)
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke (\asig ax-> TestSig{
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke msign = Sign.addToSig (msign asig) $ Id.simpleIdToId ax
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , occurence = occ
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , tdiagnosis = tdiagnosis tsig ++
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke tsig $ (\(AS_BASIC.Pred_item xs _)-> xs) apred
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke (\asig ax-> TestSig{
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke msign = Sign.addToSig (msign asig) $ Id.simpleIdToId ax
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , occurence = occ
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , tdiagnosis = tdiagnosis tsig ++
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , Result.diagString = "Definition of proposition " ++
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke (show $ pretty ax) ++
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke " after first axiom"
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke tsig $ (\(AS_BASIC.Pred_item xs _)-> xs) apred
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke (AS_BASIC.Axiom_items _) -> TestSig { msign = msign tsig
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , occurence = occ + 1
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , tdiagnosis = tdiagnosis tsig ++
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , Result.diagString = "First axiom"
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke-- | Retrieve the formulas out of a basic spec
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckemakeFormulas ::
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Luecke -> [DIAG_FORM]
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik LueckemakeFormulas (AS_BASIC.Basic_spec bspec) sig =
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke List.foldl (\xs bs -> retrieveFormulaItem xs bs sig) [] bspec
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke-- Helper for makeFormulas
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckeretrieveFormulaItem ::
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Luecke -> [DIAG_FORM]
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckeretrieveFormulaItem axs x sig =
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke List.foldl (\xs bs -> addFormula xs bs sig) axs $ numberFormulae ax 0
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Luecke-- Number formulae
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik LueckenumberFormulae :: [AS_Anno.Annoted (AS_BASIC.FORMULA)] -> Integer -> [NUM_FORM]
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik LueckenumberFormulae [] _ = []
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik LueckenumberFormulae (x:xs) i
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik Luecke | label == "" = NumForm{nfformula = x, nfnum = i} : (numberFormulae xs $ i + 1)
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik Luecke | otherwise = NumForm{nfformula = x, nfnum = 0} : (numberFormulae xs $ i)
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke-- Add a formula to a named list of formulas
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik LueckeaddFormula :: [DIAG_FORM]
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Luecke -> [DIAG_FORM]
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik LueckeaddFormula formulae nf sign
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke | isLegal == True = formulae ++
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke formula = makeNamed f i
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke | otherwise = formulae ++
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke formula = makeNamed f i
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke , Result.diagString = "Unknown propositions "
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke ++ (show $ pretty difference)
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke ++ " in formula "
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke ++ (show $ pretty nakedFormula)
080f5fd32e4367ac0c8efb468bc9b75f8c08cc87Dominik Luecke f = nfformula nf
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke nakedFormula = AS_Anno.item f
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke varsOfFormula = propsOfFormula nakedFormula
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke isLegal = Sign.isSubSigOf varsOfFormula sign
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke difference = Sign.sigDiff varsOfFormula sign
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Luecke-- generates a named formula
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik LueckemakeNamed :: AS_Anno.Annoted (AS_BASIC.FORMULA) -> Integer -> AS_Anno.Named (AS_BASIC.FORMULA)
61091743da1a9ed6dfd5e077fdcc972553358962Christian MaedermakeNamed f i = (AS_Anno.makeNamed (if label == "" then "Ax_" ++ show i
61091743da1a9ed6dfd5e077fdcc972553358962Christian Maeder else label) $ AS_Anno.item f)
61091743da1a9ed6dfd5e077fdcc972553358962Christian Maeder { AS_Anno.isAxiom = not isTheorem }
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke isImplies = foldl (\y x -> AS_Anno.isImplies x || y) False annos
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke isImplied = foldl (\y x -> AS_Anno.isImplied x || y) False annos
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke isTheorem = isImplies || isImplied
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Luecke-- Retrives the signature of a formula
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckepropsOfFormula :: AS_BASIC.FORMULA -> Sign.Sign
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckepropsOfFormula (AS_BASIC.Negation form _) = propsOfFormula form
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckepropsOfFormula (AS_BASIC.Implication form1 form2 _) = Sign.unite (propsOfFormula form1)
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke (propsOfFormula form2)
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckepropsOfFormula (AS_BASIC.Equivalence form1 form2 _) = Sign.unite (propsOfFormula form1)
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke (propsOfFormula form2)
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckepropsOfFormula (AS_BASIC.True_atom _) = Sign.emptySig
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckepropsOfFormula (AS_BASIC.False_atom _) = Sign.emptySig
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckepropsOfFormula (AS_BASIC.Predication x) = Sign.Sign{Sign.items = Set.singleton $
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckepropsOfFormula (AS_BASIC.Conjunction xs _) = List.foldl (\ sig frm -> Sign.unite sig $
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke propsOfFormula frm)
548f3850942936a8c6021185c8391dfcd3b03018Dominik LueckepropsOfFormula (AS_BASIC.Disjunction xs _) = List.foldl (\ sig frm -> Sign.unite sig $
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke propsOfFormula frm)
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke-- Basic analysis for propostional logic
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik LueckebasicAnalysis bs sig _
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke | exErrs == False = Result.Result diags $ Just (bs, sigItems, formulae)
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Luecke | otherwise = Result.Result diags $ Nothing
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Luecke bsSig = makeSig bs sig
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke sigItems = msign bsSig
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke bsForm = makeFormulas bs sigItems
648e803bb79a00b1c246d093fe2a2707c6b2cf7bDominik Luecke formulae = map (\x -> formula x) bsForm
c5c3ba1d1735af9ae35b4b1cb6e60777c9ea2fb1Dominik Luecke diags = map (\x -> diagnosis x) bsForm ++ tdiagnosis bsSig
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke-- | Wrapper for the interface defined in Logic.Logic
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckebasicPropositionalAnalysis :: (
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik LueckebasicPropositionalAnalysis (bs, sig, ga) = basicAnalysis bs sig ga
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke-- | Static analysis for symbol maps
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckemkStatSymbMapItem :: [AS_BASIC.SYMB_MAP_ITEMS]
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke -> Result.Result (Map.Map Symbol.Symbol Symbol.Symbol)
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckemkStatSymbMapItem xs =
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke Map.union smap $ statSymbMapItem sitem
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckestatSymbMapItem :: [AS_BASIC.SYMB_OR_MAP]
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckestatSymbMapItem xs =
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke AS_BASIC.Symb sym -> Map.insert (symbToSymbol sym) (symbToSymbol sym) mmap
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke -> Map.insert (symbToSymbol s1) (symbToSymbol s2) mmap
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke-- | Retrieve raw symbols
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckemkStatSymbItems :: [AS_BASIC.SYMB_ITEMS] -> Result.Result [Symbol.Symbol]
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckemkStatSymbItems a = Result.Result
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke , Result.maybeResult = Just $ statSymbItems a
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckestatSymbItems :: [AS_BASIC.SYMB_ITEMS] -> [Symbol.Symbol]
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckestatSymbItems si = concat $ map symbItemsToSymbol si
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckesymbItemsToSymbol :: AS_BASIC.SYMB_ITEMS -> [Symbol.Symbol]
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckesymbItemsToSymbol (AS_BASIC.Symb_items syms _) = map symbToSymbol syms
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckesymbToSymbol :: AS_BASIC.SYMB -> Symbol.Symbol
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckesymbToSymbol (AS_BASIC.Symb_id tok) =
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke Symbol.Symbol{Symbol.symName = Id.simpleIdToId tok}
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke-- | Induce a signature morphism from a source signature and a raw symbol map
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckeinducedFromMorphism :: Map.Map Symbol.Symbol Symbol.Symbol
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckeinducedFromMorphism imap sig =
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke sigItems = Sign.items sig
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke y = Symbol.symName $ Symbol.applySymMap imap symOf
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke-- | Induce a signature morphism from a source signature and a raw symbol map
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckeinducedFromToMorphism :: Map.Map Symbol.Symbol Symbol.Symbol
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik LueckeinducedFromToMorphism imap sig tSig =
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke sigItems = Sign.items sig
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke y = Symbol.symName $ Symbol.applySymMap imap symOf
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke isSub = (Sign.items targetSig) `Set.isSubsetOf` (Sign.items tSig)
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke case isSub of
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke , Result.diagString = "Incompatible mapping"