From 16c7ced0eb86ea344fe62f356ad4b7909d7b67c0 Mon Sep 17 00:00:00 2001 From: mscodescu Date: Fri, 9 Aug 2019 13:41:44 +0200 Subject: [PATCH 01/33] parser for GDOL, first attempt --- Common/Lexer.hs | 4 + Driver/WriteFn.hs | 2 +- Logic/Logic.hs | 7 ++ OWL2/AS.hs | 13 ++- OWL2/DMU2OWL2.hs | 2 +- OWL2/Logic_OWL2.hs | 9 +- OWL2/ManchesterParser.hs | 120 +++++++++++++----------- OWL2/Parse.hs | 80 +++++++++------- OWL2/Print.hs | 4 + OWL2/StaticAnalysis.hs | 10 +- Syntax/AS_Library.der.hs | 11 +++ Syntax/AS_Structured.der.hs | 1 + Syntax/Parse_AS_Architecture.hs | 4 +- Syntax/Parse_AS_Library.hs | 78 +++++++++++++--- Syntax/Parse_AS_Structured.hs | 160 ++++++++++++++++++-------------- 15 files changed, 320 insertions(+), 185 deletions(-) diff --git a/Common/Lexer.hs b/Common/Lexer.hs index cfdb27d95e..56362ece35 100644 --- a/Common/Lexer.hs +++ b/Common/Lexer.hs @@ -291,6 +291,10 @@ commaT = asSeparator "," semiT :: CharParser st Token semiT = pToken $ string ";" << notFollowedBy (char ';') +-- a double colon +doubleColonT :: CharParser st Token +doubleColonT = pToken $ string "::" << notFollowedBy (char ':') + oBraceT :: CharParser st Token oBraceT = asSeparator "{" diff --git a/Driver/WriteFn.hs b/Driver/WriteFn.hs index ffa16e38dd..eb5af303e1 100644 --- a/Driver/WriteFn.hs +++ b/Driver/WriteFn.hs @@ -302,7 +302,7 @@ writeTheory ins nam opts filePrefix ga (printTheory ms OWL2 $ OWL2.prepareBasicTheory th2) "\n" showDiags opts ds when (null sy) - $ case parse (OWL2.basicSpec Map.empty >> eof) f owltext of + $ case parse ((OWL2.basicSpec True) Map.empty >> eof) f owltext of Left err -> putIfVerbose opts 0 $ show err _ -> putIfVerbose opts 3 $ "reparsed: " ++ f writeVerbFile opts f owltext diff --git a/Logic/Logic.hs b/Logic/Logic.hs index 4695d4088b..d1fc2b16ed 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -257,6 +257,9 @@ class (Language lid, PrintTypeConv basic_spec, GetRange basic_spec, Just p -> makeDefault (p, pretty) -- | parser for basic specifications parse_basic_spec :: lid -> Maybe (PrefixMap -> AParser st basic_spec) + -- | parser for macros + parse_macro :: lid -> Maybe (PrefixMap -> AParser st basic_spec) + parse_macro _ = Nothing -- | parser for a single symbol returned as list parseSingleSymbItem :: lid -> Maybe (AParser st symb_items) -- | parser for symbol lists @@ -277,6 +280,10 @@ basicSpecParser :: Syntax lid basic_spec symbol symb_items symb_map_items => Maybe IRI -> lid -> Maybe (PrefixMap -> AParser st basic_spec) basicSpecParser sm = fmap fst . parserAndPrinter sm +macroParser :: Syntax lid basic_spec symbol symb_items symb_map_items + => Maybe IRI -> lid -> Maybe (PrefixMap -> AParser st basic_spec) +macroParser _ = parse_macro + basicSpecPrinter :: Syntax lid basic_spec symbol symb_items symb_map_items => Maybe IRI -> lid -> Maybe (basic_spec -> Doc) basicSpecPrinter sm = fmap snd . parserAndPrinter sm diff --git a/OWL2/AS.hs b/OWL2/AS.hs index 21aaa41e7c..8410d00be7 100644 --- a/OWL2/AS.hs +++ b/OWL2/AS.hs @@ -371,6 +371,7 @@ data EntityType = | DataProperty | AnnotationProperty | NamedIndividual + | UnsolvedEntity deriving (Enum, Bounded, Show, Read, Eq, Ord, Typeable, Data) showEntityType :: EntityType -> String @@ -409,7 +410,7 @@ pairSymbols (Entity lb1 k1 i1) (Entity lb2 k2 i2) = data TypedOrUntyped = Typed Datatype | Untyped (Maybe LanguageTag) deriving (Show, Eq, Ord, Typeable, Data) -data Literal = Literal LexicalForm TypedOrUntyped | NumberLit FloatLit +data Literal = Literal LexicalForm TypedOrUntyped | NumberLit FloatLit | LiteralVar IRI deriving (Show, Eq, Ord, Typeable, Data) -- | non-negative integers given by the sequence of digits @@ -515,12 +516,15 @@ type InverseObjectProperty = ObjectPropertyExpression data ObjectPropertyExpression = ObjectProp ObjectProperty | ObjectInverseOf InverseObjectProperty + | ObjectPropertyVar IRI + | UnsolvedObjProp IRI deriving (Show, Eq, Ord, Typeable, Data) objPropToIRI :: ObjectPropertyExpression -> Individual objPropToIRI opExp = case opExp of ObjectProp u -> u ObjectInverseOf objProp -> objPropToIRI objProp + _ -> error "nyi" type DataPropertyExpression = DataProperty @@ -533,10 +537,12 @@ data DataRange = | DataOneOf [Literal] deriving (Show, Eq, Ord, Typeable, Data) --- * CLASS EXPERSSIONS +-- * CLASS EXPRESSIONS data ClassExpression = Expression Class + | UnsolvedClass IRI + | VarExpression MVarOrTerm | ObjectJunction JunctionType [ClassExpression] | ObjectComplementOf ClassExpression | ObjectOneOf [Individual] @@ -549,6 +555,9 @@ data ClassExpression = | DataCardinality (Cardinality DataPropertyExpression DataRange) deriving (Show, Eq, Ord, Typeable, Data) +data MVarOrTerm = MVar IRI | MUnion IRI IRI -- the name of the head and the name of the tail TODO: should be extended with other terms + deriving (Show, Eq, Ord, Typeable, Data) + -- * ANNOTATIONS data Annotation = Annotation [Annotation] AnnotationProperty AnnotationValue diff --git a/OWL2/DMU2OWL2.hs b/OWL2/DMU2OWL2.hs index 244b798fa3..1459a4b534 100644 --- a/OWL2/DMU2OWL2.hs +++ b/OWL2/DMU2OWL2.hs @@ -81,7 +81,7 @@ runOntoDMU str = if null str then return "" else do return out readOWL :: Monad m => String -> m (Sign, [Named Axiom]) -readOWL str = case runParser (liftM2 const (basicSpec Map.empty) eof) () "" str of +readOWL str = case runParser (liftM2 const ((basicSpec True) Map.empty) eof) () "" str of Left er -> fail $ show er Right ontoFile -> let newont = function Expand (StringMap $ prefixDeclaration ontoFile) ontoFile diff --git a/OWL2/Logic_OWL2.hs b/OWL2/Logic_OWL2.hs index 27dc614f50..6f5161087b 100644 --- a/OWL2/Logic_OWL2.hs +++ b/OWL2/Logic_OWL2.hs @@ -57,6 +57,7 @@ import OWL2.Symbols import OWL2.Taxonomy import OWL2.Theorem import OWL2.ExtractModule +-- import OWL2.Macros data OWL2 = OWL2 @@ -86,9 +87,10 @@ instance Monoid OntologyDocument where OntologyDocument (Map.union p1 p2) $ mappend o1 o2 instance Syntax OWL2 OntologyDocument Entity SymbItems SymbMapItems where - parsersAndPrinters OWL2 = addSyntax "Ship" (basicSpec, ppShipOnt) - $ addSyntax "Manchester" (basicSpec, pretty) - $ makeDefault (basicSpec, pretty) + parsersAndPrinters OWL2 = addSyntax "Ship" (basicSpec True, ppShipOnt) + $ addSyntax "Manchester" (basicSpec True, pretty) + $ makeDefault (basicSpec True, pretty) + parse_macro OWL2 = Just (basicSpec False) parseSingleSymbItem OWL2 = Just symbItem parse_symb_items OWL2 = Just symbItems parse_symb_map_items OWL2 = Just symbMapItems @@ -176,6 +178,7 @@ instance StaticAnalysis OWL2 OntologyDocument Axiom #ifdef UNI_PACKAGE theory_to_taxonomy OWL2 = onto2Tax #endif + instance Logic OWL2 ProfSub OntologyDocument Axiom SymbItems SymbMapItems Sign OWLMorphism Entity RawSymb ProofTree where diff --git a/OWL2/ManchesterParser.hs b/OWL2/ManchesterParser.hs index a02134c880..e30714f09f 100644 --- a/OWL2/ManchesterParser.hs +++ b/OWL2/ManchesterParser.hs @@ -28,6 +28,8 @@ import qualified Common.GlobalAnnotations as GA (PrefixMap) import Text.ParserCombinators.Parsec import qualified Data.Map as Map +import Debug.Trace + optAnnos :: CharParser st a -> CharParser st (Annotations, a) optAnnos p = do as <- optionalAnnos @@ -43,8 +45,8 @@ annotations = do fmap (map $ \ (as, (i, v)) -> Annotation as i v) . sepByComma . optAnnos $ pair uriP annotationValue -descriptionAnnotatedList :: CharParser st [(Annotations, ClassExpression)] -descriptionAnnotatedList = sepByComma $ optAnnos description +descriptionAnnotatedList :: Bool -> CharParser st [(Annotations, ClassExpression)] +descriptionAnnotatedList flag = sepByComma $ optAnnos (description flag) makeFrame :: Extended -> [FrameBit] -> Frame makeFrame ext fbl = Frame ext @@ -85,45 +87,45 @@ datatypeBit = do Just (ans, dr) -> [AnnFrameBit ans $ DatatypeBit dr] ++ map (`AnnFrameBit` AnnotationFrameBit Assertion) as2 -classFrame :: CharParser st Frame -classFrame = do +classFrame :: Bool -> CharParser st Frame +classFrame flag = do pkeyword classC - i <- description - plain <- many classFrameBit + i <- description flag + plain <- many (classFrameBit flag) -- ignore Individuals: ... ! optional $ pkeyword individualsC >> sepByComma individual return $ makeFrame (ClassEntity i) plain -classFrameBit :: CharParser st FrameBit -classFrameBit = do +classFrameBit :: Bool -> CharParser st FrameBit +classFrameBit flag = do pkeyword subClassOfC - ds <- descriptionAnnotatedList + ds <- descriptionAnnotatedList flag return $ ListFrameBit (Just SubClass) $ ExpressionBit ds <|> do e <- equivOrDisjoint - ds <- descriptionAnnotatedList + ds <- descriptionAnnotatedList flag return $ ListFrameBit (Just $ EDRelation e) $ ExpressionBit ds <|> do pkeyword disjointUnionOfC as <- optionalAnnos - ds <- sepByComma description + ds <- sepByComma (description flag) return $ AnnFrameBit as $ ClassDisjointUnion ds <|> do pkeyword hasKeyC as <- optionalAnnos - o <- sepByComma objectPropertyExpr + o <- sepByComma (objectPropertyExpr flag) return $ AnnFrameBit as $ ClassHasKey o [] <|> do as <- annotations return $ AnnFrameBit as $ AnnotationFrameBit Assertion -objPropExprAList :: CharParser st [(Annotations, ObjectPropertyExpression)] -objPropExprAList = sepByComma $ optAnnos objectPropertyExpr +objPropExprAList :: Bool -> CharParser st [(Annotations, ObjectPropertyExpression)] +objPropExprAList flag = sepByComma $ optAnnos (objectPropertyExpr flag) -objectFrameBit :: CharParser st FrameBit -objectFrameBit = do +objectFrameBit :: Bool -> CharParser st FrameBit +objectFrameBit flag = do r <- domainOrRange - ds <- descriptionAnnotatedList + ds <- descriptionAnnotatedList flag return $ ListFrameBit (Just $ DRRelation r) $ ExpressionBit ds <|> do characterKey @@ -131,39 +133,39 @@ objectFrameBit = do return $ ListFrameBit Nothing $ ObjectCharacteristics ds <|> do subPropertyKey - ds <- objPropExprAList + ds <- objPropExprAList flag return $ ListFrameBit (Just SubPropertyOf) $ ObjectBit ds <|> do e <- equivOrDisjoint - ds <- objPropExprAList + ds <- objPropExprAList flag return $ ListFrameBit (Just $ EDRelation e) $ ObjectBit ds <|> do pkeyword inverseOfC - ds <- objPropExprAList + ds <- objPropExprAList flag return $ ListFrameBit (Just InverseOf) $ ObjectBit ds <|> do pkeyword subPropertyChainC as <- optionalAnnos - os <- sepBy1 objectPropertyExpr (keyword oS) + os <- sepBy1 (objectPropertyExpr flag) (keyword oS) return $ AnnFrameBit as $ ObjectSubPropertyChain os <|> do as <- annotations return $ AnnFrameBit as $ AnnotationFrameBit Assertion -objectPropertyFrame :: CharParser st Frame -objectPropertyFrame = do +objectPropertyFrame :: Bool -> CharParser st Frame +objectPropertyFrame flag = do pkeyword objectPropertyC - ouri <- objectPropertyExpr - as <- many objectFrameBit + ouri <- objectPropertyExpr flag + as <- many (objectFrameBit flag) return $ makeFrame (ObjectEntity ouri) as dataPropExprAList :: CharParser st [(Annotations, DataPropertyExpression)] dataPropExprAList = sepByComma $ optAnnos uriP -dataFrameBit :: CharParser st FrameBit -dataFrameBit = do +dataFrameBit :: Bool -> CharParser st FrameBit +dataFrameBit flag = do pkeyword domainC - ds <- descriptionAnnotatedList + ds <- descriptionAnnotatedList flag return $ ListFrameBit (Just (DRRelation ADomain)) $ ExpressionBit ds <|> do pkeyword rangeC @@ -186,11 +188,11 @@ dataFrameBit = do as <- annotations return $ AnnFrameBit as $ AnnotationFrameBit Assertion -dataPropertyFrame :: CharParser st Frame -dataPropertyFrame = do +dataPropertyFrame :: Bool -> CharParser st Frame +dataPropertyFrame flag = do pkeyword dataPropertyC duri <- uriP - as <- many dataFrameBit + as <- many (dataFrameBit flag) return $ makeFrame (SimpleEntity $ mkEntity DataProperty duri) as fact :: CharParser st Fact @@ -204,10 +206,10 @@ fact = do t <- individual return $ ObjectPropertyFact pn (ObjectProp u) t -iFrameBit :: CharParser st FrameBit -iFrameBit = do +iFrameBit :: Bool -> CharParser st FrameBit +iFrameBit flag = do pkeyword typesC - ds <- descriptionAnnotatedList + ds <- descriptionAnnotatedList flag return $ ListFrameBit (Just Types) $ ExpressionBit ds <|> do s <- sameOrDifferent @@ -221,24 +223,26 @@ iFrameBit = do a <- annotations return $ AnnFrameBit a $ AnnotationFrameBit Assertion -individualFrame :: CharParser st Frame -individualFrame = do +individualFrame :: Bool -> CharParser st Frame +individualFrame flag = do pkeyword individualC iuri <- individual - as <- many iFrameBit - return $ makeFrame (SimpleEntity $ mkEntity NamedIndividual iuri) as + as <- many (iFrameBit flag) + let ent = if flag then SimpleEntity $ mkEntity NamedIndividual iuri + else SimpleEntity $ mkEntity UnsolvedEntity iuri + return $ makeFrame ent as -misc :: CharParser st Frame -misc = do +misc :: Bool -> CharParser st Frame +misc flag = do e <- equivOrDisjointKeyword classesC as <- optionalAnnos - ds <- sepByComma description + ds <- sepByComma (description flag) return $ Frame (Misc as) [ListFrameBit (Just $ EDRelation e) $ ExpressionBit $ emptyAnnoList ds] <|> do e <- equivOrDisjointKeyword propertiesC as <- optionalAnnos - es <- sepByComma objectPropertyExpr + es <- sepByComma (objectPropertyExpr flag) -- indistinguishable from dataProperties return $ Frame (Misc as) [ListFrameBit (Just $ EDRelation e) $ ObjectBit $ emptyAnnoList es] @@ -249,24 +253,28 @@ misc = do return $ Frame (Misc as) [ListFrameBit (Just $ SDRelation s) $ IndividualSameOrDifferent $ emptyAnnoList is] -frames :: CharParser st [Frame] -frames = many $ datatypeBit <|> classFrame - <|> objectPropertyFrame <|> dataPropertyFrame <|> individualFrame - <|> annotationPropertyFrame <|> misc +frames :: Bool -> CharParser st [Frame] +frames flag = + many $ datatypeBit <|> classFrame flag + <|> objectPropertyFrame flag <|> dataPropertyFrame flag <|> individualFrame flag + <|> annotationPropertyFrame <|> misc flag -basicSpec :: GA.PrefixMap -> CharParser st OntologyDocument -basicSpec pm = do +-- the Bool flag is true for ontologies and false for macros +basicSpec :: Bool -> GA.PrefixMap -> CharParser st OntologyDocument +basicSpec flag pm = do nss <- many nsEntry ou <- option nullIRI $ pkeyword ontologyC >> option nullIRI uriP ie <- many importEntry ans <- many annotations - as <- frames + as <- frames flag if null nss && null ie && null ans && null as && ou == nullIRI then fail "empty ontology" - else return $ OntologyDocument - (Map.union (Map.fromList $ map (\ (p, q) -> (p, showIRICompact q)) nss) - (convertPrefixMap pm)) - (emptyOntology as) - { imports = ie - , ann = ans - , name = ou } + else do + let o = OntologyDocument + (Map.union (Map.fromList $ map (\ (p, q) -> (p, showIRICompact q)) nss) + (convertPrefixMap pm)) + (emptyOntology as) + { imports = ie + , ann = ans + , name = ou } + trace ("o:" ++ show o) $ return o diff --git a/OWL2/Parse.hs b/OWL2/Parse.hs index 0fa3dc19ea..9c78ac7bc5 100644 --- a/OWL2/Parse.hs +++ b/OWL2/Parse.hs @@ -329,15 +329,15 @@ keyword :: String -> CharParser st String keyword s = keywordNotFollowedBy s (alphaNum <|> char '_') -- base OWLClass excluded -atomic :: CharParser st ClassExpression -atomic = parensP description +atomic :: Bool -> CharParser st ClassExpression +atomic flag = parensP (description flag) <|> fmap ObjectOneOf (bracesP $ sepByComma individual) -objectPropertyExpr :: CharParser st ObjectPropertyExpression -objectPropertyExpr = do +objectPropertyExpr :: Bool -> CharParser st ObjectPropertyExpression +objectPropertyExpr flag = do keyword inverseS - fmap ObjectInverseOf objectPropertyExpr - <|> fmap ObjectProp uriP + fmap ObjectInverseOf (objectPropertyExpr flag) + <|> fmap (if flag then ObjectProp else UnsolvedObjProp) uriP -- creating the facet-value pairs facetValuePair :: CharParser st (ConstrainingFacet, RestrictionValue) @@ -416,23 +416,23 @@ individualOrConstantList = do Right c -> fmap (Right . (c :)) $ optionL $ commaP >> sepByComma literal -primaryOrDataRange :: CharParser st (Either ClassExpression DataRange) -primaryOrDataRange = do +primaryOrDataRange :: Bool -> CharParser st (Either ClassExpression DataRange) +primaryOrDataRange flag = do ns <- many $ keyword notS -- allows multiple not before primary ed <- do u <- datatypeUri - fmap Left (restrictionAny $ ObjectProp u) + fmap Left ((restrictionAny flag) $ ObjectProp u) <|> fmap (Right . DataType u) (bracketsP $ sepByComma facetValuePair) <|> return (if isDatatypeKey u then Right $ DataType u [] - else Left $ Expression u) -- could still be a datatypeUri + else Left $ (if flag then Expression else UnsolvedClass) u) -- could still be a datatypeUri <|> do e <- bracesP individualOrConstantList return $ case e of Left us -> Left $ ObjectOneOf us Right cs -> Right $ DataOneOf cs - <|> fmap Left restrictionOrAtomic + <|> fmap Left (restrictionOrAtomic flag) return $ if even (length ns) then ed else case ed of Left d -> Left $ ObjectComplementOf d @@ -444,8 +444,8 @@ mkObjectJunction ty ds = case nubOrd ds of [x] -> x ns -> ObjectJunction ty ns -restrictionAny :: ObjectPropertyExpression -> CharParser st ClassExpression -restrictionAny opExpr = do +restrictionAny :: Bool -> ObjectPropertyExpression -> CharParser st ClassExpression +restrictionAny flag opExpr = do keyword valueS e <- individualOrConstant case e of @@ -458,7 +458,7 @@ restrictionAny opExpr = do return $ ObjectHasSelf opExpr <|> do -- sugar keyword onlysomeS - ds <- bracketsP $ sepByComma description + ds <- bracketsP $ sepByComma (description flag) let as = map (ObjectValuesFrom SomeValuesFrom opExpr) ds o = ObjectValuesFrom AllValuesFrom opExpr $ mkObjectJunction UnionOf ds @@ -469,7 +469,7 @@ restrictionAny opExpr = do return $ ObjectValuesFrom SomeValuesFrom opExpr $ ObjectOneOf [iu] <|> do v <- someOrOnly - pr <- primaryOrDataRange + pr <- primaryOrDataRange flag case pr of Left p -> return $ ObjectValuesFrom v opExpr p Right r -> case opExpr of @@ -477,7 +477,7 @@ restrictionAny opExpr = do _ -> unexpected $ "dataRange after " ++ showQuantifierType v <|> do (c, n) <- card - mp <- optionMaybe primaryOrDataRange + mp <- optionMaybe (primaryOrDataRange flag) case mp of Nothing -> return $ ObjectCardinality $ Cardinality c n opExpr Nothing Just pr -> case pr of @@ -488,34 +488,44 @@ restrictionAny opExpr = do return $ DataCardinality $ Cardinality c n dpExpr $ Just r _ -> unexpected $ "dataRange after " ++ showCardinalityType c -restriction :: CharParser st ClassExpression -restriction = objectPropertyExpr >>= restrictionAny +restriction :: Bool -> CharParser st ClassExpression +restriction flag = (objectPropertyExpr flag) >>= (restrictionAny flag) -restrictionOrAtomic :: CharParser st ClassExpression -restrictionOrAtomic = do - opExpr <- objectPropertyExpr - restrictionAny opExpr <|> case opExpr of - ObjectProp euri -> return $ Expression euri +restrictionOrAtomic :: Bool -> CharParser st ClassExpression +restrictionOrAtomic flag = do + opExpr <- objectPropertyExpr flag + (restrictionAny flag) opExpr <|> case opExpr of + ObjectProp euri -> return $ (if flag then Expression else UnsolvedClass) euri + UnsolvedObjProp euri -> return $ UnsolvedClass euri _ -> unexpected "inverse object property" - <|> atomic + <|> atomic flag optNot :: (a -> a) -> CharParser st a -> CharParser st a optNot f p = (keyword notS >> fmap f p) <|> p -primary :: CharParser st ClassExpression -primary = optNot ObjectComplementOf restrictionOrAtomic - -conjunction :: CharParser st ClassExpression -conjunction = do - curi <- fmap Expression $ try (owlClassUri << keyword thatS) - rs <- sepBy1 (optNot ObjectComplementOf restriction) $ keyword andS +primary :: Bool -> CharParser st ClassExpression +primary flag = optNot ObjectComplementOf (restrictionOrAtomic flag) + +conjunction :: Bool -> CharParser st ClassExpression +conjunction flag = do + _ <- keyword andS + _ <- oParenT + hd <- uriP + _ <- commaT + tl <- uriP + _ <- cParenT + return $ VarExpression $ MUnion hd tl + <|> do + curi <- fmap (if flag then Expression else UnsolvedClass) + $ try (owlClassUri << keyword thatS) + rs <- sepBy1 (optNot ObjectComplementOf (restriction flag)) $ keyword andS return $ mkObjectJunction IntersectionOf $ curi : rs <|> fmap (mkObjectJunction IntersectionOf) - (sepBy1 primary $ keyword andS) + (sepBy1 (primary flag) $ keyword andS) -description :: CharParser st ClassExpression -description = - fmap (mkObjectJunction UnionOf) $ sepBy1 conjunction $ keyword orS +description :: Bool -> CharParser st ClassExpression +description flag = + fmap (mkObjectJunction UnionOf) $ sepBy1 (conjunction flag) $ keyword orS entityType :: CharParser st EntityType entityType = choice $ map (\ f -> keyword (show f) >> return f) diff --git a/OWL2/Print.hs b/OWL2/Print.hs index 87a6f01520..cb31d1f580 100644 --- a/OWL2/Print.hs +++ b/OWL2/Print.hs @@ -119,6 +119,7 @@ instance Pretty Literal where Nothing -> empty Just tag2 -> text asP <> text tag2 NumberLit f -> text (show f) + _ -> error "nyi" instance Pretty ObjectPropertyExpression where pretty = printObjPropExp @@ -127,6 +128,8 @@ printObjPropExp :: ObjectPropertyExpression -> Doc printObjPropExp obExp = case obExp of ObjectProp ou -> pretty ou ObjectInverseOf iopExp -> keyword inverseS <+> printObjPropExp iopExp + ObjectPropertyVar ou -> text "var" <+> pretty ou + UnsolvedObjProp ou -> text "unsolved" <+> pretty ou printFV :: (ConstrainingFacet, RestrictionValue) -> Doc printFV (facet, restValue) = pretty (fromCF facet) <+> pretty restValue @@ -159,6 +162,7 @@ printDataRange dr = case dr of instance Pretty ClassExpression where pretty desc = case desc of Expression ocUri -> printIRI ocUri + UnsolvedClass anIri -> text "unsolved" <+> printIRI anIri ObjectJunction ty ds -> let (k, p) = case ty of UnionOf -> (orS, pretty) diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 50499cbf14..804b3afdc7 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -38,6 +38,8 @@ import Control.Monad import Logic.Logic +import Debug.Trace + -- | Error messages for static analysis failMsg :: Entity -> ClassExpression -> Result a failMsg (Entity _ ty e) desc = @@ -96,7 +98,8 @@ checkObjPropList s ol = do unless (and ls) $ fail $ "undeclared object properties:\n" ++ showDoc (map (\o -> case o of ObjectProp _ -> o - ObjectInverseOf x -> x) ol) "" + ObjectInverseOf x -> x + _ -> error "unsolved string or variable") ol) "" checkDataPropList :: Sign -> [DataPropertyExpression] -> Result () checkDataPropList s dl = do @@ -181,6 +184,7 @@ checkClassExpression s desc = Nothing -> return desc Just d -> checkDataRange s d >> return desc else datErr dExp + _ -> error "unsolved string or class variable" checkFact :: Sign -> Fact -> Result () checkFact s f = case f of @@ -400,9 +404,9 @@ generateLabelMap sig = foldr (\ (Frame ext fbl) -> case ext of -- | adding annotations for theorems anaAxiom :: Axiom -> Named Axiom -anaAxiom ax = findImplied ax $ makeNamed name ax +anaAxiom ax = findImplied ax $ makeNamed nm ax where names = getNames ax - name = concat $ intersperse "_" names + nm = concat $ intersperse "_" names findImplied :: Axiom -> Named Axiom -> Named Axiom findImplied ax sent = diff --git a/Syntax/AS_Library.der.hs b/Syntax/AS_Library.der.hs index 5a8d050886..3fd3bf3a91 100644 --- a/Syntax/AS_Library.der.hs +++ b/Syntax/AS_Library.der.hs @@ -82,8 +82,13 @@ data LIB_ITEM = Spec_defn SPEC_NAME GENERICITY (Annoted SPEC) Range -- pos: "newlogic", Logic_name, "=", opt "end" | Newcomorphism_defn ComorphismDef Range -- pos: "newcomorphism", Comorphism_name, "=", opt "end" + | Pattern_defn SPEC_NAME [PatternParam] IMPORTED LocalOrSpec Range deriving (Show, Typeable) +data LocalOrSpec = Local_pattern [LIB_ITEM] (Annoted SPEC) | + Spec_pattern (Annoted SPEC) + deriving (Show, Typeable) + data AlignSem = SingleDomain | GlobalDomain | ContextualizedDomain deriving (Show, Typeable, Bounded, Enum) @@ -110,6 +115,12 @@ addDownloadAux unique j = (if unique then UniqueItem i else ItemMaps [ItemNameMap i Nothing]) $ iriPos i +data PatternParam = OntoParam Bool (Annoted SPEC) | ListParam OntoList + deriving (Show, Typeable) + -- the bool flag is true for optional parameters + +data OntoList = EmptyParamList | OntoListCons [Annoted SPEC] deriving (Show, Typeable) + data GENERICITY = Genericity PARAMS IMPORTED Range deriving (Show, Typeable) -- pos: many of "[","]" opt ("given", commas) diff --git a/Syntax/AS_Structured.der.hs b/Syntax/AS_Structured.der.hs index f8b2cc376f..aa55a3067c 100644 --- a/Syntax/AS_Structured.der.hs +++ b/Syntax/AS_Structured.der.hs @@ -75,6 +75,7 @@ data SPEC = Basic_spec G_basic_spec Range -- pos: "combine" | Apply IRI G_basic_spec Range -- pos: "apply", use a basic spec parser to parse a sentence + | UnsolvedName IRI Range deriving (Show, Typeable) data Network = Network [LABELED_ONTO_OR_INTPR_REF] [IRI] Range diff --git a/Syntax/Parse_AS_Architecture.hs b/Syntax/Parse_AS_Architecture.hs index 0308e0018a..668902c613 100644 --- a/Syntax/Parse_AS_Architecture.hs +++ b/Syntax/Parse_AS_Architecture.hs @@ -119,7 +119,7 @@ unitSpec l = NOTE: this can also be a spec name. If this is the case, this unit spec will be converted on the static analysis stage. See Static.AnalysisArchitecture.ana_UNIT_SPEC. -} - do gps@(gs : gss, _) <- annoParser (caslGroupSpec l) `separatedBy` crossT + do gps@(gs : gss, _) <- annoParser (caslGroupSpec l True) `separatedBy` crossT let rest = unitRestType l gps if null gss then option ( {- case item gs of @@ -130,7 +130,7 @@ unitSpec l = unitRestType :: LogicGraph -> ([Annoted SPEC], [Token]) -> AParser st UNIT_SPEC unitRestType l (gs, ps) = do a <- asKey funS -- see Note - g <- annoParser $ caslGroupSpec l + g <- annoParser $ caslGroupSpec l True -- TODO: if wrong here and line 122, set True to False return (Unit_type gs g $ catRange (ps ++ [a])) {- Note: the minus from funS (and crossT) would be misinterpreted as diff --git a/Syntax/Parse_AS_Library.hs b/Syntax/Parse_AS_Library.hs index 3a950aec3a..d027986f3a 100644 --- a/Syntax/Parse_AS_Library.hs +++ b/Syntax/Parse_AS_Library.hs @@ -41,6 +41,8 @@ import Control.Monad import Framework.AS +import Debug.Trace + lGAnnos :: LogicGraph -> AParser st (LogicGraph, [Annotation]) lGAnnos lG = do as <- annos @@ -119,7 +121,7 @@ specDefn l = do n <- hetIRI l g <- generics l e <- equalT - a <- aSpec l + a <- aSpec l True -- OMS, not macros q <- optEnd return . Spec_defn n g a . catRange $ [s, e] ++ maybeToList q @@ -166,9 +168,9 @@ queryDefn l = do lg <- lookupCurrentLogic "query-defn" l (vs, cs) <- parseItemsList lg w <- asKey whereS - Basic_spec bs _ <- lookupCurrentSyntax "query-defn" l >>= basicSpec l + Basic_spec bs _ <- lookupCurrentSyntax "query-defn" l >>= basicSpec l True i <- asKey inS - oms <- aSpec l + oms <- aSpec l True mt <- optionMaybe $ asKey "along" >> hetIRI l o <- optEnd return . Query_defn n vs bs oms mt . catRange @@ -214,7 +216,7 @@ libItem l = specDefn l s2 <- colonT et <- equivType l s3 <- equalT - sp <- fmap MkOms $ aSpec l + sp <- fmap MkOms $ aSpec l True ep <- optEnd return . Equiv_defn en et sp . catRange $ s1 : s2 : s3 : maybeToList ep @@ -305,11 +307,37 @@ libItem l = specDefn l (catRange ([s1, s2, s3, s4, s5, s6, s7, s8] ++ maybeToList q))) <|> -- just a spec (turned into "spec spec = sp") do p1 <- getPos - a <- aSpec l + a <- aSpec l True p2 <- getPos if p1 == p2 then fail "cannot parse spec" else return (Spec_defn nullIRI (Genericity (Params []) (Imported []) nullRange) a nullRange) + <|> -- pattern defn + patternParser l + +patternParser :: LogicGraph -> AParser st LIB_ITEM +patternParser l = do + s1 <- asKey "pattern" + n <- hetIRI l + (pars, ps1) <- macroParams l + (imp, ps2) <- option ([], nullRange) (imports l) + s2 <- equalT + a <- localOrSpec l + q <- optEnd + let pattern = Pattern_defn n pars (Imported imp) a nullRange + trace ("pattern:" ++ show pattern) $ return . Pattern_defn n pars (Imported imp) a + . catRange $ [s1, s2] ++ maybeToList q + +localOrSpec :: LogicGraph -> AParser st LocalOrSpec +localOrSpec l = do + _s1 <- asKey "let" + (locals, _) <- separatedBy (patternParser l) skip -- this might have problems + _s2 <- asKey "in" + a <- aSpec l False + return $ Local_pattern locals a + <|> do + a <- aSpec l False -- TODO: this makes sure that the bodies are parsed as macros and not as ontologies + return $ Spec_pattern a downloadItems :: LogicGraph -> AParser st (DownloadItems, [Token]) downloadItems l = do @@ -332,12 +360,12 @@ entailType l = do i <- asKey inS nw <- parseNetwork l r <- asKey entailsS - g <- groupSpec l + g <- groupSpec l True return . OMSInNetwork n nw g $ catRange [i, r] _ -> fail "OMSName expected" omsOrNetwork :: LogicGraph -> AParser st OmsOrNetwork -omsOrNetwork l = fmap (MkOms . emptyAnno) $ groupSpec l +omsOrNetwork l = fmap (MkOms . emptyAnno) $ groupSpec l True -- no macros in networks equivType :: LogicGraph -> AParser st EQUIV_TYPE equivType l = do @@ -361,16 +389,16 @@ alignArity = choice $ map (\ a -> asKey (showAlignArity a) >> return a) -- | Parse view type also used in alignments viewType :: LogicGraph -> AParser st VIEW_TYPE viewType l = do - sp1 <- annoParser (groupSpec l) + sp1 <- annoParser (groupSpec l True) s <- asKey toS - sp2 <- annoParser (groupSpec l) + sp2 <- annoParser (groupSpec l True) return $ View_type sp1 sp2 $ tokPos s moduleType :: LogicGraph -> AParser st MODULE_TYPE moduleType l = do - sp1 <- aSpec l + sp1 <- aSpec l True s <- asKey ofS - sp2 <- aSpec l + sp2 <- aSpec l True return $ Module_type sp1 sp2 (tokPos s) restrictionSignature :: LogicGraph -> AParser st G_symb_items_list @@ -415,14 +443,38 @@ params l = do param :: LogicGraph -> AParser st (Annoted SPEC, Range) param l = do b <- oBracketT - pa <- aSpec l + pa <- aSpec l True + -- macros not allowed in params of CASL generic specs c <- cBracketT return (pa, toRange b [] c) +macroParams :: LogicGraph -> AParser st ([PatternParam], Range) +macroParams l = do + _o <- oBracketT + (pars, _ps) <- separatedBy (macroParam l) semiT + _c <- cBracketT + return (pars, nullRange) --TODO: get the range right + + +macroParam :: LogicGraph -> AParser st PatternParam +macroParam l = do + (elems, _ps) <- separatedBy (elemParser l) doubleColonT + case elems of + [(x, isOpt)] -> return $ OntoParam isOpt x + _ -> return $ ListParam $ OntoListCons $ map fst elems + +elemParser :: LogicGraph -> AParser st (Annoted SPEC, Bool) +elemParser lg = do + optParam <- option nullTok $ asKey "?" + a <- aSpec lg True + return (a, (optParam /= nullTok)) + -- TODO: always True, macros not allowed as parameters + -- TODO: for now, but we need to handle the empty list + imports :: LogicGraph -> AParser st ([Annoted SPEC], Range) imports l = do s <- asKey givenS - (sps, ps) <- separatedBy (annoParser $ groupSpec l) anComma + (sps, ps) <- separatedBy (annoParser $ groupSpec l True) anComma -- macro not allowed in imports, always True return (sps, catRange (s : ps)) newlogicP :: AParser st (IRI, Token) diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index b83441f8e4..a5ccdefaaa 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -271,51 +271,51 @@ flatExts = concatMap $ \ as -> case item as of sps -> sps _ -> [as] -spec :: LogicGraph -> AParser st (Annoted SPEC) -spec l = do - sp1 <- annoParser2 (specThen l) +spec :: LogicGraph -> Bool -> AParser st (Annoted SPEC) +spec l flag = do + sp1 <- annoParser2 (specThen l flag) option sp1 $ do k <- asKey "bridge" rs <- many (renaming l) - sp2 <- annoParser2 (specThen l) + sp2 <- annoParser2 (specThen l flag) return . emptyAnno . Bridge sp1 rs sp2 $ tokPos k -specThen :: LogicGraph -> AParser st (Annoted SPEC) -specThen l = do - (sps, ps) <- annoParser2 (specA l) `separatedBy` asKey thenS +specThen :: LogicGraph -> Bool -> AParser st (Annoted SPEC) +specThen l flag = do + (sps, ps) <- annoParser2 (specA l flag) `separatedBy` asKey thenS return $ case sps of [sp] -> sp _ -> emptyAnno (Extension (flatExts sps) $ catRange ps) -specA :: LogicGraph -> AParser st (Annoted SPEC) -specA l = do - sp <- annoParser2 $ specB l +specA :: LogicGraph -> Bool -> AParser st (Annoted SPEC) +specA l flag = do + sp <- annoParser2 $ specB l flag option sp $ do t <- asKey andS <|> asKey intersectS - (sps, ts) <- annoParser2 (specB l) `separatedBy` asKey (tokStr t) + (sps, ts) <- annoParser2 (specB l flag) `separatedBy` asKey (tokStr t) let cons = case tokStr t of "and" -> Union _ -> Intersection return $ emptyAnno (cons (sp : sps) $ catRange (t : ts)) -specB :: LogicGraph -> AParser st (Annoted SPEC) -specB l = do +specB :: LogicGraph -> Bool -> AParser st (Annoted SPEC) +specB l flag = do p1 <- asKey localS - sp1 <- aSpec l + sp1 <- aSpec l flag p2 <- asKey withinS - sp2 <- annoParser2 $ specB l + sp2 <- annoParser2 $ specB l flag return (emptyAnno $ Local_spec sp1 sp2 $ tokPos p1 `appRange` tokPos p2) - <|> specC l + <|> specC l flag -specC :: LogicGraph -> AParser st (Annoted SPEC) -specC lG = do - let spD = annoParser $ specD lG +specC :: LogicGraph -> Bool -> AParser st (Annoted SPEC) +specC lG flag = do + let spD = annoParser $ specD lG flag rest = spD >>= translationList [ (`fmap` extraction lG) . Extraction , (`fmap` renaming lG) . Translation , (`fmap` restriction lG) . Reduction , (`fmap` approximation lG) . Approximation - , (`fmap` filtering lG) . Filtering + , (`fmap` filtering lG flag) . Filtering , (`fmap` minimization lG) . Minimization] l@(Logic lid) <- lookupCurrentLogic "specC" lG {- if the current logic has an associated data_logic, @@ -326,8 +326,8 @@ specC lG = do Nothing -> rest Just lD@(Logic dl) -> do p1 <- asKey dataS -- not a keyword - sp1 <- annoParser $ basicSpec lG (lD, Nothing) - <|> caslGroupSpec (setCurLogic (language_name dl) lG) + sp1 <- annoParser $ basicSpec lG flag (lD, Nothing) + <|> caslGroupSpec (setCurLogic (language_name dl) lG) flag sp2 <- spD return (emptyAnno $ Data lD l sp1 sp2 $ tokPos p1) <|> rest @@ -393,16 +393,16 @@ extraction lg = do (is,commas) <- separatedBy (hetIRI lg) commaT return . ExtractOrRemove (tokStr p == extractS) is $ catRange (p:commas) -filtering :: LogicGraph -> AParser st FILTERING -filtering lg = do +filtering :: LogicGraph -> Bool -> AParser st FILTERING +filtering lg flag = do p <- asKey selectS <|> asKey rejectS - filtering_aux p lg + filtering_aux p lg flag -filtering_aux :: Token -> LogicGraph -> AParser st FILTERING -filtering_aux p lg = +filtering_aux :: Token -> LogicGraph -> Bool -> AParser st FILTERING +filtering_aux p lg flag = do s <- lookupCurrentSyntax "filtering" lg - Basic_spec bs _ <- basicSpec lg s + Basic_spec bs _ <- basicSpec lg flag s return . FilterBasicSpec (tokStr p == selectS) bs $ tokPos p <|> do @@ -422,52 +422,53 @@ groupSpecLookhead lG = minimizeKey :: AParser st Token minimizeKey = choice $ map asKey [minimizeS, closedworldS, "maximize"] -specD :: LogicGraph -> AParser st SPEC +specD :: LogicGraph -> Bool -> AParser st SPEC -- do some lookahead for free spec, to avoid clash with free type -specD l = do +specD l flag = do p <- asKey freeS `followedWith` groupSpecLookhead l - sp <- annoParser $ groupSpec l + sp <- annoParser $ groupSpec l flag return (Free_spec sp $ tokPos p) <|> do p <- asKey cofreeS `followedWith` groupSpecLookhead l - sp <- annoParser $ groupSpec l + sp <- annoParser $ groupSpec l flag return (Cofree_spec sp $ tokPos p) <|> do p <- minimizeKey `followedWith` groupSpecLookhead l - sp <- annoParser $ groupSpec l + sp <- annoParser $ groupSpec l flag return (Minimize_spec sp $ tokPos p) <|> do p <- asKey closedS `followedWith` groupSpecLookhead l - sp <- annoParser $ groupSpec l + sp <- annoParser $ groupSpec l flag return (Closed_spec sp $ tokPos p) - <|> specE l + <|> specE l flag -specE :: LogicGraph -> AParser st SPEC -specE l = logicSpec l +specE :: LogicGraph -> Bool -> AParser st SPEC +specE l flag = logicSpec l flag <|> combineSpec l - <|> (lookAhead (groupSpecLookhead l) >> groupSpec l) - <|> (lookupCurrentSyntax "basic spec" l >>= basicSpec l) + <|> (lookAhead (groupSpecLookhead l) >> groupSpec l flag) + <|> (lookupCurrentSyntax "basic spec" l >>= basicSpec l flag) -- | call a logic specific parser if it exists callParser :: Maybe (AParser st a) -> String -> String -> AParser st a callParser p name itemType = fromMaybe (unexpected $ "no " ++ itemType ++ " parser for " ++ name) p -basicSpec :: LogicGraph -> (AnyLogic, Maybe IRI) -> AParser st SPEC -basicSpec lG (Logic lid, sm) = do +basicSpec :: LogicGraph -> Bool -> (AnyLogic, Maybe IRI) -> AParser st SPEC +basicSpec lG flag (Logic lid, sm) = do p <- getPos + let whichParser = if flag then basicSpecParser sm lid else macroParser sm lid bspec <- callParser - (liftM (\ ps -> ps (prefixes lG)) (basicSpecParser sm lid)) + (liftM (\ ps -> ps (prefixes lG)) whichParser) (showSyntax lid sm) "basic specification" q <- getPos return $ Basic_spec (G_basic_spec lid bspec) $ Range [p, q] -logicSpec :: LogicGraph -> AParser st SPEC -logicSpec lG = do +logicSpec :: LogicGraph -> Bool -> AParser st SPEC +logicSpec lG flag = do (s1, ln) <- qualification lG many $ qualification lG -- ignore multiple qualifications for now s2 <- colonT - sp <- annoParser $ specD $ setLogicName ln lG + sp <- annoParser $ specD (setLogicName ln lG) flag return $ Qualified_spec ln sp $ toRange s1 [] s2 combineSpec :: LogicGraph -> AParser st SPEC @@ -499,55 +500,76 @@ lookupAndSetComorphismName c lg = do Comorphism cid <- lookupComorphism c lg return $ setCurLogic (language_name $ targetLogic cid) lg -aSpec :: LogicGraph -> AParser st (Annoted SPEC) -aSpec = annoParser2 . spec +aSpec :: LogicGraph -> Bool -> AParser st (Annoted SPEC) +aSpec lg flag = annoParser2 $ spec lg flag -- | grouped spec or spec-inst without optional DOL import -caslGroupSpec :: LogicGraph -> AParser st SPEC +caslGroupSpec :: LogicGraph -> Bool -> AParser st SPEC caslGroupSpec = groupSpecAux False -- | grouped spec or spec-inst with optional import -groupSpec :: LogicGraph -> AParser st SPEC -groupSpec = groupSpecAux True +groupSpec :: LogicGraph -> Bool -> AParser st SPEC +groupSpec = groupSpecAux True -groupSpecAux :: Bool -> LogicGraph -> AParser st SPEC -groupSpecAux withImport l = do +groupSpecAux :: Bool -> LogicGraph -> Bool -> AParser st SPEC +groupSpecAux withImport l flag = do b <- oBraceT do c <- cBraceT return $ EmptySpec $ catRange [b, c] <|> do - a <- aSpec l + a <- aSpec l flag addAnnos c <- cBraceT return $ Group a $ catRange [b, c] <|> do n <- hetIRI l - (f, ps) <- fitArgs l + {- (f, ps) <- fitArgs l flag mi <- if withImport then optionMaybe (hetIRI l) else return Nothing - return (Spec_inst n f mi ps) + case f of + [] -> return $ UnsolvedName n nullRange + _ -> return (Spec_inst n f mi ps) + -} + mf <- optionMaybe (fitArgsPattern l flag withImport) + case mf of + Nothing -> return $ UnsolvedName n nullRange + Just ((f, mi), ps) -> return $ Spec_inst n f mi ps + +fitArgsPattern :: LogicGraph -> Bool -> Bool -> AParser st (([Annoted FIT_ARG], Maybe IRI), Range) +fitArgsPattern l flag withImport = do + o <- oBracketT + (fas, _) <- separatedBy (fitArg l flag) semiT + let (fas1, _ps) = unzip fas + c <- cBracketT + return ((fas1, Nothing), toRange o [] c) -fitArgs :: LogicGraph -> AParser st ([Annoted FIT_ARG], Range) -fitArgs l = do - fas <- many (fitArg l) +fitArgs :: LogicGraph -> Bool -> AParser st ([Annoted FIT_ARG], Range) +fitArgs l flag = do + fas <- many (fitArg l flag) let (fas1, ps) = unzip fas return (fas1, concatMapRange id ps) -fitArg :: LogicGraph -> AParser st (Annoted FIT_ARG, Range) -fitArg l = do - b <- oBracketT - fa <- annoParser (fittingArg l) - c <- cBracketT - return (fa, toRange b [] c) - -fittingArg :: LogicGraph -> AParser st FIT_ARG -fittingArg l = do +fitArg :: LogicGraph -> Bool -> AParser st (Annoted FIT_ARG, Range) +fitArg l flag = do + -- b <- oBracketT + fa <- annoParser (fitString l flag) -- TODO: this should be a :: separated list of fittingArgs + -- TODO: how to parse SP[a; ; c] if the optional argument is missing? + -- c <- cBracketT + return (fa, nullRange) + +fitString :: LogicGraph -> Bool -> AParser st FIT_ARG +fitString _l _ = do + s <- compoundIriCurie -- TODO: hetIRI did not parse compound Ids + return $ Fit_spec (Annoted (UnsolvedName s nullRange) nullRange [][]) [] nullRange + +fittingArg :: LogicGraph -> Bool -> AParser st FIT_ARG +fittingArg l flag = do s <- asKey viewS vn <- hetIRI l - (fa, ps) <- fitArgs l + (fa, ps) <- fitArgs l flag return (Fit_view vn fa (tokPos s `appRange` ps)) <|> do - sp <- aSpec l + sp <- aSpec l flag (symbit, ps) <- option ([], nullRange) $ do s <- asKey fitS (m, qs) <- parseMapping l From 4829c8abe4e8f5431c8ebda0acd278ebb2706a5e Mon Sep 17 00:00:00 2001 From: mscodescu Date: Mon, 12 Aug 2019 14:43:24 +0200 Subject: [PATCH 02/33] hacked semicolons and :: as terminating, pattern is starting, use lookAhead for empty optional arg --- Common/Token.hs | 5 +++-- Syntax/Parse_AS_Library.hs | 2 +- Syntax/Parse_AS_Structured.hs | 22 ++++++++++++++++------ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Common/Token.hs b/Common/Token.hs index 2c1e243b41..8a7b57b28e 100644 --- a/Common/Token.hs +++ b/Common/Token.hs @@ -113,14 +113,15 @@ criticalKeywords = terminatingKeywords ++ startingKeywords terminatingKeywords :: [String] terminatingKeywords = [ andS, endS, extractS, fitS, forgetS, hideS, keepS, rejectS, removeS, - revealS, selectS, thenS, withS, withinS, ofS, forS, toS, intersectS] + revealS, selectS, thenS, withS, withinS, ofS, forS, toS, intersectS, + ";", "::"] -- | keywords starting a library item startingKeywords :: [String] startingKeywords = [ archS, fromS, logicS, newlogicS, refinementS, specS, unitS, viewS , ontologyS, alignmentS, networkS, equivalenceS, newcomorphismS - , interpretationS, entailmentS ] + , interpretationS, entailmentS, "pattern" ] -- | keywords that may follow a defining equal sign otherStartKeywords :: [String] diff --git a/Syntax/Parse_AS_Library.hs b/Syntax/Parse_AS_Library.hs index d027986f3a..ca2451fe29 100644 --- a/Syntax/Parse_AS_Library.hs +++ b/Syntax/Parse_AS_Library.hs @@ -331,7 +331,7 @@ patternParser l = do localOrSpec :: LogicGraph -> AParser st LocalOrSpec localOrSpec l = do _s1 <- asKey "let" - (locals, _) <- separatedBy (patternParser l) skip -- this might have problems + locals <- many1 (patternParser l) -- separatedBy (patternParser l) skip -- this might have problems _s2 <- asKey "in" a <- aSpec l False return $ Local_pattern locals a diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index a5ccdefaaa..1618218216 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -55,6 +55,8 @@ import Data.Char import Data.Maybe import Control.Monad +import Debug.Trace + expandCurieM :: LogicGraph -> IRI -> GenParser Char st IRI expandCurieM lG i = case expandCurie (prefixes lG) i of @@ -533,7 +535,8 @@ groupSpecAux withImport l flag = do mf <- optionMaybe (fitArgsPattern l flag withImport) case mf of Nothing -> return $ UnsolvedName n nullRange - Just ((f, mi), ps) -> return $ Spec_inst n f mi ps + Just ((f, mi), ps) -> let inst = Spec_inst n f mi ps + in trace ("inst:" ++ show inst) $ return inst fitArgsPattern :: LogicGraph -> Bool -> Bool -> AParser st (([Annoted FIT_ARG], Maybe IRI), Range) fitArgsPattern l flag withImport = do @@ -551,11 +554,18 @@ fitArgs l flag = do fitArg :: LogicGraph -> Bool -> AParser st (Annoted FIT_ARG, Range) fitArg l flag = do - -- b <- oBracketT - fa <- annoParser (fitString l flag) -- TODO: this should be a :: separated list of fittingArgs - -- TODO: how to parse SP[a; ; c] if the optional argument is missing? - -- c <- cBracketT - return (fa, nullRange) + -- b <- oBracketT + fa <- annoParser (fitString l flag) -- TODO: this should be a :: separated list of fittingArgs + -- TODO: how to parse SP[a; ; c] if the optional argument is missing? + -- c <- cBracketT + return (fa, nullRange) + <|> do + let emptyParam = do + _ <- lookAhead $ try semiT + return $ Missing_arg nullRange + fa <- annoParser emptyParam + return (fa, nullRange) + fitString :: LogicGraph -> Bool -> AParser st FIT_ARG fitString _l _ = do From 54ec5ba810851a3c37c2b20ebfc993759911a10c Mon Sep 17 00:00:00 2001 From: mscodescu Date: Mon, 12 Aug 2019 15:52:14 +0200 Subject: [PATCH 03/33] parse lists in arguments of instantiations --- Syntax/Parse_AS_Structured.hs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index 1618218216..c2654f0b8c 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -554,23 +554,29 @@ fitArgs l flag = do fitArg :: LogicGraph -> Bool -> AParser st (Annoted FIT_ARG, Range) fitArg l flag = do - -- b <- oBracketT - fa <- annoParser (fitString l flag) -- TODO: this should be a :: separated list of fittingArgs - -- TODO: how to parse SP[a; ; c] if the optional argument is missing? - -- c <- cBracketT - return (fa, nullRange) - <|> do let emptyParam = do _ <- lookAhead $ try semiT return $ Missing_arg nullRange fa <- annoParser emptyParam - return (fa, nullRange) + return (fa, nullRange) + <|> do + -- b <- oBracketT + fa <- annoParser $ fitString l flag + -- c <- cBracketT + return (fa, nullRange) fitString :: LogicGraph -> Bool -> AParser st FIT_ARG fitString _l _ = do - s <- compoundIriCurie -- TODO: hetIRI did not parse compound Ids - return $ Fit_spec (Annoted (UnsolvedName s nullRange) nullRange [][]) [] nullRange + let iParser = do + i <- compoundIriCurie + _ <- skip + return i + (s, _) <- separatedBy iParser doubleColonT + case s of + [] -> error "should be caught by the other case" + [x] -> return $ Fit_spec (Annoted (UnsolvedName x nullRange) nullRange [][]) [] nullRange + _ -> return $ Fit_list (map (\x -> Annoted (UnsolvedName x nullRange) nullRange [][]) s) nullRange fittingArg :: LogicGraph -> Bool -> AParser st FIT_ARG fittingArg l flag = do From 0f396a14b2e24b43fb95f81bfe5d6852f59e2c70 Mon Sep 17 00:00:00 2001 From: mscodescu Date: Mon, 12 Aug 2019 16:49:34 +0200 Subject: [PATCH 04/33] allow more complex arguments in instantiations --- Syntax/Parse_AS_Structured.hs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index c2654f0b8c..da0c8a0471 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -564,13 +564,16 @@ fitArg l flag = do fa <- annoParser $ fitString l flag -- c <- cBracketT return (fa, nullRange) + <|> do + fa <- annoParser $ fittingArg l flag + return (fa, nullRange) fitString :: LogicGraph -> Bool -> AParser st FIT_ARG fitString _l _ = do let iParser = do i <- compoundIriCurie - _ <- skip + _ <- option () skip return i (s, _) <- separatedBy iParser doubleColonT case s of From 60276e7f280f463c49cfabbe2e94db394e2f08bb Mon Sep 17 00:00:00 2001 From: mscodescu Date: Tue, 13 Aug 2019 10:08:54 +0200 Subject: [PATCH 05/33] moved additions in der.hs file --- Syntax/AS_Structured.der.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Syntax/AS_Structured.der.hs b/Syntax/AS_Structured.der.hs index aa55a3067c..725a22b174 100644 --- a/Syntax/AS_Structured.der.hs +++ b/Syntax/AS_Structured.der.hs @@ -121,6 +121,8 @@ data FIT_ARG = Fit_spec (Annoted SPEC) [G_mapping] Range -- pos: opt "fit" | Fit_view IRI [Annoted FIT_ARG] Range -- annotations before the view keyword are stored in Spec_inst + | Fit_list [Annoted SPEC] Range + | Missing_arg Range deriving (Show, Typeable) type SPEC_NAME = IRI From 2ceb13012f1cc8d198ffaca5e37eca5e993cda78 Mon Sep 17 00:00:00 2001 From: mscodescu Date: Tue, 13 Aug 2019 10:13:20 +0200 Subject: [PATCH 06/33] allow ontologies in instantiations, see Examples/GradedRelsGE.dol --- Syntax/Parse_AS_Library.hs | 2 +- Syntax/Parse_AS_Structured.hs | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Syntax/Parse_AS_Library.hs b/Syntax/Parse_AS_Library.hs index ca2451fe29..797c4415d0 100644 --- a/Syntax/Parse_AS_Library.hs +++ b/Syntax/Parse_AS_Library.hs @@ -322,7 +322,7 @@ patternParser l = do (pars, ps1) <- macroParams l (imp, ps2) <- option ([], nullRange) (imports l) s2 <- equalT - a <- localOrSpec l + a <- trace ("parsed equal:" ++ show pars) $ localOrSpec l q <- optEnd let pattern = Pattern_defn n pars (Imported imp) a nullRange trace ("pattern:" ++ show pattern) $ return . Pattern_defn n pars (Imported imp) a diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index da0c8a0471..8f6d19cab5 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -570,16 +570,17 @@ fitArg l flag = do fitString :: LogicGraph -> Bool -> AParser st FIT_ARG -fitString _l _ = do +fitString l flag = do let iParser = do - i <- compoundIriCurie - _ <- option () skip - return i + i <- compoundIriCurie + _ <- option () skip + return $ Annoted (UnsolvedName i nullRange) nullRange [][] + <|> aSpec l flag (s, _) <- separatedBy iParser doubleColonT case s of [] -> error "should be caught by the other case" - [x] -> return $ Fit_spec (Annoted (UnsolvedName x nullRange) nullRange [][]) [] nullRange - _ -> return $ Fit_list (map (\x -> Annoted (UnsolvedName x nullRange) nullRange [][]) s) nullRange + [x] -> return $ Fit_spec x [] nullRange + _ -> return $ Fit_list s nullRange fittingArg :: LogicGraph -> Bool -> AParser st FIT_ARG fittingArg l flag = do From e189330222144918c6894fd408646eb339ea06f7 Mon Sep 17 00:00:00 2001 From: mscodescu Date: Fri, 6 Sep 2019 09:47:43 +0200 Subject: [PATCH 07/33] started analysis of generic dol, very preliminary --- Logic/Logic.hs | 18 ++ OWL2/AS.hs | 3 + OWL2/Logic_OWL2.hs | 4 + OWL2/ManchesterParser.hs | 3 +- OWL2/StaticAnalysis.hs | 110 ++++++++++++ Static/AnalysisLibrary.hs | 190 +++++++++++++++++++- Static/AnalysisStructured.hs | 316 +++++++++++++++++++++++++++++++++- Static/DevGraph.hs | 10 ++ Syntax/AS_Library.der.hs | 3 + Syntax/AS_Structured.der.hs | 9 + Syntax/Parse_AS_Library.hs | 13 +- Syntax/Parse_AS_Structured.hs | 2 +- Syntax/Print_AS_Structured.hs | 1 + 13 files changed, 668 insertions(+), 14 deletions(-) diff --git a/Logic/Logic.hs b/Logic/Logic.hs index d1fc2b16ed..6e87951f9d 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -379,6 +379,9 @@ class (Language lid, Category sign morphism, Ord sentence, -- | symbols have a name, see CASL RefMan p. 192 sym_name :: lid -> symbol -> Id sym_name l _ = statError l "sym_name" + -- | allow to change name + rename_symbol :: lid -> symbol -> Id -> symbol + rename_symbol lid _ _ = error $ "symbol renaming nyi for logic " ++ show lid -- | some symbols have a label for better readability sym_label :: lid -> symbol -> Maybe String sym_label _ _ = Nothing @@ -632,6 +635,21 @@ class ( Syntax lid basic_spec symbol symb_items symb_map_items extract_module :: lid -> [IRI] -> (sign, [Named sentence]) -> Result (sign, [Named sentence]) extract_module _ _ = return + -- solving symbols in patterns + solve_symbols :: lid -> Set.Set symbol -> PatternVarMap -> basic_spec -> Result basic_spec + solve_symbols _ _ _ = error "solve_symbols nyi" + -- instantiating macros + instantiate_macro :: lid -> PatternVarMap -> Map.Map (IRI, String) IRI -> basic_spec -> Result basic_spec + instantiate_macro _ _ _ _ = error "instantiate_macro nyi" + -- delete all occurences of a set of symbols in a solved macro + delete_symbols_macro :: lid -> Set.Set symbol -> basic_spec -> Result basic_spec + delete_symbols_macro _ _ _ = error "delete_symbols_macro nyi" + +type PatternVarMap = Map.Map IRI (Bool, String) +-- Bool is true for list- and false for non-list variables +-- TODO: ideally we should have logic-dependent kinds, but strings will do +-- TODO: this does not allow to have same name for different kinds. +-- One idea would be to work with lists of strings. Future work, should be easy. -- | print a whole theory printTheory :: StaticAnalysis lid basic_spec sentence symb_items symb_map_items diff --git a/OWL2/AS.hs b/OWL2/AS.hs index 8410d00be7..9f5f6e26de 100644 --- a/OWL2/AS.hs +++ b/OWL2/AS.hs @@ -386,6 +386,9 @@ showEntityType e = case e of entityTypes :: [EntityType] entityTypes = [minBound .. maxBound] +renameSymbol :: Entity -> Id -> Entity +renameSymbol e i = e {cutIRI = idToIRI i} + pairSymbols :: Entity -> Entity -> Result Entity -- TODO: improve! pairSymbols (Entity lb1 k1 i1) (Entity lb2 k2 i2) = if k1 /= k2 then diff --git a/OWL2/Logic_OWL2.hs b/OWL2/Logic_OWL2.hs index 6f5161087b..74049cb165 100644 --- a/OWL2/Logic_OWL2.hs +++ b/OWL2/Logic_OWL2.hs @@ -102,6 +102,7 @@ instance Sentences OWL2 Axiom Sign OWLMorphism Entity where sym_of OWL2 = singletonList . symOf symmap_of OWL2 = symMapOf sym_name OWL2 = entityToId + rename_symbol OWL2 = renameSymbol sym_label OWL2 = label fullSymName OWL2 s = let i = cutIRI s @@ -175,6 +176,9 @@ instance StaticAnalysis OWL2 OntologyDocument Axiom corresp2th OWL2 = corr2theo equiv2cospan OWL2 = addEquiv extract_module OWL2 = extractModule + solve_symbols OWL2 = solveSymbols + instantiate_macro OWL2 = instantiateMacro + delete_symbols_macro OWL2 = deleteSymbolsMacro #ifdef UNI_PACKAGE theory_to_taxonomy OWL2 = onto2Tax #endif diff --git a/OWL2/ManchesterParser.hs b/OWL2/ManchesterParser.hs index e30714f09f..6e16279ecc 100644 --- a/OWL2/ManchesterParser.hs +++ b/OWL2/ManchesterParser.hs @@ -277,4 +277,5 @@ basicSpec flag pm = do { imports = ie , ann = ans , name = ou } - trace ("o:" ++ show o) $ return o + -- trace ("o:" ++ show o) $ + return o diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 804b3afdc7..b6c6fdf0fa 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -585,3 +585,113 @@ corr2theo _aname flag ssig tsig l1 l2 eMap1 eMap2 rref = do _ -> fail $ "non-unique symbol match:" ++ showDoc l1 " " ++ showDoc l2 " " _ -> fail "terms not yet supported in alignments" + + +solveSymbols :: Set.Set Entity -> PatternVarMap -> OntologyDocument -> Result OntologyDocument +solveSymbols impSyms vMap (OntologyDocument pd (Ontology n is as fs)) = do + (declSyms, usedSyms, fs') <- + foldM (\(ds, us, flist) f -> do + (f', ds', us') <- solveFrame impSyms vMap f + return (Set.union ds ds', Set.union us us', flist++[f']) ) + (Set.empty, Set.empty, []) fs + let getKind str = case str of + "Class" -> Class + "ObjectProperty" -> ObjectProperty + "Individual" -> NamedIndividual + _ -> error $ "nyi:" ++ str + varSyms = foldl Set.union Set.empty $ map (\(x, (f, k)) -> if f then Set.empty else Set.singleton $ Entity Nothing (getKind k) x) $ Map.toList vMap + diffSyms = Set.difference usedSyms (Set.union declSyms $ Set.union impSyms varSyms) + -- each used symbol must be declared, imported or variable + if Set.null diffSyms then + return $ OntologyDocument pd $ Ontology n is as fs' + else error $ "undeclared symbols in the body of the pattern. impSyms:" ++ show impSyms ++ + " declSyms:" ++ show declSyms ++ " usedSyms:" ++ show usedSyms ++ + " varSyms:" ++ show varSyms ++ " diffSyms:" ++ show diffSyms + +solveFrame :: Set.Set Entity -> PatternVarMap -> Frame -> Result (Frame, Set.Set Entity, Set.Set Entity) +solveFrame impSyms vMap (Frame ext fBits) = do + let (ext', decl) = + case ext of + Misc _ -> (ext, Set.empty) + ClassEntity (UnsolvedClass i) -> + if i `elem` Map.keys vMap then (ClassEntity $ VarExpression $ MVar i, Set.empty) + else (ClassEntity $ Expression i, Set.singleton $ Entity Nothing Class i) -- add only if not member of impSyms + ClassEntity _ -> error $ show ext + ObjectEntity oExp -> (ext, Set.empty) + SimpleEntity ent -> (ext, Set.empty) + (fBits', used) <- foldM (\(fbs, us) fbit -> do + (fbit', us') <- solveFrameBit impSyms vMap fbit + return (fbs ++ [fbit'], Set.union us us')) ([], Set.empty) fBits + {- | ObjectBit (AnnotatedList ObjectPropertyExpression) -- relation + | DataBit (AnnotatedList DataPropertyExpression) -- relation + | IndividualSameOrDifferent (AnnotatedList Individual) -- relation + | ObjectCharacteristics (AnnotatedList Character) + | DataPropRange (AnnotatedList DataRange) + | IndividualFacts (AnnotatedList Fact) -} + return (Frame ext' fBits', decl, used) + +solveFrameBit :: Set.Set Entity -> PatternVarMap -> FrameBit -> Result (FrameBit, Set.Set Entity) +solveFrameBit impSyms vMap fbit = + case fbit of + ListFrameBit mr lft -> + case lft of + AnnotationBit _ -> return (fbit, Set.empty) + ExpressionBit aces -> do + let (aces', used') = foldl (\(as, us) ace -> let (ace', us') = solveClassExpression impSyms vMap ace + in (as ++ [ace'], Set.union us us')) ([], Set.empty) aces + return (ListFrameBit mr $ ExpressionBit aces', used') + _ -> error "nyi" + +solveClassExpression :: Set.Set Entity -> PatternVarMap -> (Annotations, ClassExpression) -> ((Annotations, ClassExpression), Set.Set Entity) +solveClassExpression impSyms vMap (annos, cexp) = + let (cexp', used) = case cexp of + UnsolvedClass i -> if i `elem` Map.keys vMap then (VarExpression $ MVar i, Set.empty) + else (Expression i, Set.singleton $ Entity Nothing Class i) + _ -> error "nyi" + in ((annos, cexp'), used) + + +-- TODO: + -- write a method that solves a class, an obj prop etc. + -- cover all cases in OWL in the methods above + + +instantiateMacro :: PatternVarMap -> Map.Map (IRI, String) IRI -> OntologyDocument -> Result OntologyDocument +instantiateMacro vars subst (OntologyDocument pd (Ontology n is as fs)) = do + fs'<- instantiateFrames subst vars fs + return $ OntologyDocument pd $ Ontology n is as fs' + +instantiateFrames :: Map.Map (IRI, String) IRI -> PatternVarMap -> [Frame] -> Result [Frame] +instantiateFrames subst vars = + mapM (instantiateFrame subst vars) + +instantiateFrame :: Map.Map (IRI, String) IRI -> PatternVarMap -> Frame -> Result Frame +instantiateFrame subst var (Frame ext fBits) = do + ext' <- case ext of + ClassEntity (VarExpression (MVar i)) -> + if (i, "Class") `elem` Map.keys subst then do + let j = Map.findWithDefault (error "instantiateFrame") (i, "Class") subst + return $ ClassEntity $ Expression j + else fail $ "unknown class variable: " ++ show i + _ -> return ext -- TODO: missing cases + return $ Frame ext' fBits -- TODO: fBits can also have variables! + + +-- delete symbols from a solved macro +deleteSymbolsMacro :: Set.Set Entity -> OntologyDocument -> Result OntologyDocument +deleteSymbolsMacro delSyms (OntologyDocument pd (Ontology n is as fs)) = do + fs' <- deleteSymbolsFrames delSyms fs + return $ OntologyDocument pd $ Ontology n is as fs' + +deleteSymbolsFrames :: Set.Set Entity -> [Frame] -> Result [Frame] +deleteSymbolsFrames delSyms fs = do + fs' <- mapM (deleteSymbolsFrame delSyms) fs + return $ concat fs' + +deleteSymbolsFrame :: Set.Set Entity -> Frame -> Result [Frame] +deleteSymbolsFrame delSyms f@(Frame ext fBits) = + case ext of + ClassEntity (VarExpression (MVar i)) -> + if Set.member i $ Set.map (\x -> idToIRI $ entityToId x) delSyms + then return [] + else return [f] diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index 2564176bc8..5c08fd4880 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -22,6 +22,8 @@ module Static.AnalysisLibrary , LNS ) where +import Debug.Trace + import Logic.Logic import Logic.Grothendieck import Logic.Coerce @@ -205,7 +207,7 @@ anaStringAux mln lgraph opts topLns initDG mt file posFileName (_, libenv) -- lookup or read a library anaLibFile :: LogicGraph -> HetcatsOpts -> LNS -> LibEnv -> DGraph -> LibName -> ResultT IO (LibName, LibEnv) -anaLibFile lgraph opts topLns libenv initDG ln = +anaLibFile lgraph opts topLns libenv initDG ln = trace ("known:" ++ show (Map.keys libenv)) $ let lnstr = show ln in case find (== ln) $ Map.keys libenv of Just ln' -> do analyzing opts $ "from " ++ lnstr @@ -369,6 +371,21 @@ anaGenericity lg libEnv ln dg opts eo name return (Genericity (Params ps') (Imported imps') pos, GenSig nsigI nsigPs $ JustNode ns, dg'') +anaImports :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts + -> ExpOverrides -> NodeName -> IMPORTED + -> Result (IMPORTED, MaybeNode, DGraph) +anaImports lg libEnv ln dg opts eo name (Imported imps) = do + l <- lookupCurrentLogic "IMPORTS" lg + let baseNode = maybe (EmptyNode l) JustNode (currentBaseTheory dg) + (imps', nsigI, dg') <- case imps of + [] -> return ([], baseNode, dg) + _ -> do + (is', _, nsig', dgI) <- anaUnion False lg libEnv ln dg baseNode + (extName "Imports" name) opts eo imps $ getRange imps + return (is', JustNode nsig', dgI) + return (Imported imps', nsigI, dg') +-- TODO: this method should be called in anaGenericity + expCurieT :: GlobalAnnos -> ExpOverrides -> IRI -> ResultT IO IRI expCurieT ga eo = liftR . expCurieR ga eo @@ -376,7 +393,7 @@ expCurieT ga eo = liftR . expCurieR ga eo anaLibItem :: LogicGraph -> HetcatsOpts -> LNS -> LibName -> LibEnv -> DGraph -> ExpOverrides -> LIB_ITEM -> ResultT IO (LIB_ITEM, DGraph, LibEnv, LogicGraph, ExpOverrides) -anaLibItem lg opts topLns currLn libenv dg eo itm = +anaLibItem lg opts topLns currLn libenv dg eo itm = trace ("itm:" ++ show itm) $ case itm of Spec_defn spn2 gen asp pos -> do let spn' = if null (iriToStringUnsecure spn2) then @@ -564,8 +581,177 @@ anaLibItem lg opts topLns currLn libenv dg eo itm = Newcomorphism_defn com _ -> ResultT $ do dg' <- anaComorphismDef com dg return $ Result [] $ Just (itm, dg', libenv, lg, eo) + Pattern_defn sname params imps body r -> do + let spn' = if null (iriToStringUnsecure sname) then + simpleIdToIRI $ mkSimpleId "Spec" else sname + spn <- expCurieT (globalAnnos dg) eo spn' + let spstr = iriToStringUnsecure spn + nName = makeName spn + analyzing opts $ "pattern " ++ spstr + -- spec names are parsed as unsolved. But here we solve them all to specs, no vars possible. + let imps'' = case imps of + Imported asps -> Imported $ + map (\asp -> + case item asp of + UnsolvedName x rg -> asp{item =Spec_inst x [] Nothing rg} + _ -> asp) + asps + (imps', nsigI, dg1) <- liftR $ anaImports lg libenv currLn dg opts eo nName imps'' + (params', pinfos, vMap, _, dg2) <- liftR $ anaPatternParams lg libenv currLn dg1 opts eo nName nsigI params + expBody <- liftR $ anaPatternBody lg libenv currLn dg2 opts eo nName vMap nsigI body + let entry = PatternEntry $ PatternSig nsigI pinfos vMap expBody + itm' = Pattern_defn sname params' imps' expBody r + genv = globalEnv dg2 + if Map.member spn genv + then + liftR $ plain_error (itm, dg, libenv, lg, eo) + (alreadyDefined spstr) r + else trace ("inserting:" ++ show spn) $ + return (itm', dg2{globalEnv = Map.insert spn entry genv}, + libenv, lg, eo) _ -> return (itm, dg, libenv, lg, eo) +anaPatternParams :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts + -> ExpOverrides -> NodeName -> MaybeNode -> [PatternParam] + -> Result ([PatternParam], [PatternParamInfo], PatternVarMap, MaybeNode, DGraph) +anaPatternParams lg lenv ln dg opts eo name impN params = + foldM (\(plist, slist, vMap, aNode, aDG) param -> do + (p, s, vMap', bNode, bDG) <- anaPatternParam lg lenv ln aDG opts eo name vMap aNode param + return (plist ++ [p], slist ++ [s], vMap', bNode, bDG)) + ([], [], Map.empty, impN, dg) params + +anaPatternParam :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts + -> ExpOverrides -> NodeName -> PatternVarMap -> MaybeNode -> PatternParam + -> Result (PatternParam, PatternParamInfo, PatternVarMap, MaybeNode, DGraph) +anaPatternParam lg lenv ln dg opts eo name vMap prevParamNode pParam = + case pParam of + OntoParam isOpt aSpec -> do + (sp', psig, dg') <- anaSpecTop None False lg lenv ln dg prevParamNode name opts eo (item aSpec) nullRange + let aSpec' = aSpec{item = sp'} + aSig = getSig psig + iSig = case prevParamNode of + EmptyNode _ -> Nothing + JustNode x -> Just $ getSig x + (vMap', _) <- addSigSymsToVarMap vMap iSig aSig + return (OntoParam isOpt aSpec', + SingleParamInfo isOpt psig, + vMap', + JustNode psig, + dg') + ListParam oList -> + case oList of + EmptyParamList -> do + l <- lookupCurrentLogic "anaPatternParam" lg + return (ListParam oList, + ListParamInfo 0 True (EmptyNode l), + vMap, + EmptyNode l, + dg) + OntoListCons aSpecs -> + case reverse aSpecs of + [] -> error "empty list, should never happen" + lastSpec : sps' -> do + let emptyListName = mkIRI "empty" + (lastSpecSolved, exactSize, listVar) = + case item lastSpec of + UnsolvedName x rg -> if x == emptyListName + then (lastSpec{item = EmptyList}, True, mkIRI "") + else (lastSpec{item = ListVariable x}, False, x) + _ -> error "Last element of the list must be empty or a variable name" + (aSpecs', nsigs, dg') <- + foldM (\(slist, nlist, aDg) asp -> do + (sp', mnsig, bDg) <- anaListElem lg lenv ln aDg opts eo name vMap prevParamNode asp + return ( slist ++ [sp'] + , nlist ++ case mnsig of + EmptyNode _ -> [] + _ -> [mnsig] + , bDg) + ) + ([], [], dg) $ map item $ reverse $ lastSpecSolved : sps' + let oList' = map (\(x,y) -> x{item = y}) $ zip aSpecs aSpecs' + pParam' = ListParam $ OntoListCons oList' + size = length aSpecs - 1 + firstNode = head nsigs + iSig = case prevParamNode of + EmptyNode _ -> Nothing + JustNode x -> Just $ getSig x + (vMap', newKinds) <- + foldM (\(f, k) nsig -> addSigSymsToVarMap f iSig $ getSig nsig) (vMap, Set.empty) $ + map (\n -> case n of + JustNode x -> x + _ -> error "should never happen" + ) nsigs + let newK = case Set.toList newKinds of + [x] -> x + _ -> "ontology" + return (pParam', ListParamInfo size exactSize firstNode, Map.insert listVar (True, newK) vMap', firstNode, dg') + +anaListElem :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts + -> ExpOverrides -> NodeName -> PatternVarMap -> MaybeNode -> SPEC + -> Result (SPEC, MaybeNode, DGraph) +anaListElem lg lenv ln dg opts eo name vMap prevParamNode sp = do + l <- lookupCurrentLogic "anaListElem" lg + case sp of + EmptyList -> return (sp, EmptyNode l, dg) + ListVariable _ -> return (sp, EmptyNode l, dg) + _ -> do + (sp', nsig, bDg) <- anaSpecTop None False lg lenv ln dg + prevParamNode name opts eo + sp nullRange + return (sp', JustNode nsig, bDg) + +addSigSymsToVarMap :: PatternVarMap -> Maybe G_sign -> G_sign -> Result (PatternVarMap, Set.Set String) +addSigSymsToVarMap vMap exclSig aSig = + case aSig of + G_sign lid (ExtSign sig _) _ -> do + let syms = symset_of lid sig + symsExcl <- case exclSig of + Nothing -> return Set.empty + Just (G_sign lidE (ExtSign sigE _) _) -> do + sigE' <- coercePlainSign lidE lid "coerce in addSigSymsToVarMap" sigE + return $ symset_of lid sigE' + let insertOrFail f s k = + let sIRI = idToIRI $ sym_name lid s in + if sIRI `elem` Map.keys f then + error $ "variable named " ++ show s ++ "already used in " ++ show f + else (Map.insert sIRI (False, symKind lid s) f, Set.insert (symKind lid s) k) + (vMap', newKinds) = foldl (\(f, k) s -> insertOrFail f s k) (vMap, Set.empty) $ Set.toList $ Set.difference syms symsExcl + return (vMap', newKinds) + +anaPatternBody :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts + -> ExpOverrides -> NodeName -> PatternVarMap -> MaybeNode -> LocalOrSpec -> + Result LocalOrSpec +anaPatternBody lg lenv ln dg opts eo name vMap impNode body = + case body of + Spec_pattern aspec -> do + sp'<- solveBody lg lenv ln dg opts eo name vMap impNode $ item aspec + trace ("sp':" ++ show sp') $ return $ Spec_pattern aspec{item = sp'} + Local_pattern locals aspec -> error "nyi" + +solveBody :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts + -> ExpOverrides -> NodeName -> PatternVarMap -> MaybeNode -> SPEC -> + Result SPEC +solveBody lg lenv ln dg opts eo name vMap impNode sp = + case sp of + Basic_spec (G_basic_spec lid bspec) r -> do + iSyms <- case impNode of + EmptyNode _ -> return Set.empty + JustNode n -> + case getSig n of + G_sign lidI (ExtSign sigI _) _ -> do + sigI' <- coercePlainSign lidI lid "solveBody" sigI -- TODO: heterogenity won't work + return $ symset_of lid sigI' + bspec' <- solve_symbols lid iSyms vMap bspec + return $ Basic_spec (G_basic_spec lid bspec') r + Extension aspecs r -> do + aspecs' <- mapM (solveBody lg lenv ln dg opts eo name vMap impNode) (map item aspecs) + return $ Extension (map (\(x,y) -> x{item = y}) $ zip aspecs aspecs') r + Group aspec r -> do + sp' <- solveBody lg lenv ln dg opts eo name vMap impNode $ item aspec + return $ Group (aspec{item = sp'}) r + Spec_inst n fitArgs miri r -> return sp + _ -> error $ show sp + symbolsOf :: LogicGraph -> G_sign -> G_sign -> [CORRESPONDENCE] -> Result (Set.Set (G_symbol, G_symbol)) symbolsOf lg gs1@(G_sign l1 (ExtSign sig1 sys1) _) diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index b841b96448..38ab419570 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -53,6 +53,7 @@ import Static.GTheory import Syntax.AS_Structured import Syntax.Print_AS_Structured +import Syntax.AS_Library import Common.AS_Annotation hiding (isAxiom, isDef) import Common.Consistency @@ -65,6 +66,7 @@ import Common.LibName import Common.Result import Common.Utils (number) import Common.Lib.MapSet (imageSet, setInsert) +import qualified Common.OrderedMap as OMap import Data.Graph.Inductive.Graph import qualified Data.Set as Set @@ -79,6 +81,9 @@ import Proofs.ComputeColimit (insertColimitInGraph) import Common.Lib.Graph import Static.ComputeTheory +import Static.History + +import Debug.Trace -- overrides CUIRIE expansion for Download_items type ExpOverrides = Map.Map IRI FilePath @@ -527,6 +532,27 @@ anaSpecAux conser addSyms optNodes lg spname <- expCurieR (globalAnnos dg) eo spname' let pos = if null afitargs then iriPos spname else pos0 adjustPos pos $ case lookupGlobalEnvDG spname dg of + Just (PatternEntry patSig@(PatternSig imp params vMap body)) -> -- trace ("patSig:" ++ show patSig) $ + -- 1. solve afitargs using params and imp + case (length afitargs, length params) of + (la, lp) -> do + if (la == lp) && la > 0 then do + (afitargs', patSig'@(PatternSig _imp' _params' vMap' body'), dg', nsig', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs + (Logic cl) <- lookupCurrentLogic "anaGmaps" lg + spB <- instantiateMacro subst vMap' body' + -- the body should extend the last argument + (sp', nsig'', dg'') <- -- trace ("spB:" ++ show spB) $ + anaSpecTop conser addSyms lg libEnv ln dg' (JustNode nsig') (extName "Body" name) opts eo (item spB) nullRange + --incl <- ginclusion lg (getSig nsig') (getSig nsig'') + --let dg3 = insLink dg'' incl globalDef SeeTarget (getNode nsig') (getNode nsig'') + return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg'') + else if la == 0 then error "arguments missing in instantiation" + else if lp == 0 then error "pattern without arguments" + else error "mismatch in length of arguments" + -- 2. generate fitting morphisms and theorem links from the params to the nodes of fitargs + -- 3. substitute vars in body using the fitting morphisms -> a structured DOL spec, Body + -- here is also where the missing arguments induce rejections in the body + -- 4. replace the P[A1; ... An] with (Imp then A1) and ... and (Imp then An) then Body Just (SpecEntry gs@(ExtGenSig (GenSig _ params _) body@(NodeSig nB gsigmaB))) -> case (length afitargs, length params) of @@ -1025,15 +1051,38 @@ anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) -} anaFitArg :: LogicGraph -> LibEnv -> LibName -> DGraph -> IRI -> MaybeNode - -> NodeSig -> HetcatsOpts -> NodeName -> ExpOverrides -> FIT_ARG + -> NodeSig -> HetcatsOpts -> NodeName -> ExpOverrides + -> MaybeNode -> MaybeNode -> FIT_ARG -> Result (FIT_ARG, DGraph, (G_morphism, NodeSig)) -anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo fv = +anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo csig prevSig fv = let ga = globalAnnos dg in case fv of Fit_spec asp gsis pos -> do - (sp', nsigA, dg') <- anaSpec False True lg libEnv ln + (sp', nsigA', dg0) <- -- trace ("calling ana spec:" ++ show asp) $ + anaSpec False True lg libEnv ln dg nsigI name opts eo (item asp) pos - (_, Comorphism aid) <- + -- if the context and the previous argument are both not EmptyNodes + -- unite the argument, the context and the previous argument + (nsigA, dg') <- + case (prevSig, csig) of + (EmptyNode _, EmptyNode _) -> return (nsigA', dg0) + _ -> do + let pN = case prevSig of + EmptyNode _ -> [] + JustNode x -> [x] + cN = case csig of + EmptyNode _ -> [] + JustNode x -> [x] + gbigSigma <- gsigManyUnion lg $ map getSig $ + pN ++ cN ++ [nsigA'] + let (uSig@(NodeSig unode _), dg1) = insGSig dg0 (extName "Union" name) + DGUnion gbigSigma + insE dgl (NodeSig n gs) = do + incl <- ginclusion lg gs gbigSigma + trace ("inclusion for:" ++ show gs ++ " is " ++ show incl ) $ return $ insLink dgl incl globalDef SeeTarget n unode + dg2 <- foldM insE dg1 $ pN ++ cN ++ [nsigA'] + return (uSig, dg2) + (_, Comorphism aid) <- -- trace ("actSig:" ++ show actSig) $ logicUnion lg (getNodeLogic nsigP) (getNodeLogic nsigA) let tl = Logic $ targetLogic aid (nsigA'@(NodeSig nA' gsigA'), dg'') <- coerceNode lg dg' nsigA name tl @@ -1046,6 +1095,71 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo f insLink dg'' eGmor globalThm (DGLinkInst spname $ Fitted gsis) nP nA' , (gmor, nsigA')) + Fit_ctx (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do + let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym + sRSym = symbol_to_raw slid ssym + case csig of + EmptyNode _ -> error "anaFitArg: empty context" -- should never happen + JustNode c -> do + (nsigA@(NodeSig na sigA), dg') <- + case (prevSig, nsigI) of + (EmptyNode _, EmptyNode _) -> return (c, dg) + (EmptyNode _, JustNode i) -> do -- first argument is an abbreviation, unite i and c + gUnionSig <- gsigManyUnion lg $ map getSig [c, i] + let (usig@(NodeSig unode _), dg0) = + insGSig dg (extName "Union" name) DGUnion gUnionSig + insE dgl (NodeSig n gs) = do + incl <- ginclusion lg gs gUnionSig + return $ insLink dgl incl globalDef SeeTarget n unode + dg1 <- foldM insE dg0 [c, i] + return (usig, dg1) + (JustNode x, _) -> do -- isig should be already in prevSig, don't add another link + gUnionSig <- gsigManyUnion lg $ map getSig [c, x] + let (usig@(NodeSig unode _), dg0) = + insGSig dg (extName "Union" name) DGUnion gUnionSig + insE dgl (NodeSig n gs) = do + incl <- ginclusion lg gs gUnionSig + return $ insLink dgl incl globalDef SeeTarget n unode + dg1 <- foldM insE dg0 [c, x] + return (usig, dg1) + ssig <- case gsigmaP of + G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig + tsig <- case sigA of + G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig + mor <- induced_from_to_morphism slid (Map.fromList [(sRSym, tRSym)]) ssig tsig + let gmor = mkG_morphism slid mor + dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na + return (fv, dg'', (gmor, nsigA)) + Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do + let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym + sRSym = symbol_to_raw slid ssym + sigA <- add_symb_to_sign tlid (empty_signature tlid) tsym + let extSigA = ExtSign sigA (Set.singleton tsym) + (asig@(NodeSig a ga), dg0) = insGSig dg (extName "Actual" name) (DGInst spname) $ G_sign tlid extSigA startSigId + uNodes = (case csig of + EmptyNode _ -> [] + JustNode x -> [x]) ++ + (case prevSig of + EmptyNode _ -> case nsigI of + EmptyNode _ -> [] + JustNode x -> [x] + JustNode x -> [x]) + gUnionSig <- gsigManyUnion lg $ [ga] ++ map getSig uNodes + let (usig@(NodeSig unode _), dg1) = + insGSig dg0 (extName "Union" name) DGUnion gUnionSig + insE dgl (NodeSig n gs) = do + incl <- ginclusion lg gs gUnionSig + return $ insLink dgl incl globalDef SeeTarget n unode + dg2 <- foldM insE dg1 $ asig:uNodes + ssig <- case gsigmaP of + G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig + tsig <- case gUnionSig of + G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig + mor <- induced_from_to_morphism slid (Map.fromList [(sRSym, tRSym)]) ssig tsig + let gmor = mkG_morphism slid mor + dg'' = insLink dg2 (gEmbed gmor) globalThm (DGLinkInstArg spname) nP unode + return (fv, dg'', (gmor, usig)) + -- trace ("sigA:" ++ show usig) $ error "fit_new nyi" Fit_view vn' afitargs pos -> do vn <- expCurieR ga eo vn' case lookupGlobalEnvDG vn dg of @@ -1106,7 +1220,7 @@ anaFitArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> -> Result ([FIT_ARG], DGraph, [(G_morphism, NodeSig)], NodeName) anaFitArgs lg libEnv opts eo ln spname imps (fas', dg1, args, name') (nsig', fa) = do let n1 = inc name' - (fa', dg', arg) <- anaFitArg lg libEnv ln dg1 spname imps nsig' opts n1 eo fa + (fa', dg', arg) <- anaFitArg lg libEnv ln dg1 spname imps nsig' opts n1 eo imps imps fa -- TODO: this is wrong! return (fa' : fas', dg', arg : args, n1) anaAllFitArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph @@ -1128,6 +1242,198 @@ anaAllFitArgs lg libEnv opts eo ln dg nsig name spname return ( zipWith replaceAnnoted (reverse fitargs') afitargs, dg3 , (morDelta, gsigma', ns)) +type GSubst = Map.Map (IRI, String) IRI + +anaPatternInstArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph + -> MaybeNode -> MaybeNode + -> NodeName -> IRI -> PatternSig -> [Annoted FIT_ARG] + -> Result ([Annoted FIT_ARG], PatternSig, DGraph, NodeSig, GSubst) +anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSig imps params vMap body) afitargs = trace ("all args:" ++ show afitargs ++ "\n params:" ++ show params) $ do + l <- lookupCurrentLogic "anaPatternInstArgs" lg + -- before the arguments are analysed, we have to go through their list + -- and check if any Missing_arg nullRange appears + -- if it does, check that it occurs on the position of an optional parameter + -- if it doesn't give an error + -- else construct a new PatternSig that keeps only the present parameters and has a new body + -- add it temporarily to globalEnv and proceed, then restore the old definition + let zipped = zip params afitargs + (missingNodes, zipped', _, dgP) <- + foldM (\(ns, ls, lastParam, dg0) (p,a) -> + case item a of + Missing_arg _ -> trace ("p:" ++ show p) $ + case p of + SingleParamInfo True parSig -> return (ns ++ [parSig], ls, lastParam, dg0) + _ -> fail $ "unexpected missing argument for non-optional parameter:" ++ show p + _ -> do + (dg1, newParam, p') <- removeMissingSymbolsParam lg libEnv ln dg0 lastParam ns p + -- don't return p, add a new node in the DG extending the previous argument that removes all symbols and sentences from dgn_theory p + -- that include symbols from ns + return $ (ns, ls ++ [(p',a)], newParam, dg1) ) + ([], [], isig, dg) zipped + (afitargs', dg', nsig', subst) <- trace ("zipped':" ++ (show $ map (\x -> case x of + SingleParamInfo _ xs -> dgn_theory $ labDG dgP $ getNode xs + _ -> error "nyi") $ map fst zipped')) $ + foldM (\(args0, dg0, nsig0, subst0) (par0, arg0) -> do + (arg1, dg1, nsig1, subst1) <- -- trace ("subst0:" ++ show subst0) $ + anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname subst0 par0 arg0 + return (args0 ++ [arg1], dg1, nsig1, subst1)) + ([], dgP, EmptyNode l, Map.empty) $ zipped' + let lastParamSig = case nsig' of + EmptyNode _ -> error "should not happen" + JustNode x -> x + case missingNodes of + [] -> return (afitargs', psig, dg', lastParamSig, subst) + _ -> do + (vMap', body') <- removeMissingOptionalSymbols lg libEnv ln missingNodes vMap body + return (afitargs', PatternSig imps (map fst zipped') vMap' body', dg', lastParamSig, subst) + +removeMissingSymbolsParam :: LogicGraph -> LibEnv -> LibName -> DGraph -> MaybeNode -> [NodeSig] -> PatternParamInfo -> Result (DGraph, MaybeNode, PatternParamInfo) +removeMissingSymbolsParam lg libEnv ln dg lastParam ns p = do + case p of + SingleParamInfo optFlag psig -> do + let gth = dgn_theory $ labDG dg $ getNode psig + case gth of + G_theory lid syn (ExtSign sig nIsyms) sid sens tid -> do + let delSyms = concatMap (\n -> let gs = getSig n in + case gs of + G_sign slid (ExtSign _ syms) _ -> map (\x -> coerceSymbol slid lid x) $ Set.toList syms ) ns + mor <- cogenerated_sign lid (Set.fromList delSyms) sig + let sens' = OMap.fromList $ filter (\(_, y) -> Set.null $ Set.intersection (Set.fromList delSyms) $ Set.fromList $ symsOfSen lid sig $ sentence y) $ OMap.toList sens + gth' = G_theory lid syn (ExtSign (dom mor) nIsyms) sid sens' tid + newNode = newInfoNodeLab (makeName $ mkIRI "NewParam") + (newNodeInfo DGFormalParams) gth' + newNodeNr = getNewNodeDG dg + dg' = changesDGH dg [InsertNode (newNodeNr, newNode)] + newParNode = NodeSig newNodeNr $ signOf gth' + dg'' <- case lastParam of + EmptyNode _ -> return dg' + JustNode prevSig -> do + incl <- ginclusion lg (getSig prevSig) $ signOf gth' + return $ insLink dg' incl + globalDef DGLinkImports (getNode prevSig) newNodeNr + trace ("newParNode:" ++ show newParNode) $ return (dg'', JustNode newParNode, SingleParamInfo optFlag newParNode) + _ -> return (dg, lastParam, p) -- don't remove from lists yet + +removeMissingOptionalSymbols :: LogicGraph -> LibEnv -> LibName -> [NodeSig] -> PatternVarMap -> LocalOrSpec + -> Result (PatternVarMap, LocalOrSpec) +removeMissingOptionalSymbols lg libEnv ln missingNodes vMap body = do + Logic lid <- lookupCurrentLogic "removeMissingOptionalSymbols" lg + let delSyms = concatMap (\n -> let gs = getSig n in + case gs of + G_sign slid (ExtSign _ syms) _ -> map (\x -> coerceSymbol slid lid x) $ Set.toList syms ) missingNodes + removeSymbolsFromSpec sp = + case sp of + Basic_spec (G_basic_spec blid bsp) rg -> do + let delSyms' = map (coerceSymbol lid blid) delSyms + bsp' <- delete_symbols_macro blid (Set.fromList delSyms') bsp + return $ Basic_spec (G_basic_spec blid bsp') rg + _ -> error "only basic specs for now" + vMap' = foldl (\f s -> Map.delete ( idToIRI $ sym_name lid s) f) vMap delSyms + body' <- case body of + Spec_pattern asp -> do + sp' <- removeSymbolsFromSpec $ item asp + return $ Spec_pattern $ asp{item = sp'} + Local_pattern locals asp -> error "2" + trace ("body:" ++ show body ++" body':"++ show body') $ return (vMap', body') + +anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph + -> MaybeNode -> MaybeNode -> MaybeNode + -> NodeName -> IRI -> GSubst -> PatternParamInfo -> Annoted FIT_ARG + -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst) +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 par0 arg0 = + case item arg0 of + Fit_spec asp gm r -> + case item asp of + UnsolvedName i rg -> + -- TODO: here we must also pass the parameter, so we can check its symbols + -- 1. if i is the name of a spec entry in globalEnv + -- solve to Spec_inst i [] Nothing nullRange + -- and the substitution should be inducedFromTo + if i `elem` Map.keys (globalEnv dg0) then + case (par0, Map.findWithDefault (error "anaPatternInstArg: already checked") i (globalEnv dg0)) of + (SingleParamInfo b pSig, SpecEntry eGSig) -> do + let arg1 = Fit_spec (emptyAnno $ Spec_inst i [] Nothing nullRange) [] nullRange + (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pSig opts (extName "Arg" name) eo csig prevSig arg1 + case gmor of + G_morphism lid mor _ -> do + let smap = symmap_of lid mor + subst1 = foldl (\f (ssym, tsym) -> + let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) + tn = idToIRI $ sym_name lid tsym + in Map.insert (sn, sk) tn f) subst0 $ Map.toList smap + -- TODO: any compatibility checks must be done here + return (arg0{item=arg2}, dg1, JustNode nsigA, subst1) + _ -> error $ "argument mismatch in instantiation. parameter: " ++ show par0 ++ "\n argument: " ++ show arg0 + else do + case par0 of + SingleParamInfo b pNSig -> + case getSig pNSig of + G_sign lid (ExtSign _ newDecls) _ -> + case Set.toList newDecls of + [sym] -> do + let noCtxOrNoMatch = do + let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange + (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig arg1 + case gmor of + G_morphism lid mor _ -> do + let smap = symmap_of lid mor + subst1 = foldl (\f (ssym, tsym) -> + let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) + tn = idToIRI $ sym_name lid tsym + in Map.insert (sn, sk) tn f) + subst0 $ Map.toList smap + -- TODO: any compatibility checks must be done here + return (arg0{item=arg2}, dg1, JustNode nsigA, subst1) + case csig of + EmptyNode _ -> trace "err1" $ noCtxOrNoMatch + JustNode c -> + case getSig c of + G_sign lid1 (ExtSign ctx _) _ -> do + let ctxSyms = filter (\csym -> ((idToIRI $ sym_name lid1 csym) == i) && (symKind lid1 csym == symKind lid sym)) $ Set.toList $ symset_of lid1 ctx + case ctxSyms of + [] -> trace "err2" $ noCtxOrNoMatch + [ctxSym] -> do + let arg1 = Fit_ctx (G_symbol lid sym) (G_symbol lid1 ctxSym) nullRange + (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig arg1 + case gmor of + G_morphism lid mor _ -> do + let smap = symmap_of lid mor + subst1 = foldl (\f (ssym, tsym) -> + let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) + tn = idToIRI $ sym_name lid tsym + in Map.insert (sn, sk) tn f) + subst0 $ Map.toList smap + -- TODO: any compatibility checks must be done here + return (arg0{item=arg2}, dg1, JustNode nsigA, subst1) + _ -> fail $ "multiple occurences of abbreviated name in the context:" ++ show ctxSyms + _ -> fail "ambiguity in use of abbreviation notation, parameter has more than one symbol" + _ -> fail "abbreviation notation can be used only for single ontology arguments, not for lists" + -- 2. if i is a symbol from the context (nsig) + -- solve to context fit x |-> i + -- and the substitution maps x to i + -- where x is the unique symbol declared in the param + -- 3. otherwise, i is a new symbol of same kind as x + -- and the substitution maps x to i + _ -> error "only unsolved names for now" + _ -> trace ("itm:" ++ (show $ item arg0)) $ error "only fit_spec for now" + +instantiateMacro :: GSubst -> PatternVarMap -> LocalOrSpec -> Result (Annoted SPEC) +instantiateMacro subst vars macro = + case macro of + Local_pattern _ _ -> error "local patterns nyi" + Spec_pattern asp -> do + let + instMacroAux asp0 = + case item asp0 of + Basic_spec gbsp rg -> + case gbsp of + G_basic_spec lid bsp -> do + bsp'<- instantiate_macro lid vars subst bsp + return asp0{item = Basic_spec (G_basic_spec lid bsp') rg} + Group asp1 rg -> instMacroAux asp1 + _ -> error "only non-structured bodies for now" + instMacroAux asp + parLink :: LogicGraph -> MaybeNode -> DGLinkOrigin -> NodeSig -> DGraph -> NodeSig -> Result DGraph parLink lg nsig orig (NodeSig node gsigma') dg (NodeSig nA_i sigA_i) = diff --git a/Static/DevGraph.hs b/Static/DevGraph.hs index 4e23d93221..d2474db235 100644 --- a/Static/DevGraph.hs +++ b/Static/DevGraph.hs @@ -685,6 +685,7 @@ data GlobalEntry = | AlignEntry AlignSig | UnitEntry UnitSig | NetworkEntry GDiagram + | PatternEntry PatternSig deriving (Show, Typeable) getGlobEntryNodes :: GlobalEntry -> Set.Set Node @@ -706,6 +707,15 @@ data AlignSig = AlignMor NodeSig GMorphism NodeSig NodeSig -- b deriving (Show, Eq, Typeable) +-- imports, list of nodes for those parameters that are ontologies, kinded vars, body +data PatternSig = PatternSig MaybeNode [PatternParamInfo] PatternVarMap LocalOrSpec + deriving (Show, Typeable) + +data PatternParamInfo = SingleParamInfo Bool NodeSig -- optional or not, node in graph + | ListParamInfo Int Bool MaybeNode -- length, exact or minimal, node of template + deriving (Show, Eq, Typeable) +-- TODO: extend for data parameters + type GlobalEnv = Map.Map IRI GlobalEntry getGlobNodes :: GlobalEnv -> Set.Set Node diff --git a/Syntax/AS_Library.der.hs b/Syntax/AS_Library.der.hs index 3fd3bf3a91..71ccf240b2 100644 --- a/Syntax/AS_Library.der.hs +++ b/Syntax/AS_Library.der.hs @@ -186,6 +186,7 @@ getDeclSpecNames :: LIB_ITEM -> [IRI] getDeclSpecNames li = case li of Spec_defn sn _ _ _ -> [sn] Download_items _ di _ -> getImportNames di + Pattern_defn sn _ _ _ _ -> [sn] _ -> [] getImportNames :: DownloadItems -> [IRI] @@ -207,4 +208,6 @@ getSpecDef li = case li of getOms s1 ++ getOms s2 ++ getOms as Align_defn _ _ (View_type s1 s2 _) _ _ _ -> [item s1, item s2] Module_defn _ (Module_type s1 s2 _) _ _ -> [item s1, item s2] + Pattern_defn _ _ _ (Spec_pattern s) _ -> [item s] + Pattern_defn _ _ _ (Local_pattern lis s) _ -> concatMap getSpecDef lis ++ [item s] _ -> [] diff --git a/Syntax/AS_Structured.der.hs b/Syntax/AS_Structured.der.hs index 725a22b174..66e497a089 100644 --- a/Syntax/AS_Structured.der.hs +++ b/Syntax/AS_Structured.der.hs @@ -31,10 +31,13 @@ import Logic.Grothendieck ( G_basic_spec , G_symb_items_list , G_symb_map_items_list + , G_symbol , LogicGraph , setCurLogic , setSyntax ) +import Debug.Trace + -- for spec-defn and view-defn see AS_Library data SPEC = Basic_spec G_basic_spec Range @@ -76,6 +79,8 @@ data SPEC = Basic_spec G_basic_spec Range | Apply IRI G_basic_spec Range -- pos: "apply", use a basic spec parser to parse a sentence | UnsolvedName IRI Range + | ListVariable IRI + | EmptyList deriving (Show, Typeable) data Network = Network [LABELED_ONTO_OR_INTPR_REF] [IRI] Range @@ -121,6 +126,8 @@ data FIT_ARG = Fit_spec (Annoted SPEC) [G_mapping] Range -- pos: opt "fit" | Fit_view IRI [Annoted FIT_ARG] Range -- annotations before the view keyword are stored in Spec_inst + | Fit_ctx G_symbol G_symbol Range + | Fit_new G_symbol G_symbol Range | Fit_list [Annoted SPEC] Range | Missing_arg Range deriving (Show, Typeable) @@ -240,3 +247,5 @@ getSpecs :: FIT_ARG -> [Annoted SPEC] getSpecs fa = case fa of Fit_spec as _ _ -> [as] Fit_view _ fas _ -> concatMap (getSpecs . item) fas + _ -> trace (show fa) [] + diff --git a/Syntax/Parse_AS_Library.hs b/Syntax/Parse_AS_Library.hs index 797c4415d0..0c77c704fe 100644 --- a/Syntax/Parse_AS_Library.hs +++ b/Syntax/Parse_AS_Library.hs @@ -123,8 +123,9 @@ specDefn l = do e <- equalT a <- aSpec l True -- OMS, not macros q <- optEnd - return . Spec_defn n g a - . catRange $ [s, e] ++ maybeToList q + trace ("spec:" ++ show (Spec_defn n g a nullRange)) $ + return . Spec_defn n g a + . catRange $ [s, e] ++ maybeToList q -- CASL view-defn or DOL IntprDefn viewDefn :: LogicGraph -> AParser st LIB_ITEM @@ -322,11 +323,13 @@ patternParser l = do (pars, ps1) <- macroParams l (imp, ps2) <- option ([], nullRange) (imports l) s2 <- equalT - a <- trace ("parsed equal:" ++ show pars) $ localOrSpec l + a <- --trace ("parsed equal:" ++ show pars) $ + localOrSpec l q <- optEnd let pattern = Pattern_defn n pars (Imported imp) a nullRange - trace ("pattern:" ++ show pattern) $ return . Pattern_defn n pars (Imported imp) a - . catRange $ [s1, s2] ++ maybeToList q + trace ("pattern:" ++ show pattern) $ + return . Pattern_defn n pars (Imported imp) a + . catRange $ [s1, s2] ++ maybeToList q localOrSpec :: LogicGraph -> AParser st LocalOrSpec localOrSpec l = do diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index 8f6d19cab5..74019854c4 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -555,7 +555,7 @@ fitArgs l flag = do fitArg :: LogicGraph -> Bool -> AParser st (Annoted FIT_ARG, Range) fitArg l flag = do let emptyParam = do - _ <- lookAhead $ try semiT + _ <- lookAhead $ try semiT <|> try cBracketT return $ Missing_arg nullRange fa <- annoParser emptyParam return (fa, nullRange) diff --git a/Syntax/Print_AS_Structured.hs b/Syntax/Print_AS_Structured.hs index f85f7ec772..c311e8c823 100644 --- a/Syntax/Print_AS_Structured.hs +++ b/Syntax/Print_AS_Structured.hs @@ -120,6 +120,7 @@ printSPEC lg spec = case spec of sep [keyword "apply" <+> pretty i, prettyLG lg $ Basic_spec bs nullRange] Bridge s1 rs s2 _ -> fsep $ [condBraces lg s1, keyword "bridge"] ++ map pretty rs ++ [condBraces lg s2] + _ -> text $ show spec instance Pretty Network where pretty (Network cs es _) = fsep $ ppWithCommas cs From d5279ee7b7448a6ddf54a487853cb503a03ac413 Mon Sep 17 00:00:00 2001 From: mscodescu Date: Tue, 10 Sep 2019 14:28:56 +0200 Subject: [PATCH 08/33] extend previous fitting morphism --- Static/AnalysisLibrary.hs | 2 +- Static/AnalysisStructured.hs | 110 ++++++++++++++++++++++++++--------- 2 files changed, 82 insertions(+), 30 deletions(-) diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index 5c08fd4880..fcd6cae666 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -852,7 +852,7 @@ anaViewDefn lg ln libenv dg opts eo vn gen vt gsis pos = do tmor <- gEmbedComorphism imor gsigmaS'' fmor <- comp mor tmor return (gsigmaS', fmor) - emor <- fmap gEmbed $ anaGmaps lg opts pos gsigmaS' gsigmaT hsis + emor <- fmap gEmbed $ anaGmaps lg opts pos gsigmaS' gsigmaT Nothing hsis -- TODO: Nothing for now! gmor <- comp tmor emor let vsig = ExtViewSig src gmor $ ExtGenSig gsig tar voidView = nodeS == nodeT && isInclusion gmor diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 38ab419570..7adbd3f40f 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -1017,16 +1017,25 @@ partitionGmaps l = let G_logic_translation _ -> False) $ reverse l in (reverse rs, reverse hs) -anaGmaps :: LogicGraph -> HetcatsOpts -> Range -> G_sign -> G_sign +anaGmaps :: LogicGraph -> HetcatsOpts -> Range -> G_sign -> G_sign -> Maybe G_morphism -> [G_mapping] -> Result G_morphism -anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) +anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) mgm gsis = adjustPos pos $ if isStructured opts then return $ mkG_morphism lidP $ ext_ide sigmaP - else if null gsis then do + else do + if null gsis then do (G_sign lidP' sigmaP' _, _) <- gSigCoerce lg psig (Logic lidA) sigmaA' <- coerceSign lidA lidP' "anaGmaps" sigmaA + prevMap <- case mgm of + Nothing -> return Map.empty + Just prevGMor -> + case prevGMor of + G_morphism prevLid prevMor _ -> do + prevMor' <- coerceMorphism prevLid lidP' "anaGmaps:coerceMorphism" prevMor + let symMap = symmap_of lidP' prevMor' + return $ Map.mapKeys (symbol_to_raw lidP') $ Map.map (symbol_to_raw lidP') symMap fmap (mkG_morphism lidP') $ - ext_induced_from_to_morphism lidP' Map.empty sigmaP' sigmaA' + ext_induced_from_to_morphism lidP' prevMap sigmaP' sigmaA' else do cl <- lookupCurrentLogic "anaGmaps" lg G_symb_map_items_list lid sis <- homogenizeGM cl gsis @@ -1034,10 +1043,21 @@ anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) sigmaP' <- coerceSign lidP' lid "anaGmaps1" sigmaP'' (G_sign lidA' sigmaA'' _, _) <- gSigCoerce lg asig (Logic lid) sigmaA' <- coerceSign lidA' lid "anaGmaps2" sigmaA'' - rmap <- stat_symb_map_items lid (plainSign sigmaP') + rMap <- stat_symb_map_items lid (plainSign sigmaP') (Just $ plainSign sigmaA') sis + prevMap <- case mgm of + Nothing -> return Map.empty + Just prevGMor -> + case prevGMor of + G_morphism prevLid prevMor _ -> do + prevMor' <- coerceMorphism prevLid lid "anaFitArg:coerceMorphism" prevMor + let symMap = symmap_of lid prevMor' + return $ Map.mapKeys (symbol_to_raw lid) $ Map.map (symbol_to_raw lid) symMap + let crtMap = if Map.intersection rMap prevMap == Map.empty + then Map.union rMap prevMap + else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection rMap prevMap) -- TODO: don't fail if the symbols are mapped in the same way fmap (mkG_morphism lid) - $ ext_induced_from_to_morphism lid rmap sigmaP' sigmaA' + $ ext_induced_from_to_morphism lid crtMap sigmaP' sigmaA' {- let symI = sym_of lidP sigmaI' @@ -1052,9 +1072,9 @@ anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) anaFitArg :: LogicGraph -> LibEnv -> LibName -> DGraph -> IRI -> MaybeNode -> NodeSig -> HetcatsOpts -> NodeName -> ExpOverrides - -> MaybeNode -> MaybeNode -> FIT_ARG + -> MaybeNode -> MaybeNode -> Maybe G_morphism -> FIT_ARG -> Result (FIT_ARG, DGraph, (G_morphism, NodeSig)) -anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo csig prevSig fv = +anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo csig prevSig mgm fv = let ga = globalAnnos dg in case fv of Fit_spec asp gsis pos -> do @@ -1088,7 +1108,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c (nsigA'@(NodeSig nA' gsigA'), dg'') <- coerceNode lg dg' nsigA name tl (gsigmaP', pmor) <- gSigCoerce lg gsigmaP tl tmor <- gEmbedComorphism pmor gsigmaP - gmor <- anaGmaps lg opts pos gsigmaP' gsigA' gsis + gmor <- anaGmaps lg opts pos gsigmaP' gsigA' mgm gsis eGmor <- comp tmor $ gEmbed gmor return ( Fit_spec (replaceAnnoted sp' asp) gsis pos , if nP == nA' && isInclusion eGmor then dg'' else @@ -1126,7 +1146,19 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig tsig <- case sigA of G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig - mor <- induced_from_to_morphism slid (Map.fromList [(sRSym, tRSym)]) ssig tsig + prevMap <- case mgm of + Nothing -> return Map.empty + Just prevGMor -> + case prevGMor of + G_morphism prevLid prevMor _ -> do + prevMor' <- coerceMorphism prevLid slid "anaFitArg:coerceMorphism" prevMor + let symMap = symmap_of slid prevMor' + return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) symMap + let crtMapAux = Map.fromList [(sRSym, tRSym)] + crtMap = if Map.intersection crtMapAux prevMap == Map.empty + then Map.union prevMap crtMapAux + else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) -- TODO: don't fail if the symbols are mapped in the same way + mor <- induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na return (fv, dg'', (gmor, nsigA)) @@ -1155,7 +1187,19 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig tsig <- case gUnionSig of G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig - mor <- induced_from_to_morphism slid (Map.fromList [(sRSym, tRSym)]) ssig tsig + prevMap <- case mgm of + Nothing -> return Map.empty + Just prevGMor -> + case prevGMor of + G_morphism prevLid prevMor _ -> do + prevMor' <- coerceMorphism prevLid slid "anaFitArg:coerceMorphism" prevMor + let symMap = symmap_of slid prevMor' + return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) symMap + let crtMapAux = Map.fromList [(sRSym, tRSym)] + crtMap = if Map.intersection crtMapAux prevMap == Map.empty + then Map.union prevMap crtMapAux + else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) -- TODO: don't fail if the symbols are mapped in the same way + mor <- induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor dg'' = insLink dg2 (gEmbed gmor) globalThm (DGLinkInstArg spname) nP unode return (fv, dg'', (gmor, usig)) @@ -1220,7 +1264,7 @@ anaFitArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> -> Result ([FIT_ARG], DGraph, [(G_morphism, NodeSig)], NodeName) anaFitArgs lg libEnv opts eo ln spname imps (fas', dg1, args, name') (nsig', fa) = do let n1 = inc name' - (fa', dg', arg) <- anaFitArg lg libEnv ln dg1 spname imps nsig' opts n1 eo imps imps fa -- TODO: this is wrong! + (fa', dg', arg) <- anaFitArg lg libEnv ln dg1 spname imps nsig' opts n1 eo imps imps Nothing fa -- TODO: this is wrong! return (fa' : fas', dg', arg : args, n1) anaAllFitArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph @@ -1249,7 +1293,7 @@ anaPatternInstArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> Lib -> NodeName -> IRI -> PatternSig -> [Annoted FIT_ARG] -> Result ([Annoted FIT_ARG], PatternSig, DGraph, NodeSig, GSubst) anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSig imps params vMap body) afitargs = trace ("all args:" ++ show afitargs ++ "\n params:" ++ show params) $ do - l <- lookupCurrentLogic "anaPatternInstArgs" lg + l@(Logic crtLid) <- lookupCurrentLogic "anaPatternInstArgs" lg -- before the arguments are analysed, we have to go through their list -- and check if any Missing_arg nullRange appears -- if it does, check that it occurs on the position of an optional parameter @@ -1257,6 +1301,14 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi -- else construct a new PatternSig that keeps only the present parameters and has a new body -- add it temporarily to globalEnv and proceed, then restore the old definition let zipped = zip params afitargs + idImps <- case isig of + EmptyNode _ -> return Nothing + JustNode (NodeSig _ gisig) -> + case gisig of + G_sign ilid (ExtSign esig _) _ -> do + esig' <- coercePlainSign ilid crtLid "coerceSign in anaPatternInstArgs" esig + let emor = ide esig' + return $ Just $ G_morphism crtLid emor startMorId (missingNodes, zipped', _, dgP) <- foldM (\(ns, ls, lastParam, dg0) (p,a) -> case item a of @@ -1264,20 +1316,20 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi case p of SingleParamInfo True parSig -> return (ns ++ [parSig], ls, lastParam, dg0) _ -> fail $ "unexpected missing argument for non-optional parameter:" ++ show p - _ -> do + _ -> do --TODO: remove missing symbols only! if there were missing arguments before! (dg1, newParam, p') <- removeMissingSymbolsParam lg libEnv ln dg0 lastParam ns p -- don't return p, add a new node in the DG extending the previous argument that removes all symbols and sentences from dgn_theory p -- that include symbols from ns return $ (ns, ls ++ [(p',a)], newParam, dg1) ) ([], [], isig, dg) zipped - (afitargs', dg', nsig', subst) <- trace ("zipped':" ++ (show $ map (\x -> case x of + (afitargs', dg', nsig', subst, gm') <- trace ("zipped':" ++ (show $ map (\x -> case x of SingleParamInfo _ xs -> dgn_theory $ labDG dgP $ getNode xs _ -> error "nyi") $ map fst zipped')) $ - foldM (\(args0, dg0, nsig0, subst0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1) <- -- trace ("subst0:" ++ show subst0) $ - anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname subst0 par0 arg0 - return (args0 ++ [arg1], dg1, nsig1, subst1)) - ([], dgP, EmptyNode l, Map.empty) $ zipped' + foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do + (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0:" ++ show subst0) $ + anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname subst0 gm0 par0 arg0 + return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) + ([], dgP, EmptyNode l, Map.empty, idImps) $ zipped' let lastParamSig = case nsig' of EmptyNode _ -> error "should not happen" JustNode x -> x @@ -1338,9 +1390,9 @@ removeMissingOptionalSymbols lg libEnv ln missingNodes vMap body = do anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> MaybeNode - -> NodeName -> IRI -> GSubst -> PatternParamInfo -> Annoted FIT_ARG - -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 par0 arg0 = + -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG + -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = case item arg0 of Fit_spec asp gm r -> case item asp of @@ -1353,7 +1405,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 case (par0, Map.findWithDefault (error "anaPatternInstArg: already checked") i (globalEnv dg0)) of (SingleParamInfo b pSig, SpecEntry eGSig) -> do let arg1 = Fit_spec (emptyAnno $ Spec_inst i [] Nothing nullRange) [] nullRange - (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pSig opts (extName "Arg" name) eo csig prevSig arg1 + (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of G_morphism lid mor _ -> do let smap = symmap_of lid mor @@ -1362,7 +1414,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 tn = idToIRI $ sym_name lid tsym in Map.insert (sn, sk) tn f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here - return (arg0{item=arg2}, dg1, JustNode nsigA, subst1) + return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> error $ "argument mismatch in instantiation. parameter: " ++ show par0 ++ "\n argument: " ++ show arg0 else do case par0 of @@ -1373,7 +1425,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 [sym] -> do let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig arg1 + (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of G_morphism lid mor _ -> do let smap = symmap_of lid mor @@ -1383,7 +1435,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 in Map.insert (sn, sk) tn f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here - return (arg0{item=arg2}, dg1, JustNode nsigA, subst1) + return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) case csig of EmptyNode _ -> trace "err1" $ noCtxOrNoMatch JustNode c -> @@ -1394,7 +1446,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 [] -> trace "err2" $ noCtxOrNoMatch [ctxSym] -> do let arg1 = Fit_ctx (G_symbol lid sym) (G_symbol lid1 ctxSym) nullRange - (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig arg1 + (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of G_morphism lid mor _ -> do let smap = symmap_of lid mor @@ -1404,7 +1456,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 in Map.insert (sn, sk) tn f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here - return (arg0{item=arg2}, dg1, JustNode nsigA, subst1) + return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> fail $ "multiple occurences of abbreviated name in the context:" ++ show ctxSyms _ -> fail "ambiguity in use of abbreviation notation, parameter has more than one symbol" _ -> fail "abbreviation notation can be used only for single ontology arguments, not for lists" From cadb3e54037711e83910f7dc99e4d3c4ba65eaf9 Mon Sep 17 00:00:00 2001 From: mscodescu Date: Wed, 18 Sep 2019 20:01:53 +0200 Subject: [PATCH 09/33] locals: body works but the dgraph is not yet what it should be --- Logic/Logic.hs | 3 + OWL2/AS.hs | 8 ++ OWL2/Logic_OWL2.hs | 1 + OWL2/StaticAnalysis.hs | 18 +++-- Static/AnalysisLibrary.hs | 84 +++++++++++++++---- Static/AnalysisStructured.hs | 151 +++++++++++++++++++++++++++-------- Static/DevGraph.hs | 13 ++- Syntax/AS_Library.der.hs | 11 ++- Syntax/AS_Structured.der.hs | 1 + 9 files changed, 227 insertions(+), 63 deletions(-) diff --git a/Logic/Logic.hs b/Logic/Logic.hs index 6e87951f9d..f5e3821c11 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -397,6 +397,9 @@ class (Language lid, Category sign morphism, Ord sentence, -- | combine two symbols into another one pair_symbols :: lid -> symbol -> symbol -> Result symbol pair_symbols lid _ _ = error $ "pair_symbols nyi for logic " ++ show lid + -- | create symbol from a name and a kind + new_symbol :: lid -> IRI -> String -> symbol + new_symbol _ _ _ = error "new_symbol nyi" -- | makes a singleton list from the given value singletonList :: a -> [a] diff --git a/OWL2/AS.hs b/OWL2/AS.hs index 9f5f6e26de..efe121994f 100644 --- a/OWL2/AS.hs +++ b/OWL2/AS.hs @@ -348,6 +348,14 @@ data Entity = Entity , cutIRI :: IRI } deriving (Show, Typeable, Data) +newSymbol :: IRI -> String -> Entity +newSymbol i s = + case s of + "Class" -> mkEntity Class i + "ObjectProperty" -> mkEntity ObjectProperty i + "Individual" -> mkEntity NamedIndividual i + _ -> error $ "nyi:" ++ show s + mkEntity :: EntityType -> IRI -> Entity mkEntity = Entity Nothing diff --git a/OWL2/Logic_OWL2.hs b/OWL2/Logic_OWL2.hs index 74049cb165..5f6a2d60dc 100644 --- a/OWL2/Logic_OWL2.hs +++ b/OWL2/Logic_OWL2.hs @@ -111,6 +111,7 @@ instance Sentences OWL2 Axiom Sign OWLMorphism Entity where symKind OWL2 = takeWhile isAlpha . showEntityType . entityKind symsOfSen OWL2 _ = Set.toList . symsOfAxiom pair_symbols OWL2 = pairSymbols + new_symbol OWL2 = newSymbol inducedFromToMor :: Map.Map RawSymb RawSymb -> ExtSign Sign Entity -> diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index b6c6fdf0fa..def22154a1 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -633,14 +633,16 @@ solveFrame impSyms vMap (Frame ext fBits) = do solveFrameBit :: Set.Set Entity -> PatternVarMap -> FrameBit -> Result (FrameBit, Set.Set Entity) solveFrameBit impSyms vMap fbit = case fbit of - ListFrameBit mr lft -> - case lft of - AnnotationBit _ -> return (fbit, Set.empty) - ExpressionBit aces -> do - let (aces', used') = foldl (\(as, us) ace -> let (ace', us') = solveClassExpression impSyms vMap ace - in (as ++ [ace'], Set.union us us')) ([], Set.empty) aces - return (ListFrameBit mr $ ExpressionBit aces', used') - _ -> error "nyi" + ListFrameBit mr lft -> + case lft of + AnnotationBit _ -> return (fbit, Set.empty) + ExpressionBit aces -> do + let (aces', used') = foldl (\(as, us) ace -> let (ace', us') = solveClassExpression impSyms vMap ace + in (as ++ [ace'], Set.union us us')) ([], Set.empty) aces + return (ListFrameBit mr $ ExpressionBit aces', used') + _ -> error "nyi" + AnnFrameBit annos (AnnotationFrameBit _) -> return (fbit, Set.empty) + _ -> error $ "nyi:" ++ show fbit solveClassExpression :: Set.Set Entity -> PatternVarMap -> (Annotations, ClassExpression) -> ((Annotations, ClassExpression), Set.Set Entity) solveClassExpression impSyms vMap (annos, cexp) = diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index fcd6cae666..6cc9af086a 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -161,7 +161,8 @@ anaStringAux mln lgraph opts topLns initDG mt file posFileName (_, libenv) $ concatMap (getSpecDef . item) is' declNs = Set.fromList . map expnd $ concatMap (getDeclSpecNames . item) is' - missNames = Set.toList $ spNs Set.\\ declNs + missNames = -- trace ("spNs:" ++ show spNs ++ " declNs:" ++ show declNs) $ + Set.toList $ spNs Set.\\ declNs unDecls = map (addDownload True) $ filter (isNothing . (`lookupGlobalEnvDG` initDG)) missNames is = unDecls ++ is' @@ -207,7 +208,7 @@ anaStringAux mln lgraph opts topLns initDG mt file posFileName (_, libenv) -- lookup or read a library anaLibFile :: LogicGraph -> HetcatsOpts -> LNS -> LibEnv -> DGraph -> LibName -> ResultT IO (LibName, LibEnv) -anaLibFile lgraph opts topLns libenv initDG ln = trace ("known:" ++ show (Map.keys libenv)) $ +anaLibFile lgraph opts topLns libenv initDG ln = -- trace ("known:" ++ show (Map.keys libenv)) $ let lnstr = show ln in case find (== ln) $ Map.keys libenv of Just ln' -> do analyzing opts $ "from " ++ lnstr @@ -393,7 +394,7 @@ expCurieT ga eo = liftR . expCurieR ga eo anaLibItem :: LogicGraph -> HetcatsOpts -> LNS -> LibName -> LibEnv -> DGraph -> ExpOverrides -> LIB_ITEM -> ResultT IO (LIB_ITEM, DGraph, LibEnv, LogicGraph, ExpOverrides) -anaLibItem lg opts topLns currLn libenv dg eo itm = trace ("itm:" ++ show itm) $ +anaLibItem lg opts topLns currLn libenv dg eo itm = -- trace ("itm:" ++ show itm) $ case itm of Spec_defn spn2 gen asp pos -> do let spn' = if null (iriToStringUnsecure spn2) then @@ -597,17 +598,18 @@ anaLibItem lg opts topLns currLn libenv dg eo itm = trace ("itm:" ++ show itm) $ _ -> asp) asps (imps', nsigI, dg1) <- liftR $ anaImports lg libenv currLn dg opts eo nName imps'' - (params', pinfos, vMap, _, dg2) <- liftR $ anaPatternParams lg libenv currLn dg1 opts eo nName nsigI params - expBody <- liftR $ anaPatternBody lg libenv currLn dg2 opts eo nName vMap nsigI body - let entry = PatternEntry $ PatternSig nsigI pinfos vMap expBody - itm' = Pattern_defn sname params' imps' expBody r - genv = globalEnv dg2 + (params', pinfos, vMap, lastParam, dg2) <- liftR $ anaPatternParams lg libenv currLn dg1 opts eo nName nsigI params + (dg3, expBodySig) <- liftR $ anaPatternBody lg libenv currLn dg2 opts eo nName pinfos vMap nsigI lastParam body + -- pinfos are sent for locals, lastParam is needed to solve their parameters correctly + let entry = PatternEntry $ PatternSig False nsigI pinfos vMap expBodySig + itm' = Pattern_defn sname params' imps' (getBody expBodySig) r + genv = globalEnv dg3 if Map.member spn genv then liftR $ plain_error (itm, dg, libenv, lg, eo) (alreadyDefined spstr) r - else trace ("inserting:" ++ show spn) $ - return (itm', dg2{globalEnv = Map.insert spn entry genv}, + else -- trace ("inserting:" ++ show spn) $ + return (itm', dg3{globalEnv = Map.insert spn entry genv}, libenv, lg, eo) _ -> return (itm, dg, libenv, lg, eo) @@ -719,20 +721,59 @@ addSigSymsToVarMap vMap exclSig aSig = return (vMap', newKinds) anaPatternBody :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts - -> ExpOverrides -> NodeName -> PatternVarMap -> MaybeNode -> LocalOrSpec -> - Result LocalOrSpec -anaPatternBody lg lenv ln dg opts eo name vMap impNode body = + -> ExpOverrides -> NodeName -> [PatternParamInfo] -> PatternVarMap -> MaybeNode -> MaybeNode -> LocalOrSpec -> + Result (DGraph, LocalOrSpecSig) +anaPatternBody lg lenv ln dg opts eo name pinfos vMap impNode lastParam body = case body of Spec_pattern aspec -> do sp'<- solveBody lg lenv ln dg opts eo name vMap impNode $ item aspec - trace ("sp':" ++ show sp') $ return $ Spec_pattern aspec{item = sp'} - Local_pattern locals aspec -> error "nyi" + -- trace ("sp':" ++ show sp') $ + return (dg, SpecSig $ Spec_pattern aspec{item = sp'}) + Local_pattern locals aspec -> do + (dg', psigs, items) <- solveLocals lg lenv ln dg opts eo name pinfos vMap impNode lastParam locals + --let vMap' = foldl Map.union vMap $ map (\(PatternSig _ _ _ f _) -> f) $ Map.elems psigs + sp' <- solveBody lg lenv ln dg' opts eo name vMap impNode $ item aspec + return (dg', LocalSig psigs $ Local_pattern items $ aspec{item = sp'}) + +solveLocals :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts + -> ExpOverrides -> NodeName -> [PatternParamInfo] -> PatternVarMap -> MaybeNode -> MaybeNode -> [LIB_ITEM] -> + Result (DGraph ,Map.Map IRI PatternSig, [LIB_ITEM]) +solveLocals lg lenv ln dg opts eo name pinfos vMap impNode lastParam locals = do + (dg', psigs, items) <- + foldM (\(dg0, psigs0, items0) l -> do + (dg1, psigs1, lItem) <- solveLocal lg lenv ln dg0 opts eo name pinfos vMap impNode lastParam l + return (dg1, Map.union psigs0 psigs1, items0 ++ [lItem])) + (dg, Map.empty, []) locals + return (dg', psigs, items) + +solveLocal :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts + -> ExpOverrides -> NodeName -> [PatternParamInfo] -> PatternVarMap -> MaybeNode -> MaybeNode -> LIB_ITEM -> + Result (DGraph, Map.Map IRI PatternSig, LIB_ITEM) +solveLocal lg lenv ln dg opts eo name pinfos vMap impNode lastParam local = + case local of + Pattern_defn lname lparams limp (Spec_pattern asp) lRange -> do + (lparams', pinfos', vMap', _aNode, dg1) <- anaPatternParams lg lenv ln dg opts eo name lastParam lparams + let intVMap = Map.intersection vMap vMap' + if intVMap == Map.empty + then do + sp' <- solveBody lg lenv ln dg1 opts eo name (Map.union vMap vMap') impNode $ item asp + let asp' = asp {item = sp'} + lpsig = PatternSig True impNode (pinfos') vMap' $ SpecSig $ Spec_pattern asp' + -- here always add the parameters of the global pattern before those of the local one + -- should be pinfos ++ pinfos' + -- but: if we don't do this,then the lists of formal and actual params with match during instantiation + trace ("solved local:" ++ show lpsig) $ return (dg1, Map.fromAscList [(lname, lpsig)], Pattern_defn lname lparams' limp (Spec_pattern asp') lRange) + -- trace ("sp':" ++ show sp') $ error "solveLocal nyi" + else fail $ "redeclaring variables in local pattern:" ++ (show $ Map.keys intVMap) + _ -> fail $ "only pattern definitions allowed in local part of a pattern" solveBody :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts -> ExpOverrides -> NodeName -> PatternVarMap -> MaybeNode -> SPEC -> Result SPEC solveBody lg lenv ln dg opts eo name vMap impNode sp = case sp of + UnsolvedName i rg -> if i `elem` Map.keys vMap then return $ NormalVariable i + else return sp -- can't solve now, will be solved later Basic_spec (G_basic_spec lid bspec) r -> do iSyms <- case impNode of EmptyNode _ -> return Set.empty @@ -746,10 +787,21 @@ solveBody lg lenv ln dg opts eo name vMap impNode sp = Extension aspecs r -> do aspecs' <- mapM (solveBody lg lenv ln dg opts eo name vMap impNode) (map item aspecs) return $ Extension (map (\(x,y) -> x{item = y}) $ zip aspecs aspecs') r + Union aspecs r -> do + aspecs' <- mapM (solveBody lg lenv ln dg opts eo name vMap impNode) (map item aspecs) + return $ Union (map (\(x,y) -> x{item = y}) $ zip aspecs aspecs') r Group aspec r -> do sp' <- solveBody lg lenv ln dg opts eo name vMap impNode $ item aspec return $ Group (aspec{item = sp'}) r - Spec_inst n fitArgs miri r -> return sp + Spec_inst n fitArgs miri r -> do + let solveFitArgs f = + case item f of + Fit_spec asp gm rg -> do + sp'<- solveBody lg lenv ln dg opts eo name vMap impNode $ item asp + return $ f{item = Fit_spec asp{item = sp'} gm rg} + _ -> return f + fitArgs' <- mapM solveFitArgs fitArgs + return $ Spec_inst n fitArgs' miri r -- maybe here unsolved arguments should be solved to variables! _ -> error $ show sp symbolsOf :: LogicGraph -> G_sign -> G_sign -> [CORRESPONDENCE] diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 7adbd3f40f..dd5bb4ed0e 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -532,14 +532,15 @@ anaSpecAux conser addSyms optNodes lg spname <- expCurieR (globalAnnos dg) eo spname' let pos = if null afitargs then iriPos spname else pos0 adjustPos pos $ case lookupGlobalEnvDG spname dg of - Just (PatternEntry patSig@(PatternSig imp params vMap body)) -> -- trace ("patSig:" ++ show patSig) $ + Just (PatternEntry patSig@(PatternSig _local imp params vMap _body)) -> -- trace ("patSig:" ++ show patSig) $ -- 1. solve afitargs using params and imp case (length afitargs, length params) of (la, lp) -> do if (la == lp) && la > 0 then do - (afitargs', patSig'@(PatternSig _imp' _params' vMap' body'), dg', nsig', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs + (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs + -- let body' = getBody bodySig (Logic cl) <- lookupCurrentLogic "anaGmaps" lg - spB <- instantiateMacro subst vMap' body' + spB <- trace ("calling instMacro:" ++ show nsig') $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig -- the body should extend the last argument (sp', nsig'', dg'') <- -- trace ("spB:" ++ show spB) $ anaSpecTop conser addSyms lg libEnv ln dg' (JustNode nsig') (extName "Body" name) opts eo (item spB) nullRange @@ -1162,7 +1163,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c let gmor = mkG_morphism slid mor dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na return (fv, dg'', (gmor, nsigA)) - Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do + Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym sRSym = symbol_to_raw slid ssym sigA <- add_symb_to_sign tlid (empty_signature tlid) tsym @@ -1180,7 +1181,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c let (usig@(NodeSig unode _), dg1) = insGSig dg0 (extName "Union" name) DGUnion gUnionSig insE dgl (NodeSig n gs) = do - incl <- ginclusion lg gs gUnionSig + incl <- trace ("inclusion from " ++ show gs ++ " to " ++ show gUnionSig) $ ginclusion lg gs gUnionSig return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ asig:uNodes ssig <- case gsigmaP of @@ -1199,9 +1200,9 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c crtMap = if Map.intersection crtMapAux prevMap == Map.empty then Map.union prevMap crtMapAux else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) -- TODO: don't fail if the symbols are mapped in the same way - mor <- induced_from_to_morphism slid crtMap ssig tsig + mor <- trace ("ssig:"++ show ssig ++ " tsig:" ++ show tsig ++ " crtMap:" ++ show crtMap ) $ induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor - dg'' = insLink dg2 (gEmbed gmor) globalThm (DGLinkInstArg spname) nP unode + dg'' = trace ("gmor after induced:" ++ show gmor) $ insLink dg2 (gEmbed gmor) globalThm (DGLinkInstArg spname) nP unode return (fv, dg'', (gmor, usig)) -- trace ("sigA:" ++ show usig) $ error "fit_new nyi" Fit_view vn' afitargs pos -> do @@ -1291,8 +1292,8 @@ type GSubst = Map.Map (IRI, String) IRI anaPatternInstArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> NodeName -> IRI -> PatternSig -> [Annoted FIT_ARG] - -> Result ([Annoted FIT_ARG], PatternSig, DGraph, NodeSig, GSubst) -anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSig imps params vMap body) afitargs = trace ("all args:" ++ show afitargs ++ "\n params:" ++ show params) $ do + -> Result ([Annoted FIT_ARG], PatternSig, DGraph, NodeSig, Maybe G_morphism, GSubst) +anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSig local imps params vMap body) afitargs = do l@(Logic crtLid) <- lookupCurrentLogic "anaPatternInstArgs" lg -- before the arguments are analysed, we have to go through their list -- and check if any Missing_arg nullRange appears @@ -1326,7 +1327,7 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi SingleParamInfo _ xs -> dgn_theory $ labDG dgP $ getNode xs _ -> error "nyi") $ map fst zipped')) $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0:" ++ show subst0) $ + (arg1, dg1, nsig1, subst1, gm1) <- trace ("subst0:" ++ show subst0) $ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname subst0 gm0 par0 arg0 return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) ([], dgP, EmptyNode l, Map.empty, idImps) $ zipped' @@ -1334,10 +1335,10 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi EmptyNode _ -> error "should not happen" JustNode x -> x case missingNodes of - [] -> return (afitargs', psig, dg', lastParamSig, subst) + [] -> return (afitargs', psig, dg', lastParamSig, gm', subst) _ -> do (vMap', body') <- removeMissingOptionalSymbols lg libEnv ln missingNodes vMap body - return (afitargs', PatternSig imps (map fst zipped') vMap' body', dg', lastParamSig, subst) + return (afitargs', PatternSig local imps (map fst zipped') vMap' body', dg', lastParamSig, gm', subst) removeMissingSymbolsParam :: LogicGraph -> LibEnv -> LibName -> DGraph -> MaybeNode -> [NodeSig] -> PatternParamInfo -> Result (DGraph, MaybeNode, PatternParamInfo) removeMissingSymbolsParam lg libEnv ln dg lastParam ns p = do @@ -1366,9 +1367,9 @@ removeMissingSymbolsParam lg libEnv ln dg lastParam ns p = do trace ("newParNode:" ++ show newParNode) $ return (dg'', JustNode newParNode, SingleParamInfo optFlag newParNode) _ -> return (dg, lastParam, p) -- don't remove from lists yet -removeMissingOptionalSymbols :: LogicGraph -> LibEnv -> LibName -> [NodeSig] -> PatternVarMap -> LocalOrSpec - -> Result (PatternVarMap, LocalOrSpec) -removeMissingOptionalSymbols lg libEnv ln missingNodes vMap body = do +removeMissingOptionalSymbols :: LogicGraph -> LibEnv -> LibName -> [NodeSig] -> PatternVarMap -> LocalOrSpecSig + -> Result (PatternVarMap, LocalOrSpecSig) +removeMissingOptionalSymbols lg libEnv ln missingNodes vMap bodySig = do Logic lid <- lookupCurrentLogic "removeMissingOptionalSymbols" lg let delSyms = concatMap (\n -> let gs = getSig n in case gs of @@ -1381,18 +1382,18 @@ removeMissingOptionalSymbols lg libEnv ln missingNodes vMap body = do return $ Basic_spec (G_basic_spec blid bsp') rg _ -> error "only basic specs for now" vMap' = foldl (\f s -> Map.delete ( idToIRI $ sym_name lid s) f) vMap delSyms - body' <- case body of + bodySig' <- case getBody bodySig of Spec_pattern asp -> do sp' <- removeSymbolsFromSpec $ item asp - return $ Spec_pattern $ asp{item = sp'} + return $ SpecSig $ Spec_pattern $ asp{item = sp'} Local_pattern locals asp -> error "2" - trace ("body:" ++ show body ++" body':"++ show body') $ return (vMap', body') + trace ("body:" ++ show bodySig ++" body':"++ show bodySig') $ return (vMap', bodySig') anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0) $ case item arg0 of Fit_spec asp gm r -> case item asp of @@ -1423,15 +1424,16 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 G_sign lid (ExtSign _ newDecls) _ -> case Set.toList newDecls of [sym] -> do - let noCtxOrNoMatch = do + let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 + (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ + anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of - G_morphism lid mor _ -> do - let smap = symmap_of lid mor + G_morphism glid mor _ -> do + let smap = symmap_of glid mor subst1 = foldl (\f (ssym, tsym) -> - let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) - tn = idToIRI $ sym_name lid tsym + let (sn, sk) = (idToIRI $ sym_name glid ssym, symKind glid ssym) + tn = idToIRI $ sym_name glid tsym in Map.insert (sn, sk) tn f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here @@ -1448,11 +1450,11 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 let arg1 = Fit_ctx (G_symbol lid sym) (G_symbol lid1 ctxSym) nullRange (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of - G_morphism lid mor _ -> do - let smap = symmap_of lid mor + G_morphism glid mor _ -> do + let smap = symmap_of glid mor subst1 = foldl (\f (ssym, tsym) -> - let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) - tn = idToIRI $ sym_name lid tsym + let (sn, sk) = (idToIRI $ sym_name glid ssym, symKind glid ssym) + tn = idToIRI $ sym_name glid tsym in Map.insert (sn, sk) tn f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here @@ -1469,11 +1471,17 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 _ -> error "only unsolved names for now" _ -> trace ("itm:" ++ (show $ item arg0)) $ error "only fit_spec for now" -instantiateMacro :: GSubst -> PatternVarMap -> LocalOrSpec -> Result (Annoted SPEC) -instantiateMacro subst vars macro = +instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> + DGraph -> MaybeNode -> MaybeNode + -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> LocalOrSpecSig -> Result (Annoted SPEC) +instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ " \n mgmPrev:" ++ show mgmPrev) $ case macro of - Local_pattern _ _ -> error "local patterns nyi" - Spec_pattern asp -> do + LocalSig localVarMaps (Local_pattern _ localBody) -> do + let gEnv' = foldl (\g (n, s) -> Map.insert n (PatternEntry s) g) (globalEnv dg) $ Map.toList localVarMaps + dg' = dg {globalEnv = gEnv'} + instantiateMacro lg libEnv opts eo ln dg' imp nsig name spname subst vars mgmPrev (SpecSig $ Spec_pattern localBody) -- TODO: will this be enough? + -- trace ("known:" ++ show (Map.keys gEnv')) $ error "local patterns nyi" + SpecSig (Spec_pattern asp) -> do let instMacroAux asp0 = case item asp0 of @@ -1482,9 +1490,82 @@ instantiateMacro subst vars macro = G_basic_spec lid bsp -> do bsp'<- instantiate_macro lid vars subst bsp return asp0{item = Basic_spec (G_basic_spec lid bsp') rg} - Group asp1 rg -> instMacroAux asp1 - _ -> error "only non-structured bodies for now" + Group asp1 _rg -> instMacroAux asp1 + Extension asps rg -> do + asps'<- mapM instMacroAux asps + return $ asp{item = Extension asps' rg} + Union asps rg -> do + asps'<- mapM instMacroAux asps + return $ asp{item = Union asps' rg} + Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! + let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv dg + case snEntry of + PatternEntry patSig@(PatternSig isLocal _ pParams pMap pBody) -> do + l@(Logic crtLid) <- lookupCurrentLogic "anaPatternInstArgs" lg + idImps <- case imp of + EmptyNode _ -> return Nothing + JustNode (NodeSig _ gisig) -> + case gisig of + G_sign ilid (ExtSign esig _) _ -> do + esig' <- coercePlainSign ilid crtLid "coerceSign in anaPatternInstArgs" esig + let emor = ide esig' + return $ Just $ G_morphism crtLid emor startMorId + let solveVars aFitArg = + case item aFitArg of + Fit_spec asp gm rg -> + case item asp of + NormalVariable i -> + if i `elem` Map.keys vars then + let (b, k) = Map.findWithDefault (error "notPossible") i vars + val = Map.findWithDefault (error "variable not mapped") (i,k) subst + in ([((i,k), (val,k))], aFitArg{item = Fit_spec asp{item= UnsolvedName val nullRange} gm rg}) + else error $ "unknown variable:" ++ show i + _ -> ([], aFitArg) + _ -> ([], aFitArg) + solved = map solveVars afitargs + afitargs0 = map snd solved + newVars = concatMap fst solved + zipped = trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! + -- TODO: if isLocal start with subst1 else start with empty subst? + gmor' <- case mgmPrev of + Nothing -> extendWithSubst l idImps newVars + Just agm -> return $ Just agm + (afitargs', dg', nsig', subst', gm') <- + foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do + (arg1, dg1, nsig1, subst1, gm1) <- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ + anaPatternInstArg lg libEnv opts eo ln dg0 + imp (EmptyNode l) nsig0 -- TODO: context is always empty now + name spname subst0 gm0 par0 arg0 + trace ("after analysis nsig':" ++ show nsig1 ++ " gm1:" ++ show gm1) $ return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) + ([], dg, nsig, subst, gmor') -- the last argument node should not be EmptyNode, but the target of gmor'. Try with nsig? + zipped + -- (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', subst') <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs + instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst subst') vars gm' pBody + -- error $ "spec_inst:" ++ show sn ++ " args:" ++ show afitargs ++ " vars:" ++ show vars ++ " subst:" ++ show subst + -- 1. afitargs should give raise to signature morphisms from the nodes of the params to the nodes of the args + -- 2. and to a subst' + -- 3. the body of sn should be instantiated in the new dgraph, with the union of subst and subst', with the varmap taken from the signature of sn in the globalEnv + _ -> fail $ "expected a pattern entry but found:" ++ show snEntry + _ -> fail $ "only non-structured bodies for now:" ++ show (globalEnv dg) instMacroAux asp + _ -> fail $ "illegal pattern signature:" ++ show macro + +extendWithSubst :: AnyLogic -> Maybe G_morphism -> [((IRI, String),(IRI, String))] -> Result (Maybe G_morphism) +extendWithSubst (Logic l) mgm newVars = do + case mgm of + Nothing -> return Nothing -- TODO: wrong, should still generate a morphism! + Just gm -> + case gm of + G_morphism crtLid emor _ -> do + let + ssyms = map (\(x,y) -> new_symbol crtLid x y) $ map fst newVars + tsyms = map (\(x,y) -> new_symbol crtLid x y) $ map snd newVars + crtMap = Map.fromList $ map (\(x, y) -> (symbol_to_raw crtLid x, symbol_to_raw crtLid y)) $ zip ssyms tsyms + ssig <- foldM (add_symb_to_sign crtLid) (empty_signature crtLid) ssyms + tsig <- foldM (add_symb_to_sign crtLid) (empty_signature crtLid) tsyms + mor <- induced_from_to_morphism crtLid crtMap (ExtSign ssig Set.empty) (ExtSign tsig Set.empty) + rmor <- morphism_union crtLid emor mor + trace ("ssig:" ++ show ssig ++ "tsig:" ++ show tsig ++ "rmor:" ++ show rmor) $ return $ Just $ G_morphism crtLid rmor startMorId parLink :: LogicGraph -> MaybeNode -> DGLinkOrigin -> NodeSig -> DGraph -> NodeSig -> Result DGraph diff --git a/Static/DevGraph.hs b/Static/DevGraph.hs index d2474db235..d98d4e825c 100644 --- a/Static/DevGraph.hs +++ b/Static/DevGraph.hs @@ -707,10 +707,19 @@ data AlignSig = AlignMor NodeSig GMorphism NodeSig NodeSig -- b deriving (Show, Eq, Typeable) --- imports, list of nodes for those parameters that are ontologies, kinded vars, body -data PatternSig = PatternSig MaybeNode [PatternParamInfo] PatternVarMap LocalOrSpec +-- true for local patterns, imports, list of nodes for those parameters that are ontologies, kinded vars, body +data PatternSig = PatternSig Bool MaybeNode [PatternParamInfo] PatternVarMap LocalOrSpecSig deriving (Show, Typeable) +data LocalOrSpecSig = SpecSig LocalOrSpec + | LocalSig (Map.Map IRI PatternSig) LocalOrSpec + -- store the varmaps of local subpatterns so they dont get recomputed every time + deriving (Show, Typeable) + +getBody :: LocalOrSpecSig -> LocalOrSpec +getBody (SpecSig x) = x +getBody (LocalSig _ x) = x + data PatternParamInfo = SingleParamInfo Bool NodeSig -- optional or not, node in graph | ListParamInfo Int Bool MaybeNode -- length, exact or minimal, node of template deriving (Show, Eq, Typeable) diff --git a/Syntax/AS_Library.der.hs b/Syntax/AS_Library.der.hs index 71ccf240b2..45703ac336 100644 --- a/Syntax/AS_Library.der.hs +++ b/Syntax/AS_Library.der.hs @@ -36,6 +36,8 @@ import Syntax.AS_Structured import Framework.AS import Framework.ATC_Framework () +import Debug.Trace + data LIB_DEFN = Lib_defn LibName [Annoted LIB_ITEM] Range [Annotation] {- pos: "library" list of annotations is parsed preceding the first LIB_ITEM @@ -186,7 +188,12 @@ getDeclSpecNames :: LIB_ITEM -> [IRI] getDeclSpecNames li = case li of Spec_defn sn _ _ _ -> [sn] Download_items _ di _ -> getImportNames di - Pattern_defn sn _ _ _ _ -> [sn] + Pattern_defn sn _ _ (Spec_pattern _) _ -> [sn] + Pattern_defn sn _ _ (Local_pattern lis _) _ -> + [sn] ++ concatMap getDeclSpecNames lis + -- here we add as declared the local subpatterns + -- should this have local visibility? + -- things get complicated _ -> [] getImportNames :: DownloadItems -> [IRI] @@ -200,7 +207,7 @@ getOms o = case o of MkNetwork _ -> [] getSpecDef :: LIB_ITEM -> [SPEC] -getSpecDef li = case li of +getSpecDef li = trace ("li:" ++ show li) $ case li of Spec_defn _ _ as _ -> [item as] View_defn _ _ (View_type s1 s2 _) _ _ -> [item s1, item s2] Entail_defn _ (Entail_type s1 s2 _) _ -> getOms s1 ++ getOms s2 diff --git a/Syntax/AS_Structured.der.hs b/Syntax/AS_Structured.der.hs index 66e497a089..6470131a58 100644 --- a/Syntax/AS_Structured.der.hs +++ b/Syntax/AS_Structured.der.hs @@ -79,6 +79,7 @@ data SPEC = Basic_spec G_basic_spec Range | Apply IRI G_basic_spec Range -- pos: "apply", use a basic spec parser to parse a sentence | UnsolvedName IRI Range + | NormalVariable IRI | ListVariable IRI | EmptyList deriving (Show, Typeable) From f3fdabf6749bc211e30e1ffa9fe7778d6029e8b2 Mon Sep 17 00:00:00 2001 From: mscodescu Date: Thu, 19 Sep 2019 11:03:31 +0200 Subject: [PATCH 10/33] got the dev graph right too --- Static/AnalysisLibrary.hs | 2 +- Static/AnalysisStructured.hs | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index 6cc9af086a..c4ef332fd8 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -752,7 +752,7 @@ solveLocal :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts solveLocal lg lenv ln dg opts eo name pinfos vMap impNode lastParam local = case local of Pattern_defn lname lparams limp (Spec_pattern asp) lRange -> do - (lparams', pinfos', vMap', _aNode, dg1) <- anaPatternParams lg lenv ln dg opts eo name lastParam lparams + (lparams', pinfos', vMap', _aNode, dg1) <- anaPatternParams lg lenv ln dg opts eo (makeName lname) lastParam lparams let intVMap = Map.intersection vMap vMap' if intVMap == Map.empty then do diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index dd5bb4ed0e..ac18bea584 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -540,13 +540,13 @@ anaSpecAux conser addSyms optNodes lg (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs -- let body' = getBody bodySig (Logic cl) <- lookupCurrentLogic "anaGmaps" lg - spB <- trace ("calling instMacro:" ++ show nsig') $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig - -- the body should extend the last argument - (sp', nsig'', dg'') <- -- trace ("spB:" ++ show spB) $ - anaSpecTop conser addSyms lg libEnv ln dg' (JustNode nsig') (extName "Body" name) opts eo (item spB) nullRange + (dg2, spB) <- trace ("calling instMacro:" ++ show nsig') $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig + --the body should extend the last argument + (sp', nsig'', dg3) <- -- trace ("spB:" ++ show spB) $ + anaSpecTop conser addSyms lg libEnv ln dg2 (JustNode nsig') (extName "Body" name) opts eo (item spB) nullRange --incl <- ginclusion lg (getSig nsig') (getSig nsig'') --let dg3 = insLink dg'' incl globalDef SeeTarget (getNode nsig') (getNode nsig'') - return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg'') + return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' else if la == 0 then error "arguments missing in instantiation" else if lp == 0 then error "pattern without arguments" else error "mismatch in length of arguments" @@ -1473,7 +1473,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode - -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> LocalOrSpecSig -> Result (Annoted SPEC) + -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> LocalOrSpecSig -> Result (DGraph, Annoted SPEC) instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ " \n mgmPrev:" ++ show mgmPrev) $ case macro of LocalSig localVarMaps (Local_pattern _ localBody) -> do @@ -1489,14 +1489,18 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev case gbsp of G_basic_spec lid bsp -> do bsp'<- instantiate_macro lid vars subst bsp - return asp0{item = Basic_spec (G_basic_spec lid bsp') rg} + return (dg, asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) Group asp1 _rg -> instMacroAux asp1 Extension asps rg -> do - asps'<- mapM instMacroAux asps - return $ asp{item = Extension asps' rg} + (dg', asps')<- foldM (\(aDg, as) a -> do + (dg',a') <- instMacroAux a + return (dg', as ++ [a']) ) (dg, []) asps + return $ (dg', asp{item = Extension asps' rg}) Union asps rg -> do - asps'<- mapM instMacroAux asps - return $ asp{item = Union asps' rg} + (dg', asps')<- foldM (\(aDg, as) a -> do + (dg',a') <- instMacroAux a + return (dg', as ++ [a']) ) (dg, []) asps + return $ (dg', asp{item = Union asps' rg}) Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv dg case snEntry of From 8c16717ea3ee0d397dbf6724c3a71b28f075e1c4 Mon Sep 17 00:00:00 2001 From: mscodescu Date: Wed, 5 Feb 2020 16:45:40 +0200 Subject: [PATCH 11/33] started lists --- OWL2/AS.hs | 14 ++- OWL2/Function.hs | 2 + OWL2/ManchesterParser.hs | 2 +- OWL2/Print.hs | 2 +- OWL2/StaticAnalysis.hs | 197 +++++++++++++++++++++++++++++----- Static/AnalysisLibrary.hs | 7 +- Static/AnalysisStructured.hs | 65 ++++++----- Syntax/AS_Library.der.hs | 7 +- Syntax/AS_Structured.der.hs | 6 +- Syntax/Parse_AS_Library.hs | 4 +- Syntax/Parse_AS_Structured.hs | 21 ++-- 11 files changed, 256 insertions(+), 71 deletions(-) diff --git a/OWL2/AS.hs b/OWL2/AS.hs index efe121994f..6245a006bd 100644 --- a/OWL2/AS.hs +++ b/OWL2/AS.hs @@ -527,7 +527,7 @@ type InverseObjectProperty = ObjectPropertyExpression data ObjectPropertyExpression = ObjectProp ObjectProperty | ObjectInverseOf InverseObjectProperty - | ObjectPropertyVar IRI + | ObjectPropertyVar Bool IRI | UnsolvedObjProp IRI deriving (Show, Eq, Ord, Typeable, Data) @@ -566,9 +566,19 @@ data ClassExpression = | DataCardinality (Cardinality DataPropertyExpression DataRange) deriving (Show, Eq, Ord, Typeable, Data) -data MVarOrTerm = MVar IRI | MUnion IRI IRI -- the name of the head and the name of the tail TODO: should be extended with other terms +data MVarOrTerm = MVar Bool IRI | MUnion IRI IRI + -- the name of the head and the name of the tail TODO: should be extended with other terms + -- True for lists! deriving (Show, Eq, Ord, Typeable, Data) +-- * INDIVIDUAL EXPRESSIONS + +data IndExpression = + IndAsExpression Individual + | UnsolvedInd IRI + | IndVar IRI + deriving (Show, Eq, Ord, Typeable, Data) + -- * ANNOTATIONS data Annotation = Annotation [Annotation] AnnotationProperty AnnotationValue diff --git a/OWL2/Function.hs b/OWL2/Function.hs index 2206c0254d..306952ee2b 100644 --- a/OWL2/Function.hs +++ b/OWL2/Function.hs @@ -134,6 +134,7 @@ instance Function ObjectPropertyExpression where function t s opr = case opr of ObjectProp op -> ObjectProp $ cutWith ObjectProperty t s op ObjectInverseOf op -> ObjectInverseOf $ function t s op + _ -> error $ show opr instance Function DataRange where function t s dra = case dra of @@ -162,6 +163,7 @@ instance Function ClassExpression where $ function t s l DataCardinality (Cardinality ct i dp mdr) -> DataCardinality $ Cardinality ct i (cutWith DataProperty t s dp) $ maybeDo t s mdr + _ -> error $ show cle instance Function Annotation where function t s (Annotation al ap av) = Annotation (map (function t s) al) diff --git a/OWL2/ManchesterParser.hs b/OWL2/ManchesterParser.hs index 6e16279ecc..47b0aff58d 100644 --- a/OWL2/ManchesterParser.hs +++ b/OWL2/ManchesterParser.hs @@ -28,7 +28,7 @@ import qualified Common.GlobalAnnotations as GA (PrefixMap) import Text.ParserCombinators.Parsec import qualified Data.Map as Map -import Debug.Trace +-- import Debug.Trace optAnnos :: CharParser st a -> CharParser st (Annotations, a) optAnnos p = do diff --git a/OWL2/Print.hs b/OWL2/Print.hs index cb31d1f580..aad92a8abf 100644 --- a/OWL2/Print.hs +++ b/OWL2/Print.hs @@ -128,7 +128,7 @@ printObjPropExp :: ObjectPropertyExpression -> Doc printObjPropExp obExp = case obExp of ObjectProp ou -> pretty ou ObjectInverseOf iopExp -> keyword inverseS <+> printObjPropExp iopExp - ObjectPropertyVar ou -> text "var" <+> pretty ou + ObjectPropertyVar _ ou -> text "var" <+> pretty ou UnsolvedObjProp ou -> text "unsolved" <+> pretty ou printFV :: (ConstrainingFacet, RestrictionValue) -> Doc diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index def22154a1..18fd94a157 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -586,6 +586,7 @@ corr2theo _aname flag ssig tsig l1 l2 eMap1 eMap2 rref = do ++ showDoc l2 " " _ -> fail "terms not yet supported in alignments" +-- solving symbols of a pattern solveSymbols :: Set.Set Entity -> PatternVarMap -> OntologyDocument -> Result OntologyDocument solveSymbols impSyms vMap (OntologyDocument pd (Ontology n is as fs)) = do @@ -607,6 +608,8 @@ solveSymbols impSyms vMap (OntologyDocument pd (Ontology n is as fs)) = do else error $ "undeclared symbols in the body of the pattern. impSyms:" ++ show impSyms ++ " declSyms:" ++ show declSyms ++ " usedSyms:" ++ show usedSyms ++ " varSyms:" ++ show varSyms ++ " diffSyms:" ++ show diffSyms + +-- solving symbols for each frame, also keep track of declared and used symbols solveFrame :: Set.Set Entity -> PatternVarMap -> Frame -> Result (Frame, Set.Set Entity, Set.Set Entity) solveFrame impSyms vMap (Frame ext fBits) = do @@ -614,24 +617,29 @@ solveFrame impSyms vMap (Frame ext fBits) = do case ext of Misc _ -> (ext, Set.empty) ClassEntity (UnsolvedClass i) -> - if i `elem` Map.keys vMap then (ClassEntity $ VarExpression $ MVar i, Set.empty) - else (ClassEntity $ Expression i, Set.singleton $ Entity Nothing Class i) -- add only if not member of impSyms - ClassEntity _ -> error $ show ext - ObjectEntity oExp -> (ext, Set.empty) - SimpleEntity ent -> (ext, Set.empty) + if i `elem` Map.keys vMap + then (ClassEntity $ VarExpression $ MVar False i, Set.empty) -- TODO: handle lists + else if (Entity Nothing Class i) `elem` impSyms + then (ClassEntity $ Expression i, Set.empty) -- add only if not member of impSyms + else (ClassEntity $ Expression i, Set.singleton $ Entity Nothing Class i) + ClassEntity _ -> error $ show ext + ObjectEntity (UnsolvedObjProp i) -> + if i `elem` Map.keys vMap + then (ObjectEntity $ ObjectPropertyVar False i, Set.empty) -- TODO: handle lists + else if (Entity Nothing ObjectProperty i) `elem` impSyms + then (ObjectEntity $ ObjectProp i, Set.empty) -- add only if not member of impSyms + else (ObjectEntity $ ObjectProp i, Set.singleton $ Entity Nothing ObjectProperty i) + ObjectEntity _ -> error $ show ext -- TODO: handle oexp + SimpleEntity ent -> (ext, Set.empty) -- TODO: when do we get this? (fBits', used) <- foldM (\(fbs, us) fbit -> do (fbit', us') <- solveFrameBit impSyms vMap fbit return (fbs ++ [fbit'], Set.union us us')) ([], Set.empty) fBits - {- | ObjectBit (AnnotatedList ObjectPropertyExpression) -- relation - | DataBit (AnnotatedList DataPropertyExpression) -- relation - | IndividualSameOrDifferent (AnnotatedList Individual) -- relation - | ObjectCharacteristics (AnnotatedList Character) - | DataPropRange (AnnotatedList DataRange) - | IndividualFacts (AnnotatedList Fact) -} return (Frame ext' fBits', decl, used) +-- solve symbols for each frame bit + solveFrameBit :: Set.Set Entity -> PatternVarMap -> FrameBit -> Result (FrameBit, Set.Set Entity) -solveFrameBit impSyms vMap fbit = +solveFrameBit impSyms vMap fbit = trace ("fbit:" ++ show fbit) $ case fbit of ListFrameBit mr lft -> case lft of @@ -639,47 +647,183 @@ solveFrameBit impSyms vMap fbit = ExpressionBit aces -> do let (aces', used') = foldl (\(as, us) ace -> let (ace', us') = solveClassExpression impSyms vMap ace in (as ++ [ace'], Set.union us us')) ([], Set.empty) aces - return (ListFrameBit mr $ ExpressionBit aces', used') - _ -> error "nyi" + trace ("solved lft:" ++ show (ExpressionBit aces')) $ return (ListFrameBit mr $ ExpressionBit aces', used') + ObjectBit aopes -> error "nyi" + DataBit adpes -> error "nyi" + IndividualSameOrDifferent ainds -> error "nyi" + ObjectCharacteristics achars -> return (fbit, Set.empty) + IndividualFacts afacts -> error "nyi" AnnFrameBit annos (AnnotationFrameBit _) -> return (fbit, Set.empty) - _ -> error $ "nyi:" ++ show fbit + AnnFrameBit annos DataFunctional -> return (fbit, Set.empty) + AnnFrameBit annos (DatatypeBit drg) -> error "nyi" + AnnFrameBit annos (ClassDisjointUnion cexps) -> error "nyi" + AnnFrameBit annos (ClassHasKey opexps dpexps) -> error "nyi" + AnnFrameBit annos (ObjectSubPropertyChain opexps) -> error "nyi" + +-- solve class expressions solveClassExpression :: Set.Set Entity -> PatternVarMap -> (Annotations, ClassExpression) -> ((Annotations, ClassExpression), Set.Set Entity) solveClassExpression impSyms vMap (annos, cexp) = let (cexp', used) = case cexp of - UnsolvedClass i -> if i `elem` Map.keys vMap then (VarExpression $ MVar i, Set.empty) + UnsolvedClass i -> if i `elem` Map.keys vMap then + let (b, _) = Map.findWithDefault (error "just checked") i vMap + in (VarExpression $ MVar b i, Set.empty) else (Expression i, Set.singleton $ Entity Nothing Class i) - _ -> error "nyi" + ObjectJunction j cexps -> let (cexps', used') = foldl (\(cs, u) c -> let ((_a, c'), u') = solveClassExpression impSyms vMap ([], c) + in (cs ++ [c'], Set.union u u')) ([], Set.empty) cexps + in (ObjectJunction j cexps', used') + Expression i -> error "nyi" + ObjectComplementOf cexp -> let ((_a, cexp'), u) = solveClassExpression impSyms vMap ([], cexp) + in (ObjectComplementOf cexp', u) + VarExpression _ -> error $ "should get a class expression but instead got " ++ show cexp + ObjectOneOf indivs -> error "nyi" + ObjectValuesFrom q opexp cexp -> let ((_, cexp'), u1) = solveClassExpression impSyms vMap ([], cexp) + ((_, opexp'), u2) = solveObjPropExpression impSyms vMap ([], opexp) + in (ObjectValuesFrom q opexp' cexp', Set.union u1 u2) + ObjectHasValue opexp indiv -> error "nyi" + ObjectHasSelf opexp -> let ((_, opexp'), u) = solveObjPropExpression impSyms vMap ([], opexp) + in (ObjectHasSelf opexp', u) + ObjectCardinality (Cardinality cType aInt opexp mcexp) -> + let + (mcexp', u1) = + case mcexp of + Nothing -> (Nothing, Set.empty) + Just cexp -> let ((_a, cexp'), u) = solveClassExpression impSyms vMap ([], cexp) + in (Just cexp', u) + ((_, opexp'), u2) = solveObjPropExpression impSyms vMap ([], opexp) + in ( ObjectCardinality (Cardinality cType aInt opexp' mcexp'), Set.union u1 u2) + DataValuesFrom q dpexp drg -> error "nyi" + DataHasValue dpexp lit -> error "nyi" + DataCardinality (Cardinality cType aInt dpexp mdrg) -> error "nyi" + -- _ -> error $ "nyi:" ++ show cexp in ((annos, cexp'), used) +solveObjPropExpression :: Set.Set Entity -> PatternVarMap -> (Annotations, ObjectPropertyExpression) -> ((Annotations, ObjectPropertyExpression), Set.Set Entity) +solveObjPropExpression impSyms vMap (annos, opexp) = + let (opexp', used) = + case opexp of + ObjectProp r -> (opexp, Set.singleton $ Entity Nothing ObjectProperty r) + ObjectInverseOf opexp0 -> let ((_, opexp1), u) = solveObjPropExpression impSyms vMap ([], opexp0) + in (ObjectInverseOf opexp1, u) + ObjectPropertyVar _ _ -> error $ "expected object property expression but got " ++ show opexp + UnsolvedObjProp i -> if i `elem` Map.keys vMap then + let (b, _) = Map.findWithDefault (error "just checked") i vMap + in (ObjectPropertyVar b i, Set.empty) + else (ObjectProp i, Set.singleton $ Entity Nothing ObjectProperty i) + in ((annos, opexp'), used) + +solveIndividual :: Set.Set Entity -> PatternVarMap -> (Annotations, IndExpression) -> ((Annotations, IndExpression), Set.Set Entity) +solveIndividual _ _ _ = error "nyi" -- TODO: - -- write a method that solves a class, an obj prop etc. - -- cover all cases in OWL in the methods above + -- write a method that solves an obj prop expression, data property expression etc. + +-- instantiate a macro with the values stored in a substitution instantiateMacro :: PatternVarMap -> Map.Map (IRI, String) IRI -> OntologyDocument -> Result OntologyDocument instantiateMacro vars subst (OntologyDocument pd (Ontology n is as fs)) = do fs'<- instantiateFrames subst vars fs return $ OntologyDocument pd $ Ontology n is as fs' +-- instantiate frames + instantiateFrames :: Map.Map (IRI, String) IRI -> PatternVarMap -> [Frame] -> Result [Frame] instantiateFrames subst vars = mapM (instantiateFrame subst vars) +-- instantiate a single frame + instantiateFrame :: Map.Map (IRI, String) IRI -> PatternVarMap -> Frame -> Result Frame instantiateFrame subst var (Frame ext fBits) = do ext' <- case ext of - ClassEntity (VarExpression (MVar i)) -> + ClassEntity (VarExpression (MVar b i)) -> -- TODO: handle lists! if (i, "Class") `elem` Map.keys subst then do let j = Map.findWithDefault (error "instantiateFrame") (i, "Class") subst return $ ClassEntity $ Expression j else fail $ "unknown class variable: " ++ show i - _ -> return ext -- TODO: missing cases - return $ Frame ext' fBits -- TODO: fBits can also have variables! - + ClassEntity _ -> error $ show ext + ObjectEntity (ObjectPropertyVar b i) -> -- TODO: handle lists! + if (i, "ObjectProperty") `elem` Map.keys subst then do + let j = Map.findWithDefault (error "instantiateFrame") (i, "ObjectProperty") subst + return $ ObjectEntity $ ObjectProp j + else fail $ "unknown object property variable: " ++ show i + ObjectEntity _oExp -> error $ show ext -- TODO: handle oexp + SimpleEntity ent -> error $ show ext -- TODO: when do we get this? + Misc _ -> error $ show ext + fBits' <- mapM (instantiateFrameBit subst var) fBits + return $ Frame ext' fBits' + +instantiateFrameBit :: Map.Map (IRI, String) IRI -> PatternVarMap -> FrameBit -> Result FrameBit +instantiateFrameBit subst var fbit = + case fbit of + ListFrameBit mr lfb -> + case lfb of + AnnotationBit _ -> return fbit + ExpressionBit aces -> do + aces' <- mapM (instantiateClassExpression subst var) aces + return $ ListFrameBit mr $ ExpressionBit aces' + ObjectBit aopexps -> error $ show lfb + DataBit adpexps -> error $ show lfb + IndividualSameOrDifferent aindivs -> error $ show lfb + ObjectCharacteristics _achars -> return fbit + DataPropRange _ -> return fbit + IndividualFacts afacts -> error $ show fbit + AnnFrameBit annos afb -> error $ show afb + +instantiateClassExpression :: Map.Map (IRI, String) IRI -> PatternVarMap -> (Annotations, ClassExpression) -> Result (Annotations, ClassExpression) +instantiateClassExpression subst var (annos, cexp) = + case cexp of + UnsolvedClass _ -> error $ "unsolved class at instantiation: " ++ show cexp + VarExpression (MVar b x) -> do + let v = Map.findWithDefault (error $ "unknown var:" ++ show x) (x, "Class") subst + return (annos, Expression v) + ObjectJunction q cexps -> do + acexps <- mapM (instantiateClassExpression subst var) $ + map (\x -> ([], x)) cexps + return (annos, ObjectJunction q $ map snd acexps) + ObjectComplementOf cexp -> do + (_, cexp') <- instantiateClassExpression subst var ([], cexp) + return (annos, ObjectComplementOf cexp') + ObjectOneOf indivs -> error "nyi" + ObjectValuesFrom q opexp cexp -> do + (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) + (_, cexp') <- instantiateClassExpression subst var ([], cexp) + return (annos, ObjectValuesFrom q opexp' cexp') + ObjectHasValue opexp indiv -> do + (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) + error "nyi" + ObjectHasSelf opexp -> do + (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) + return (annos, ObjectHasSelf opexp') + ObjectCardinality (Cardinality cType aInt opexp mcexp) -> do + mcexp' <- case mcexp of + Nothing -> return Nothing + Just cexp0 -> do + (_, cexp') <- instantiateClassExpression subst var ([], cexp0) + return $ Just cexp' + (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) + return (annos, ObjectCardinality (Cardinality cType aInt opexp' mcexp')) + _ -> return (annos, cexp) +{- Expression Class -- nothing to instantiate + | DataValuesFrom QuantifierType DataPropertyExpression DataRange --TODO: once we have data properties as well! + | DataHasValue DataPropertyExpression Literal + | DataCardinality (Cardinality DataPropertyExpression DataRange) +-} --- delete symbols from a solved macro +instantiateObjectPropertyExpression :: Map.Map (IRI, String) IRI -> PatternVarMap -> (Annotations, ObjectPropertyExpression) -> Result (Annotations, ObjectPropertyExpression) +instantiateObjectPropertyExpression subst var (annos, obexp) = + case obexp of + ObjectProp _ -> return (annos, obexp) -- nothing to instantiate + ObjectInverseOf opexp -> do + (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) + return (annos, ObjectInverseOf opexp') + ObjectPropertyVar b x -> do -- TODO: lists + let v = Map.findWithDefault (error $ "unknown var:" ++ show x) (x, "ObjectProperty") subst + return (annos, ObjectProp v) + UnsolvedObjProp _ -> error $ "unsolved object property at instantiation: " ++ show obexp + +-- delete symbols from a solved macro, for optional parameters deleteSymbolsMacro :: Set.Set Entity -> OntologyDocument -> Result OntologyDocument deleteSymbolsMacro delSyms (OntologyDocument pd (Ontology n is as fs)) = do fs' <- deleteSymbolsFrames delSyms fs @@ -693,7 +837,8 @@ deleteSymbolsFrames delSyms fs = do deleteSymbolsFrame :: Set.Set Entity -> Frame -> Result [Frame] deleteSymbolsFrame delSyms f@(Frame ext fBits) = case ext of - ClassEntity (VarExpression (MVar i)) -> - if Set.member i $ Set.map (\x -> idToIRI $ entityToId x) delSyms + ClassEntity (VarExpression (MVar b i)) -> + if Set.member i $ Set.map (\x -> idToIRI $ entityToId x) delSyms --TODO: handle lists then return [] else return [f] + _ -> error "nyi" diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index c4ef332fd8..31737cc6d6 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -22,7 +22,7 @@ module Static.AnalysisLibrary , LNS ) where -import Debug.Trace +--import Debug.Trace import Logic.Logic import Logic.Grothendieck @@ -728,6 +728,7 @@ anaPatternBody lg lenv ln dg opts eo name pinfos vMap impNode lastParam body = Spec_pattern aspec -> do sp'<- solveBody lg lenv ln dg opts eo name vMap impNode $ item aspec -- trace ("sp':" ++ show sp') $ + -- trace ("solvedBody:" ++ show sp') $ return (dg, SpecSig $ Spec_pattern aspec{item = sp'}) Local_pattern locals aspec -> do (dg', psigs, items) <- solveLocals lg lenv ln dg opts eo name pinfos vMap impNode lastParam locals @@ -762,7 +763,9 @@ solveLocal lg lenv ln dg opts eo name pinfos vMap impNode lastParam local = -- here always add the parameters of the global pattern before those of the local one -- should be pinfos ++ pinfos' -- but: if we don't do this,then the lists of formal and actual params with match during instantiation - trace ("solved local:" ++ show lpsig) $ return (dg1, Map.fromAscList [(lname, lpsig)], Pattern_defn lname lparams' limp (Spec_pattern asp') lRange) + -- trace ("solved local:" ++ show lpsig) $ + return (dg1, Map.fromAscList [(lname, lpsig)], + Pattern_defn lname lparams' limp (Spec_pattern asp') lRange) -- trace ("sp':" ++ show sp') $ error "solveLocal nyi" else fail $ "redeclaring variables in local pattern:" ++ (show $ Map.keys intVMap) _ -> fail $ "only pattern definitions allowed in local part of a pattern" diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index ac18bea584..276fd2119c 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -83,7 +83,7 @@ import Common.Lib.Graph import Static.ComputeTheory import Static.History -import Debug.Trace +-- import Debug.Trace -- overrides CUIRIE expansion for Download_items type ExpOverrides = Map.Map IRI FilePath @@ -540,7 +540,8 @@ anaSpecAux conser addSyms optNodes lg (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs -- let body' = getBody bodySig (Logic cl) <- lookupCurrentLogic "anaGmaps" lg - (dg2, spB) <- trace ("calling instMacro:" ++ show nsig') $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig + (dg2, spB) <- -- trace ("calling instMacro:" ++ show nsig') $ + instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig --the body should extend the last argument (sp', nsig'', dg3) <- -- trace ("spB:" ++ show spB) $ anaSpecTop conser addSyms lg libEnv ln dg2 (JustNode nsig') (extName "Body" name) opts eo (item spB) nullRange @@ -1100,7 +1101,8 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c DGUnion gbigSigma insE dgl (NodeSig n gs) = do incl <- ginclusion lg gs gbigSigma - trace ("inclusion for:" ++ show gs ++ " is " ++ show incl ) $ return $ insLink dgl incl globalDef SeeTarget n unode + -- trace ("inclusion for:" ++ show gs ++ " is " ++ show incl ) $ + return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ pN ++ cN ++ [nsigA'] return (uSig, dg2) (_, Comorphism aid) <- -- trace ("actSig:" ++ show actSig) $ @@ -1163,7 +1165,8 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c let gmor = mkG_morphism slid mor dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na return (fv, dg'', (gmor, nsigA)) - Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do + Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do + --trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym sRSym = symbol_to_raw slid ssym sigA <- add_symb_to_sign tlid (empty_signature tlid) tsym @@ -1181,7 +1184,8 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c let (usig@(NodeSig unode _), dg1) = insGSig dg0 (extName "Union" name) DGUnion gUnionSig insE dgl (NodeSig n gs) = do - incl <- trace ("inclusion from " ++ show gs ++ " to " ++ show gUnionSig) $ ginclusion lg gs gUnionSig + incl <- -- trace ("inclusion from " ++ show gs ++ " to " ++ show gUnionSig) $ + ginclusion lg gs gUnionSig return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ asig:uNodes ssig <- case gsigmaP of @@ -1200,9 +1204,11 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c crtMap = if Map.intersection crtMapAux prevMap == Map.empty then Map.union prevMap crtMapAux else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) -- TODO: don't fail if the symbols are mapped in the same way - mor <- trace ("ssig:"++ show ssig ++ " tsig:" ++ show tsig ++ " crtMap:" ++ show crtMap ) $ induced_from_to_morphism slid crtMap ssig tsig + mor <- -- trace ("ssig:"++ show ssig ++ " tsig:" ++ show tsig ++ " crtMap:" ++ show crtMap ) $ + induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor - dg'' = trace ("gmor after induced:" ++ show gmor) $ insLink dg2 (gEmbed gmor) globalThm (DGLinkInstArg spname) nP unode + dg'' = -- trace ("gmor after induced:" ++ show gmor) $ + insLink dg2 (gEmbed gmor) globalThm (DGLinkInstArg spname) nP unode return (fv, dg'', (gmor, usig)) -- trace ("sigA:" ++ show usig) $ error "fit_new nyi" Fit_view vn' afitargs pos -> do @@ -1313,7 +1319,7 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi (missingNodes, zipped', _, dgP) <- foldM (\(ns, ls, lastParam, dg0) (p,a) -> case item a of - Missing_arg _ -> trace ("p:" ++ show p) $ + Missing_arg _ -> -- trace ("p:" ++ show p) $ case p of SingleParamInfo True parSig -> return (ns ++ [parSig], ls, lastParam, dg0) _ -> fail $ "unexpected missing argument for non-optional parameter:" ++ show p @@ -1323,11 +1329,11 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi -- that include symbols from ns return $ (ns, ls ++ [(p',a)], newParam, dg1) ) ([], [], isig, dg) zipped - (afitargs', dg', nsig', subst, gm') <- trace ("zipped':" ++ (show $ map (\x -> case x of - SingleParamInfo _ xs -> dgn_theory $ labDG dgP $ getNode xs - _ -> error "nyi") $ map fst zipped')) $ + (afitargs', dg', nsig', subst, gm') <- --trace ("zipped':" ++ (show $ map (\x -> case x of + -- SingleParamInfo _ xs -> dgn_theory $ labDG dgP $ getNode xs + -- _ -> error "nyi") $ map fst zipped')) $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- trace ("subst0:" ++ show subst0) $ + (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0:" ++ show subst0) $ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname subst0 gm0 par0 arg0 return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) ([], dgP, EmptyNode l, Map.empty, idImps) $ zipped' @@ -1364,7 +1370,8 @@ removeMissingSymbolsParam lg libEnv ln dg lastParam ns p = do incl <- ginclusion lg (getSig prevSig) $ signOf gth' return $ insLink dg' incl globalDef DGLinkImports (getNode prevSig) newNodeNr - trace ("newParNode:" ++ show newParNode) $ return (dg'', JustNode newParNode, SingleParamInfo optFlag newParNode) + -- trace ("newParNode:" ++ show newParNode) $ + return (dg'', JustNode newParNode, SingleParamInfo optFlag newParNode) _ -> return (dg, lastParam, p) -- don't remove from lists yet removeMissingOptionalSymbols :: LogicGraph -> LibEnv -> LibName -> [NodeSig] -> PatternVarMap -> LocalOrSpecSig @@ -1387,13 +1394,14 @@ removeMissingOptionalSymbols lg libEnv ln missingNodes vMap bodySig = do sp' <- removeSymbolsFromSpec $ item asp return $ SpecSig $ Spec_pattern $ asp{item = sp'} Local_pattern locals asp -> error "2" - trace ("body:" ++ show bodySig ++" body':"++ show bodySig') $ return (vMap', bodySig') + -- trace ("body:" ++ show bodySig ++" body':"++ show bodySig') $ + return (vMap', bodySig') anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = --trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0) $ case item arg0 of Fit_spec asp gm r -> case item asp of @@ -1426,7 +1434,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 [sym] -> do let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ + (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of G_morphism glid mor _ -> do @@ -1439,13 +1447,15 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- TODO: any compatibility checks must be done here return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) case csig of - EmptyNode _ -> trace "err1" $ noCtxOrNoMatch + EmptyNode _ -> -- trace "err1" $ + noCtxOrNoMatch JustNode c -> case getSig c of G_sign lid1 (ExtSign ctx _) _ -> do let ctxSyms = filter (\csym -> ((idToIRI $ sym_name lid1 csym) == i) && (symKind lid1 csym == symKind lid sym)) $ Set.toList $ symset_of lid1 ctx case ctxSyms of - [] -> trace "err2" $ noCtxOrNoMatch + [] -> -- trace "err2" $ + noCtxOrNoMatch [ctxSym] -> do let arg1 = Fit_ctx (G_symbol lid sym) (G_symbol lid1 ctxSym) nullRange (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 @@ -1469,12 +1479,13 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- 3. otherwise, i is a new symbol of same kind as x -- and the substitution maps x to i _ -> error "only unsolved names for now" - _ -> trace ("itm:" ++ (show $ item arg0)) $ error "only fit_spec for now" + _ -> -- trace ("itm:" ++ (show $ item arg0)) $ + error "only fit_spec for now" instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> LocalOrSpecSig -> Result (DGraph, Annoted SPEC) -instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ " \n mgmPrev:" ++ show mgmPrev) $ +instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = --trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ " \n mgmPrev:" ++ show mgmPrev) $ case macro of LocalSig localVarMaps (Local_pattern _ localBody) -> do let gEnv' = foldl (\g (n, s) -> Map.insert n (PatternEntry s) g) (globalEnv dg) $ Map.toList localVarMaps @@ -1501,7 +1512,8 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev (dg',a') <- instMacroAux a return (dg', as ++ [a']) ) (dg, []) asps return $ (dg', asp{item = Union asps' rg}) - Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! + Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0)) $ + do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv dg case snEntry of PatternEntry patSig@(PatternSig isLocal _ pParams pMap pBody) -> do @@ -1529,18 +1541,20 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev solved = map solveVars afitargs afitargs0 = map snd solved newVars = concatMap fst solved - zipped = trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! + zipped = -- trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ + zip pParams afitargs0 -- TODO: allow optionals in locals!!!! -- TODO: if isLocal start with subst1 else start with empty subst? gmor' <- case mgmPrev of Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm (afitargs', dg', nsig', subst', gm') <- foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ + (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 imp (EmptyNode l) nsig0 -- TODO: context is always empty now name spname subst0 gm0 par0 arg0 - trace ("after analysis nsig':" ++ show nsig1 ++ " gm1:" ++ show gm1) $ return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) + -- trace ("after analysis nsig':" ++ show nsig1 ++ " gm1:" ++ show gm1) $ + return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) ([], dg, nsig, subst, gmor') -- the last argument node should not be EmptyNode, but the target of gmor'. Try with nsig? zipped -- (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', subst') <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs @@ -1569,7 +1583,8 @@ extendWithSubst (Logic l) mgm newVars = do tsig <- foldM (add_symb_to_sign crtLid) (empty_signature crtLid) tsyms mor <- induced_from_to_morphism crtLid crtMap (ExtSign ssig Set.empty) (ExtSign tsig Set.empty) rmor <- morphism_union crtLid emor mor - trace ("ssig:" ++ show ssig ++ "tsig:" ++ show tsig ++ "rmor:" ++ show rmor) $ return $ Just $ G_morphism crtLid rmor startMorId + -- trace ("ssig:" ++ show ssig ++ "tsig:" ++ show tsig ++ "rmor:" ++ show rmor) $ + return $ Just $ G_morphism crtLid rmor startMorId parLink :: LogicGraph -> MaybeNode -> DGLinkOrigin -> NodeSig -> DGraph -> NodeSig -> Result DGraph diff --git a/Syntax/AS_Library.der.hs b/Syntax/AS_Library.der.hs index 45703ac336..d4fa04710e 100644 --- a/Syntax/AS_Library.der.hs +++ b/Syntax/AS_Library.der.hs @@ -36,7 +36,7 @@ import Syntax.AS_Structured import Framework.AS import Framework.ATC_Framework () -import Debug.Trace +-- import Debug.Trace data LIB_DEFN = Lib_defn LibName [Annoted LIB_ITEM] Range [Annotation] {- pos: "library" @@ -117,7 +117,7 @@ addDownloadAux unique j = (if unique then UniqueItem i else ItemMaps [ItemNameMap i Nothing]) $ iriPos i -data PatternParam = OntoParam Bool (Annoted SPEC) | ListParam OntoList +data PatternParam = OntoParam Bool (Annoted SPEC) | ListParam OntoList deriving (Show, Typeable) -- the bool flag is true for optional parameters @@ -207,7 +207,8 @@ getOms o = case o of MkNetwork _ -> [] getSpecDef :: LIB_ITEM -> [SPEC] -getSpecDef li = trace ("li:" ++ show li) $ case li of +getSpecDef li = -- trace ("li:" ++ show li) $ + case li of Spec_defn _ _ as _ -> [item as] View_defn _ _ (View_type s1 s2 _) _ _ -> [item s1, item s2] Entail_defn _ (Entail_type s1 s2 _) _ -> getOms s1 ++ getOms s2 diff --git a/Syntax/AS_Structured.der.hs b/Syntax/AS_Structured.der.hs index 6470131a58..4f6bfa11c8 100644 --- a/Syntax/AS_Structured.der.hs +++ b/Syntax/AS_Structured.der.hs @@ -36,7 +36,7 @@ import Logic.Grothendieck , setCurLogic , setSyntax ) -import Debug.Trace +-- import Debug.Trace -- for spec-defn and view-defn see AS_Library @@ -81,6 +81,8 @@ data SPEC = Basic_spec G_basic_spec Range | UnsolvedName IRI Range | NormalVariable IRI | ListVariable IRI + | ListValue [IRI] + | OntoList [(Annoted SPEC)] | EmptyList deriving (Show, Typeable) @@ -248,5 +250,5 @@ getSpecs :: FIT_ARG -> [Annoted SPEC] getSpecs fa = case fa of Fit_spec as _ _ -> [as] Fit_view _ fas _ -> concatMap (getSpecs . item) fas - _ -> trace (show fa) [] + _ -> [] -- trace (show fa) [] diff --git a/Syntax/Parse_AS_Library.hs b/Syntax/Parse_AS_Library.hs index 0c77c704fe..0f4331d6e8 100644 --- a/Syntax/Parse_AS_Library.hs +++ b/Syntax/Parse_AS_Library.hs @@ -123,8 +123,8 @@ specDefn l = do e <- equalT a <- aSpec l True -- OMS, not macros q <- optEnd - trace ("spec:" ++ show (Spec_defn n g a nullRange)) $ - return . Spec_defn n g a + --trace ("spec:" ++ show (Spec_defn n g a nullRange)) $ + return . Spec_defn n g a . catRange $ [s, e] ++ maybeToList q -- CASL view-defn or DOL IntprDefn diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index 74019854c4..4dd30d3e4d 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -55,7 +55,7 @@ import Data.Char import Data.Maybe import Control.Monad -import Debug.Trace +-- import Debug.Trace expandCurieM :: LogicGraph -> IRI -> GenParser Char st IRI expandCurieM lG i = @@ -536,7 +536,8 @@ groupSpecAux withImport l flag = do case mf of Nothing -> return $ UnsolvedName n nullRange Just ((f, mi), ps) -> let inst = Spec_inst n f mi ps - in trace ("inst:" ++ show inst) $ return inst + in -- trace ("inst:" ++ show inst) $ + return inst fitArgsPattern :: LogicGraph -> Bool -> Bool -> AParser st (([Annoted FIT_ARG], Maybe IRI), Range) fitArgsPattern l flag withImport = do @@ -567,16 +568,22 @@ fitArg l flag = do <|> do fa <- annoParser $ fittingArg l flag return (fa, nullRange) - + <|> do + _b <- oBracketT + (aspecs, _) <- separatedBy (iParser l flag) commaT + _c <- cBracketT + return (Annoted (Fit_spec (Annoted (OntoList aspecs) nullRange [] []) [] nullRange) nullRange [] [], nullRange) -fitString :: LogicGraph -> Bool -> AParser st FIT_ARG -fitString l flag = do - let iParser = do +iParser :: LogicGraph -> Bool -> AParser st (Annoted SPEC) +iParser l flag = do i <- compoundIriCurie _ <- option () skip return $ Annoted (UnsolvedName i nullRange) nullRange [][] <|> aSpec l flag - (s, _) <- separatedBy iParser doubleColonT + +fitString :: LogicGraph -> Bool -> AParser st FIT_ARG +fitString l flag = do + (s, _) <- separatedBy (iParser l flag) doubleColonT case s of [] -> error "should be caught by the other case" [x] -> return $ Fit_spec x [] nullRange From 637b461f0af2e7a0ec72698aeabe9efd9ea86834 Mon Sep 17 00:00:00 2001 From: mscodescu Date: Fri, 7 Feb 2020 08:58:14 +0200 Subject: [PATCH 12/33] string arguments and instantiation in annotations --- OWL2/StaticAnalysis.hs | 65 ++++++++++++++++++++++++++++------- Static/AnalysisLibrary.hs | 7 ++-- Static/AnalysisStructured.hs | 21 +++++++---- Static/DevGraph.hs | 1 + Syntax/AS_Library.der.hs | 2 +- Syntax/AS_Structured.der.hs | 1 + Syntax/Parse_AS_Library.hs | 4 +++ Syntax/Parse_AS_Structured.hs | 3 ++ 8 files changed, 82 insertions(+), 22 deletions(-) diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 18fd94a157..3387a565b0 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -639,7 +639,7 @@ solveFrame impSyms vMap (Frame ext fBits) = do -- solve symbols for each frame bit solveFrameBit :: Set.Set Entity -> PatternVarMap -> FrameBit -> Result (FrameBit, Set.Set Entity) -solveFrameBit impSyms vMap fbit = trace ("fbit:" ++ show fbit) $ +solveFrameBit impSyms vMap fbit = -- trace ("fbit:" ++ show fbit) $ case fbit of ListFrameBit mr lft -> case lft of @@ -647,8 +647,14 @@ solveFrameBit impSyms vMap fbit = trace ("fbit:" ++ show fbit) $ ExpressionBit aces -> do let (aces', used') = foldl (\(as, us) ace -> let (ace', us') = solveClassExpression impSyms vMap ace in (as ++ [ace'], Set.union us us')) ([], Set.empty) aces - trace ("solved lft:" ++ show (ExpressionBit aces')) $ return (ListFrameBit mr $ ExpressionBit aces', used') - ObjectBit aopes -> error "nyi" + -- trace ("solved lft:" ++ show (ExpressionBit aces')) $ + return (ListFrameBit mr $ ExpressionBit aces', used') + ObjectBit aopes -> do + let (aopes', used') = foldl (\(as, us) aope -> + let (aope', us') = solveObjPropExpression impSyms vMap aope + in (as ++ [aope'], Set.union us us')) ([], Set.empty) aopes + -- trace ("solved lft:" ++ show (ObjectBit aopes')) $ + return (ListFrameBit mr $ ObjectBit aopes', used') DataBit adpes -> error "nyi" IndividualSameOrDifferent ainds -> error "nyi" ObjectCharacteristics achars -> return (fbit, Set.empty) @@ -658,7 +664,12 @@ solveFrameBit impSyms vMap fbit = trace ("fbit:" ++ show fbit) $ AnnFrameBit annos (DatatypeBit drg) -> error "nyi" AnnFrameBit annos (ClassDisjointUnion cexps) -> error "nyi" AnnFrameBit annos (ClassHasKey opexps dpexps) -> error "nyi" - AnnFrameBit annos (ObjectSubPropertyChain opexps) -> error "nyi" + AnnFrameBit annos (ObjectSubPropertyChain opexps) -> do + let (opexps', used') = foldl (\(as, us) ope -> + let (aope', us') = solveObjPropExpression impSyms vMap ([], ope) + in (as ++ [snd aope'], Set.union us us')) ([], Set.empty) opexps + -- trace ("solved lft:" ++ show (ObjectSubPropertyChain opexps')) $ + return (AnnFrameBit annos $ ObjectSubPropertyChain opexps', used') -- solve class expressions @@ -742,13 +753,13 @@ instantiateFrame subst var (Frame ext fBits) = do let j = Map.findWithDefault (error "instantiateFrame") (i, "Class") subst return $ ClassEntity $ Expression j else fail $ "unknown class variable: " ++ show i - ClassEntity _ -> error $ show ext + ClassEntity _ -> return ext ObjectEntity (ObjectPropertyVar b i) -> -- TODO: handle lists! if (i, "ObjectProperty") `elem` Map.keys subst then do let j = Map.findWithDefault (error "instantiateFrame") (i, "ObjectProperty") subst return $ ObjectEntity $ ObjectProp j else fail $ "unknown object property variable: " ++ show i - ObjectEntity _oExp -> error $ show ext -- TODO: handle oexp + ObjectEntity _ -> return ext SimpleEntity ent -> error $ show ext -- TODO: when do we get this? Misc _ -> error $ show ext fBits' <- mapM (instantiateFrameBit subst var) fBits @@ -763,13 +774,43 @@ instantiateFrameBit subst var fbit = ExpressionBit aces -> do aces' <- mapM (instantiateClassExpression subst var) aces return $ ListFrameBit mr $ ExpressionBit aces' - ObjectBit aopexps -> error $ show lfb + ObjectBit aopexps -> do + aopexps' <- mapM (instantiateObjectPropertyExpression subst var) aopexps + return $ ListFrameBit mr $ ObjectBit aopexps' DataBit adpexps -> error $ show lfb IndividualSameOrDifferent aindivs -> error $ show lfb ObjectCharacteristics _achars -> return fbit DataPropRange _ -> return fbit IndividualFacts afacts -> error $ show fbit - AnnFrameBit annos afb -> error $ show afb + AnnFrameBit annos afb -> do + annos' <- + case annos of + [] -> return annos + _ -> mapM (instantiateAnno subst var) annos + case afb of + AnnotationFrameBit at -> return $ AnnFrameBit annos' afb + DataFunctional -> return $ AnnFrameBit annos' afb + DatatypeBit drg -> error $ show fbit + ClassDisjointUnion cexps -> do + aexps' <- mapM (instantiateClassExpression subst var) $ map (\x -> ([], x)) cexps + return $ AnnFrameBit annos' $ ClassDisjointUnion $ map snd aexps' + ClassHasKey opexps dpexps -> error $ show fbit + ObjectSubPropertyChain opexps -> do + aopexps' <- mapM (instantiateObjectPropertyExpression subst var) $ map (\x -> ([], x)) opexps + return $ AnnFrameBit annos' $ ObjectSubPropertyChain $ map snd aopexps' + +instantiateAnno :: Map.Map (IRI, String) IRI -> PatternVarMap -> Annotation -> Result Annotation +instantiateAnno subst var anno = + case anno of + Annotation annos aprop aval -> do + case aval of + AnnValue x -> + if (x, "String") `elem` Map.keys subst then do + let v = Map.findWithDefault (error "already checked") (x, "String") subst + return $ Annotation annos aprop $ AnnValue v -- should be AnnValLit but I don't know yet what to put in it! TODO + else return anno + _ -> return anno + instantiateClassExpression :: Map.Map (IRI, String) IRI -> PatternVarMap -> (Annotations, ClassExpression) -> Result (Annotations, ClassExpression) instantiateClassExpression subst var (annos, cexp) = @@ -782,13 +823,13 @@ instantiateClassExpression subst var (annos, cexp) = acexps <- mapM (instantiateClassExpression subst var) $ map (\x -> ([], x)) cexps return (annos, ObjectJunction q $ map snd acexps) - ObjectComplementOf cexp -> do - (_, cexp') <- instantiateClassExpression subst var ([], cexp) + ObjectComplementOf cexp0 -> do + (_, cexp') <- instantiateClassExpression subst var ([], cexp0) return (annos, ObjectComplementOf cexp') ObjectOneOf indivs -> error "nyi" - ObjectValuesFrom q opexp cexp -> do + ObjectValuesFrom q opexp cexp0 -> do (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) - (_, cexp') <- instantiateClassExpression subst var ([], cexp) + (_, cexp') <- instantiateClassExpression subst var ([], cexp0) return (annos, ObjectValuesFrom q opexp' cexp') ObjectHasValue opexp indiv -> do (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index 31737cc6d6..bf9ab18d95 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -22,7 +22,7 @@ module Static.AnalysisLibrary , LNS ) where ---import Debug.Trace +import Debug.Trace import Logic.Logic import Logic.Grothendieck @@ -608,7 +608,7 @@ anaLibItem lg opts topLns currLn libenv dg eo itm = -- trace ("itm:" ++ show itm then liftR $ plain_error (itm, dg, libenv, lg, eo) (alreadyDefined spstr) r - else -- trace ("inserting:" ++ show spn) $ + else trace ("inserting:" ++ show entry) $ return (itm', dg3{globalEnv = Map.insert spn entry genv}, libenv, lg, eo) _ -> return (itm, dg, libenv, lg, eo) @@ -627,6 +627,9 @@ anaPatternParam :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts -> Result (PatternParam, PatternParamInfo, PatternVarMap, MaybeNode, DGraph) anaPatternParam lg lenv ln dg opts eo name vMap prevParamNode pParam = case pParam of + StringParam i -> do + l <- lookupCurrentLogic "anaPatternParam" lg + return (pParam, StringParamInfo i, Map.insert i (False, "String") vMap, EmptyNode l, dg) OntoParam isOpt aSpec -> do (sp', psig, dg') <- anaSpecTop None False lg lenv ln dg prevParamNode name opts eo (item aSpec) nullRange let aSpec' = aSpec{item = sp'} diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 276fd2119c..d3acc246e1 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -83,7 +83,7 @@ import Common.Lib.Graph import Static.ComputeTheory import Static.History --- import Debug.Trace +import Debug.Trace -- overrides CUIRIE expansion for Download_items type ExpOverrides = Map.Map IRI FilePath @@ -532,7 +532,7 @@ anaSpecAux conser addSyms optNodes lg spname <- expCurieR (globalAnnos dg) eo spname' let pos = if null afitargs then iriPos spname else pos0 adjustPos pos $ case lookupGlobalEnvDG spname dg of - Just (PatternEntry patSig@(PatternSig _local imp params vMap _body)) -> -- trace ("patSig:" ++ show patSig) $ + Just (PatternEntry patSig@(PatternSig _local imp params vMap _body)) -> trace ("patSig:" ++ show patSig) $ -- 1. solve afitargs using params and imp case (length afitargs, length params) of (la, lp) -> do @@ -1079,6 +1079,7 @@ anaFitArg :: LogicGraph -> LibEnv -> LibName -> DGraph -> IRI -> MaybeNode anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo csig prevSig mgm fv = let ga = globalAnnos dg in case fv of + Fit_string s _ -> error $ "nyi for " ++ (show s) Fit_spec asp gsis pos -> do (sp', nsigA', dg0) <- -- trace ("calling ana spec:" ++ show asp) $ anaSpec False True lg libEnv ln @@ -1403,6 +1404,12 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = --trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0) $ case item arg0 of + Fit_string s r -> + case par0 of + StringParamInfo i -> do + l <- lookupCurrentLogic "fit string" lg + return (arg0, dg0, EmptyNode l, Map.insert (i, "String") s subst0, mgm0) + _ -> error $ "parameter mismatch, got a string when expecting a " ++ show par0 Fit_spec asp gm r -> case item asp of UnsolvedName i rg -> @@ -1512,7 +1519,7 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev (dg',a') <- instMacroAux a return (dg', as ++ [a']) ) (dg, []) asps return $ (dg', asp{item = Union asps' rg}) - Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0)) $ + Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv dg case snEntry of @@ -1528,20 +1535,20 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev return $ Just $ G_morphism crtLid emor startMorId let solveVars aFitArg = case item aFitArg of - Fit_spec asp gm rg -> - case item asp of + Fit_spec asp1 gm rg -> + case item asp1 of NormalVariable i -> if i `elem` Map.keys vars then let (b, k) = Map.findWithDefault (error "notPossible") i vars val = Map.findWithDefault (error "variable not mapped") (i,k) subst - in ([((i,k), (val,k))], aFitArg{item = Fit_spec asp{item= UnsolvedName val nullRange} gm rg}) + in ([((i,k), (val,k))], aFitArg{item = Fit_spec asp1{item= UnsolvedName val nullRange} gm rg}) else error $ "unknown variable:" ++ show i _ -> ([], aFitArg) _ -> ([], aFitArg) solved = map solveVars afitargs afitargs0 = map snd solved newVars = concatMap fst solved - zipped = -- trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ + zipped = trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! -- TODO: if isLocal start with subst1 else start with empty subst? gmor' <- case mgmPrev of diff --git a/Static/DevGraph.hs b/Static/DevGraph.hs index d98d4e825c..af961f3ff9 100644 --- a/Static/DevGraph.hs +++ b/Static/DevGraph.hs @@ -722,6 +722,7 @@ getBody (LocalSig _ x) = x data PatternParamInfo = SingleParamInfo Bool NodeSig -- optional or not, node in graph | ListParamInfo Int Bool MaybeNode -- length, exact or minimal, node of template + | StringParamInfo IRI deriving (Show, Eq, Typeable) -- TODO: extend for data parameters diff --git a/Syntax/AS_Library.der.hs b/Syntax/AS_Library.der.hs index d4fa04710e..2dddcdb0ba 100644 --- a/Syntax/AS_Library.der.hs +++ b/Syntax/AS_Library.der.hs @@ -117,7 +117,7 @@ addDownloadAux unique j = (if unique then UniqueItem i else ItemMaps [ItemNameMap i Nothing]) $ iriPos i -data PatternParam = OntoParam Bool (Annoted SPEC) | ListParam OntoList +data PatternParam = StringParam IRI | OntoParam Bool (Annoted SPEC) | ListParam OntoList deriving (Show, Typeable) -- the bool flag is true for optional parameters diff --git a/Syntax/AS_Structured.der.hs b/Syntax/AS_Structured.der.hs index 4f6bfa11c8..dbaa5d0a4e 100644 --- a/Syntax/AS_Structured.der.hs +++ b/Syntax/AS_Structured.der.hs @@ -129,6 +129,7 @@ data FIT_ARG = Fit_spec (Annoted SPEC) [G_mapping] Range -- pos: opt "fit" | Fit_view IRI [Annoted FIT_ARG] Range -- annotations before the view keyword are stored in Spec_inst + | Fit_string IRI Range | Fit_ctx G_symbol G_symbol Range | Fit_new G_symbol G_symbol Range | Fit_list [Annoted SPEC] Range diff --git a/Syntax/Parse_AS_Library.hs b/Syntax/Parse_AS_Library.hs index 0f4331d6e8..f665fce345 100644 --- a/Syntax/Parse_AS_Library.hs +++ b/Syntax/Parse_AS_Library.hs @@ -465,6 +465,10 @@ macroParam l = do case elems of [(x, isOpt)] -> return $ OntoParam isOpt x _ -> return $ ListParam $ OntoListCons $ map fst elems + <|> do + _ <- asKey "xsd:string" + i <- hetIRI l + return $ StringParam i elemParser :: LogicGraph -> AParser st (Annoted SPEC, Bool) elemParser lg = do diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index 4dd30d3e4d..c4de15d5d2 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -568,6 +568,9 @@ fitArg l flag = do <|> do fa <- annoParser $ fittingArg l flag return (fa, nullRange) + <|> do + s <- scanString + return (Annoted (Fit_string (mkIRI s) nullRange) nullRange [][], nullRange) <|> do _b <- oBracketT (aspecs, _) <- separatedBy (iParser l flag) commaT From 8789de262af8e0a0b16f8068274aa8ced2d3a13a Mon Sep 17 00:00:00 2001 From: mcodescu Date: Mon, 9 Mar 2020 08:24:41 +0100 Subject: [PATCH 13/33] individuals, not very clean --- OWL2/MS.hs | 1 + OWL2/ManchesterParser.hs | 9 ++++---- OWL2/StaticAnalysis.hs | 46 +++++++++++++++++++++++++++++++++------- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/OWL2/MS.hs b/OWL2/MS.hs index 09d37088f3..b51bd167f2 100644 --- a/OWL2/MS.hs +++ b/OWL2/MS.hs @@ -35,6 +35,7 @@ data Extended = | ClassEntity ClassExpression | ObjectEntity ObjectPropertyExpression | SimpleEntity Entity + | IndividualVar IRI deriving (Show, Eq, Ord, Typeable, Data) -- | frames with annotated lists diff --git a/OWL2/ManchesterParser.hs b/OWL2/ManchesterParser.hs index 47b0aff58d..30f8309ef9 100644 --- a/OWL2/ManchesterParser.hs +++ b/OWL2/ManchesterParser.hs @@ -195,8 +195,8 @@ dataPropertyFrame flag = do as <- many (dataFrameBit flag) return $ makeFrame (SimpleEntity $ mkEntity DataProperty duri) as -fact :: CharParser st Fact -fact = do +fact :: Bool -> CharParser st Fact +fact flag = do pn <- option Positive $ keyword notS >> return Negative u <- uriP do @@ -204,7 +204,8 @@ fact = do return $ DataPropertyFact pn u c <|> do t <- individual - return $ ObjectPropertyFact pn (ObjectProp u) t + let ope = if flag then ObjectProp u else UnsolvedObjProp u + return $ ObjectPropertyFact pn ope t iFrameBit :: Bool -> CharParser st FrameBit iFrameBit flag = do @@ -217,7 +218,7 @@ iFrameBit flag = do return $ ListFrameBit (Just $ SDRelation s) $ IndividualSameOrDifferent is <|> do pkeyword factsC - fs <- sepByComma $ optAnnos fact + fs <- sepByComma $ optAnnos (fact flag) return $ ListFrameBit Nothing $ IndividualFacts fs <|> do a <- annotations diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 3387a565b0..a07a135f95 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -630,7 +630,11 @@ solveFrame impSyms vMap (Frame ext fBits) = do then (ObjectEntity $ ObjectProp i, Set.empty) -- add only if not member of impSyms else (ObjectEntity $ ObjectProp i, Set.singleton $ Entity Nothing ObjectProperty i) ObjectEntity _ -> error $ show ext -- TODO: handle oexp - SimpleEntity ent -> (ext, Set.empty) -- TODO: when do we get this? + SimpleEntity (Entity l UnsolvedEntity i) -> + if i `elem` Map.keys vMap + then -- TODO: tests that it's an individual! + (IndividualVar i, Set.empty) + else (SimpleEntity $ Entity l NamedIndividual i, Set.singleton $ Entity l NamedIndividual i) (fBits', used) <- foldM (\(fbs, us) fbit -> do (fbit', us') <- solveFrameBit impSyms vMap fbit return (fbs ++ [fbit'], Set.union us us')) ([], Set.empty) fBits @@ -656,9 +660,16 @@ solveFrameBit impSyms vMap fbit = -- trace ("fbit:" ++ show fbit) $ -- trace ("solved lft:" ++ show (ObjectBit aopes')) $ return (ListFrameBit mr $ ObjectBit aopes', used') DataBit adpes -> error "nyi" - IndividualSameOrDifferent ainds -> error "nyi" + IndividualSameOrDifferent ainds -> return (fbit, Set.empty) ObjectCharacteristics achars -> return (fbit, Set.empty) - IndividualFacts afacts -> error "nyi" + IndividualFacts afacts -> do + let (afacts', used') = foldl (\(afs, usyms) (a, af) -> do + case af of + ObjectPropertyFact pn ope i -> let ((_, ope'), us') = solveObjPropExpression impSyms vMap ([], ope) + in (afs ++ [(a,ObjectPropertyFact pn ope' i)], Set.union usyms us') + DataPropertyFact _ _ _ -> error "data property nyi") + ([], Set.empty) afacts + return (ListFrameBit mr $ IndividualFacts afacts', used') AnnFrameBit annos (AnnotationFrameBit _) -> return (fbit, Set.empty) AnnFrameBit annos DataFunctional -> return (fbit, Set.empty) AnnFrameBit annos (DatatypeBit drg) -> error "nyi" @@ -760,7 +771,12 @@ instantiateFrame subst var (Frame ext fBits) = do return $ ObjectEntity $ ObjectProp j else fail $ "unknown object property variable: " ++ show i ObjectEntity _ -> return ext - SimpleEntity ent -> error $ show ext -- TODO: when do we get this? + SimpleEntity ent -> error $ show ext -- TODO: when do we get this? + IndividualVar i -> + if (i, "Individual") `elem` Map.keys subst then do + let j = Map.findWithDefault (error "instantiateFrame") (i, "Individual") subst + return $ SimpleEntity $ Entity Nothing NamedIndividual j + else fail $ "unknown individual variable: " ++ show i Misc _ -> error $ show ext fBits' <- mapM (instantiateFrameBit subst var) fBits return $ Frame ext' fBits' @@ -778,10 +794,24 @@ instantiateFrameBit subst var fbit = aopexps' <- mapM (instantiateObjectPropertyExpression subst var) aopexps return $ ListFrameBit mr $ ObjectBit aopexps' DataBit adpexps -> error $ show lfb - IndividualSameOrDifferent aindivs -> error $ show lfb + IndividualSameOrDifferent aindivs -> + return $ ListFrameBit mr $ IndividualSameOrDifferent $ + map (\(a,i) -> if (i,"Individual")`elem` Map.keys subst then + let j = Map.findWithDefault (error "instantiateFrameBit") (i, "Individual") subst + in (a, j) + else (a,i)) aindivs ObjectCharacteristics _achars -> return fbit DataPropRange _ -> return fbit - IndividualFacts afacts -> error $ show fbit + IndividualFacts afacts -> do + afacts' <- mapM (\(a, af) -> case af of + ObjectPropertyFact pn ope i -> do + (_, ope') <- instantiateObjectPropertyExpression subst var ([], ope) + let j = if (i,"Individual")`elem` Map.keys subst then + Map.findWithDefault (error "instantiateFrameBit") (i, "Individual") subst + else i + return (a, ObjectPropertyFact pn ope' j) + DataPropertyFact pn dpe lit -> error "data property fact nyi") afacts + return $ ListFrameBit mr $ IndividualFacts afacts' AnnFrameBit annos afb -> do annos' <- case annos of @@ -860,7 +890,7 @@ instantiateObjectPropertyExpression subst var (annos, obexp) = (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) return (annos, ObjectInverseOf opexp') ObjectPropertyVar b x -> do -- TODO: lists - let v = Map.findWithDefault (error $ "unknown var:" ++ show x) (x, "ObjectProperty") subst + let v = Map.findWithDefault (error $ "unknown var:" ++ show x ++ " subst:" ++ show subst) (x, "ObjectProperty") subst return (annos, ObjectProp v) UnsolvedObjProp _ -> error $ "unsolved object property at instantiation: " ++ show obexp @@ -882,4 +912,4 @@ deleteSymbolsFrame delSyms f@(Frame ext fBits) = if Set.member i $ Set.map (\x -> idToIRI $ entityToId x) delSyms --TODO: handle lists then return [] else return [f] - _ -> error "nyi" + _ -> error $ "nyi: " ++ show ext From b127402c15e37ca58b71a020be8d870fa5a23338 Mon Sep 17 00:00:00 2001 From: mcodescu Date: Mon, 9 Mar 2020 12:22:30 +0100 Subject: [PATCH 14/33] only extend previous morphism if the pattern is non-local --- Static/AnalysisStructured.hs | 51 +++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index d3acc246e1..6b2772c208 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -1072,6 +1072,10 @@ anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) mgm -- also output symbols that are affected -} +-- TODO: analysis of fit args must be different for non-local patterns +-- the signature morphism will not expand the previous definitions +-- different argument in the call in spec_inst!!!! + anaFitArg :: LogicGraph -> LibEnv -> LibName -> DGraph -> IRI -> MaybeNode -> NodeSig -> HetcatsOpts -> NodeName -> ExpOverrides -> MaybeNode -> MaybeNode -> Maybe G_morphism -> FIT_ARG @@ -1109,7 +1113,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c (_, Comorphism aid) <- -- trace ("actSig:" ++ show actSig) $ logicUnion lg (getNodeLogic nsigP) (getNodeLogic nsigA) let tl = Logic $ targetLogic aid - (nsigA'@(NodeSig nA' gsigA'), dg'') <- coerceNode lg dg' nsigA name tl + (nsigA''@(NodeSig nA' gsigA'), dg'') <- coerceNode lg dg' nsigA name tl (gsigmaP', pmor) <- gSigCoerce lg gsigmaP tl tmor <- gEmbedComorphism pmor gsigmaP gmor <- anaGmaps lg opts pos gsigmaP' gsigA' mgm gsis @@ -1118,7 +1122,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c , if nP == nA' && isInclusion eGmor then dg'' else insLink dg'' eGmor globalThm (DGLinkInst spname $ Fitted gsis) nP nA' - , (gmor, nsigA')) + , (gmor, nsigA'')) Fit_ctx (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym sRSym = symbol_to_raw slid ssym @@ -1162,17 +1166,18 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c crtMap = if Map.intersection crtMapAux prevMap == Map.empty then Map.union prevMap crtMapAux else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) -- TODO: don't fail if the symbols are mapped in the same way - mor <- induced_from_to_morphism slid crtMap ssig tsig + mor <- -- trace ("induced:" ++ show crtMap ++ " ssig:" ++ show ssig ++ " tsig:" ++ show tsig) $ + induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na return (fv, dg'', (gmor, nsigA)) Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do - --trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do + trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym sRSym = symbol_to_raw slid ssym sigA <- add_symb_to_sign tlid (empty_signature tlid) tsym let extSigA = ExtSign sigA (Set.singleton tsym) - (asig@(NodeSig a ga), dg0) = insGSig dg (extName "Actual" name) (DGInst spname) $ G_sign tlid extSigA startSigId + (asig@(NodeSig a gA), dg0) = insGSig dg (extName "Actual" name) (DGInst spname) $ G_sign tlid extSigA startSigId uNodes = (case csig of EmptyNode _ -> [] JustNode x -> [x]) ++ @@ -1181,11 +1186,11 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c EmptyNode _ -> [] JustNode x -> [x] JustNode x -> [x]) - gUnionSig <- gsigManyUnion lg $ [ga] ++ map getSig uNodes + gUnionSig <- gsigManyUnion lg $ [gA] ++ map getSig uNodes let (usig@(NodeSig unode _), dg1) = insGSig dg0 (extName "Union" name) DGUnion gUnionSig insE dgl (NodeSig n gs) = do - incl <- -- trace ("inclusion from " ++ show gs ++ " to " ++ show gUnionSig) $ + incl <- trace ("inclusion from " ++ show gs ++ " to " ++ show gUnionSig) $ ginclusion lg gs gUnionSig return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ asig:uNodes @@ -1204,11 +1209,18 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c let crtMapAux = Map.fromList [(sRSym, tRSym)] crtMap = if Map.intersection crtMapAux prevMap == Map.empty then Map.union prevMap crtMapAux - else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) -- TODO: don't fail if the symbols are mapped in the same way - mor <- -- trace ("ssig:"++ show ssig ++ " tsig:" ++ show tsig ++ " crtMap:" ++ show crtMap ) $ + else -- don't fail if the symbols are mapped in the same way + let intersMapKeys = Map.keys $ Map.intersection crtMapAux prevMap + allMappedSameWay = foldl (\b k -> let v1 = Map.findWithDefault (error "not in crt") k crtMapAux + v2 = Map.findWithDefault (error "not in prev") k prevMap + in (v1 == v2) && b ) True intersMapKeys + in if allMappedSameWay then Map.union prevMap crtMapAux + else + error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) + mor <- trace ("ssig:"++ show ssig ++ " tsig:" ++ show tsig ++ " crtMap:" ++ show crtMap ) $ induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor - dg'' = -- trace ("gmor after induced:" ++ show gmor) $ + dg'' = trace ("gmor after induced:" ++ show gmor) $ insLink dg2 (gEmbed gmor) globalThm (DGLinkInstArg spname) nP unode return (fv, dg'', (gmor, usig)) -- trace ("sigA:" ++ show usig) $ error "fit_new nyi" @@ -1402,7 +1414,7 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = --trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1441,7 +1453,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 [sym] -> do let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ + (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of G_morphism glid mor _ -> do @@ -1523,7 +1535,7 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv dg case snEntry of - PatternEntry patSig@(PatternSig isLocal _ pParams pMap pBody) -> do + PatternEntry patSig@(PatternSig isLocal _ pParams pMap pBody) -> trace ("isLocal:" ++ show isLocal) $ do l@(Logic crtLid) <- lookupCurrentLogic "anaPatternInstArgs" lg idImps <- case imp of EmptyNode _ -> return Nothing @@ -1551,12 +1563,15 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev zipped = trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! -- TODO: if isLocal start with subst1 else start with empty subst? - gmor' <- case mgmPrev of - Nothing -> extendWithSubst l idImps newVars - Just agm -> return $ Just agm - (afitargs', dg', nsig', subst', gm') <- + gmor' <- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ + if isLocal then + case mgmPrev of + Nothing -> extendWithSubst l idImps newVars + Just agm -> return $ Just agm + else extendWithSubst l idImps newVars + (afitargs', dg', nsig', subst', gm') <- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ + (arg1, dg1, nsig1, subst1, gm1) <- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 imp (EmptyNode l) nsig0 -- TODO: context is always empty now name spname subst0 gm0 par0 arg0 From e5fac940e28e714ec393593d897000a91849eb6c Mon Sep 17 00:00:00 2001 From: mcodescu Date: Wed, 18 Mar 2020 12:21:55 +0100 Subject: [PATCH 15/33] instantiation with parameterized names --- OWL2/StaticAnalysis.hs | 34 +++++++++++++++++++++++++++++++--- Static/AnalysisLibrary.hs | 2 +- Static/AnalysisStructured.hs | 28 +++++++++++++++------------- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index a07a135f95..69ec5a95d3 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -32,6 +32,7 @@ import Common.GlobalAnnotations hiding (PrefixMap) import Common.ExtSign import Common.Lib.State import Common.IRI --(iriToStringUnsecure, setAngles) +import Common.Id import Common.SetColimit import Control.Monad @@ -738,7 +739,8 @@ solveIndividual :: Set.Set Entity -> PatternVarMap -> (Annotations, IndExpress solveIndividual _ _ _ = error "nyi" -- TODO: - -- write a method that solves an obj prop expression, data property expression etc. +-- write a method that solves a data property expression etc. +-- note: individuals are not stored as vars at any moment. -- instantiate a macro with the values stored in a substitution @@ -764,14 +766,19 @@ instantiateFrame subst var (Frame ext fBits) = do let j = Map.findWithDefault (error "instantiateFrame") (i, "Class") subst return $ ClassEntity $ Expression j else fail $ "unknown class variable: " ++ show i + ClassEntity (Expression i) -> return $ ClassEntity $ Expression $ instParamName subst i ClassEntity _ -> return ext ObjectEntity (ObjectPropertyVar b i) -> -- TODO: handle lists! if (i, "ObjectProperty") `elem` Map.keys subst then do let j = Map.findWithDefault (error "instantiateFrame") (i, "ObjectProperty") subst return $ ObjectEntity $ ObjectProp j else fail $ "unknown object property variable: " ++ show i + ObjectEntity (ObjectProp i) -> + return $ ObjectEntity $ ObjectProp + $ instParamName subst i ObjectEntity _ -> return ext - SimpleEntity ent -> error $ show ext -- TODO: when do we get this? + SimpleEntity ent -> return $ SimpleEntity $ ent{cutIRI = instParamName subst $ cutIRI ent} + -- TODO: we get this for individuals declared in the bodies! what about data props? IndividualVar i -> if (i, "Individual") `elem` Map.keys subst then do let j = Map.findWithDefault (error "instantiateFrame") (i, "Individual") subst @@ -781,6 +788,26 @@ instantiateFrame subst var (Frame ext fBits) = do fBits' <- mapM (instantiateFrameBit subst var) fBits return $ Frame ext' fBits' +-- instantiate paramerized names +-- p[X] becomes p[V] if subst maps X to V +-- the string argument is the kind + +instParamName :: Map.Map (IRI, String) IRI -> IRI -> IRI +instParamName subst p = + let pPath = iriPath p + comps = getComps pPath + solveId t = + let tIRI = idToIRI t + k = let tSubsts = filter (\(x,y) -> x == tIRI) $ Map.keys subst + in case tSubsts of + [(a,b)] -> b + []-> "Class" -- does not matter + (a,b):_ -> b + in Map.findWithDefault tIRI (tIRI,k) subst -- this will most likely need to change for complex nesting! + comps' = map (\t -> iriPath $ solveId t) comps + newPath = trace ("comps:" ++ show comps ++ " comps':" ++ show comps') $ pPath{getComps = comps'} + in p{iriPath = newPath} + instantiateFrameBit :: Map.Map (IRI, String) IRI -> PatternVarMap -> FrameBit -> Result FrameBit instantiateFrameBit subst var fbit = case fbit of @@ -846,6 +873,7 @@ instantiateClassExpression :: Map.Map (IRI, String) IRI -> PatternVarMap -> (Ann instantiateClassExpression subst var (annos, cexp) = case cexp of UnsolvedClass _ -> error $ "unsolved class at instantiation: " ++ show cexp + Expression i -> return (annos, Expression $ instParamName subst i) VarExpression (MVar b x) -> do let v = Map.findWithDefault (error $ "unknown var:" ++ show x) (x, "Class") subst return (annos, Expression v) @@ -885,7 +913,7 @@ instantiateClassExpression subst var (annos, cexp) = instantiateObjectPropertyExpression :: Map.Map (IRI, String) IRI -> PatternVarMap -> (Annotations, ObjectPropertyExpression) -> Result (Annotations, ObjectPropertyExpression) instantiateObjectPropertyExpression subst var (annos, obexp) = case obexp of - ObjectProp _ -> return (annos, obexp) -- nothing to instantiate + ObjectProp i -> return (annos, ObjectProp $ instParamName subst i) -- nothing to instantiate ObjectInverseOf opexp -> do (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) return (annos, ObjectInverseOf opexp') diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index bf9ab18d95..730371309b 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -675,7 +675,7 @@ anaPatternParam lg lenv ln dg opts eo name vMap prevParamNode pParam = ([], [], dg) $ map item $ reverse $ lastSpecSolved : sps' let oList' = map (\(x,y) -> x{item = y}) $ zip aSpecs aSpecs' pParam' = ListParam $ OntoListCons oList' - size = length aSpecs - 1 + size = length aSpecs - 1 -- TODO: this needs to be 3, not 1! firstNode = head nsigs iSig = case prevParamNode of EmptyNode _ -> Nothing diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 6b2772c208..778721ae1a 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -1172,7 +1172,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na return (fv, dg'', (gmor, nsigA)) Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do - trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do + -- trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym sRSym = symbol_to_raw slid ssym sigA <- add_symb_to_sign tlid (empty_signature tlid) tsym @@ -1190,7 +1190,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c let (usig@(NodeSig unode _), dg1) = insGSig dg0 (extName "Union" name) DGUnion gUnionSig insE dgl (NodeSig n gs) = do - incl <- trace ("inclusion from " ++ show gs ++ " to " ++ show gUnionSig) $ + incl <- -- trace ("inclusion from " ++ show gs ++ " to " ++ show gUnionSig) $ ginclusion lg gs gUnionSig return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ asig:uNodes @@ -1217,10 +1217,10 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c in if allMappedSameWay then Map.union prevMap crtMapAux else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) - mor <- trace ("ssig:"++ show ssig ++ " tsig:" ++ show tsig ++ " crtMap:" ++ show crtMap ) $ + mor <- -- trace ("ssig:"++ show ssig ++ " tsig:" ++ show tsig ++ " crtMap:" ++ show crtMap ) $ induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor - dg'' = trace ("gmor after induced:" ++ show gmor) $ + dg'' = -- trace ("gmor after induced:" ++ show gmor) $ insLink dg2 (gEmbed gmor) globalThm (DGLinkInstArg spname) nP unode return (fv, dg'', (gmor, usig)) -- trace ("sigA:" ++ show usig) $ error "fit_new nyi" @@ -1414,7 +1414,7 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = -- trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1453,7 +1453,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 [sym] -> do let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ + (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of G_morphism glid mor _ -> do @@ -1497,7 +1497,9 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- where x is the unique symbol declared in the param -- 3. otherwise, i is a new symbol of same kind as x -- and the substitution maps x to i - _ -> error "only unsolved names for now" + OntoList aspecs -> error $ "lists as args not yet supported: " ++ show aspecs ++ " par0:" ++ show par0 + _ -> -- trace ("itm:" ++ (show $ item arg0) ) $ + error "only unsolved names for now" _ -> -- trace ("itm:" ++ (show $ item arg0)) $ error "only fit_spec for now" @@ -1531,11 +1533,11 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev (dg',a') <- instMacroAux a return (dg', as ++ [a']) ) (dg, []) asps return $ (dg', asp{item = Union asps' rg}) - Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ + Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv dg case snEntry of - PatternEntry patSig@(PatternSig isLocal _ pParams pMap pBody) -> trace ("isLocal:" ++ show isLocal) $ do + PatternEntry patSig@(PatternSig isLocal _ pParams pMap pBody) -> do l@(Logic crtLid) <- lookupCurrentLogic "anaPatternInstArgs" lg idImps <- case imp of EmptyNode _ -> return Nothing @@ -1560,18 +1562,18 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev solved = map solveVars afitargs afitargs0 = map snd solved newVars = concatMap fst solved - zipped = trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ + zipped = -- trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! -- TODO: if isLocal start with subst1 else start with empty subst? - gmor' <- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ + gmor' <- -- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ if isLocal then case mgmPrev of Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm else extendWithSubst l idImps newVars - (afitargs', dg', nsig', subst', gm') <- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ + (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ + (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 imp (EmptyNode l) nsig0 -- TODO: context is always empty now name spname subst0 gm0 par0 arg0 From 3b341835694c38f608d9f8c1fdc1b8a0e1eb7c2e Mon Sep 17 00:00:00 2001 From: mcodescu Date: Wed, 8 Apr 2020 20:49:49 +0200 Subject: [PATCH 16/33] list parameters --- Logic/Logic.hs | 14 +++- OWL2/StaticAnalysis.hs | 98 +++++++++++++++++------ Static/AnalysisLibrary.hs | 26 +++--- Static/AnalysisStructured.hs | 149 ++++++++++++++++++++++++++++++----- Static/DevGraph.hs | 4 +- 5 files changed, 235 insertions(+), 56 deletions(-) diff --git a/Logic/Logic.hs b/Logic/Logic.hs index f5e3821c11..b01f3344e7 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -642,12 +642,24 @@ class ( Syntax lid basic_spec symbol symb_items symb_map_items solve_symbols :: lid -> Set.Set symbol -> PatternVarMap -> basic_spec -> Result basic_spec solve_symbols _ _ _ = error "solve_symbols nyi" -- instantiating macros - instantiate_macro :: lid -> PatternVarMap -> Map.Map (IRI, String) IRI -> basic_spec -> Result basic_spec + instantiate_macro :: lid -> PatternVarMap -> GSubst -> basic_spec -> Result basic_spec instantiate_macro _ _ _ _ = error "instantiate_macro nyi" -- delete all occurences of a set of symbols in a solved macro delete_symbols_macro :: lid -> Set.Set symbol -> basic_spec -> Result basic_spec delete_symbols_macro _ _ _ = error "delete_symbols_macro nyi" + +type GSubst = Map.Map (IRI, String) GSubstVal -- TODO: use GSubstVal instead of IRI! + +data GSubstVal = PlainVal IRI | + ListVal String [IRI] -- the string stores the kind! + deriving (Eq, Show) + +getIRIVal :: GSubstVal -> IRI +getIRIVal v = case v of + PlainVal x -> x + _ -> error $ "expecting plain value but got list:" ++ show v + type PatternVarMap = Map.Map IRI (Bool, String) -- Bool is true for list- and false for non-list variables -- TODO: ideally we should have logic-dependent kinds, but strings will do diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 69ec5a95d3..c5122c2fcb 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -674,7 +674,10 @@ solveFrameBit impSyms vMap fbit = -- trace ("fbit:" ++ show fbit) $ AnnFrameBit annos (AnnotationFrameBit _) -> return (fbit, Set.empty) AnnFrameBit annos DataFunctional -> return (fbit, Set.empty) AnnFrameBit annos (DatatypeBit drg) -> error "nyi" - AnnFrameBit annos (ClassDisjointUnion cexps) -> error "nyi" + AnnFrameBit annos (ClassDisjointUnion cexps) -> do + let (aces', used') = foldl (\(as, us) ce -> let (ace', us') = solveClassExpression impSyms vMap ([], ce) + in (as ++ [ace'], Set.union us us')) ([], Set.empty) cexps + return (AnnFrameBit annos $ ClassDisjointUnion $ map snd aces', used') AnnFrameBit annos (ClassHasKey opexps dpexps) -> error "nyi" AnnFrameBit annos (ObjectSubPropertyChain opexps) -> do let (opexps', used') = foldl (\(as, us) ope -> @@ -745,33 +748,33 @@ solveIndividual _ _ _ = error "nyi" -- instantiate a macro with the values stored in a substitution -instantiateMacro :: PatternVarMap -> Map.Map (IRI, String) IRI -> OntologyDocument -> Result OntologyDocument +instantiateMacro :: PatternVarMap -> GSubst -> OntologyDocument -> Result OntologyDocument instantiateMacro vars subst (OntologyDocument pd (Ontology n is as fs)) = do fs'<- instantiateFrames subst vars fs return $ OntologyDocument pd $ Ontology n is as fs' -- instantiate frames -instantiateFrames :: Map.Map (IRI, String) IRI -> PatternVarMap -> [Frame] -> Result [Frame] +instantiateFrames :: GSubst -> PatternVarMap -> [Frame] -> Result [Frame] instantiateFrames subst vars = mapM (instantiateFrame subst vars) -- instantiate a single frame -instantiateFrame :: Map.Map (IRI, String) IRI -> PatternVarMap -> Frame -> Result Frame +instantiateFrame :: GSubst -> PatternVarMap -> Frame -> Result Frame instantiateFrame subst var (Frame ext fBits) = do ext' <- case ext of ClassEntity (VarExpression (MVar b i)) -> -- TODO: handle lists! if (i, "Class") `elem` Map.keys subst then do let j = Map.findWithDefault (error "instantiateFrame") (i, "Class") subst - return $ ClassEntity $ Expression j + return $ ClassEntity $ Expression $ getIRIVal j else fail $ "unknown class variable: " ++ show i ClassEntity (Expression i) -> return $ ClassEntity $ Expression $ instParamName subst i ClassEntity _ -> return ext ObjectEntity (ObjectPropertyVar b i) -> -- TODO: handle lists! if (i, "ObjectProperty") `elem` Map.keys subst then do let j = Map.findWithDefault (error "instantiateFrame") (i, "ObjectProperty") subst - return $ ObjectEntity $ ObjectProp j + return $ ObjectEntity $ ObjectProp $ getIRIVal j else fail $ "unknown object property variable: " ++ show i ObjectEntity (ObjectProp i) -> return $ ObjectEntity $ ObjectProp @@ -782,7 +785,7 @@ instantiateFrame subst var (Frame ext fBits) = do IndividualVar i -> if (i, "Individual") `elem` Map.keys subst then do let j = Map.findWithDefault (error "instantiateFrame") (i, "Individual") subst - return $ SimpleEntity $ Entity Nothing NamedIndividual j + return $ SimpleEntity $ Entity Nothing NamedIndividual $ getIRIVal j else fail $ "unknown individual variable: " ++ show i Misc _ -> error $ show ext fBits' <- mapM (instantiateFrameBit subst var) fBits @@ -792,7 +795,7 @@ instantiateFrame subst var (Frame ext fBits) = do -- p[X] becomes p[V] if subst maps X to V -- the string argument is the kind -instParamName :: Map.Map (IRI, String) IRI -> IRI -> IRI +instParamName :: GSubst -> IRI -> IRI instParamName subst p = let pPath = iriPath p comps = getComps pPath @@ -803,12 +806,12 @@ instParamName subst p = [(a,b)] -> b []-> "Class" -- does not matter (a,b):_ -> b - in Map.findWithDefault tIRI (tIRI,k) subst -- this will most likely need to change for complex nesting! - comps' = map (\t -> iriPath $ solveId t) comps + in Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst -- this will most likely need to change for complex nesting! + comps' = map (\t -> iriPath $ getIRIVal $ solveId t) comps newPath = trace ("comps:" ++ show comps ++ " comps':" ++ show comps') $ pPath{getComps = comps'} in p{iriPath = newPath} -instantiateFrameBit :: Map.Map (IRI, String) IRI -> PatternVarMap -> FrameBit -> Result FrameBit +instantiateFrameBit :: GSubst -> PatternVarMap -> FrameBit -> Result FrameBit instantiateFrameBit subst var fbit = case fbit of ListFrameBit mr lfb -> @@ -825,7 +828,7 @@ instantiateFrameBit subst var fbit = return $ ListFrameBit mr $ IndividualSameOrDifferent $ map (\(a,i) -> if (i,"Individual")`elem` Map.keys subst then let j = Map.findWithDefault (error "instantiateFrameBit") (i, "Individual") subst - in (a, j) + in (a, getIRIVal j) else (a,i)) aindivs ObjectCharacteristics _achars -> return fbit DataPropRange _ -> return fbit @@ -834,7 +837,7 @@ instantiateFrameBit subst var fbit = ObjectPropertyFact pn ope i -> do (_, ope') <- instantiateObjectPropertyExpression subst var ([], ope) let j = if (i,"Individual")`elem` Map.keys subst then - Map.findWithDefault (error "instantiateFrameBit") (i, "Individual") subst + getIRIVal $ Map.findWithDefault (error "instantiateFrameBit") (i, "Individual") subst else i return (a, ObjectPropertyFact pn ope' j) DataPropertyFact pn dpe lit -> error "data property fact nyi") afacts @@ -856,31 +859,34 @@ instantiateFrameBit subst var fbit = aopexps' <- mapM (instantiateObjectPropertyExpression subst var) $ map (\x -> ([], x)) opexps return $ AnnFrameBit annos' $ ObjectSubPropertyChain $ map snd aopexps' -instantiateAnno :: Map.Map (IRI, String) IRI -> PatternVarMap -> Annotation -> Result Annotation +instantiateAnno :: GSubst -> PatternVarMap -> Annotation -> Result Annotation instantiateAnno subst var anno = case anno of Annotation annos aprop aval -> do case aval of AnnValue x -> if (x, "String") `elem` Map.keys subst then do - let v = Map.findWithDefault (error "already checked") (x, "String") subst + let v = getIRIVal $ Map.findWithDefault (error "already checked") (x, "String") subst return $ Annotation annos aprop $ AnnValue v -- should be AnnValLit but I don't know yet what to put in it! TODO else return anno _ -> return anno -instantiateClassExpression :: Map.Map (IRI, String) IRI -> PatternVarMap -> (Annotations, ClassExpression) -> Result (Annotations, ClassExpression) +-- TODO: this needs an aux that gives back a list of classexpressions! +instantiateClassExpression :: GSubst -> PatternVarMap -> (Annotations, ClassExpression) -> Result (Annotations, ClassExpression) instantiateClassExpression subst var (annos, cexp) = case cexp of UnsolvedClass _ -> error $ "unsolved class at instantiation: " ++ show cexp Expression i -> return (annos, Expression $ instParamName subst i) - VarExpression (MVar b x) -> do - let v = Map.findWithDefault (error $ "unknown var:" ++ show x) (x, "Class") subst + VarExpression (MVar b x) -> + if b then error "list occuring in not allowed position" + else do + let v = getIRIVal $ Map.findWithDefault (error $ "unknown var:" ++ show x ++ " b:" ++ show b ++ " subst:" ++ show subst) (x, "Class") subst return (annos, Expression v) ObjectJunction q cexps -> do - acexps <- mapM (instantiateClassExpression subst var) $ + acexps <- mapM (instClassExprAux subst var) $ map (\x -> ([], x)) cexps - return (annos, ObjectJunction q $ map snd acexps) + return (annos, ObjectJunction q $ map snd $ concat acexps) ObjectComplementOf cexp0 -> do (_, cexp') <- instantiateClassExpression subst var ([], cexp0) return (annos, ObjectComplementOf cexp') @@ -910,7 +916,55 @@ instantiateClassExpression subst var (annos, cexp) = | DataCardinality (Cardinality DataPropertyExpression DataRange) -} -instantiateObjectPropertyExpression :: Map.Map (IRI, String) IRI -> PatternVarMap -> (Annotations, ObjectPropertyExpression) -> Result (Annotations, ObjectPropertyExpression) +instClassExprAux :: GSubst -> PatternVarMap -> (Annotations, ClassExpression) -> Result [(Annotations, ClassExpression)] +instClassExprAux subst var (annos, cexp) = + case cexp of + UnsolvedClass _ -> error $ "unsolved class at instantiation: " ++ show cexp + Expression i -> return [(annos, Expression $ instParamName subst i)] + VarExpression (MVar b x) -> + if b then do + let v = Map.findWithDefault (error $ "unknown var:" ++ show x ++ " b:" ++ show b ++ " subst:" ++ show subst) (x, "list") subst + case v of + ListVal str iris -> + if str == "Class" then return $ map (\i -> ([], Expression i)) iris + else error $ "expected list of classes, kind mismatch" + _ -> error $ "list variable with non-list value" + else do + let v = getIRIVal $ Map.findWithDefault (error $ "unknown var:" ++ show x ++ " b:" ++ show b ++ " subst:" ++ show subst) (x, "Class") subst + return [(annos, Expression v)] + ObjectJunction q cexps -> do + acexps <- mapM (instClassExprAux subst var) $ + map (\x -> ([], x)) cexps + return [(annos, ObjectJunction q $ map snd $ concat acexps)] + ObjectComplementOf cexp0 -> do + (_, cexp') <- instantiateClassExpression subst var ([], cexp0) + return [(annos, ObjectComplementOf cexp')] + ObjectOneOf indivs -> error "nyi" + ObjectValuesFrom q opexp cexp0 -> do + (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) + (_, cexp') <- instantiateClassExpression subst var ([], cexp0) + return [(annos, ObjectValuesFrom q opexp' cexp')] + ObjectHasValue opexp indiv -> do + (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) + error "nyi" + ObjectHasSelf opexp -> do + (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) + return [(annos, ObjectHasSelf opexp')] + ObjectCardinality (Cardinality cType aInt opexp mcexp) -> do + mcexp' <- case mcexp of + Nothing -> return Nothing + Just cexp0 -> do + (_, cexp') <- instantiateClassExpression subst var ([], cexp0) -- TODO: test for this, are lists allowed here? + return $ Just cexp' + (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) + return [(annos, ObjectCardinality (Cardinality cType aInt opexp' mcexp'))] + _ -> return [(annos, cexp)] + + + + + +instantiateObjectPropertyExpression :: GSubst -> PatternVarMap -> (Annotations, ObjectPropertyExpression) -> Result (Annotations, ObjectPropertyExpression) instantiateObjectPropertyExpression subst var (annos, obexp) = case obexp of ObjectProp i -> return (annos, ObjectProp $ instParamName subst i) -- nothing to instantiate @@ -918,7 +972,7 @@ instantiateObjectPropertyExpression subst var (annos, obexp) = (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) return (annos, ObjectInverseOf opexp') ObjectPropertyVar b x -> do -- TODO: lists - let v = Map.findWithDefault (error $ "unknown var:" ++ show x ++ " subst:" ++ show subst) (x, "ObjectProperty") subst + let v = getIRIVal $ Map.findWithDefault (error $ "unknown var:" ++ show x ++ " subst:" ++ show subst) (x, "ObjectProperty") subst return (annos, ObjectProp v) UnsolvedObjProp _ -> error $ "unsolved object property at instantiation: " ++ show obexp diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index 730371309b..2535a2af04 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -637,7 +637,7 @@ anaPatternParam lg lenv ln dg opts eo name vMap prevParamNode pParam = iSig = case prevParamNode of EmptyNode _ -> Nothing JustNode x -> Just $ getSig x - (vMap', _) <- addSigSymsToVarMap vMap iSig aSig + (vMap', _) <- addSigSymsToVarMap vMap (mkIRI "") iSig aSig return (OntoParam isOpt aSpec', SingleParamInfo isOpt psig, vMap', @@ -648,7 +648,7 @@ anaPatternParam lg lenv ln dg opts eo name vMap prevParamNode pParam = EmptyParamList -> do l <- lookupCurrentLogic "anaPatternParam" lg return (ListParam oList, - ListParamInfo 0 True (EmptyNode l), + ListParamInfo 0 True (EmptyNode l) Nothing, vMap, EmptyNode l, dg) @@ -660,7 +660,7 @@ anaPatternParam lg lenv ln dg opts eo name vMap prevParamNode pParam = (lastSpecSolved, exactSize, listVar) = case item lastSpec of UnsolvedName x rg -> if x == emptyListName - then (lastSpec{item = EmptyList}, True, mkIRI "") + then (lastSpec{item = EmptyList}, True, emptyListName) else (lastSpec{item = ListVariable x}, False, x) _ -> error "Last element of the list must be empty or a variable name" (aSpecs', nsigs, dg') <- @@ -675,13 +675,13 @@ anaPatternParam lg lenv ln dg opts eo name vMap prevParamNode pParam = ([], [], dg) $ map item $ reverse $ lastSpecSolved : sps' let oList' = map (\(x,y) -> x{item = y}) $ zip aSpecs aSpecs' pParam' = ListParam $ OntoListCons oList' - size = length aSpecs - 1 -- TODO: this needs to be 3, not 1! + size = length aSpecs - 1 -- TODO: check this! firstNode = head nsigs iSig = case prevParamNode of EmptyNode _ -> Nothing JustNode x -> Just $ getSig x (vMap', newKinds) <- - foldM (\(f, k) nsig -> addSigSymsToVarMap f iSig $ getSig nsig) (vMap, Set.empty) $ + foldM (\(f, k) nsig -> addSigSymsToVarMap f listVar iSig $ getSig nsig) (vMap, Set.empty) $ map (\n -> case n of JustNode x -> x _ -> error "should never happen" @@ -689,7 +689,8 @@ anaPatternParam lg lenv ln dg opts eo name vMap prevParamNode pParam = let newK = case Set.toList newKinds of [x] -> x _ -> "ontology" - return (pParam', ListParamInfo size exactSize firstNode, Map.insert listVar (True, newK) vMap', firstNode, dg') + (vMap'', tailName) = if listVar == emptyListName then (vMap', Nothing) else (Map.insert listVar (True, "list") vMap', Just listVar) + return (pParam', ListParamInfo size exactSize firstNode tailName, vMap'', firstNode, dg') anaListElem :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts -> ExpOverrides -> NodeName -> PatternVarMap -> MaybeNode -> SPEC @@ -705,8 +706,8 @@ anaListElem lg lenv ln dg opts eo name vMap prevParamNode sp = do sp nullRange return (sp', JustNode nsig, bDg) -addSigSymsToVarMap :: PatternVarMap -> Maybe G_sign -> G_sign -> Result (PatternVarMap, Set.Set String) -addSigSymsToVarMap vMap exclSig aSig = +addSigSymsToVarMap :: PatternVarMap -> IRI -> Maybe G_sign -> G_sign -> Result (PatternVarMap, Set.Set String) +addSigSymsToVarMap vMap tailName exclSig aSig = case aSig of G_sign lid (ExtSign sig _) _ -> do let syms = symset_of lid sig @@ -719,8 +720,8 @@ addSigSymsToVarMap vMap exclSig aSig = let sIRI = idToIRI $ sym_name lid s in if sIRI `elem` Map.keys f then error $ "variable named " ++ show s ++ "already used in " ++ show f - else (Map.insert sIRI (False, symKind lid s) f, Set.insert (symKind lid s) k) - (vMap', newKinds) = foldl (\(f, k) s -> insertOrFail f s k) (vMap, Set.empty) $ Set.toList $ Set.difference syms symsExcl + else (Map.insert sIRI (if sIRI == tailName then (True, "list") else (False, symKind lid s)) f, Set.insert (symKind lid s) k) + (vMap', newKinds) = foldl (\(f, k) s -> trace ("f:" ++ show f) $ insertOrFail f s k) (vMap, Set.empty) $ Set.toList $ Set.difference syms symsExcl return (vMap', newKinds) anaPatternBody :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts @@ -778,7 +779,10 @@ solveBody :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts Result SPEC solveBody lg lenv ln dg opts eo name vMap impNode sp = case sp of - UnsolvedName i rg -> if i `elem` Map.keys vMap then return $ NormalVariable i + UnsolvedName i rg -> if i `elem` Map.keys vMap then + let (_, j) = Map.findWithDefault (error "just checked") i vMap + in if j == "list" then return $ ListVariable i + else return $ NormalVariable i else return sp -- can't solve now, will be solved later Basic_spec (G_basic_spec lid bspec) r -> do iSyms <- case impNode of diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 778721ae1a..8cedc60993 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -528,7 +528,7 @@ anaSpecAux conser addSyms optNodes lg anaSpecTop conser addSyms lg libEnv ln dg nsig name opts eo (item asp) rg return (Group (replaceAnnoted sp' asp) pos, nsig', dg') - Spec_inst spname' afitargs mImp pos0 -> do + Spec_inst spname' afitargs mImp pos0 -> trace ("**** ana spec inst *** afitargs:" ++ show afitargs) $ do spname <- expCurieR (globalAnnos dg) eo spname' let pos = if null afitargs then iriPos spname else pos0 adjustPos pos $ case lookupGlobalEnvDG spname dg of @@ -643,7 +643,7 @@ anaSpecAux conser addSyms optNodes lg let (cNodes', cEdges') = networkDiagram dg cItems eItems (ns, dg') <- insertColimitInGraph libEnv ln dg cNodes' cEdges' name return (sp, ns, dg') - _ -> fail $ "AnalysisStructured: " ++ show (prettyLG lg sp) + _ -> fail $ "in AnalysisStructured: " ++ show (prettyLG lg sp) -- | build the diagram of a network specified as a list of network elements to be added @@ -779,6 +779,21 @@ anaExtraction lg libEnv ln dg nsig name rg (ExtractOrRemove b iris _) = if not b let dg'' = insLink dg' incl globalThm SeeSource (getNode nsig') n return (nsig', dg'') +unionNodes :: LogicGraph -> DGraph -> NodeName -> [NodeSig] -> Result (DGraph, NodeSig) +unionNodes lg dg name nsigs = + case nsigs of + [ns] -> return (dg, ns) + _ -> do + let nsigs' = reverse nsigs + gbigSigma <- gsigManyUnion lg (map getSig nsigs') + let (ns@(NodeSig node _), dg2) = insGSig dg name + DGUnion gbigSigma + insE dgl (NodeSig n gsigma) = do + incl <- ginclusion lg gsigma gbigSigma + return $ insLink dgl incl globalDef SeeTarget n node + dg3 <- foldM insE dg2 nsigs' + return (dg3, ns) + anaUnion :: Bool -> LogicGraph -> LibEnv -> LibName -> DGraph -> MaybeNode -> NodeName -> HetcatsOpts -> ExpOverrides -> [Annoted SPEC] -> Range -> Result ([Annoted SPEC], [NodeSig], NodeSig, DGraph) @@ -1306,8 +1321,6 @@ anaAllFitArgs lg libEnv opts eo ln dg nsig name spname return ( zipWith replaceAnnoted (reverse fitargs') afitargs, dg3 , (morDelta, gsigma', ns)) -type GSubst = Map.Map (IRI, String) IRI - anaPatternInstArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> NodeName -> IRI -> PatternSig -> [Annoted FIT_ARG] @@ -1348,7 +1361,10 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0:" ++ show subst0) $ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname subst0 gm0 par0 arg0 - return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) + let nsig2 = case nsig1 of -- this is a trick to make lists work! + EmptyNode _ -> nsig0 + _ -> nsig1 + return (args0 ++ [arg1], dg1, nsig2, subst1, gm1)) ([], dgP, EmptyNode l, Map.empty, idImps) $ zipped' let lastParamSig = case nsig' of EmptyNode _ -> error "should not happen" @@ -1414,13 +1430,13 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = -- trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of StringParamInfo i -> do l <- lookupCurrentLogic "fit string" lg - return (arg0, dg0, EmptyNode l, Map.insert (i, "String") s subst0, mgm0) + return (arg0, dg0, EmptyNode l, Map.insert (i, "String") (PlainVal s) subst0, mgm0) _ -> error $ "parameter mismatch, got a string when expecting a " ++ show par0 Fit_spec asp gm r -> case item asp of @@ -1440,7 +1456,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 subst1 = foldl (\f (ssym, tsym) -> let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) tn = idToIRI $ sym_name lid tsym - in Map.insert (sn, sk) tn f) subst0 $ Map.toList smap + in Map.insert (sn, sk) (PlainVal tn) f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> error $ "argument mismatch in instantiation. parameter: " ++ show par0 ++ "\n argument: " ++ show arg0 @@ -1461,7 +1477,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 subst1 = foldl (\f (ssym, tsym) -> let (sn, sk) = (idToIRI $ sym_name glid ssym, symKind glid ssym) tn = idToIRI $ sym_name glid tsym - in Map.insert (sn, sk) tn f) + in Map.insert (sn, sk) (PlainVal tn) f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) @@ -1484,20 +1500,74 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 subst1 = foldl (\f (ssym, tsym) -> let (sn, sk) = (idToIRI $ sym_name glid ssym, symKind glid ssym) tn = idToIRI $ sym_name glid tsym - in Map.insert (sn, sk) tn f) + in Map.insert (sn, sk) (PlainVal tn) f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> fail $ "multiple occurences of abbreviated name in the context:" ++ show ctxSyms _ -> fail "ambiguity in use of abbreviation notation, parameter has more than one symbol" - _ -> fail "abbreviation notation can be used only for single ontology arguments, not for lists" + _ -> fail $ "abbreviation notation can be used only for single ontology arguments, not for lists: " ++ show i -- 2. if i is a symbol from the context (nsig) -- solve to context fit x |-> i -- and the substitution maps x to i -- where x is the unique symbol declared in the param -- 3. otherwise, i is a new symbol of same kind as x -- and the substitution maps x to i - OntoList aspecs -> error $ "lists as args not yet supported: " ++ show aspecs ++ " par0:" ++ show par0 + OntoList aspecs -> do + -- 1. generate a temporary param for the template node of the param list + -- SingleParamInfo False n where the signature of the head of the param list is JustNode n + let (par1, tailName) = case par0 of + SingleParamInfo _ _ -> error $ "expecting single argument: " ++ show par0 ++ " but got a list: " ++ show aspecs + ListParamInfo _ _ (JustNode n) tn -> let x = case tn of + Nothing -> nullIRI + Just y -> y + in (SingleParamInfo False n, x) -- TODO: not safe for empty, do it right!!!! + _ -> error $ "instantiating empty list pattern:" ++ show par0 ++ " with non-empty argument:" ++ show aspecs + -- 2. analyze each elem of aspecs as a single argument for this param + -- use anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 + -- it returns the analysed arg, the dgraph, justnode node of argument, substitution, generated morphism + -- but update prevSig name spname subst0 mgm0 par0 arg0; fold the dgs; store the nodes of args + (aspecs', aNodes, subst1, dg1) <- + foldM (\(anaSpecs, specNodes, substI, aDg) crtSp -> do + (crtSp', aDg', argNode, aSubst, aMor) <- + anaPatternInstArg lg libEnv opts eo ln + aDg isig csig prevSig -- TODO: check that prevSig is fine here + name spname -- TODO: give proper names + subst0 mgm0 -- TODO: check that this is ok + par1 $ + emptyAnno $ Fit_spec crtSp [] nullRange + return (anaSpecs ++ [crtSp'], + specNodes ++ [argNode], + if substI == Map.empty then aSubst else substI, -- only interested in the first substitution + aDg') + ) ([], [], Map.empty, dg0) aspecs + -- 3. unite the resulting nodes + (dg2, argUnion) <- unionNodes lg dg1 (makeName $ mkIRI "UnionNode") $ concatMap (\aN -> case aN of + JustNode x -> [x] + _ -> []) aNodes + -- 4. generate a substitution: subst of the first argument plus list variable to tail of analyzed args + let subst2 = case aspecs' of + _:asps -> + let (k, iriList) = foldl (\(_k0, iris0) as0 -> case item as0 of + Fit_new gsym1 gsym2 _ -> + case gsym1 of + G_symbol lid1 sym1 -> + case gsym2 of + G_symbol lid2 sym2 -> + let -- name1 = idToIRI $ sym_name lid1 sym1 + kind1 = symKind lid1 sym1 + name2 = idToIRI $ sym_name lid2 sym2 + in (kind1, iris0 ++ [name2]) + _ -> error "only fit new for now!" + ) + + ("", []) asps + in Map.insert (tailName, "list") (ListVal k iriList) subst1 + + _ -> subst1 + return (arg0, dg2, JustNode argUnion, subst2, mgm0) + -- 5. instantiate the body with the substitution and add a link from the united arguments to the body + -- this should happen elsewhere! _ -> -- trace ("itm:" ++ (show $ item arg0) ) $ error "only unsolved names for now" _ -> -- trace ("itm:" ++ (show $ item arg0)) $ @@ -1506,7 +1576,13 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> LocalOrSpecSig -> Result (DGraph, Annoted SPEC) -instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = --trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ " \n mgmPrev:" ++ show mgmPrev) $ +instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ " \n mgmPrev:" ++ show mgmPrev) $ + {- (Logic lid) <- lookupCurrentLogic "macro" lg + let th = (empty_signature lid, []) + bsp = case convertTheory lid of + Just f -> f th + _ -> error "cannot convert theory" + return (dg, emptyAnno $ Basic_spec (G_basic_spec lid bsp) nullRange) -} case macro of LocalSig localVarMaps (Local_pattern _ localBody) -> do let gEnv' = foldl (\g (n, s) -> Map.insert n (PatternEntry s) g) (globalEnv dg) $ Map.toList localVarMaps @@ -1533,7 +1609,7 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev (dg',a') <- instMacroAux a return (dg', as ++ [a']) ) (dg, []) asps return $ (dg', asp{item = Union asps' rg}) - Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0)) $ + Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv dg case snEntry of @@ -1551,28 +1627,59 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev case item aFitArg of Fit_spec asp1 gm rg -> case item asp1 of - NormalVariable i -> + UnsolvedName i _ -> + if i == mkIRI "empty" then error "empty list as argument in instantiation of pattern nyi" + else ([], aFitArg) + NormalVariable i -> trace ("normal variable: " ++ show i ++ " subst:" ++ show subst ++ " vars:" ++ show vars) $ if i `elem` Map.keys vars then let (b, k) = Map.findWithDefault (error "notPossible") i vars val = Map.findWithDefault (error "variable not mapped") (i,k) subst - in ([((i,k), (val,k))], aFitArg{item = Fit_spec asp1{item= UnsolvedName val nullRange} gm rg}) + valIRI = case val of + PlainVal valiri -> valiri + _ -> error "normal variable mapped to list" + in ([((i,k), (valIRI,k))], aFitArg{item = Fit_spec asp1{item= UnsolvedName valIRI nullRange} gm rg}) + else error $ "unknown variable:" ++ show i + ListVariable i -> + if i `elem` Map.keys vars then + let (b, k) = Map.findWithDefault (error "not possible") i vars + in if k == "list" then + let val = Map.findWithDefault (error "variable not mapped") (i, k) subst + in case val of + ListVal k' vals -> + let genItem = Fit_spec asp1{item= OntoList $ map (\v -> emptyAnno $ UnsolvedName v nullRange) vals} gm rg + in if not $ null vals then ([], aFitArg{item = genItem}) -- error $ "genItem:" ++ show genItem + else trace "******** got to empty **************" $ ([], aFitArg{item = Fit_spec asp1{item = UnsolvedName (mkIRI "empty") nullRange} gm rg}) + -- TODO: this does not suffice, we need to generate empty ontology here already! + _ -> error $ "expected list argument but got single element" + else error $ "expected list but got " ++ k else error $ "unknown variable:" ++ show i _ -> ([], aFitArg) _ -> ([], aFitArg) solved = map solveVars afitargs afitargs0 = map snd solved + aitems = filter (\x -> case item x of + Fit_spec y _ _ -> case item y of + UnsolvedName anIRI _ -> anIRI == mkIRI "empty" + _ -> False) afitargs0 newVars = concatMap fst solved zipped = -- trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! - -- TODO: if isLocal start with subst1 else start with empty subst? - gmor' <- -- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ + -- TODO: if isLocal start with subst1 else start with empty subst? + (Logic lid) <- lookupCurrentLogic "macro" lg + let th = (empty_signature lid, []) + bsp = case convertTheory lid of + Just f -> f th + _ -> error "cannot convert theory" + if not $ null aitems then instMacroAux $ asp0{item = Basic_spec (G_basic_spec lid bsp) nullRange} -- error "empty list as argument, not yet done" + else do + gmor' <- -- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ if isLocal then case mgmPrev of Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm else extendWithSubst l idImps newVars - (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ - foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do + (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ + foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 imp (EmptyNode l) nsig0 -- TODO: context is always empty now @@ -1582,7 +1689,7 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev ([], dg, nsig, subst, gmor') -- the last argument node should not be EmptyNode, but the target of gmor'. Try with nsig? zipped -- (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', subst') <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs - instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst subst') vars gm' pBody + trace ("\n\n\n recursive call:" ++ show subst') $ instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' subst) vars gm' pBody -- error $ "spec_inst:" ++ show sn ++ " args:" ++ show afitargs ++ " vars:" ++ show vars ++ " subst:" ++ show subst -- 1. afitargs should give raise to signature morphisms from the nodes of the params to the nodes of the args -- 2. and to a subst' diff --git a/Static/DevGraph.hs b/Static/DevGraph.hs index af961f3ff9..a21e6a9bb1 100644 --- a/Static/DevGraph.hs +++ b/Static/DevGraph.hs @@ -721,7 +721,9 @@ getBody (SpecSig x) = x getBody (LocalSig _ x) = x data PatternParamInfo = SingleParamInfo Bool NodeSig -- optional or not, node in graph - | ListParamInfo Int Bool MaybeNode -- length, exact or minimal, node of template + | ListParamInfo Int Bool MaybeNode (Maybe IRI) + -- length (currrently saves the number of elements before last), + -- exact or minimal, node of list template, name of tail if not empty list | StringParamInfo IRI deriving (Show, Eq, Typeable) -- TODO: extend for data parameters From 277e6cccc45d960f69e9124e30de1b968b8b78dd Mon Sep 17 00:00:00 2001 From: mcodescu Date: Tue, 14 Apr 2020 07:47:15 +0200 Subject: [PATCH 17/33] made context work --- Static/AnalysisStructured.hs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 8cedc60993..ff064501fe 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -1558,7 +1558,16 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 kind1 = symKind lid1 sym1 name2 = idToIRI $ sym_name lid2 sym2 in (kind1, iris0 ++ [name2]) - _ -> error "only fit new for now!" + Fit_ctx gsym1 gsym2 _ -> + case gsym1 of + G_symbol lid1 sym1 -> + case gsym2 of + G_symbol lid2 sym2 -> + let -- name1 = idToIRI $ sym_name lid1 sym1 + kind1 = symKind lid1 sym1 + name2 = idToIRI $ sym_name lid2 sym2 + in (kind1, iris0 ++ [name2]) + _ -> error "only fit new/ctx for now!" ) ("", []) asps From c78081bafa7854da673f01e8aa86be6c45a8a869 Mon Sep 17 00:00:00 2001 From: mcodescu Date: Thu, 16 Apr 2020 22:32:06 +0200 Subject: [PATCH 18/33] now goes through, should revert the change to instantiateMacro after more tests --- Logic/ExtSign.hs | 6 ++- OWL2/StaticAnalysis.hs | 2 +- Static/AnalysisStructured.hs | 97 ++++++++++++++++++++---------------- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/Logic/ExtSign.hs b/Logic/ExtSign.hs index da50967e16..025926382f 100644 --- a/Logic/ExtSign.hs +++ b/Logic/ExtSign.hs @@ -21,6 +21,8 @@ import Common.DocUtils import Common.ExtSign import Logic.Logic +import Debug.Trace + ext_sym_of :: Logic lid sublogics basic_spec sentence symb_items symb_map_items @@ -138,7 +140,7 @@ checkRawSyms :: Logic lid sublogics basic_spec sentence symb_items symb_map_items sign morphism symbol raw_symbol proof_tree => lid -> [raw_symbol] -> Set.Set symbol -> Result () -checkRawSyms l rsyms syms = do +checkRawSyms l rsyms syms = trace ("rsyms:" ++ show rsyms ++ " syms:" ++ show syms) $ do let unknownSyms = filter ( \ rsy -> Set.null $ Set.filter (flip (matches l) rsy) syms) rsyms @@ -154,7 +156,7 @@ ext_induced_from_to_morphism l r s@(ExtSign p sy) t = do checkExtSign l "from" s checkExtSign l "to" t checkRawMap l r p - checkRawSyms l (Map.elems r) $ nonImportedSymbols t + -- checkRawSyms l (Map.elems r) $ nonImportedSymbols t mor <- induced_from_to_morphism l r s t let sysI = Set.toList $ Set.difference (symset_of l p) sy morM = symmap_of l mor diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index c5122c2fcb..e448d71c9b 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -52,7 +52,7 @@ failMsg (Entity _ ty e) desc = -- | checks if an entity is in the signature checkEntity :: Sign -> Entity -> Result () checkEntity s t@(Entity _ ty e) = - let errMsg = mkError ("unknown " ++ showEntityType ty) e + let errMsg = mkError ("unknown " ++ showEntityType ty ++ " in " ++ show s) e in case ty of Datatype -> unless (Set.member e (datatypes s) || isDatatypeKey e) errMsg Class -> unless (Set.member e (concepts s) || isThing e) errMsg diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index ff064501fe..0bf7f17d81 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -325,7 +325,7 @@ anaSpecAux conser addSyms optNodes lg if isStructured opts then return (bspec, mkExtSign $ empty_signature lid, []) else - let res@(Result ds mb) = extBasicAnalysis lid (getName name) + let res@(Result ds mb) = trace ("sig in extBasicAnalysis:" ++ show sig ++ " bspec:" ++ show bspec) $ extBasicAnalysis lid (getName name) ln bspec sig $ globalAnnos dg0 in case mb of Nothing | null ds -> @@ -344,8 +344,8 @@ anaSpecAux conser addSyms optNodes lg (if addSyms then Set.union sys sysd else sysd) $ symset_of lid sigma_complete) startSigId (toThSens ax) startThId - dg'' <- createConsLink DefLink conser lg dg' nsig' ns DGLinkExtension - return (Basic_spec (G_basic_spec lid bspec') pos, ns, dg'') + dg'' <- trace ("gsysd:" ++ show gsysd) $ createConsLink DefLink conser lg dg' nsig' ns DGLinkExtension + trace ("dg'':" ++ show (edges $ dgBody dg'')) $ return (Basic_spec (G_basic_spec lid bspec') pos, ns, dg'') EmptySpec pos -> case nsig of EmptyNode _ -> do warning () "empty spec" pos @@ -528,7 +528,7 @@ anaSpecAux conser addSyms optNodes lg anaSpecTop conser addSyms lg libEnv ln dg nsig name opts eo (item asp) rg return (Group (replaceAnnoted sp' asp) pos, nsig', dg') - Spec_inst spname' afitargs mImp pos0 -> trace ("**** ana spec inst *** afitargs:" ++ show afitargs) $ do + Spec_inst spname' afitargs mImp pos0 -> trace ("\n\n**** ana spec inst *** " ++ show spname' ++ " afitargs:" ++ show afitargs ++ "nsig:" ++ show nsig) $ do spname <- expCurieR (globalAnnos dg) eo spname' let pos = if null afitargs then iriPos spname else pos0 adjustPos pos $ case lookupGlobalEnvDG spname dg of @@ -540,14 +540,16 @@ anaSpecAux conser addSyms optNodes lg (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs -- let body' = getBody bodySig (Logic cl) <- lookupCurrentLogic "anaGmaps" lg - (dg2, spB) <- -- trace ("calling instMacro:" ++ show nsig') $ + (dg2, lastParamAndNewNodes, spB) <- trace ("calling instMacro:" ++ show nsig') $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig + (dgI, allPrevDefs) <- unionNodes lg dg2 (makeName $ mkIRI "TESTNAME") $ nub lastParamAndNewNodes --the body should extend the last argument - (sp', nsig'', dg3) <- -- trace ("spB:" ++ show spB) $ - anaSpecTop conser addSyms lg libEnv ln dg2 (JustNode nsig') (extName "Body" name) opts eo (item spB) nullRange + (sp', nsig'', dg3) <- trace ("spB:" ++ show spB ++ "\n allPrevDefs:" ++ show allPrevDefs) $ + anaSpecTop conser addSyms lg libEnv ln dgI (JustNode allPrevDefs) (makeName $ addSuffixToIRI "_source" $ getName name) opts eo (item spB) nullRange + -- TODO: nsig' should be the node of instantiateMacro!!! --incl <- ginclusion lg (getSig nsig') (getSig nsig'') --let dg3 = insLink dg'' incl globalDef SeeTarget (getNode nsig') (getNode nsig'') - return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' + trace ("sp':" ++ show sp' ++ " nsig'':" ++ show nsig'' ++ "dg3:"++ show (labEdges $ dgBody dg3)) $ return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' else if la == 0 then error "arguments missing in instantiation" else if lp == 0 then error "pattern without arguments" else error "mismatch in length of arguments" @@ -566,15 +568,15 @@ anaSpecAux conser addSyms optNodes lg -- then just return the body return (sp, body, dg2) -- otherwise, we need to create a new one - _ -> do + _ -> trace "on this branch" $ do gsigma <- gsigUnionMaybe lg addSyms nsig gsigmaB let (fsig@(NodeSig node _), dg2) = insGSig dg name (DGInst spname) gsigma - incl <- ginclusion lg gsigmaB gsigma + incl <- trace ("gsigmaB:" ++ show gsigmaB ++ " gsigma:" ++ show gsigma) $ ginclusion lg gsigmaB gsigma let dg3 = case nsig of JustNode (NodeSig nI _) | nI == nB -> dg2 _ -> insLink dg2 incl globalDef (DGLinkMorph spname) nB node - dg4 <- createConsLink DefLink conser lg dg3 nsig fsig SeeTarget + dg4 <- trace ("nsig:" ++ show nsig ++ " fsig:" ++ show fsig) $ createConsLink DefLink conser lg dg3 nsig fsig SeeTarget return (sp, fsig, dg4) -- now the case with parameters (la, lp) | la == lp -> do @@ -643,6 +645,9 @@ anaSpecAux conser addSyms optNodes lg let (cNodes', cEdges') = networkDiagram dg cItems eItems (ns, dg') <- insertColimitInGraph libEnv ln dg cNodes' cEdges' name return (sp, ns, dg') + UnsolvedName x pos -> -- this should not happen, but when it does, solve as spec_inst + anaSpecAux conser addSyms optNodes lg + libEnv ln dg nsig name opts eo (Spec_inst x [] Nothing pos) rg _ -> fail $ "in AnalysisStructured: " ++ show (prettyLG lg sp) @@ -1040,7 +1045,7 @@ anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) mgm gsis = adjustPos pos $ if isStructured opts then return $ mkG_morphism lidP $ ext_ide sigmaP else do - if null gsis then do + if null gsis then trace ("gsis is null psig:" ++ show psig ++ " asig:" ++ show asig) $ do (G_sign lidP' sigmaP' _, _) <- gSigCoerce lg psig (Logic lidA) sigmaA' <- coerceSign lidA lidP' "anaGmaps" sigmaA prevMap <- case mgm of @@ -1051,7 +1056,7 @@ anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) mgm prevMor' <- coerceMorphism prevLid lidP' "anaGmaps:coerceMorphism" prevMor let symMap = symmap_of lidP' prevMor' return $ Map.mapKeys (symbol_to_raw lidP') $ Map.map (symbol_to_raw lidP') symMap - fmap (mkG_morphism lidP') $ + trace ("prevMap:" ++ show prevMap) $ fmap (mkG_morphism lidP') $ ext_induced_from_to_morphism lidP' prevMap sigmaP' sigmaA' else do cl <- lookupCurrentLogic "anaGmaps" lg @@ -1100,7 +1105,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c case fv of Fit_string s _ -> error $ "nyi for " ++ (show s) Fit_spec asp gsis pos -> do - (sp', nsigA', dg0) <- -- trace ("calling ana spec:" ++ show asp) $ + (sp', nsigA', dg0) <- trace ("calling ana spec:" ++ show asp ++ " nsigI:"++ show nsigI ++ "nsigP:" ++ show nsigP)$ anaSpec False True lg libEnv ln dg nsigI name opts eo (item asp) pos -- if the context and the previous argument are both not EmptyNodes @@ -1125,15 +1130,15 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ pN ++ cN ++ [nsigA'] return (uSig, dg2) - (_, Comorphism aid) <- -- trace ("actSig:" ++ show actSig) $ + (_, Comorphism aid) <- trace ("nsigA:" ++ show nsigA) $ logicUnion lg (getNodeLogic nsigP) (getNodeLogic nsigA) let tl = Logic $ targetLogic aid (nsigA''@(NodeSig nA' gsigA'), dg'') <- coerceNode lg dg' nsigA name tl (gsigmaP', pmor) <- gSigCoerce lg gsigmaP tl tmor <- gEmbedComorphism pmor gsigmaP - gmor <- anaGmaps lg opts pos gsigmaP' gsigA' mgm gsis + gmor <- trace ("gsis:" ++ show gsis ++ " gsigmaP':" ++ show gsigmaP' ++ " gsigA':" ++ show gsigA' ++ " mgm:" ++ show mgm) $ anaGmaps lg opts pos gsigmaP' gsigA' mgm gsis eGmor <- comp tmor $ gEmbed gmor - return ( Fit_spec (replaceAnnoted sp' asp) gsis pos + trace ("eGmor:"++ show eGmor) $ return ( Fit_spec (replaceAnnoted sp' asp) gsis pos , if nP == nA' && isInclusion eGmor then dg'' else insLink dg'' eGmor globalThm (DGLinkInst spname $ Fitted gsis) nP nA' @@ -1187,7 +1192,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na return (fv, dg'', (gmor, nsigA)) Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do - -- trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do + trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym sRSym = symbol_to_raw slid ssym sigA <- add_symb_to_sign tlid (empty_signature tlid) tsym @@ -1440,7 +1445,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 _ -> error $ "parameter mismatch, got a string when expecting a " ++ show par0 Fit_spec asp gm r -> case item asp of - UnsolvedName i rg -> + UnsolvedName i rg -> trace ("solving an unsolved name in inst arg:" ++ show i) $ -- TODO: here we must also pass the parameter, so we can check its symbols -- 1. if i is the name of a spec entry in globalEnv -- solve to Spec_inst i [] Nothing nullRange @@ -1449,30 +1454,33 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 case (par0, Map.findWithDefault (error "anaPatternInstArg: already checked") i (globalEnv dg0)) of (SingleParamInfo b pSig, SpecEntry eGSig) -> do let arg1 = Fit_spec (emptyAnno $ Spec_inst i [] Nothing nullRange) [] nullRange + l <- lookupCurrentLogic "fit string" lg + -- empty node was isig in next line (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of - G_morphism lid mor _ -> do + G_morphism lid mor _ -> trace ("arg2:"++ show arg2 ++ " gmor:" ++ show gmor ++ " nsigA:"++ show nsigA) $ do let smap = symmap_of lid mor subst1 = foldl (\f (ssym, tsym) -> let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) tn = idToIRI $ sym_name lid tsym in Map.insert (sn, sk) (PlainVal tn) f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here - return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) + trace ("subst1:"++ show subst1 ) $ return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> error $ "argument mismatch in instantiation. parameter: " ++ show par0 ++ "\n argument: " ++ show arg0 - else do + else trace ("unsolved is not in globalEnv") $ do case par0 of SingleParamInfo b pNSig -> case getSig pNSig of G_sign lid (ExtSign _ newDecls) _ -> case Set.toList newDecls of - [sym] -> do + [sym] -> trace ("one new sym in param:" ++ show sym) $ do let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ + (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 + -- try: only extend previous morphism if the pattern is local! case gmor of - G_morphism glid mor _ -> do + G_morphism glid mor _ -> trace ("gmor:" ++ show gmor) $ do let smap = symmap_of glid mor subst1 = foldl (\f (ssym, tsym) -> let (sn, sk) = (idToIRI $ sym_name glid ssym, symKind glid ssym) @@ -1482,18 +1490,20 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- TODO: any compatibility checks must be done here return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) case csig of - EmptyNode _ -> -- trace "err1" $ + EmptyNode _ -> trace "err1" $ noCtxOrNoMatch JustNode c -> case getSig c of G_sign lid1 (ExtSign ctx _) _ -> do let ctxSyms = filter (\csym -> ((idToIRI $ sym_name lid1 csym) == i) && (symKind lid1 csym == symKind lid sym)) $ Set.toList $ symset_of lid1 ctx - case ctxSyms of + trace ("ctxSyms:"++ show ctxSyms) $ case ctxSyms of [] -> -- trace "err2" $ noCtxOrNoMatch [ctxSym] -> do let arg1 = Fit_ctx (G_symbol lid sym) (G_symbol lid1 ctxSym) nullRange - (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 + (arg2, dg1, (gmor, nsigA)) <- trace ("arg1:" ++ show arg1) $ anaFitArg lg libEnv ln dg0 spname isig pNSig + opts (extName "Arg" name) eo csig + prevSig mgm0 arg1 case gmor of G_morphism glid mor _ -> do let smap = symmap_of glid mor @@ -1584,7 +1594,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode - -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> LocalOrSpecSig -> Result (DGraph, Annoted SPEC) + -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> LocalOrSpecSig -> Result (DGraph, [NodeSig], Annoted SPEC) instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ " \n mgmPrev:" ++ show mgmPrev) $ {- (Logic lid) <- lookupCurrentLogic "macro" lg let th = (empty_signature lid, []) @@ -1606,18 +1616,21 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev case gbsp of G_basic_spec lid bsp -> do bsp'<- instantiate_macro lid vars subst bsp - return (dg, asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) + let lastNode = case nsig of + JustNode x -> x + _ -> error "no last param of a pattern, should not happend" + return (dg, [lastNode], asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) Group asp1 _rg -> instMacroAux asp1 Extension asps rg -> do - (dg', asps')<- foldM (\(aDg, as) a -> do - (dg',a') <- instMacroAux a - return (dg', as ++ [a']) ) (dg, []) asps - return $ (dg', asp{item = Extension asps' rg}) + (dg1, ns1, asps1) <- foldM (\(aDg, ns, as) a -> do + (dg', ns', a') <- instMacroAux a + return (dg', ns ++ ns', as ++ [a']) ) (dg, [], []) asps + return (dg1, ns1, asp{item = Extension asps1 rg}) Union asps rg -> do - (dg', asps')<- foldM (\(aDg, as) a -> do - (dg',a') <- instMacroAux a - return (dg', as ++ [a']) ) (dg, []) asps - return $ (dg', asp{item = Union asps' rg}) + (dg1, ns1, asps1)<- foldM (\(aDg, ns, as) a -> do + (dg',ns',a') <- instMacroAux a + return (dg', ns ++ ns', as ++ [a']) ) (dg, [], []) asps + return (dg1, ns1, asp{item = Union asps1 rg}) Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv dg @@ -1671,7 +1684,7 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev UnsolvedName anIRI _ -> anIRI == mkIRI "empty" _ -> False) afitargs0 newVars = concatMap fst solved - zipped = -- trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ + zipped = trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! -- TODO: if isLocal start with subst1 else start with empty subst? (Logic lid) <- lookupCurrentLogic "macro" lg @@ -1686,8 +1699,8 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev case mgmPrev of Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm - else extendWithSubst l idImps newVars - (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ + else return idImps -- TODO: old variant: extendWithSubst l idImps newVars + (afitargs', dg', nsig', subst', gm') <- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 @@ -1698,7 +1711,7 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev ([], dg, nsig, subst, gmor') -- the last argument node should not be EmptyNode, but the target of gmor'. Try with nsig? zipped -- (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', subst') <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs - trace ("\n\n\n recursive call:" ++ show subst') $ instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' subst) vars gm' pBody + trace ("\n\n\n recursive call:" ++ show subst' ++ " nsig':" ++ show nsig') $ instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' subst) vars gm' pBody -- error $ "spec_inst:" ++ show sn ++ " args:" ++ show afitargs ++ " vars:" ++ show vars ++ " subst:" ++ show subst -- 1. afitargs should give raise to signature morphisms from the nodes of the params to the nodes of the args -- 2. and to a subst' From fbb17daa7ffb12f16b2925283efd5a13db6114fd Mon Sep 17 00:00:00 2001 From: mcodescu Date: Fri, 17 Apr 2020 22:44:31 +0200 Subject: [PATCH 19/33] corrected macro instantiation --- Logic/ExtSign.hs | 5 +- OWL2/StaticAnalysis.hs | 3 +- Static/AnalysisLibrary.hs | 2 +- Static/AnalysisStructured.hs | 135 ++++++++++++++++++++--------------- 4 files changed, 81 insertions(+), 64 deletions(-) diff --git a/Logic/ExtSign.hs b/Logic/ExtSign.hs index 025926382f..1a7becca6f 100644 --- a/Logic/ExtSign.hs +++ b/Logic/ExtSign.hs @@ -21,9 +21,6 @@ import Common.DocUtils import Common.ExtSign import Logic.Logic -import Debug.Trace - - ext_sym_of :: Logic lid sublogics basic_spec sentence symb_items symb_map_items sign morphism symbol raw_symbol proof_tree @@ -140,7 +137,7 @@ checkRawSyms :: Logic lid sublogics basic_spec sentence symb_items symb_map_items sign morphism symbol raw_symbol proof_tree => lid -> [raw_symbol] -> Set.Set symbol -> Result () -checkRawSyms l rsyms syms = trace ("rsyms:" ++ show rsyms ++ " syms:" ++ show syms) $ do +checkRawSyms l rsyms syms = do let unknownSyms = filter ( \ rsy -> Set.null $ Set.filter (flip (matches l) rsy) syms) rsyms diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index e448d71c9b..82cb65003c 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -808,7 +808,8 @@ instParamName subst p = (a,b):_ -> b in Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst -- this will most likely need to change for complex nesting! comps' = map (\t -> iriPath $ getIRIVal $ solveId t) comps - newPath = trace ("comps:" ++ show comps ++ " comps':" ++ show comps') $ pPath{getComps = comps'} + newPath = -- trace ("comps:" ++ show comps ++ " comps':" ++ show comps') $ + pPath{getComps = comps'} in p{iriPath = newPath} instantiateFrameBit :: GSubst -> PatternVarMap -> FrameBit -> Result FrameBit diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index 2535a2af04..bdf61f13e1 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -721,7 +721,7 @@ addSigSymsToVarMap vMap tailName exclSig aSig = if sIRI `elem` Map.keys f then error $ "variable named " ++ show s ++ "already used in " ++ show f else (Map.insert sIRI (if sIRI == tailName then (True, "list") else (False, symKind lid s)) f, Set.insert (symKind lid s) k) - (vMap', newKinds) = foldl (\(f, k) s -> trace ("f:" ++ show f) $ insertOrFail f s k) (vMap, Set.empty) $ Set.toList $ Set.difference syms symsExcl + (vMap', newKinds) = foldl (\(f, k) s -> insertOrFail f s k) (vMap, Set.empty) $ Set.toList $ Set.difference syms symsExcl return (vMap', newKinds) anaPatternBody :: LogicGraph -> LibEnv -> LibName -> DGraph -> HetcatsOpts diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 0bf7f17d81..68b0612ddb 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -325,7 +325,8 @@ anaSpecAux conser addSyms optNodes lg if isStructured opts then return (bspec, mkExtSign $ empty_signature lid, []) else - let res@(Result ds mb) = trace ("sig in extBasicAnalysis:" ++ show sig ++ " bspec:" ++ show bspec) $ extBasicAnalysis lid (getName name) + let res@(Result ds mb) = -- trace ("sig in extBasicAnalysis:" ++ show sig ++ " bspec:" ++ show bspec) $ + extBasicAnalysis lid (getName name) ln bspec sig $ globalAnnos dg0 in case mb of Nothing | null ds -> @@ -344,8 +345,9 @@ anaSpecAux conser addSyms optNodes lg (if addSyms then Set.union sys sysd else sysd) $ symset_of lid sigma_complete) startSigId (toThSens ax) startThId - dg'' <- trace ("gsysd:" ++ show gsysd) $ createConsLink DefLink conser lg dg' nsig' ns DGLinkExtension - trace ("dg'':" ++ show (edges $ dgBody dg'')) $ return (Basic_spec (G_basic_spec lid bspec') pos, ns, dg'') + dg'' <- -- trace ("gsysd:" ++ show gsysd) $ + createConsLink DefLink conser lg dg' nsig' ns DGLinkExtension + return (Basic_spec (G_basic_spec lid bspec') pos, ns, dg'') EmptySpec pos -> case nsig of EmptyNode _ -> do warning () "empty spec" pos @@ -528,11 +530,11 @@ anaSpecAux conser addSyms optNodes lg anaSpecTop conser addSyms lg libEnv ln dg nsig name opts eo (item asp) rg return (Group (replaceAnnoted sp' asp) pos, nsig', dg') - Spec_inst spname' afitargs mImp pos0 -> trace ("\n\n**** ana spec inst *** " ++ show spname' ++ " afitargs:" ++ show afitargs ++ "nsig:" ++ show nsig) $ do + Spec_inst spname' afitargs mImp pos0 -> do -- trace ("\n\n**** ana spec inst *** " ++ show spname' ++ " afitargs:" ++ show afitargs ++ "nsig:" ++ show nsig) $ do spname <- expCurieR (globalAnnos dg) eo spname' let pos = if null afitargs then iriPos spname else pos0 adjustPos pos $ case lookupGlobalEnvDG spname dg of - Just (PatternEntry patSig@(PatternSig _local imp params vMap _body)) -> trace ("patSig:" ++ show patSig) $ + Just (PatternEntry patSig@(PatternSig _local imp params vMap _body)) -> -- trace ("patSig:" ++ show patSig) $ -- 1. solve afitargs using params and imp case (length afitargs, length params) of (la, lp) -> do @@ -540,7 +542,7 @@ anaSpecAux conser addSyms optNodes lg (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs -- let body' = getBody bodySig (Logic cl) <- lookupCurrentLogic "anaGmaps" lg - (dg2, lastParamAndNewNodes, spB) <- trace ("calling instMacro:" ++ show nsig') $ + (dg2, lastParamAndNewNodes, spB) <- trace ("calling instMacro on " ++ show spname' ++ "[" ++ show afitargs ++ "] subst:" ++ show subst) $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig (dgI, allPrevDefs) <- unionNodes lg dg2 (makeName $ mkIRI "TESTNAME") $ nub lastParamAndNewNodes --the body should extend the last argument @@ -549,7 +551,8 @@ anaSpecAux conser addSyms optNodes lg -- TODO: nsig' should be the node of instantiateMacro!!! --incl <- ginclusion lg (getSig nsig') (getSig nsig'') --let dg3 = insLink dg'' incl globalDef SeeTarget (getNode nsig') (getNode nsig'') - trace ("sp':" ++ show sp' ++ " nsig'':" ++ show nsig'' ++ "dg3:"++ show (labEdges $ dgBody dg3)) $ return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' + -- trace ("sp':" ++ show sp' ++ " nsig'':" ++ show nsig'' ++ "dg3:"++ show (labEdges $ dgBody dg3)) $ + return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' else if la == 0 then error "arguments missing in instantiation" else if lp == 0 then error "pattern without arguments" else error "mismatch in length of arguments" @@ -568,15 +571,17 @@ anaSpecAux conser addSyms optNodes lg -- then just return the body return (sp, body, dg2) -- otherwise, we need to create a new one - _ -> trace "on this branch" $ do + _ -> do gsigma <- gsigUnionMaybe lg addSyms nsig gsigmaB let (fsig@(NodeSig node _), dg2) = insGSig dg name (DGInst spname) gsigma - incl <- trace ("gsigmaB:" ++ show gsigmaB ++ " gsigma:" ++ show gsigma) $ ginclusion lg gsigmaB gsigma + incl <- -- trace ("gsigmaB:" ++ show gsigmaB ++ " gsigma:" ++ show gsigma) $ + ginclusion lg gsigmaB gsigma let dg3 = case nsig of JustNode (NodeSig nI _) | nI == nB -> dg2 _ -> insLink dg2 incl globalDef (DGLinkMorph spname) nB node - dg4 <- trace ("nsig:" ++ show nsig ++ " fsig:" ++ show fsig) $ createConsLink DefLink conser lg dg3 nsig fsig SeeTarget + dg4 <- -- trace ("nsig:" ++ show nsig ++ " fsig:" ++ show fsig) $ + createConsLink DefLink conser lg dg3 nsig fsig SeeTarget return (sp, fsig, dg4) -- now the case with parameters (la, lp) | la == lp -> do @@ -1045,7 +1050,7 @@ anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) mgm gsis = adjustPos pos $ if isStructured opts then return $ mkG_morphism lidP $ ext_ide sigmaP else do - if null gsis then trace ("gsis is null psig:" ++ show psig ++ " asig:" ++ show asig) $ do + if null gsis then do -- trace ("gsis is null psig:" ++ show psig ++ " asig:" ++ show asig) $ do (G_sign lidP' sigmaP' _, _) <- gSigCoerce lg psig (Logic lidA) sigmaA' <- coerceSign lidA lidP' "anaGmaps" sigmaA prevMap <- case mgm of @@ -1056,7 +1061,7 @@ anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) mgm prevMor' <- coerceMorphism prevLid lidP' "anaGmaps:coerceMorphism" prevMor let symMap = symmap_of lidP' prevMor' return $ Map.mapKeys (symbol_to_raw lidP') $ Map.map (symbol_to_raw lidP') symMap - trace ("prevMap:" ++ show prevMap) $ fmap (mkG_morphism lidP') $ + fmap (mkG_morphism lidP') $ ext_induced_from_to_morphism lidP' prevMap sigmaP' sigmaA' else do cl <- lookupCurrentLogic "anaGmaps" lg @@ -1105,7 +1110,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c case fv of Fit_string s _ -> error $ "nyi for " ++ (show s) Fit_spec asp gsis pos -> do - (sp', nsigA', dg0) <- trace ("calling ana spec:" ++ show asp ++ " nsigI:"++ show nsigI ++ "nsigP:" ++ show nsigP)$ + (sp', nsigA', dg0) <- -- trace ("calling ana spec:" ++ show asp ++ " nsigI:"++ show nsigI ++ "nsigP:" ++ show nsigP)$ anaSpec False True lg libEnv ln dg nsigI name opts eo (item asp) pos -- if the context and the previous argument are both not EmptyNodes @@ -1130,15 +1135,16 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ pN ++ cN ++ [nsigA'] return (uSig, dg2) - (_, Comorphism aid) <- trace ("nsigA:" ++ show nsigA) $ + (_, Comorphism aid) <- -- trace ("nsigA:" ++ show nsigA) $ logicUnion lg (getNodeLogic nsigP) (getNodeLogic nsigA) let tl = Logic $ targetLogic aid (nsigA''@(NodeSig nA' gsigA'), dg'') <- coerceNode lg dg' nsigA name tl (gsigmaP', pmor) <- gSigCoerce lg gsigmaP tl tmor <- gEmbedComorphism pmor gsigmaP - gmor <- trace ("gsis:" ++ show gsis ++ " gsigmaP':" ++ show gsigmaP' ++ " gsigA':" ++ show gsigA' ++ " mgm:" ++ show mgm) $ anaGmaps lg opts pos gsigmaP' gsigA' mgm gsis + gmor <- -- trace ("gsis:" ++ show gsis ++ " gsigmaP':" ++ show gsigmaP' ++ " gsigA':" ++ show gsigA' ++ " mgm:" ++ show mgm) $ + anaGmaps lg opts pos gsigmaP' gsigA' mgm gsis eGmor <- comp tmor $ gEmbed gmor - trace ("eGmor:"++ show eGmor) $ return ( Fit_spec (replaceAnnoted sp' asp) gsis pos + return ( Fit_spec (replaceAnnoted sp' asp) gsis pos , if nP == nA' && isInclusion eGmor then dg'' else insLink dg'' eGmor globalThm (DGLinkInst spname $ Fitted gsis) nP nA' @@ -1192,7 +1198,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na return (fv, dg'', (gmor, nsigA)) Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do - trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do + --trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym sRSym = symbol_to_raw slid ssym sigA <- add_symb_to_sign tlid (empty_signature tlid) tsym @@ -1435,7 +1441,7 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = -- trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1445,7 +1451,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 _ -> error $ "parameter mismatch, got a string when expecting a " ++ show par0 Fit_spec asp gm r -> case item asp of - UnsolvedName i rg -> trace ("solving an unsolved name in inst arg:" ++ show i) $ + UnsolvedName i rg -> -- trace ("solving an unsolved name in inst arg:" ++ show i) $ -- TODO: here we must also pass the parameter, so we can check its symbols -- 1. if i is the name of a spec entry in globalEnv -- solve to Spec_inst i [] Nothing nullRange @@ -1458,29 +1464,29 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- empty node was isig in next line (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of - G_morphism lid mor _ -> trace ("arg2:"++ show arg2 ++ " gmor:" ++ show gmor ++ " nsigA:"++ show nsigA) $ do + G_morphism lid mor _ -> do -- trace ("arg2:"++ show arg2 ++ " gmor:" ++ show gmor ++ " nsigA:"++ show nsigA) $ do let smap = symmap_of lid mor subst1 = foldl (\f (ssym, tsym) -> let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) tn = idToIRI $ sym_name lid tsym in Map.insert (sn, sk) (PlainVal tn) f) subst0 $ Map.toList smap -- TODO: any compatibility checks must be done here - trace ("subst1:"++ show subst1 ) $ return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) + return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> error $ "argument mismatch in instantiation. parameter: " ++ show par0 ++ "\n argument: " ++ show arg0 - else trace ("unsolved is not in globalEnv") $ do + else do case par0 of SingleParamInfo b pNSig -> case getSig pNSig of G_sign lid (ExtSign _ newDecls) _ -> case Set.toList newDecls of - [sym] -> trace ("one new sym in param:" ++ show sym) $ do + [sym] -> do let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ + (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 -- try: only extend previous morphism if the pattern is local! case gmor of - G_morphism glid mor _ -> trace ("gmor:" ++ show gmor) $ do + G_morphism glid mor _ -> do -- trace ("gmor:" ++ show gmor) $ do let smap = symmap_of glid mor subst1 = foldl (\f (ssym, tsym) -> let (sn, sk) = (idToIRI $ sym_name glid ssym, symKind glid ssym) @@ -1490,18 +1496,17 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- TODO: any compatibility checks must be done here return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) case csig of - EmptyNode _ -> trace "err1" $ - noCtxOrNoMatch + EmptyNode _ -> noCtxOrNoMatch JustNode c -> case getSig c of G_sign lid1 (ExtSign ctx _) _ -> do let ctxSyms = filter (\csym -> ((idToIRI $ sym_name lid1 csym) == i) && (symKind lid1 csym == symKind lid sym)) $ Set.toList $ symset_of lid1 ctx - trace ("ctxSyms:"++ show ctxSyms) $ case ctxSyms of + case ctxSyms of [] -> -- trace "err2" $ noCtxOrNoMatch [ctxSym] -> do let arg1 = Fit_ctx (G_symbol lid sym) (G_symbol lid1 ctxSym) nullRange - (arg2, dg1, (gmor, nsigA)) <- trace ("arg1:" ++ show arg1) $ anaFitArg lg libEnv ln dg0 spname isig pNSig + (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 case gmor of @@ -1595,7 +1600,9 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> LocalOrSpecSig -> Result (DGraph, [NodeSig], Annoted SPEC) -instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ " \n mgmPrev:" ++ show mgmPrev) $ +instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = + trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ "\nsubst:" ++ show subst ++ + "\nnsig:" ++ show nsig ++ "\nimp:"++ show imp ++ "\nmgmPrev:"++ show mgmPrev ++ "\nvars:" ++ show vars) $ {- (Logic lid) <- lookupCurrentLogic "macro" lg let th = (empty_signature lid, []) bsp = case convertTheory lid of @@ -1608,32 +1615,41 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev dg' = dg {globalEnv = gEnv'} instantiateMacro lg libEnv opts eo ln dg' imp nsig name spname subst vars mgmPrev (SpecSig $ Spec_pattern localBody) -- TODO: will this be enough? -- trace ("known:" ++ show (Map.keys gEnv')) $ error "local patterns nyi" - SpecSig (Spec_pattern asp) -> do - let - instMacroAux asp0 = + SpecSig (Spec_pattern asp) -> + instMacroAux lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev asp + _ -> fail $ "illegal pattern signature:" ++ show macro + + +instMacroAux :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> + DGraph -> MaybeNode -> MaybeNode + -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism + -> Annoted SPEC -> Result (DGraph, [NodeSig], Annoted SPEC) +instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM asp0 = trace ("+++++ instMacroAux for " ++ show asp0) $ case item asp0 of Basic_spec gbsp rg -> case gbsp of G_basic_spec lid bsp -> do - bsp'<- instantiate_macro lid vars subst bsp - let lastNode = case nsig of + bsp'<- instantiate_macro lid vars crtSubst bsp + let lastNode = case crtNSig of JustNode x -> x _ -> error "no last param of a pattern, should not happend" - return (dg, [lastNode], asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) - Group asp1 _rg -> instMacroAux asp1 + trace ("bsp':" ++ show bsp') $ return (crtDG, [lastNode], asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) + Group asp1 rg -> do + (dg2, ns2, asp2) <- instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM asp1 + return (dg2, ns2, asp0{item = Group asp2 rg}) Extension asps rg -> do (dg1, ns1, asps1) <- foldM (\(aDg, ns, as) a -> do - (dg', ns', a') <- instMacroAux a - return (dg', ns ++ ns', as ++ [a']) ) (dg, [], []) asps - return (dg1, ns1, asp{item = Extension asps1 rg}) + (dg', ns', a') <- instMacroAux lg libEnv opts eo ln aDg imp crtNSig name spname crtSubst vars crtGM a + return (dg', ns ++ ns', as ++ [a']) ) (crtDG, [], []) asps + return (dg1, ns1, asp0{item = Extension asps1 rg}) Union asps rg -> do (dg1, ns1, asps1)<- foldM (\(aDg, ns, as) a -> do - (dg',ns',a') <- instMacroAux a - return (dg', ns ++ ns', as ++ [a']) ) (dg, [], []) asps - return (dg1, ns1, asp{item = Union asps1 rg}) - Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ + (dg',ns',a') <- instMacroAux lg libEnv opts eo ln aDg imp crtNSig name spname crtSubst vars crtGM a + return (dg', ns ++ ns', as ++ [a']) ) (crtDG, [], []) asps + trace ("ns1:" ++ show ns1) $ return (dg1, ns1, asp0{item = Union asps1 rg}) + Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! - let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv dg + let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv crtDG case snEntry of PatternEntry patSig@(PatternSig isLocal _ pParams pMap pBody) -> do l@(Logic crtLid) <- lookupCurrentLogic "anaPatternInstArgs" lg @@ -1652,10 +1668,10 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev UnsolvedName i _ -> if i == mkIRI "empty" then error "empty list as argument in instantiation of pattern nyi" else ([], aFitArg) - NormalVariable i -> trace ("normal variable: " ++ show i ++ " subst:" ++ show subst ++ " vars:" ++ show vars) $ + NormalVariable i -> -- trace ("normal variable: " ++ show i ++ " subst:" ++ show subst ++ " vars:" ++ show vars) $ if i `elem` Map.keys vars then let (b, k) = Map.findWithDefault (error "notPossible") i vars - val = Map.findWithDefault (error "variable not mapped") (i,k) subst + val = Map.findWithDefault (error "variable not mapped") (i,k) crtSubst valIRI = case val of PlainVal valiri -> valiri _ -> error "normal variable mapped to list" @@ -1665,12 +1681,12 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev if i `elem` Map.keys vars then let (b, k) = Map.findWithDefault (error "not possible") i vars in if k == "list" then - let val = Map.findWithDefault (error "variable not mapped") (i, k) subst + let val = Map.findWithDefault (error "variable not mapped") (i, k) crtSubst in case val of ListVal k' vals -> let genItem = Fit_spec asp1{item= OntoList $ map (\v -> emptyAnno $ UnsolvedName v nullRange) vals} gm rg in if not $ null vals then ([], aFitArg{item = genItem}) -- error $ "genItem:" ++ show genItem - else trace "******** got to empty **************" $ ([], aFitArg{item = Fit_spec asp1{item = UnsolvedName (mkIRI "empty") nullRange} gm rg}) + else ([], aFitArg{item = Fit_spec asp1{item = UnsolvedName (mkIRI "empty") nullRange} gm rg}) -- TODO: this does not suffice, we need to generate empty ontology here already! _ -> error $ "expected list argument but got single element" else error $ "expected list but got " ++ k @@ -1692,15 +1708,16 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev bsp = case convertTheory lid of Just f -> f th _ -> error "cannot convert theory" - if not $ null aitems then instMacroAux $ asp0{item = Basic_spec (G_basic_spec lid bsp) nullRange} -- error "empty list as argument, not yet done" + if not $ null aitems then + instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM $ asp0{item = Basic_spec (G_basic_spec lid bsp) nullRange} -- error "empty list as argument, not yet done" else do - gmor' <- -- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ + gmor' <- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ if isLocal then - case mgmPrev of + case crtGM of Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm else return idImps -- TODO: old variant: extendWithSubst l idImps newVars - (afitargs', dg', nsig', subst', gm') <- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ + (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 @@ -1708,18 +1725,20 @@ instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev name spname subst0 gm0 par0 arg0 -- trace ("after analysis nsig':" ++ show nsig1 ++ " gm1:" ++ show gm1) $ return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) - ([], dg, nsig, subst, gmor') -- the last argument node should not be EmptyNode, but the target of gmor'. Try with nsig? + ([], crtDG, crtNSig, crtSubst, gmor') -- the last argument node should not be EmptyNode, but the target of gmor'. Try with nsig? zipped -- (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', subst') <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs - trace ("\n\n\n recursive call:" ++ show subst' ++ " nsig':" ++ show nsig') $ instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' subst) vars gm' pBody + trace ("\n\n\n recursive call:" ++ show subst' ++ " nsig':" ++ show nsig') $ + instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' crtSubst) vars gm' pBody + -- instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' subst) vars gm' pBody -- error $ "spec_inst:" ++ show sn ++ " args:" ++ show afitargs ++ " vars:" ++ show vars ++ " subst:" ++ show subst -- 1. afitargs should give raise to signature morphisms from the nodes of the params to the nodes of the args -- 2. and to a subst' -- 3. the body of sn should be instantiated in the new dgraph, with the union of subst and subst', with the varmap taken from the signature of sn in the globalEnv _ -> fail $ "expected a pattern entry but found:" ++ show snEntry - _ -> fail $ "only non-structured bodies for now:" ++ show (globalEnv dg) - instMacroAux asp - _ -> fail $ "illegal pattern signature:" ++ show macro + _ -> fail $ "only non-structured bodies for now:" ++ show (globalEnv crtDG) + -- instMacroAux dg nsig subst mgmPrev asp + -- _ -> fail $ "illegal pattern signature:" ++ show macro extendWithSubst :: AnyLogic -> Maybe G_morphism -> [((IRI, String),(IRI, String))] -> Result (Maybe G_morphism) extendWithSubst (Logic l) mgm newVars = do From 2057fa468be1544b7633c490f5d825ae1e53a207 Mon Sep 17 00:00:00 2001 From: mcodescu Date: Sat, 18 Apr 2020 12:57:57 +0200 Subject: [PATCH 20/33] quick fix: remove symbols that come from imports. should be fixed properly --- Static/AnalysisStructured.hs | 93 +++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 68b0612ddb..e38d085363 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -1187,18 +1187,23 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c G_morphism prevLid prevMor _ -> do prevMor' <- coerceMorphism prevLid slid "anaFitArg:coerceMorphism" prevMor let symMap = symmap_of slid prevMor' - return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) symMap + isysms = case nsigI of + EmptyNode _ -> Set.empty + JustNode inode -> case getSig inode of + G_sign ilid (ExtSign isig _) _ -> Set.map (\x -> coerceSymbol ilid slid x) $ symset_of ilid isig + trace ("***isysms:" ++ show isysms) $ return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) $ + Map.filterWithKey (\x _ -> not $ Set.member x isysms) symMap -- TODO: dont map the symbols in imports! let crtMapAux = Map.fromList [(sRSym, tRSym)] crtMap = if Map.intersection crtMapAux prevMap == Map.empty then Map.union prevMap crtMapAux else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) -- TODO: don't fail if the symbols are mapped in the same way - mor <- -- trace ("induced:" ++ show crtMap ++ " ssig:" ++ show ssig ++ " tsig:" ++ show tsig) $ + mor <- trace ("\n***********************\ninduced:" ++ show crtMap ++ " ssig:" ++ show ssig ++ " tsig:" ++ show tsig) $ induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na return (fv, dg'', (gmor, nsigA)) Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do - --trace ("____mgm in fit_new:" ++ show mgm ++ " nsigP:" ++ show nsigP ++ " prevSig:" ++ show prevSig) $ do + -- trace ("_________________________mgm in fit_new:" ++ show mgm ++ "\n_________nsigP:" ++ show nsigP ++ "\n_____prevSig:" ++ show prevSig) $ do let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym sRSym = symbol_to_raw slid ssym sigA <- add_symb_to_sign tlid (empty_signature tlid) tsym @@ -1212,11 +1217,11 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c EmptyNode _ -> [] JustNode x -> [x] JustNode x -> [x]) - gUnionSig <- gsigManyUnion lg $ [gA] ++ map getSig uNodes + gUnionSig <- trace ("asig:" ++ show asig) $ gsigManyUnion lg $ [gA] ++ map getSig uNodes let (usig@(NodeSig unode _), dg1) = insGSig dg0 (extName "Union" name) DGUnion gUnionSig insE dgl (NodeSig n gs) = do - incl <- -- trace ("inclusion from " ++ show gs ++ " to " ++ show gUnionSig) $ + incl <- trace ("\n=============\ninclusion from " ++ show gs ++ "\n\nto " ++ show gUnionSig) $ ginclusion lg gs gUnionSig return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ asig:uNodes @@ -1224,14 +1229,20 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig tsig <- case gUnionSig of G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig - prevMap <- case mgm of + prevMap <- trace ("\n=============\nmgm:"++ show mgm ++ "\nssig:"++show ssig ++ "\ntsig:"++ show tsig) $ case mgm of Nothing -> return Map.empty Just prevGMor -> case prevGMor of G_morphism prevLid prevMor _ -> do prevMor' <- coerceMorphism prevLid slid "anaFitArg:coerceMorphism" prevMor let symMap = symmap_of slid prevMor' - return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) symMap + isysms = case nsigI of + EmptyNode _ -> Set.empty + JustNode inode -> case getSig inode of + G_sign ilid (ExtSign isig _) _ -> Set.map (\x -> coerceSymbol ilid slid x) $ symset_of ilid isig + trace ("symMap:" ++ show symMap) $ + return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) $ + Map.filterWithKey (\x _ -> not $ Set.member x isysms) symMap let crtMapAux = Map.fromList [(sRSym, tRSym)] crtMap = if Map.intersection crtMapAux prevMap == Map.empty then Map.union prevMap crtMapAux @@ -1243,7 +1254,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c in if allMappedSameWay then Map.union prevMap crtMapAux else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) - mor <- -- trace ("ssig:"++ show ssig ++ " tsig:" ++ show tsig ++ " crtMap:" ++ show crtMap ) $ + mor <- trace ("\n=============\nssig:"++ show ssig ++ "\n tsig:" ++ show tsig ++ "\n prevMap:" ++ show prevMap ) $ induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor dg'' = -- trace ("gmor after induced:" ++ show gmor) $ @@ -1441,7 +1452,7 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = -- trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1466,33 +1477,41 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 case gmor of G_morphism lid mor _ -> do -- trace ("arg2:"++ show arg2 ++ " gmor:" ++ show gmor ++ " nsigA:"++ show nsigA) $ do let smap = symmap_of lid mor + isyms = case isig of + EmptyNode _ -> Set.empty + JustNode inode -> case getSig inode of + G_sign ilid (ExtSign sigI _) _ -> Set.map (coerceSymbol ilid lid) $ symset_of ilid sigI subst1 = foldl (\f (ssym, tsym) -> - let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) - tn = idToIRI $ sym_name lid tsym - in Map.insert (sn, sk) (PlainVal tn) f) subst0 $ Map.toList smap + let (sn, sk) = (idToIRI $ sym_name lid ssym, symKind lid ssym) + tn = idToIRI $ sym_name lid tsym + in Map.insert (sn, sk) (PlainVal tn) f) subst0 $ filter (\(x,_) -> not $ Set.member x isyms) $ Map.toList smap -- TODO: any compatibility checks must be done here return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> error $ "argument mismatch in instantiation. parameter: " ++ show par0 ++ "\n argument: " ++ show arg0 else do case par0 of - SingleParamInfo b pNSig -> + SingleParamInfo b pNSig -> case getSig pNSig of G_sign lid (ExtSign _ newDecls) _ -> case Set.toList newDecls of [sym] -> do let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ + (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 -- try: only extend previous morphism if the pattern is local! case gmor of - G_morphism glid mor _ -> do -- trace ("gmor:" ++ show gmor) $ do + G_morphism glid mor _ -> do let smap = symmap_of glid mor + isyms = case isig of + EmptyNode _ -> Set.empty + JustNode inode -> case getSig inode of + G_sign ilid (ExtSign sigI _) _ -> Set.map (coerceSymbol ilid glid) $ symset_of ilid sigI subst1 = foldl (\f (ssym, tsym) -> let (sn, sk) = (idToIRI $ sym_name glid ssym, symKind glid ssym) tn = idToIRI $ sym_name glid tsym in Map.insert (sn, sk) (PlainVal tn) f) - subst0 $ Map.toList smap + subst0 $ filter (\(x,_) -> not $ Set.member x isyms) $ Map.toList smap -- TODO: any compatibility checks must be done here return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) case csig of @@ -1502,9 +1521,9 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 G_sign lid1 (ExtSign ctx _) _ -> do let ctxSyms = filter (\csym -> ((idToIRI $ sym_name lid1 csym) == i) && (symKind lid1 csym == symKind lid sym)) $ Set.toList $ symset_of lid1 ctx case ctxSyms of - [] -> -- trace "err2" $ + [] -> trace "err2" $ noCtxOrNoMatch - [ctxSym] -> do + [ctxSym] -> trace "symbol in ctx" $ do let arg1 = Fit_ctx (G_symbol lid sym) (G_symbol lid1 ctxSym) nullRange (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig @@ -1512,13 +1531,18 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 case gmor of G_morphism glid mor _ -> do let smap = symmap_of glid mor + isyms = case isig of + EmptyNode _ -> Set.empty + JustNode inode -> case getSig inode of + G_sign ilid (ExtSign sigI _) _ -> + Set.map (coerceSymbol ilid glid) $ symset_of ilid sigI subst1 = foldl (\f (ssym, tsym) -> - let (sn, sk) = (idToIRI $ sym_name glid ssym, symKind glid ssym) - tn = idToIRI $ sym_name glid tsym - in Map.insert (sn, sk) (PlainVal tn) f) - subst0 $ Map.toList smap + let (sn, sk) = (idToIRI $ sym_name glid ssym, symKind glid ssym) + tn = idToIRI $ sym_name glid tsym + in Map.insert (sn, sk) (PlainVal tn) f) + subst0 $ filter (\(x,_) -> not $ Set.member x isyms) $ Map.toList smap -- TODO: any compatibility checks must be done here - return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) + trace ("++++++ computed subst1:"++ show subst1) $ return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> fail $ "multiple occurences of abbreviated name in the context:" ++ show ctxSyms _ -> fail "ambiguity in use of abbreviation notation, parameter has more than one symbol" _ -> fail $ "abbreviation notation can be used only for single ontology arguments, not for lists: " ++ show i @@ -1601,8 +1625,8 @@ instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibNam DGraph -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> LocalOrSpecSig -> Result (DGraph, [NodeSig], Annoted SPEC) instantiateMacro lg libEnv opts eo ln dg imp nsig name spname subst vars mgmPrev macro = - trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ "\nsubst:" ++ show subst ++ - "\nnsig:" ++ show nsig ++ "\nimp:"++ show imp ++ "\nmgmPrev:"++ show mgmPrev ++ "\nvars:" ++ show vars) $ + --trace ("~~~~~~~~~~~~~~~~instantiateMacro:" ++ show macro ++ "\nsubst:" ++ show subst ++ + -- "\nnsig:" ++ show nsig ++ "\nimp:"++ show imp ++ "\nmgmPrev:"++ show mgmPrev ++ "\nvars:" ++ show vars) $ {- (Logic lid) <- lookupCurrentLogic "macro" lg let th = (empty_signature lid, []) bsp = case convertTheory lid of @@ -1624,7 +1648,7 @@ instMacroAux :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> PatternVarMap -> Maybe G_morphism -> Annoted SPEC -> Result (DGraph, [NodeSig], Annoted SPEC) -instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM asp0 = trace ("+++++ instMacroAux for " ++ show asp0) $ +instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM asp0 = -- trace ("+++++ instMacroAux for " ++ show asp0) $ case item asp0 of Basic_spec gbsp rg -> case gbsp of @@ -1633,7 +1657,8 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr let lastNode = case crtNSig of JustNode x -> x _ -> error "no last param of a pattern, should not happend" - trace ("bsp':" ++ show bsp') $ return (crtDG, [lastNode], asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) + -- trace ("bsp':" ++ show bsp') $ + return (crtDG, [lastNode], asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) Group asp1 rg -> do (dg2, ns2, asp2) <- instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM asp1 return (dg2, ns2, asp0{item = Group asp2 rg}) @@ -1700,7 +1725,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr UnsolvedName anIRI _ -> anIRI == mkIRI "empty" _ -> False) afitargs0 newVars = concatMap fst solved - zipped = trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ + zipped = -- trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! -- TODO: if isLocal start with subst1 else start with empty subst? (Logic lid) <- lookupCurrentLogic "macro" lg @@ -1711,7 +1736,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr if not $ null aitems then instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM $ asp0{item = Basic_spec (G_basic_spec lid bsp) nullRange} -- error "empty list as argument, not yet done" else do - gmor' <- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ + gmor' <- -- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ if isLocal then case crtGM of Nothing -> extendWithSubst l idImps newVars @@ -1719,17 +1744,17 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr else return idImps -- TODO: old variant: extendWithSubst l idImps newVars (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ + (arg1, dg1, nsig1, subst1, gm1) <- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 imp (EmptyNode l) nsig0 -- TODO: context is always empty now name spname subst0 gm0 par0 arg0 - -- trace ("after analysis nsig':" ++ show nsig1 ++ " gm1:" ++ show gm1) $ - return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) + trace ("$$$after analysis nsig':" ++ show nsig1 ++ " gm1:" ++ show gm1) $ + return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) ([], crtDG, crtNSig, crtSubst, gmor') -- the last argument node should not be EmptyNode, but the target of gmor'. Try with nsig? zipped -- (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', subst') <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs - trace ("\n\n\n recursive call:" ++ show subst' ++ " nsig':" ++ show nsig') $ - instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' crtSubst) vars gm' pBody + --trace ("\n\n\n recursive call:" ++ show subst' ++ " nsig':" ++ show nsig') $ + instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' crtSubst) vars gm' pBody -- instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' subst) vars gm' pBody -- error $ "spec_inst:" ++ show sn ++ " args:" ++ show afitargs ++ " vars:" ++ show vars ++ " subst:" ++ show subst -- 1. afitargs should give raise to signature morphisms from the nodes of the params to the nodes of the args From 4ef7367a2d00532d597ac6aac4bf0ea6bf3b29ae Mon Sep 17 00:00:00 2001 From: mcodescu Date: Mon, 20 Apr 2020 18:51:44 +0200 Subject: [PATCH 21/33] also instantiate parameterized names in arguments of pattern instantiations in pattern bodies --- Logic/Logic.hs | 21 ++++++++++++++ OWL2/StaticAnalysis.hs | 25 ----------------- Static/AnalysisStructured.hs | 53 ++++++++++++++++++++++-------------- 3 files changed, 54 insertions(+), 45 deletions(-) diff --git a/Logic/Logic.hs b/Logic/Logic.hs index b01f3344e7..ddbf0596ff 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -660,6 +660,27 @@ getIRIVal v = case v of PlainVal x -> x _ -> error $ "expecting plain value but got list:" ++ show v +-- instantiate paramerized names +-- p[X] becomes p[V] if subst maps X to V +-- the string argument is the kind + +instParamName :: GSubst -> IRI -> IRI +instParamName subst p = -- trace ("\nsubst:"++ show subst ++ " i:" ++ show p ) $ + let pPath = iriPath p + comps = getComps pPath + solveId t = + let tIRI = idToIRI t + k = let tSubsts = filter (\(x,y) -> x == tIRI) $ Map.keys subst + in case tSubsts of + [(a,b)] -> b + []-> "Class" -- does not matter + (a,b):_ -> b + in Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst -- this will most likely need to change for complex nesting! + comps' = map (\t -> iriPath $ getIRIVal $ solveId t) comps + newPath = -- trace ("comps:" ++ show comps ++ " comps':" ++ show comps') $ + pPath{getComps = comps'} + in p{iriPath = newPath} + type PatternVarMap = Map.Map IRI (Bool, String) -- Bool is true for list- and false for non-list variables -- TODO: ideally we should have logic-dependent kinds, but strings will do diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 82cb65003c..2fe9188c52 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -791,27 +791,6 @@ instantiateFrame subst var (Frame ext fBits) = do fBits' <- mapM (instantiateFrameBit subst var) fBits return $ Frame ext' fBits' --- instantiate paramerized names --- p[X] becomes p[V] if subst maps X to V --- the string argument is the kind - -instParamName :: GSubst -> IRI -> IRI -instParamName subst p = - let pPath = iriPath p - comps = getComps pPath - solveId t = - let tIRI = idToIRI t - k = let tSubsts = filter (\(x,y) -> x == tIRI) $ Map.keys subst - in case tSubsts of - [(a,b)] -> b - []-> "Class" -- does not matter - (a,b):_ -> b - in Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst -- this will most likely need to change for complex nesting! - comps' = map (\t -> iriPath $ getIRIVal $ solveId t) comps - newPath = -- trace ("comps:" ++ show comps ++ " comps':" ++ show comps') $ - pPath{getComps = comps'} - in p{iriPath = newPath} - instantiateFrameBit :: GSubst -> PatternVarMap -> FrameBit -> Result FrameBit instantiateFrameBit subst var fbit = case fbit of @@ -961,10 +940,6 @@ instClassExprAux subst var (annos, cexp) = return [(annos, ObjectCardinality (Cardinality cType aInt opexp' mcexp'))] _ -> return [(annos, cexp)] - - - - instantiateObjectPropertyExpression :: GSubst -> PatternVarMap -> (Annotations, ObjectPropertyExpression) -> Result (Annotations, ObjectPropertyExpression) instantiateObjectPropertyExpression subst var (annos, obexp) = case obexp of diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index e38d085363..d0096c0d67 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -546,7 +546,7 @@ anaSpecAux conser addSyms optNodes lg instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig (dgI, allPrevDefs) <- unionNodes lg dg2 (makeName $ mkIRI "TESTNAME") $ nub lastParamAndNewNodes --the body should extend the last argument - (sp', nsig'', dg3) <- trace ("spB:" ++ show spB ++ "\n allPrevDefs:" ++ show allPrevDefs) $ + (sp', nsig'', dg3) <- trace ("spB:" ++ show spB) $ anaSpecTop conser addSyms lg libEnv ln dgI (JustNode allPrevDefs) (makeName $ addSuffixToIRI "_source" $ getName name) opts eo (item spB) nullRange -- TODO: nsig' should be the node of instantiateMacro!!! --incl <- ginclusion lg (getSig nsig') (getSig nsig'') @@ -1191,13 +1191,14 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c EmptyNode _ -> Set.empty JustNode inode -> case getSig inode of G_sign ilid (ExtSign isig _) _ -> Set.map (\x -> coerceSymbol ilid slid x) $ symset_of ilid isig - trace ("***isysms:" ++ show isysms) $ return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) $ + -- trace ("***isysms:" ++ show isysms) $ + return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) $ Map.filterWithKey (\x _ -> not $ Set.member x isysms) symMap -- TODO: dont map the symbols in imports! let crtMapAux = Map.fromList [(sRSym, tRSym)] crtMap = if Map.intersection crtMapAux prevMap == Map.empty then Map.union prevMap crtMapAux else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) -- TODO: don't fail if the symbols are mapped in the same way - mor <- trace ("\n***********************\ninduced:" ++ show crtMap ++ " ssig:" ++ show ssig ++ " tsig:" ++ show tsig) $ + mor <- -- trace ("\n***********************\ninduced:" ++ show crtMap ++ " ssig:" ++ show ssig ++ " tsig:" ++ show tsig) $ induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na @@ -1217,11 +1218,12 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c EmptyNode _ -> [] JustNode x -> [x] JustNode x -> [x]) - gUnionSig <- trace ("asig:" ++ show asig) $ gsigManyUnion lg $ [gA] ++ map getSig uNodes + gUnionSig <- -- trace ("asig:" ++ show asig) $ + gsigManyUnion lg $ [gA] ++ map getSig uNodes let (usig@(NodeSig unode _), dg1) = insGSig dg0 (extName "Union" name) DGUnion gUnionSig insE dgl (NodeSig n gs) = do - incl <- trace ("\n=============\ninclusion from " ++ show gs ++ "\n\nto " ++ show gUnionSig) $ + incl <- -- trace ("\n=============\ninclusion from " ++ show gs ++ "\n\nto " ++ show gUnionSig) $ ginclusion lg gs gUnionSig return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ asig:uNodes @@ -1229,7 +1231,8 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig tsig <- case gUnionSig of G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig - prevMap <- trace ("\n=============\nmgm:"++ show mgm ++ "\nssig:"++show ssig ++ "\ntsig:"++ show tsig) $ case mgm of + prevMap <- -- trace ("\n=============\nmgm:"++ show mgm ++ "\nssig:"++show ssig ++ "\ntsig:"++ show tsig) $ + case mgm of Nothing -> return Map.empty Just prevGMor -> case prevGMor of @@ -1240,8 +1243,8 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c EmptyNode _ -> Set.empty JustNode inode -> case getSig inode of G_sign ilid (ExtSign isig _) _ -> Set.map (\x -> coerceSymbol ilid slid x) $ symset_of ilid isig - trace ("symMap:" ++ show symMap) $ - return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) $ + -- trace ("symMap:" ++ show symMap) $ + return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) $ Map.filterWithKey (\x _ -> not $ Set.member x isysms) symMap let crtMapAux = Map.fromList [(sRSym, tRSym)] crtMap = if Map.intersection crtMapAux prevMap == Map.empty @@ -1254,7 +1257,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c in if allMappedSameWay then Map.union prevMap crtMapAux else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) - mor <- trace ("\n=============\nssig:"++ show ssig ++ "\n tsig:" ++ show tsig ++ "\n prevMap:" ++ show prevMap ) $ + mor <- -- trace ("\n=============\nssig:"++ show ssig ++ "\n tsig:" ++ show tsig ++ "\n prevMap:" ++ show prevMap ) $ induced_from_to_morphism slid crtMap ssig tsig let gmor = mkG_morphism slid mor dg'' = -- trace ("gmor after induced:" ++ show gmor) $ @@ -1452,7 +1455,7 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = -- trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1496,8 +1499,8 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 case Set.toList newDecls of [sym] -> do let noCtxOrNoMatch = do - let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ + let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show $ instParamName subst0 i))) nullRange + (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 -- try: only extend previous morphism if the pattern is local! case gmor of @@ -1671,8 +1674,9 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr (dg1, ns1, asps1)<- foldM (\(aDg, ns, as) a -> do (dg',ns',a') <- instMacroAux lg libEnv opts eo ln aDg imp crtNSig name spname crtSubst vars crtGM a return (dg', ns ++ ns', as ++ [a']) ) (crtDG, [], []) asps - trace ("ns1:" ++ show ns1) $ return (dg1, ns1, asp0{item = Union asps1 rg}) - Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0)) $ + -- trace ("ns1:" ++ show ns1) $ + return (dg1, ns1, asp0{item = Union asps1 rg}) + Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv crtDG case snEntry of @@ -1693,7 +1697,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr UnsolvedName i _ -> if i == mkIRI "empty" then error "empty list as argument in instantiation of pattern nyi" else ([], aFitArg) - NormalVariable i -> -- trace ("normal variable: " ++ show i ++ " subst:" ++ show subst ++ " vars:" ++ show vars) $ + NormalVariable i -> trace ("normal variable: " ++ show i ++ " crtSubst:" ++ show crtSubst ++ " vars:" ++ show vars ++ " pMap:" ++ show pMap) $ if i `elem` Map.keys vars then let (b, k) = Map.findWithDefault (error "notPossible") i vars val = Map.findWithDefault (error "variable not mapped") (i,k) crtSubst @@ -1701,7 +1705,16 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr PlainVal valiri -> valiri _ -> error "normal variable mapped to list" in ([((i,k), (valIRI,k))], aFitArg{item = Fit_spec asp1{item= UnsolvedName valIRI nullRange} gm rg}) - else error $ "unknown variable:" ++ show i + else + case filter (\(x,_y) -> x == i) $ Map.keys crtSubst of + [(a, k)] -> + let + val = Map.findWithDefault (error "variable not mapped") (i,k) crtSubst + valIRI = case val of + PlainVal valiri -> valiri + _ -> error "normal variable mapped to list" + in ([((i,k), (valIRI,k))], aFitArg{item = Fit_spec asp1{item= UnsolvedName valIRI nullRange} gm rg}) + _ -> error $ "unknown variable:" ++ show i ++ " in " ++ show vars ListVariable i -> if i `elem` Map.keys vars then let (b, k) = Map.findWithDefault (error "not possible") i vars @@ -1715,7 +1728,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr -- TODO: this does not suffice, we need to generate empty ontology here already! _ -> error $ "expected list argument but got single element" else error $ "expected list but got " ++ k - else error $ "unknown variable:" ++ show i + else error $ "unknown list variable:" ++ show i _ -> ([], aFitArg) _ -> ([], aFitArg) solved = map solveVars afitargs @@ -1744,12 +1757,12 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr else return idImps -- TODO: old variant: extendWithSubst l idImps newVars (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ + (arg1, dg1, nsig1, subst1, gm1) <- -- strace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 imp (EmptyNode l) nsig0 -- TODO: context is always empty now name spname subst0 gm0 par0 arg0 - trace ("$$$after analysis nsig':" ++ show nsig1 ++ " gm1:" ++ show gm1) $ - return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) + --trace ("$$$after analysis nsig':" ++ show nsig1 ++ " gm1:" ++ show gm1) $ + return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) ([], crtDG, crtNSig, crtSubst, gmor') -- the last argument node should not be EmptyNode, but the target of gmor'. Try with nsig? zipped -- (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', subst') <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs From 0d019fae04e831a059e08e2936e19f83e2489175 Mon Sep 17 00:00:00 2001 From: mcodescu Date: Tue, 21 Apr 2020 11:01:08 +0200 Subject: [PATCH 22/33] properly instantiate parameterized ids --- Logic/Logic.hs | 30 ++++++++++++++++-------------- Static/AnalysisStructured.hs | 4 ++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Logic/Logic.hs b/Logic/Logic.hs index ddbf0596ff..1c3ac5954c 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -155,6 +155,8 @@ import Data.Ord import Data.Typeable import Control.Monad (unless) +import Debug.Trace + -- | Stability of logic implementations data Stability = Stable | Testing | Unstable | Experimental deriving (Eq, Show) @@ -666,20 +668,20 @@ getIRIVal v = case v of instParamName :: GSubst -> IRI -> IRI instParamName subst p = -- trace ("\nsubst:"++ show subst ++ " i:" ++ show p ) $ - let pPath = iriPath p - comps = getComps pPath - solveId t = - let tIRI = idToIRI t - k = let tSubsts = filter (\(x,y) -> x == tIRI) $ Map.keys subst - in case tSubsts of - [(a,b)] -> b - []-> "Class" -- does not matter - (a,b):_ -> b - in Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst -- this will most likely need to change for complex nesting! - comps' = map (\t -> iriPath $ getIRIVal $ solveId t) comps - newPath = -- trace ("comps:" ++ show comps ++ " comps':" ++ show comps') $ - pPath{getComps = comps'} - in p{iriPath = newPath} + p{iriPath = solveId subst (iriPath p)} + +solveId :: GSubst -> Id -> Id +solveId subst t = + case getComps t of + [] -> let tIRI = idToIRI t + k = let tSubsts = filter (\(x,y) -> x == tIRI) $ Map.keys subst + in case tSubsts of + [(a,b)] -> b + []-> "Class" -- does not matter + (a,b):_ -> b + in iriPath $ getIRIVal $ Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst + cs -> let cs' = map (solveId subst) cs + in t{getComps = cs'} type PatternVarMap = Map.Map IRI (Bool, String) -- Bool is true for list- and false for non-list variables diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index d0096c0d67..90bc23012c 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -546,7 +546,7 @@ anaSpecAux conser addSyms optNodes lg instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig (dgI, allPrevDefs) <- unionNodes lg dg2 (makeName $ mkIRI "TESTNAME") $ nub lastParamAndNewNodes --the body should extend the last argument - (sp', nsig'', dg3) <- trace ("spB:" ++ show spB) $ + (sp', nsig'', dg3) <- -- trace ("spB:" ++ show spB) $ anaSpecTop conser addSyms lg libEnv ln dgI (JustNode allPrevDefs) (makeName $ addSuffixToIRI "_source" $ getName name) opts eo (item spB) nullRange -- TODO: nsig' should be the node of instantiateMacro!!! --incl <- ginclusion lg (getSig nsig') (getSig nsig'') @@ -1676,7 +1676,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr return (dg', ns ++ ns', as ++ [a']) ) (crtDG, [], []) asps -- trace ("ns1:" ++ show ns1) $ return (dg1, ns1, asp0{item = Union asps1 rg}) - Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0)) $ + Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0)) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv crtDG case snEntry of From 30e5159e5c311c4ed1453a304af9e6450e96075f Mon Sep 17 00:00:00 2001 From: mcodescu Date: Mon, 27 Apr 2020 15:43:18 +0200 Subject: [PATCH 23/33] lists as arguments in spec_insts --- OWL2/Logic_OWL2.hs | 4 +- Static/AnalysisLibrary.hs | 3 + Static/AnalysisStructured.hs | 101 +++++++++++++++++++++++----------- Syntax/AS_Structured.der.hs | 2 +- Syntax/Parse_AS_Structured.hs | 2 +- 5 files changed, 77 insertions(+), 35 deletions(-) diff --git a/OWL2/Logic_OWL2.hs b/OWL2/Logic_OWL2.hs index 5f6a2d60dc..8495a446a1 100644 --- a/OWL2/Logic_OWL2.hs +++ b/OWL2/Logic_OWL2.hs @@ -59,6 +59,8 @@ import OWL2.Theorem import OWL2.ExtractModule -- import OWL2.Macros +import Debug.Trace + data OWL2 = OWL2 instance Show OWL2 where @@ -142,7 +144,7 @@ inducedFromToMorphismAux :: Map.Map RawSymb RawSymb -> ExtSign Sign Entity -> ExtSign Sign Entity -> Result OWLMorphism -inducedFromToMorphismAux rm s@(ExtSign ssig _) t@(ExtSign tsig _) = do +inducedFromToMorphismAux rm s@(ExtSign ssig _) t@(ExtSign tsig _) = do -- trace ("\n aux ssig:" ++ show ssig ++ "\ntsig:" ++ show tsig ++ "\nrm:" ++ show rm) $ do mor <- inducedFromMor rm ssig let csig = cod mor incl <- inclusion OWL2 csig tsig diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index bdf61f13e1..bf48ffd85a 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -812,6 +812,9 @@ solveBody lg lenv ln dg opts eo name vMap impNode sp = _ -> return f fitArgs' <- mapM solveFitArgs fitArgs return $ Spec_inst n fitArgs' miri r -- maybe here unsolved arguments should be solved to variables! + OntoList aspecs -> do + aspecs' <- mapM (solveBody lg lenv ln dg opts eo name vMap impNode) $ map item aspecs + return $ OntoList $ map (\(x,y) -> x{item = y}) $ zip aspecs aspecs' _ -> error $ show sp symbolsOf :: LogicGraph -> G_sign -> G_sign -> [CORRESPONDENCE] diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 90bc23012c..01bf4a598e 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -530,7 +530,7 @@ anaSpecAux conser addSyms optNodes lg anaSpecTop conser addSyms lg libEnv ln dg nsig name opts eo (item asp) rg return (Group (replaceAnnoted sp' asp) pos, nsig', dg') - Spec_inst spname' afitargs mImp pos0 -> do -- trace ("\n\n**** ana spec inst *** " ++ show spname' ++ " afitargs:" ++ show afitargs ++ "nsig:" ++ show nsig) $ do + Spec_inst spname' afitargs mImp pos0 -> trace ("\n\n**** ana spec inst *** " ++ show spname' ++ " afitargs:" ++ show afitargs) $ do spname <- expCurieR (globalAnnos dg) eo spname' let pos = if null afitargs then iriPos spname else pos0 adjustPos pos $ case lookupGlobalEnvDG spname dg of @@ -542,7 +542,7 @@ anaSpecAux conser addSyms optNodes lg (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs -- let body' = getBody bodySig (Logic cl) <- lookupCurrentLogic "anaGmaps" lg - (dg2, lastParamAndNewNodes, spB) <- trace ("calling instMacro on " ++ show spname' ++ "[" ++ show afitargs ++ "] subst:" ++ show subst) $ + (dg2, lastParamAndNewNodes, spB) <- trace ("calling instMacro on " ++ show spname' ++ "[" ++ show afitargs ++ "] subst:" ++ show subst ++ " gm':" ++ show gm') $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig (dgI, allPrevDefs) <- unionNodes lg dg2 (makeName $ mkIRI "TESTNAME") $ nub lastParamAndNewNodes --the body should extend the last argument @@ -1103,9 +1103,9 @@ anaGmaps lg opts pos psig@(G_sign lidP sigmaP _) asig@(G_sign lidA sigmaA _) mgm anaFitArg :: LogicGraph -> LibEnv -> LibName -> DGraph -> IRI -> MaybeNode -> NodeSig -> HetcatsOpts -> NodeName -> ExpOverrides - -> MaybeNode -> MaybeNode -> Maybe G_morphism -> FIT_ARG + -> MaybeNode -> MaybeNode -> Maybe G_morphism -> GSubst -> FIT_ARG -> Result (FIT_ARG, DGraph, (G_morphism, NodeSig)) -anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo csig prevSig mgm fv = +anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo csig prevSig mgm prevSubst fv = let ga = globalAnnos dg in case fv of Fit_string s _ -> error $ "nyi for " ++ (show s) @@ -1204,7 +1204,7 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c dg'' = insLink dg' (gEmbed gmor) globalThm (DGLinkInstArg spname) nP na return (fv, dg'', (gmor, nsigA)) Fit_new (G_symbol slid ssym) (G_symbol tlid tsym) pos -> do - -- trace ("_________________________mgm in fit_new:" ++ show mgm ++ "\n_________nsigP:" ++ show nsigP ++ "\n_____prevSig:" ++ show prevSig) $ do + -- trace ("_________________________mgm in fit_new:" ++ show mgm ++ "\n_________nsigP:" ++ show nsigP ++ "\n_____prevSig:" ++ show prevSig ++ "\n ssym:" ++ show ssym ++ "\ntsym:" ++ show tsym) $ do let tRSym = symbol_to_raw slid $ coerceSymbol tlid slid tsym sRSym = symbol_to_raw slid ssym sigA <- add_symb_to_sign tlid (empty_signature tlid) tsym @@ -1218,20 +1218,20 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c EmptyNode _ -> [] JustNode x -> [x] JustNode x -> [x]) - gUnionSig <- -- trace ("asig:" ++ show asig) $ + gUnionSig <- -- trace ("\nextSigA:" ++ show extSigA ++ " unodes:" ++ (show $ map getSig uNodes)) $ gsigManyUnion lg $ [gA] ++ map getSig uNodes let (usig@(NodeSig unode _), dg1) = insGSig dg0 (extName "Union" name) DGUnion gUnionSig insE dgl (NodeSig n gs) = do - incl <- -- trace ("\n=============\ninclusion from " ++ show gs ++ "\n\nto " ++ show gUnionSig) $ + incl <- -- trace ("\n=============\ninclusion from " ++ show gs ++ "\n\nto " ++ show gUnionSig) $ ginclusion lg gs gUnionSig return $ insLink dgl incl globalDef SeeTarget n unode dg2 <- foldM insE dg1 $ asig:uNodes - ssig <- case gsigmaP of + ssig@(ExtSign pssig _) <- case gsigmaP of G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig tsig <- case gUnionSig of G_sign plid psig _ -> coerceSign plid slid "anaFitArg:coercePlainSign" psig - prevMap <- -- trace ("\n=============\nmgm:"++ show mgm ++ "\nssig:"++show ssig ++ "\ntsig:"++ show tsig) $ + prevMap <- -- trace ("\n=============\nmgm:"++ show mgm ++ "\nssig:"++show ssig ++ "\ntsig:"++ show tsig) $ case mgm of Nothing -> return Map.empty Just prevGMor -> @@ -1247,6 +1247,15 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c return $ Map.mapKeys (symbol_to_raw slid) $ Map.map (symbol_to_raw slid) $ Map.filterWithKey (\x _ -> not $ Set.member x isysms) symMap let crtMapAux = Map.fromList [(sRSym, tRSym)] + substMap = Map.fromList $ concatMap (\((xIRI, xKind),y)-> case y of + PlainVal yIRI -> let xSym = symbol_to_raw slid $ new_symbol slid xIRI xKind + ySym = symbol_to_raw slid $ new_symbol slid yIRI xKind + + in -- [(xSym, ySym)] + if Set.member xSym (Set.map (symbol_to_raw slid) $ symset_of slid pssig) + then [(xSym, ySym)] + else [] + _ -> []) $ Map.toList prevSubst crtMap = if Map.intersection crtMapAux prevMap == Map.empty then Map.union prevMap crtMapAux else -- don't fail if the symbols are mapped in the same way @@ -1257,10 +1266,10 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c in if allMappedSameWay then Map.union prevMap crtMapAux else error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) - mor <- -- trace ("\n=============\nssig:"++ show ssig ++ "\n tsig:" ++ show tsig ++ "\n prevMap:" ++ show prevMap ) $ - induced_from_to_morphism slid crtMap ssig tsig + mor <- -- trace ("\n=============\nssig:"++ show ssig ++ "\n tsig:" ++ show tsig ++ "\n prevMap:" ++ show prevMap ++ "\n substMap:" ++ show substMap) $ + induced_from_to_morphism slid (Map.union crtMap substMap) ssig tsig let gmor = mkG_morphism slid mor - dg'' = -- trace ("gmor after induced:" ++ show gmor) $ + dg'' = -- trace ("gmor after induced:" ++ show gmor) $ insLink dg2 (gEmbed gmor) globalThm (DGLinkInstArg spname) nP unode return (fv, dg'', (gmor, usig)) -- trace ("sigA:" ++ show usig) $ error "fit_new nyi" @@ -1324,7 +1333,7 @@ anaFitArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> -> Result ([FIT_ARG], DGraph, [(G_morphism, NodeSig)], NodeName) anaFitArgs lg libEnv opts eo ln spname imps (fas', dg1, args, name') (nsig', fa) = do let n1 = inc name' - (fa', dg', arg) <- anaFitArg lg libEnv ln dg1 spname imps nsig' opts n1 eo imps imps Nothing fa -- TODO: this is wrong! + (fa', dg', arg) <- anaFitArg lg libEnv ln dg1 spname imps nsig' opts n1 eo imps imps Nothing Map.empty fa -- TODO: this is wrong! return (fa' : fas', dg', arg : args, n1) anaAllFitArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph @@ -1384,7 +1393,7 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi -- SingleParamInfo _ xs -> dgn_theory $ labDG dgP $ getNode xs -- _ -> error "nyi") $ map fst zipped')) $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0:" ++ show subst0) $ + (arg1, dg1, nsig1, subst1, gm1) <- trace ("\nFOLD\nsubst0:" ++ show subst0 ++ "\ngm0:" ++ show gm0) $ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname subst0 gm0 par0 arg0 let nsig2 = case nsig1 of -- this is a trick to make lists work! EmptyNode _ -> nsig0 @@ -1465,6 +1474,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 _ -> error $ "parameter mismatch, got a string when expecting a " ++ show par0 Fit_spec asp gm r -> case item asp of + NormalVariable i -> error "vars should be solved by now!" UnsolvedName i rg -> -- trace ("solving an unsolved name in inst arg:" ++ show i) $ -- TODO: here we must also pass the parameter, so we can check its symbols -- 1. if i is the name of a spec entry in globalEnv @@ -1476,7 +1486,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 let arg1 = Fit_spec (emptyAnno $ Spec_inst i [] Nothing nullRange) [] nullRange l <- lookupCurrentLogic "fit string" lg -- empty node was isig in next line - (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 + (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pSig opts (extName "Arg" name) eo csig prevSig mgm0 subst0 arg1 case gmor of G_morphism lid mor _ -> do -- trace ("arg2:"++ show arg2 ++ " gmor:" ++ show gmor ++ " nsigA:"++ show nsigA) $ do let smap = symmap_of lid mor @@ -1500,11 +1510,11 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 [sym] -> do let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show $ instParamName subst0 i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. prevSig:" ++ show prevSig ++ " arg1:" ++ show arg1) $ - anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 arg1 + (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. pNSig:" ++ show pNSig ++ " arg1:" ++ show arg1 ++ " mgm0:" ++ show mgm0) $ + anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 subst0 arg1 -- try: only extend previous morphism if the pattern is local! case gmor of - G_morphism glid mor _ -> do + G_morphism glid mor _ -> do -- trace ("gmor:" ++ show gmor) $ do let smap = symmap_of glid mor isyms = case isig of EmptyNode _ -> Set.empty @@ -1524,13 +1534,13 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 G_sign lid1 (ExtSign ctx _) _ -> do let ctxSyms = filter (\csym -> ((idToIRI $ sym_name lid1 csym) == i) && (symKind lid1 csym == symKind lid sym)) $ Set.toList $ symset_of lid1 ctx case ctxSyms of - [] -> trace "err2" $ + [] -> trace ("err2:"++ show i) $ noCtxOrNoMatch [ctxSym] -> trace "symbol in ctx" $ do let arg1 = Fit_ctx (G_symbol lid sym) (G_symbol lid1 ctxSym) nullRange (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig - prevSig mgm0 arg1 + prevSig mgm0 subst0 arg1 case gmor of G_morphism glid mor _ -> do let smap = symmap_of glid mor @@ -1575,12 +1585,14 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 anaPatternInstArg lg libEnv opts eo ln aDg isig csig prevSig -- TODO: check that prevSig is fine here name spname -- TODO: give proper names - subst0 mgm0 -- TODO: check that this is ok + subst0 + mgm0 -- TODO: check that this is ok par1 $ emptyAnno $ Fit_spec crtSp [] nullRange + -- trace ("\naMor:" ++ show aMor) $ return (anaSpecs ++ [crtSp'], specNodes ++ [argNode], - if substI == Map.empty then aSubst else substI, -- only interested in the first substitution + if substI == Map.empty then aSubst else substI, -- only interested in the first substitution. TODO: add it to mgm0? aDg') ) ([], [], Map.empty, dg0) aspecs -- 3. unite the resulting nodes @@ -1616,12 +1628,13 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 in Map.insert (tailName, "list") (ListVal k iriList) subst1 _ -> subst1 + -- trace ("subst1:" ++ show subst1 ++ "subst2:" ++ show subst2 ++ "\nmgm0:" ++ show mgm0) $ return (arg0, dg2, JustNode argUnion, subst2, mgm0) -- 5. instantiate the body with the substitution and add a link from the united arguments to the body -- this should happen elsewhere! - _ -> -- trace ("itm:" ++ (show $ item arg0) ) $ + _ -> trace ("itm:" ++ (show $ item arg0) ) $ error "only unsolved names for now" - _ -> -- trace ("itm:" ++ (show $ item arg0)) $ + _ -> trace ("itm:" ++ (show $ item arg0)) $ error "only fit_spec for now" instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> @@ -1676,7 +1689,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr return (dg', ns ++ ns', as ++ [a']) ) (crtDG, [], []) asps -- trace ("ns1:" ++ show ns1) $ return (dg1, ns1, asp0{item = Union asps1 rg}) - Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0)) $ + Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0) ++ " crtSubst:" ++ show crtSubst) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv crtDG case snEntry of @@ -1690,14 +1703,27 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr esig' <- coercePlainSign ilid crtLid "coerceSign in anaPatternInstArgs" esig let emor = ide esig' return $ Just $ G_morphism crtLid emor startMorId - let solveVars aFitArg = + let solveVars aFitArg = trace ("solving:" ++ show aFitArg) $ case item aFitArg of Fit_spec asp1 gm rg -> case item asp1 of + OntoList asps -> + let + args0 = map (\a-> emptyAnno $ Fit_spec a [] nullRange) asps + res = map solveVars args0 + listToOntoList aList = + let + allNames = concatMap (\a -> case item a of + Fit_spec x _ _ -> + case item x of + UnsolvedName _ _ -> [item x] + OntoList xs -> map item xs) aList + in emptyAnno $ OntoList $ map emptyAnno allNames + in trace ("vars: " ++ show (concatMap fst res)) $ (concatMap fst res, aFitArg{item= Fit_spec (listToOntoList $ map snd res) gm rg}) UnsolvedName i _ -> if i == mkIRI "empty" then error "empty list as argument in instantiation of pattern nyi" else ([], aFitArg) - NormalVariable i -> trace ("normal variable: " ++ show i ++ " crtSubst:" ++ show crtSubst ++ " vars:" ++ show vars ++ " pMap:" ++ show pMap) $ + NormalVariable i -> -- trace ("normal variable: " ++ show i ++ " crtSubst:" ++ show crtSubst ++ " vars:" ++ show vars ++ " pMap:" ++ show pMap) $ if i `elem` Map.keys vars then let (b, k) = Map.findWithDefault (error "notPossible") i vars val = Map.findWithDefault (error "variable not mapped") (i,k) crtSubst @@ -1723,12 +1749,22 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr in case val of ListVal k' vals -> let genItem = Fit_spec asp1{item= OntoList $ map (\v -> emptyAnno $ UnsolvedName v nullRange) vals} gm rg - in if not $ null vals then ([], aFitArg{item = genItem}) -- error $ "genItem:" ++ show genItem + in if not $ null vals then ([], aFitArg{item = genItem}) -- error $ "genItem:" ++ show genItem. TODO: dont map i to values? else ([], aFitArg{item = Fit_spec asp1{item = UnsolvedName (mkIRI "empty") nullRange} gm rg}) -- TODO: this does not suffice, we need to generate empty ontology here already! _ -> error $ "expected list argument but got single element" else error $ "expected list but got " ++ k - else error $ "unknown list variable:" ++ show i + else + case filter (\(x,_y) -> x == i) $ Map.keys crtSubst of + [(a, k)] -> + let + val = Map.findWithDefault (error "variable not mapped") (i,k) crtSubst + in case val of + PlainVal valiri -> error "list mapped to normal value" + ListVal valKind valVals -> case valVals of + [] -> ([], aFitArg{item = Fit_spec asp1{item = UnsolvedName (mkIRI "empty") nullRange} gm rg}) + _ -> ([], aFitArg{item = Fit_spec asp1{item = OntoList $ map (\x -> emptyAnno $ UnsolvedName x nullRange) valVals} gm rg}) + _ -> error $ "unknown list variable: " ++ show i ++ " vars: " ++ show vars _ -> ([], aFitArg) _ -> ([], aFitArg) solved = map solveVars afitargs @@ -1736,7 +1772,8 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr aitems = filter (\x -> case item x of Fit_spec y _ _ -> case item y of UnsolvedName anIRI _ -> anIRI == mkIRI "empty" - _ -> False) afitargs0 + _ -> False + _ -> False) afitargs0 newVars = concatMap fst solved zipped = -- trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! @@ -1755,9 +1792,9 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm else return idImps -- TODO: old variant: extendWithSubst l idImps newVars - (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~gmor':"++ show gmor') $ + (afitargs', dg', nsig', subst', gm') <- trace ("~~~~~~~~~~~~~crtSubst:"++ show crtSubst ++ " gmor':" ++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- -- strace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ + (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 imp (EmptyNode l) nsig0 -- TODO: context is always empty now name spname subst0 gm0 par0 arg0 diff --git a/Syntax/AS_Structured.der.hs b/Syntax/AS_Structured.der.hs index dbaa5d0a4e..d4cd573982 100644 --- a/Syntax/AS_Structured.der.hs +++ b/Syntax/AS_Structured.der.hs @@ -82,7 +82,7 @@ data SPEC = Basic_spec G_basic_spec Range | NormalVariable IRI | ListVariable IRI | ListValue [IRI] - | OntoList [(Annoted SPEC)] + | OntoList [Annoted SPEC] | EmptyList deriving (Show, Typeable) diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index c4de15d5d2..cf3167042f 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -590,7 +590,7 @@ fitString l flag = do case s of [] -> error "should be caught by the other case" [x] -> return $ Fit_spec x [] nullRange - _ -> return $ Fit_list s nullRange + _ -> return $ Fit_spec (Annoted (OntoList s) nullRange [][]) [] nullRange -- Fit_list s nullRange fittingArg :: LogicGraph -> Bool -> AParser st FIT_ARG fittingArg l flag = do From fe53c5d86d184f6c01a909653ca676b53bf8aa71 Mon Sep 17 00:00:00 2001 From: mcodescu Date: Tue, 28 Apr 2020 13:55:59 +0200 Subject: [PATCH 24/33] instantiate param names for individuals --- OWL2/StaticAnalysis.hs | 2 +- Static/AnalysisStructured.hs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 2fe9188c52..6043d534db 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -818,7 +818,7 @@ instantiateFrameBit subst var fbit = (_, ope') <- instantiateObjectPropertyExpression subst var ([], ope) let j = if (i,"Individual")`elem` Map.keys subst then getIRIVal $ Map.findWithDefault (error "instantiateFrameBit") (i, "Individual") subst - else i + else instParamName subst i return (a, ObjectPropertyFact pn ope' j) DataPropertyFact pn dpe lit -> error "data property fact nyi") afacts return $ ListFrameBit mr $ IndividualFacts afacts' diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 01bf4a598e..5dbb9dd4bb 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -1464,7 +1464,7 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = -- trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1475,7 +1475,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 Fit_spec asp gm r -> case item asp of NormalVariable i -> error "vars should be solved by now!" - UnsolvedName i rg -> -- trace ("solving an unsolved name in inst arg:" ++ show i) $ + UnsolvedName i rg -> trace ("solving an unsolved name in inst arg:" ++ show i) $ -- TODO: here we must also pass the parameter, so we can check its symbols -- 1. if i is the name of a spec entry in globalEnv -- solve to Spec_inst i [] Nothing nullRange @@ -1558,7 +1558,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 trace ("++++++ computed subst1:"++ show subst1) $ return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> fail $ "multiple occurences of abbreviated name in the context:" ++ show ctxSyms _ -> fail "ambiguity in use of abbreviation notation, parameter has more than one symbol" - _ -> fail $ "abbreviation notation can be used only for single ontology arguments, not for lists: " ++ show i + _ -> fail $ "abbreviation notation can be used only for single ontology arguments, not for lists: " ++ show i ++ " par0:" ++ show par0 -- 2. if i is a symbol from the context (nsig) -- solve to context fit x |-> i -- and the substitution maps x to i From b525431b7f288af6d63a9f468832639787137d7c Mon Sep 17 00:00:00 2001 From: mcodescu Date: Sun, 3 May 2020 09:07:26 +0200 Subject: [PATCH 25/33] x[y] to v1[v2] --- Logic/Logic.hs | 28 ++++++++++++++++++++-------- Static/AnalysisStructured.hs | 14 ++++++++------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/Logic/Logic.hs b/Logic/Logic.hs index 1c3ac5954c..1bff70b91a 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -155,8 +155,6 @@ import Data.Ord import Data.Typeable import Control.Monad (unless) -import Debug.Trace - -- | Stability of logic implementations data Stability = Stable | Testing | Unstable | Experimental deriving (Eq, Show) @@ -667,21 +665,35 @@ getIRIVal v = case v of -- the string argument is the kind instParamName :: GSubst -> IRI -> IRI -instParamName subst p = -- trace ("\nsubst:"++ show subst ++ " i:" ++ show p ) $ +instParamName subst p = p{iriPath = solveId subst (iriPath p)} solveId :: GSubst -> Id -> Id solveId subst t = case getComps t of [] -> let tIRI = idToIRI t - k = let tSubsts = filter (\(x,y) -> x == tIRI) $ Map.keys subst + k = let tSubsts = filter (\(x,_) -> x == tIRI) $ Map.keys subst in case tSubsts of - [(a,b)] -> b + [(_,b)] -> b []-> "Class" -- does not matter - (a,b):_ -> b + (_, b):_ -> b in iriPath $ getIRIVal $ Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst - cs -> let cs' = map (solveId subst) cs - in t{getComps = cs'} + cs -> let ts' = map (solveToken subst) $ getTokens t + cs' = map (solveId subst) cs + in t{getTokens = ts', getComps = cs'} + +solveToken :: GSubst -> Token -> Token +solveToken subst tok = + let tIRI = idToIRI $ mkId [tok] + k = let tSubsts = filter (\(x,_) -> x == tIRI) $ Map.keys subst + in case tSubsts of + [(_,b)] -> b + []-> "Class" -- does not matter + (_,b):_ -> b + idVal = iriPath $ getIRIVal $ Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst + in case getTokens idVal of + [y] -> y + _ -> error $ "expecting simple id but got a composed one: " ++ show idVal type PatternVarMap = Map.Map IRI (Bool, String) -- Bool is true for list- and false for non-list variables diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 5dbb9dd4bb..503ccf33d9 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -552,7 +552,7 @@ anaSpecAux conser addSyms optNodes lg --incl <- ginclusion lg (getSig nsig') (getSig nsig'') --let dg3 = insLink dg'' incl globalDef SeeTarget (getNode nsig') (getNode nsig'') -- trace ("sp':" ++ show sp' ++ " nsig'':" ++ show nsig'' ++ "dg3:"++ show (labEdges $ dgBody dg3)) $ - return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' + trace ("dg3:"++ show (length $ nodes $ dgBody dg3)) $ return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' else if la == 0 then error "arguments missing in instantiation" else if lp == 0 then error "pattern without arguments" else error "mismatch in length of arguments" @@ -1262,10 +1262,12 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c let intersMapKeys = Map.keys $ Map.intersection crtMapAux prevMap allMappedSameWay = foldl (\b k -> let v1 = Map.findWithDefault (error "not in crt") k crtMapAux v2 = Map.findWithDefault (error "not in prev") k prevMap - in (v1 == v2) && b ) True intersMapKeys - in if allMappedSameWay then Map.union prevMap crtMapAux - else - error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) + in if v1 == v2 then (v1 == v2) && b + else trace ("K:"++ show k ++ " v1:" ++ show v1 ++ " v2:" ++ show v2) $ (v1==v2) && b ) True intersMapKeys + in Map.union crtMapAux prevMap -- TODO: why does the check fail? + --if allMappedSameWay then Map.union prevMap crtMapAux + --else + -- error $ "trying to map previously mapped symbol:" ++ (show $ Map.intersection crtMapAux prevMap) mor <- -- trace ("\n=============\nssig:"++ show ssig ++ "\n tsig:" ++ show tsig ++ "\n prevMap:" ++ show prevMap ++ "\n substMap:" ++ show substMap) $ induced_from_to_morphism slid (Map.union crtMap substMap) ssig tsig let gmor = mkG_morphism slid mor @@ -1569,7 +1571,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- 1. generate a temporary param for the template node of the param list -- SingleParamInfo False n where the signature of the head of the param list is JustNode n let (par1, tailName) = case par0 of - SingleParamInfo _ _ -> error $ "expecting single argument: " ++ show par0 ++ " but got a list: " ++ show aspecs + SingleParamInfo _ _ -> error $ "\nexpecting single argument: " ++ show par0 ++ "\nbut got a list: " ++ show aspecs ListParamInfo _ _ (JustNode n) tn -> let x = case tn of Nothing -> nullIRI Just y -> y From e34303a47330e50af7db1b66c4bccf4e2900700e Mon Sep 17 00:00:00 2001 From: mcodescu Date: Sun, 3 May 2020 09:16:11 +0200 Subject: [PATCH 26/33] cleaned up tracing --- Static/AnalysisLibrary.hs | 4 ++-- Static/AnalysisStructured.hs | 39 +++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/Static/AnalysisLibrary.hs b/Static/AnalysisLibrary.hs index bf48ffd85a..eb4603653b 100644 --- a/Static/AnalysisLibrary.hs +++ b/Static/AnalysisLibrary.hs @@ -22,7 +22,7 @@ module Static.AnalysisLibrary , LNS ) where -import Debug.Trace +--import Debug.Trace import Logic.Logic import Logic.Grothendieck @@ -608,7 +608,7 @@ anaLibItem lg opts topLns currLn libenv dg eo itm = -- trace ("itm:" ++ show itm then liftR $ plain_error (itm, dg, libenv, lg, eo) (alreadyDefined spstr) r - else trace ("inserting:" ++ show entry) $ + else -- trace ("inserting:" ++ show entry) $ return (itm', dg3{globalEnv = Map.insert spn entry genv}, libenv, lg, eo) _ -> return (itm, dg, libenv, lg, eo) diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 503ccf33d9..b162ed634b 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -83,7 +83,7 @@ import Common.Lib.Graph import Static.ComputeTheory import Static.History -import Debug.Trace +--import Debug.Trace -- overrides CUIRIE expansion for Download_items type ExpOverrides = Map.Map IRI FilePath @@ -530,7 +530,7 @@ anaSpecAux conser addSyms optNodes lg anaSpecTop conser addSyms lg libEnv ln dg nsig name opts eo (item asp) rg return (Group (replaceAnnoted sp' asp) pos, nsig', dg') - Spec_inst spname' afitargs mImp pos0 -> trace ("\n\n**** ana spec inst *** " ++ show spname' ++ " afitargs:" ++ show afitargs) $ do + Spec_inst spname' afitargs mImp pos0 -> do -- trace ("\n\n**** ana spec inst *** " ++ show spname' ++ " afitargs:" ++ show afitargs) $ do spname <- expCurieR (globalAnnos dg) eo spname' let pos = if null afitargs then iriPos spname else pos0 adjustPos pos $ case lookupGlobalEnvDG spname dg of @@ -542,7 +542,7 @@ anaSpecAux conser addSyms optNodes lg (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs -- let body' = getBody bodySig (Logic cl) <- lookupCurrentLogic "anaGmaps" lg - (dg2, lastParamAndNewNodes, spB) <- trace ("calling instMacro on " ++ show spname' ++ "[" ++ show afitargs ++ "] subst:" ++ show subst ++ " gm':" ++ show gm') $ + (dg2, lastParamAndNewNodes, spB) <- -- trace ("calling instMacro on " ++ show spname' ++ "[" ++ show afitargs ++ "] subst:" ++ show subst ++ " gm':" ++ show gm') $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig (dgI, allPrevDefs) <- unionNodes lg dg2 (makeName $ mkIRI "TESTNAME") $ nub lastParamAndNewNodes --the body should extend the last argument @@ -552,7 +552,8 @@ anaSpecAux conser addSyms optNodes lg --incl <- ginclusion lg (getSig nsig') (getSig nsig'') --let dg3 = insLink dg'' incl globalDef SeeTarget (getNode nsig') (getNode nsig'') -- trace ("sp':" ++ show sp' ++ " nsig'':" ++ show nsig'' ++ "dg3:"++ show (labEdges $ dgBody dg3)) $ - trace ("dg3:"++ show (length $ nodes $ dgBody dg3)) $ return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' + -- trace ("dg3:"++ show (length $ nodes $ dgBody dg3)) $ + return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' else if la == 0 then error "arguments missing in instantiation" else if lp == 0 then error "pattern without arguments" else error "mismatch in length of arguments" @@ -1262,8 +1263,8 @@ anaFitArg lg libEnv ln dg spname nsigI nsigP@(NodeSig nP gsigmaP) opts name eo c let intersMapKeys = Map.keys $ Map.intersection crtMapAux prevMap allMappedSameWay = foldl (\b k -> let v1 = Map.findWithDefault (error "not in crt") k crtMapAux v2 = Map.findWithDefault (error "not in prev") k prevMap - in if v1 == v2 then (v1 == v2) && b - else trace ("K:"++ show k ++ " v1:" ++ show v1 ++ " v2:" ++ show v2) $ (v1==v2) && b ) True intersMapKeys + in (v1 == v2) && b + ) True intersMapKeys in Map.union crtMapAux prevMap -- TODO: why does the check fail? --if allMappedSameWay then Map.union prevMap crtMapAux --else @@ -1395,7 +1396,7 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi -- SingleParamInfo _ xs -> dgn_theory $ labDG dgP $ getNode xs -- _ -> error "nyi") $ map fst zipped')) $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do - (arg1, dg1, nsig1, subst1, gm1) <- trace ("\nFOLD\nsubst0:" ++ show subst0 ++ "\ngm0:" ++ show gm0) $ + (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("\nFOLD\nsubst0:" ++ show subst0 ++ "\ngm0:" ++ show gm0) $ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname subst0 gm0 par0 arg0 let nsig2 = case nsig1 of -- this is a trick to make lists work! EmptyNode _ -> nsig0 @@ -1466,7 +1467,7 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = --trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1477,7 +1478,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 Fit_spec asp gm r -> case item asp of NormalVariable i -> error "vars should be solved by now!" - UnsolvedName i rg -> trace ("solving an unsolved name in inst arg:" ++ show i) $ + UnsolvedName i rg -> -- trace ("solving an unsolved name in inst arg:" ++ show i) $ -- TODO: here we must also pass the parameter, so we can check its symbols -- 1. if i is the name of a spec entry in globalEnv -- solve to Spec_inst i [] Nothing nullRange @@ -1536,9 +1537,9 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 G_sign lid1 (ExtSign ctx _) _ -> do let ctxSyms = filter (\csym -> ((idToIRI $ sym_name lid1 csym) == i) && (symKind lid1 csym == symKind lid sym)) $ Set.toList $ symset_of lid1 ctx case ctxSyms of - [] -> trace ("err2:"++ show i) $ + [] -> -- trace ("err2:"++ show i) $ noCtxOrNoMatch - [ctxSym] -> trace "symbol in ctx" $ do + [ctxSym] -> do-- trace "symbol in ctx" $ do let arg1 = Fit_ctx (G_symbol lid sym) (G_symbol lid1 ctxSym) nullRange (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig @@ -1557,7 +1558,8 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 in Map.insert (sn, sk) (PlainVal tn) f) subst0 $ filter (\(x,_) -> not $ Set.member x isyms) $ Map.toList smap -- TODO: any compatibility checks must be done here - trace ("++++++ computed subst1:"++ show subst1) $ return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) + -- trace ("++++++ computed subst1:"++ show subst1) $ + return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> fail $ "multiple occurences of abbreviated name in the context:" ++ show ctxSyms _ -> fail "ambiguity in use of abbreviation notation, parameter has more than one symbol" _ -> fail $ "abbreviation notation can be used only for single ontology arguments, not for lists: " ++ show i ++ " par0:" ++ show par0 @@ -1634,9 +1636,9 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 return (arg0, dg2, JustNode argUnion, subst2, mgm0) -- 5. instantiate the body with the substitution and add a link from the united arguments to the body -- this should happen elsewhere! - _ -> trace ("itm:" ++ (show $ item arg0) ) $ + _ -> -- trace ("itm:" ++ (show $ item arg0) ) $ error "only unsolved names for now" - _ -> trace ("itm:" ++ (show $ item arg0)) $ + _ -> -- trace ("itm:" ++ (show $ item arg0)) $ error "only fit_spec for now" instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> @@ -1691,7 +1693,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr return (dg', ns ++ ns', as ++ [a']) ) (crtDG, [], []) asps -- trace ("ns1:" ++ show ns1) $ return (dg1, ns1, asp0{item = Union asps1 rg}) - Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0) ++ " crtSubst:" ++ show crtSubst) $ + Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0) ++ " crtSubst:" ++ show crtSubst) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv crtDG case snEntry of @@ -1705,7 +1707,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr esig' <- coercePlainSign ilid crtLid "coerceSign in anaPatternInstArgs" esig let emor = ide esig' return $ Just $ G_morphism crtLid emor startMorId - let solveVars aFitArg = trace ("solving:" ++ show aFitArg) $ + let solveVars aFitArg = -- trace ("solving:" ++ show aFitArg) $ case item aFitArg of Fit_spec asp1 gm rg -> case item asp1 of @@ -1721,7 +1723,8 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr UnsolvedName _ _ -> [item x] OntoList xs -> map item xs) aList in emptyAnno $ OntoList $ map emptyAnno allNames - in trace ("vars: " ++ show (concatMap fst res)) $ (concatMap fst res, aFitArg{item= Fit_spec (listToOntoList $ map snd res) gm rg}) + in -- trace ("vars: " ++ show (concatMap fst res)) $ + (concatMap fst res, aFitArg{item= Fit_spec (listToOntoList $ map snd res) gm rg}) UnsolvedName i _ -> if i == mkIRI "empty" then error "empty list as argument in instantiation of pattern nyi" else ([], aFitArg) @@ -1794,7 +1797,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm else return idImps -- TODO: old variant: extendWithSubst l idImps newVars - (afitargs', dg', nsig', subst', gm') <- trace ("~~~~~~~~~~~~~crtSubst:"++ show crtSubst ++ " gmor':" ++ show gmor') $ + (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~crtSubst:"++ show crtSubst ++ " gmor':" ++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 From 9516fc916053a14165a604597128fcfcc7289fc2 Mon Sep 17 00:00:00 2001 From: mcodescu Date: Sun, 3 May 2020 17:01:02 +0200 Subject: [PATCH 27/33] treat ending empty differently --- Static/AnalysisStructured.hs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index b162ed634b..5b290c743e 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -83,7 +83,7 @@ import Common.Lib.Graph import Static.ComputeTheory import Static.History ---import Debug.Trace +import Debug.Trace -- overrides CUIRIE expansion for Download_items type ExpOverrides = Map.Map IRI FilePath @@ -651,7 +651,7 @@ anaSpecAux conser addSyms optNodes lg let (cNodes', cEdges') = networkDiagram dg cItems eItems (ns, dg') <- insertColimitInGraph libEnv ln dg cNodes' cEdges' name return (sp, ns, dg') - UnsolvedName x pos -> -- this should not happen, but when it does, solve as spec_inst + UnsolvedName x pos -> -- this should not happen, but when it does, solve as spec_inst. anaSpecAux conser addSyms optNodes lg libEnv ln dg nsig name opts eo (Spec_inst x [] Nothing pos) rg _ -> fail $ "in AnalysisStructured: " ++ show (prettyLG lg sp) @@ -1583,21 +1583,25 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- use anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 -- it returns the analysed arg, the dgraph, justnode node of argument, substitution, generated morphism -- but update prevSig name spname subst0 mgm0 par0 arg0; fold the dgs; store the nodes of args + let emptyOntoName = mkIRI "empty" (aspecs', aNodes, subst1, dg1) <- - foldM (\(anaSpecs, specNodes, substI, aDg) crtSp -> do - (crtSp', aDg', argNode, aSubst, aMor) <- - anaPatternInstArg lg libEnv opts eo ln - aDg isig csig prevSig -- TODO: check that prevSig is fine here - name spname -- TODO: give proper names - subst0 - mgm0 -- TODO: check that this is ok - par1 $ - emptyAnno $ Fit_spec crtSp [] nullRange - -- trace ("\naMor:" ++ show aMor) $ - return (anaSpecs ++ [crtSp'], - specNodes ++ [argNode], - if substI == Map.empty then aSubst else substI, -- only interested in the first substitution. TODO: add it to mgm0? - aDg') + foldM (\(anaSpecs, specNodes, substI, aDg) crtSp -> trace ("crtSp:" ++ show crtSp) $ + case item crtSp of + UnsolvedName x _ | x == emptyOntoName -> trace "1" $ return (anaSpecs, specNodes, substI, aDg) + _ -> do + (crtSp', aDg', argNode, aSubst, aMor) <- + anaPatternInstArg lg libEnv opts eo ln + aDg isig csig prevSig -- TODO: check that prevSig is fine here + name spname -- TODO: give proper names + subst0 + mgm0 -- TODO: check that this is ok + par1 $ + emptyAnno $ Fit_spec crtSp [] nullRange + -- trace ("\naMor:" ++ show aMor) $ + return (anaSpecs ++ [crtSp'], + specNodes ++ [argNode], + if substI == Map.empty then aSubst else substI, -- only interested in the first substitution. TODO: add it to mgm0? + aDg') ) ([], [], Map.empty, dg0) aspecs -- 3. unite the resulting nodes (dg2, argUnion) <- unionNodes lg dg1 (makeName $ mkIRI "UnionNode") $ concatMap (\aN -> case aN of From c94c1a3bf3aba01f00cb9cb1df9604a98855d00f Mon Sep 17 00:00:00 2001 From: mcodescu Date: Wed, 13 May 2020 10:57:10 +0200 Subject: [PATCH 28/33] p[C][D] to p[C, D] --- Logic/Logic.hs | 14 +++++++----- Static/AnalysisStructured.hs | 42 +++++++++++++++++------------------ Syntax/Parse_AS_Structured.hs | 12 +++++----- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Logic/Logic.hs b/Logic/Logic.hs index 1bff70b91a..8575b2fe49 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -678,11 +678,13 @@ solveId subst t = []-> "Class" -- does not matter (_, b):_ -> b in iriPath $ getIRIVal $ Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst - cs -> let ts' = map (solveToken subst) $ getTokens t + cs -> let its' = map (solveToken subst) $ getTokens t + ts' = concatMap getTokens its' + cs'' = concat $ map getComps its' cs' = map (solveId subst) cs - in t{getTokens = ts', getComps = cs'} + in t{getTokens = ts', getComps = cs'' ++ cs'} -solveToken :: GSubst -> Token -> Token +solveToken :: GSubst -> Token -> Id solveToken subst tok = let tIRI = idToIRI $ mkId [tok] k = let tSubsts = filter (\(x,_) -> x == tIRI) $ Map.keys subst @@ -691,9 +693,9 @@ solveToken subst tok = []-> "Class" -- does not matter (_,b):_ -> b idVal = iriPath $ getIRIVal $ Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst - in case getTokens idVal of - [y] -> y - _ -> error $ "expecting simple id but got a composed one: " ++ show idVal + in idVal --case getComps idVal of + -- [] -> head $ getTokens idVal + -- _ -> error $ "expecting simple id but got a composed one: " ++ show idVal type PatternVarMap = Map.Map IRI (Bool, String) -- Bool is true for list- and false for non-list variables diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 5b290c743e..3655a9e81d 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -552,11 +552,11 @@ anaSpecAux conser addSyms optNodes lg --incl <- ginclusion lg (getSig nsig') (getSig nsig'') --let dg3 = insLink dg'' incl globalDef SeeTarget (getNode nsig') (getNode nsig'') -- trace ("sp':" ++ show sp' ++ " nsig'':" ++ show nsig'' ++ "dg3:"++ show (labEdges $ dgBody dg3)) $ - -- trace ("dg3:"++ show (length $ nodes $ dgBody dg3)) $ - return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' + trace ("dg3:"++ show (length $ nodes $ dgBody dg3) ++ " theorem links:" ++ show (length $ filter (\(_,_, dglab) -> isGlobalThm $ dgl_type dglab) $ labEdges $ dgBody dg3)) $ + return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' else if la == 0 then error "arguments missing in instantiation" else if lp == 0 then error "pattern without arguments" - else error "mismatch in length of arguments" + else error $ "mismatch in length of arguments:" ++ show la ++ " " ++ show lp -- 2. generate fitting morphisms and theorem links from the params to the nodes of fitargs -- 3. substitute vars in body using the fitting morphisms -> a structured DOL spec, Body -- here is also where the missing arguments induce rejections in the body @@ -1467,7 +1467,7 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = --trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1478,7 +1478,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 Fit_spec asp gm r -> case item asp of NormalVariable i -> error "vars should be solved by now!" - UnsolvedName i rg -> -- trace ("solving an unsolved name in inst arg:" ++ show i) $ + UnsolvedName i rg -> trace ("solving an unsolved name in inst arg:" ++ "tks:" ++ show (getTokens $ iriPath i) ++ " cmps:" ++ show (getComps $ iriPath i)) $ -- TODO: here we must also pass the parameter, so we can check its symbols -- 1. if i is the name of a spec entry in globalEnv -- solve to Spec_inst i [] Nothing nullRange @@ -1512,8 +1512,8 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 case Set.toList newDecls of [sym] -> do let noCtxOrNoMatch = do - let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (stringToId $ show $ instParamName subst0 i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. pNSig:" ++ show pNSig ++ " arg1:" ++ show arg1 ++ " mgm0:" ++ show mgm0) $ + let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (iriPath $ instParamName subst0 i))) nullRange + (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. pNSig:" ++ show pNSig ++ " arg1:" ++ show arg1 ++ " mgm0:" ++ show mgm0) $ anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 subst0 arg1 -- try: only extend previous morphism if the pattern is local! case gmor of @@ -1561,7 +1561,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- trace ("++++++ computed subst1:"++ show subst1) $ return (arg0{item=arg2}, dg1, JustNode nsigA, subst1, Just gmor) _ -> fail $ "multiple occurences of abbreviated name in the context:" ++ show ctxSyms - _ -> fail "ambiguity in use of abbreviation notation, parameter has more than one symbol" + _ -> fail $ "ambiguity in use of abbreviation notation, parameter has more than one symbol:" ++ show par0 _ -> fail $ "abbreviation notation can be used only for single ontology arguments, not for lists: " ++ show i ++ " par0:" ++ show par0 -- 2. if i is a symbol from the context (nsig) -- solve to context fit x |-> i @@ -1585,9 +1585,9 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- but update prevSig name spname subst0 mgm0 par0 arg0; fold the dgs; store the nodes of args let emptyOntoName = mkIRI "empty" (aspecs', aNodes, subst1, dg1) <- - foldM (\(anaSpecs, specNodes, substI, aDg) crtSp -> trace ("crtSp:" ++ show crtSp) $ + foldM (\(anaSpecs, specNodes, substI, aDg) crtSp -> -- trace ("crtSp:" ++ show crtSp) $ case item crtSp of - UnsolvedName x _ | x == emptyOntoName -> trace "1" $ return (anaSpecs, specNodes, substI, aDg) + UnsolvedName x _ | x == emptyOntoName -> return (anaSpecs, specNodes, substI, aDg) _ -> do (crtSp', aDg', argNode, aSubst, aMor) <- anaPatternInstArg lg libEnv opts eo ln @@ -1681,8 +1681,8 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr let lastNode = case crtNSig of JustNode x -> x _ -> error "no last param of a pattern, should not happend" - -- trace ("bsp':" ++ show bsp') $ - return (crtDG, [lastNode], asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) + trace ("bsp':" ++ show bsp') $ + return (crtDG, [lastNode], asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) Group asp1 rg -> do (dg2, ns2, asp2) <- instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM asp1 return (dg2, ns2, asp0{item = Group asp2 rg}) @@ -1697,7 +1697,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr return (dg', ns ++ ns', as ++ [a']) ) (crtDG, [], []) asps -- trace ("ns1:" ++ show ns1) $ return (dg1, ns1, asp0{item = Union asps1 rg}) - Spec_inst sn afitargs _ _ -> -- trace ("\n\nspec_inst:" ++ show (item asp0) ++ " crtSubst:" ++ show crtSubst) $ + Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0) ++ " crtSubst:" ++ show crtSubst) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv crtDG case snEntry of @@ -1711,7 +1711,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr esig' <- coercePlainSign ilid crtLid "coerceSign in anaPatternInstArgs" esig let emor = ide esig' return $ Just $ G_morphism crtLid emor startMorId - let solveVars aFitArg = -- trace ("solving:" ++ show aFitArg) $ + let solveVars aFitArg = trace ("solving:" ++ show aFitArg) $ case item aFitArg of Fit_spec asp1 gm rg -> case item asp1 of @@ -1732,7 +1732,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr UnsolvedName i _ -> if i == mkIRI "empty" then error "empty list as argument in instantiation of pattern nyi" else ([], aFitArg) - NormalVariable i -> -- trace ("normal variable: " ++ show i ++ " crtSubst:" ++ show crtSubst ++ " vars:" ++ show vars ++ " pMap:" ++ show pMap) $ + NormalVariable i -> trace ("normal variable: " ++ show i ++ " crtSubst:" ++ show crtSubst ++ " vars:" ++ show vars ++ " pMap:" ++ show pMap) $ if i `elem` Map.keys vars then let (b, k) = Map.findWithDefault (error "notPossible") i vars val = Map.findWithDefault (error "variable not mapped") (i,k) crtSubst @@ -1780,11 +1780,11 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr afitargs0 = map snd solved aitems = filter (\x -> case item x of Fit_spec y _ _ -> case item y of - UnsolvedName anIRI _ -> anIRI == mkIRI "empty" + UnsolvedName anIRI _ -> trace ("anIRI:" ++ show anIRI ) $ anIRI == mkIRI "empty" _ -> False _ -> False) afitargs0 newVars = concatMap fst solved - zipped = -- trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ + zipped = trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! -- TODO: if isLocal start with subst1 else start with empty subst? (Logic lid) <- lookupCurrentLogic "macro" lg @@ -1792,16 +1792,16 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr bsp = case convertTheory lid of Just f -> f th _ -> error "cannot convert theory" - if not $ null aitems then + if not $ null aitems then trace "1" $ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM $ asp0{item = Basic_spec (G_basic_spec lid bsp) nullRange} -- error "empty list as argument, not yet done" - else do - gmor' <- -- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ + else trace "2" $ do + gmor' <- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ if isLocal then case crtGM of Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm else return idImps -- TODO: old variant: extendWithSubst l idImps newVars - (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~crtSubst:"++ show crtSubst ++ " gmor':" ++ show gmor') $ + (afitargs', dg', nsig', subst', gm') <- trace ("~~~~~~~~~~~~~crtSubst:"++ show crtSubst ++ " gmor':" ++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index cf3167042f..6516362206 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -55,7 +55,7 @@ import Data.Char import Data.Maybe import Control.Monad --- import Debug.Trace +import Debug.Trace expandCurieM :: LogicGraph -> IRI -> GenParser Char st IRI expandCurieM lG i = @@ -559,18 +559,18 @@ fitArg l flag = do _ <- lookAhead $ try semiT <|> try cBracketT return $ Missing_arg nullRange fa <- annoParser emptyParam - return (fa, nullRange) + trace ("**** just scanned 1: " ++ show fa) $ return (fa, nullRange) <|> do -- b <- oBracketT fa <- annoParser $ fitString l flag -- c <- cBracketT - return (fa, nullRange) + trace ("**** just scanned 2: " ++ show fa) $ return (fa, nullRange) <|> do fa <- annoParser $ fittingArg l flag - return (fa, nullRange) + trace ("**** just scanned 3: " ++ show fa) $ return (fa, nullRange) <|> do s <- scanString - return (Annoted (Fit_string (mkIRI s) nullRange) nullRange [][], nullRange) + trace ("**** just scanned 4: " ++ s) $ return (Annoted (Fit_string (mkIRI s) nullRange) nullRange [][], nullRange) <|> do _b <- oBracketT (aspecs, _) <- separatedBy (iParser l flag) commaT @@ -581,7 +581,7 @@ iParser :: LogicGraph -> Bool -> AParser st (Annoted SPEC) iParser l flag = do i <- compoundIriCurie _ <- option () skip - return $ Annoted (UnsolvedName i nullRange) nullRange [][] + trace ("tks:" ++ show (getTokens $ iriPath i) ++ " cmps:" ++ show (getComps $ iriPath i)) $ return $ Annoted (UnsolvedName i nullRange) nullRange [][] <|> aSpec l flag fitString :: LogicGraph -> Bool -> AParser st FIT_ARG From d81a2ae0605c25b18cb28a35f62245c1de7b6ec6 Mon Sep 17 00:00:00 2001 From: mcodescu Date: Fri, 5 Jun 2020 18:10:18 +0200 Subject: [PATCH 29/33] instantiate param names in same or different --- Common/Id.hs | 6 +++--- Logic/Logic.hs | 2 +- OWL2/StaticAnalysis.hs | 19 ++++++++++--------- Static/AnalysisStructured.hs | 4 ++-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Common/Id.hs b/Common/Id.hs index cf9bfe577f..c7b290e58d 100644 --- a/Common/Id.hs +++ b/Common/Id.hs @@ -310,9 +310,9 @@ showSepList s f l = case l of -- | shows a compound list showIds :: [Id] -> ShowS -showIds is = noShow (null is) $ showString "[" - . showSepList (showString ",") showId is - . showString "]" +showIds is = noShow (null is) $ showString "_" + . showSepList (showString "_") showId is + . showString "" -- | shows an 'Id', puts final places behind a compound list showId :: Id -> ShowS diff --git a/Logic/Logic.hs b/Logic/Logic.hs index 8575b2fe49..b746423e5d 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -680,7 +680,7 @@ solveId subst t = in iriPath $ getIRIVal $ Map.findWithDefault (PlainVal tIRI) (tIRI,k) subst cs -> let its' = map (solveToken subst) $ getTokens t ts' = concatMap getTokens its' - cs'' = concat $ map getComps its' + cs'' = concatMap getComps its' cs' = map (solveId subst) cs in t{getTokens = ts', getComps = cs'' ++ cs'} diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 6043d534db..5a91fe3844 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -604,11 +604,12 @@ solveSymbols impSyms vMap (OntologyDocument pd (Ontology n is as fs)) = do varSyms = foldl Set.union Set.empty $ map (\(x, (f, k)) -> if f then Set.empty else Set.singleton $ Entity Nothing (getKind k) x) $ Map.toList vMap diffSyms = Set.difference usedSyms (Set.union declSyms $ Set.union impSyms varSyms) -- each used symbol must be declared, imported or variable - if Set.null diffSyms then - return $ OntologyDocument pd $ Ontology n is as fs' - else error $ "undeclared symbols in the body of the pattern. impSyms:" ++ show impSyms ++ - " declSyms:" ++ show declSyms ++ " usedSyms:" ++ show usedSyms ++ - " varSyms:" ++ show varSyms ++ " diffSyms:" ++ show diffSyms + -- TODO: this test should take into account imports. Commented out for now! + --if Set.null diffSyms then + return $ OntologyDocument pd $ Ontology n is as fs' + --else error $ "undeclared symbols in the body of the pattern. impSyms:" ++ show impSyms ++ + -- " declSyms:" ++ show declSyms ++ " usedSyms:" ++ show usedSyms ++ + -- " varSyms:" ++ show varSyms ++ " diffSyms:" ++ show diffSyms -- solving symbols for each frame, also keep track of declared and used symbols @@ -804,12 +805,12 @@ instantiateFrameBit subst var fbit = aopexps' <- mapM (instantiateObjectPropertyExpression subst var) aopexps return $ ListFrameBit mr $ ObjectBit aopexps' DataBit adpexps -> error $ show lfb - IndividualSameOrDifferent aindivs -> + IndividualSameOrDifferent aindivs -> trace ("subst:" ++ show subst ++ " var:" ++ show var ++ " fbit:" ++ show fbit) $ return $ ListFrameBit mr $ IndividualSameOrDifferent $ map (\(a,i) -> if (i,"Individual")`elem` Map.keys subst then let j = Map.findWithDefault (error "instantiateFrameBit") (i, "Individual") subst - in (a, getIRIVal j) - else (a,i)) aindivs + in (a, instParamName subst $ getIRIVal j) + else (a, instParamName subst $ i)) aindivs ObjectCharacteristics _achars -> return fbit DataPropRange _ -> return fbit IndividualFacts afacts -> do @@ -970,4 +971,4 @@ deleteSymbolsFrame delSyms f@(Frame ext fBits) = if Set.member i $ Set.map (\x -> idToIRI $ entityToId x) delSyms --TODO: handle lists then return [] else return [f] - _ -> error $ "nyi: " ++ show ext + _ -> return [f] -- error $ "nyi: " ++ show ext diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 3655a9e81d..57014a4368 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -1506,7 +1506,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 _ -> error $ "argument mismatch in instantiation. parameter: " ++ show par0 ++ "\n argument: " ++ show arg0 else do case par0 of - SingleParamInfo b pNSig -> + SingleParamInfo b pNSig -> trace "here" $ case getSig pNSig of G_sign lid (ExtSign _ newDecls) _ -> case Set.toList newDecls of @@ -1642,7 +1642,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- this should happen elsewhere! _ -> -- trace ("itm:" ++ (show $ item arg0) ) $ error "only unsolved names for now" - _ -> -- trace ("itm:" ++ (show $ item arg0)) $ + _ -> trace ("itm:" ++ (show $ item arg0)) $ error "only fit_spec for now" instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> From 6a6e137945e4edd6e79d8ac236a1292b62ad9c2b Mon Sep 17 00:00:00 2001 From: mcodescu Date: Tue, 9 Jun 2020 12:37:56 +0200 Subject: [PATCH 30/33] used individuals in same/different assertions --- OWL2/StaticAnalysis.hs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 5a91fe3844..2232155894 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -604,12 +604,12 @@ solveSymbols impSyms vMap (OntologyDocument pd (Ontology n is as fs)) = do varSyms = foldl Set.union Set.empty $ map (\(x, (f, k)) -> if f then Set.empty else Set.singleton $ Entity Nothing (getKind k) x) $ Map.toList vMap diffSyms = Set.difference usedSyms (Set.union declSyms $ Set.union impSyms varSyms) -- each used symbol must be declared, imported or variable - -- TODO: this test should take into account imports. Commented out for now! - --if Set.null diffSyms then - return $ OntologyDocument pd $ Ontology n is as fs' - --else error $ "undeclared symbols in the body of the pattern. impSyms:" ++ show impSyms ++ - -- " declSyms:" ++ show declSyms ++ " usedSyms:" ++ show usedSyms ++ - -- " varSyms:" ++ show varSyms ++ " diffSyms:" ++ show diffSyms + -- TODO: this test should take into account imports. Commented out for now because of structuring! + if Set.null diffSyms then + return $ OntologyDocument pd $ Ontology n is as fs' + else error $ "undeclared symbols in the body of the pattern. impSyms:" ++ show impSyms ++ + " declSyms:" ++ show declSyms ++ " usedSyms:" ++ show usedSyms ++ + " varSyms:" ++ show varSyms ++ " diffSyms:" ++ show diffSyms -- solving symbols for each frame, also keep track of declared and used symbols @@ -620,18 +620,18 @@ solveFrame impSyms vMap (Frame ext fBits) = do Misc _ -> (ext, Set.empty) ClassEntity (UnsolvedClass i) -> if i `elem` Map.keys vMap - then (ClassEntity $ VarExpression $ MVar False i, Set.empty) -- TODO: handle lists + then (ClassEntity $ VarExpression $ MVar False i, Set.empty) -- lists are not allowed in this position, so always simple class else if (Entity Nothing Class i) `elem` impSyms then (ClassEntity $ Expression i, Set.empty) -- add only if not member of impSyms else (ClassEntity $ Expression i, Set.singleton $ Entity Nothing Class i) - ClassEntity _ -> error $ show ext + ClassEntity _ -> error $ show ext -- no GCIs for now ObjectEntity (UnsolvedObjProp i) -> if i `elem` Map.keys vMap - then (ObjectEntity $ ObjectPropertyVar False i, Set.empty) -- TODO: handle lists + then (ObjectEntity $ ObjectPropertyVar False i, Set.empty) -- lists are not allowed in this position, always object properties else if (Entity Nothing ObjectProperty i) `elem` impSyms then (ObjectEntity $ ObjectProp i, Set.empty) -- add only if not member of impSyms else (ObjectEntity $ ObjectProp i, Set.singleton $ Entity Nothing ObjectProperty i) - ObjectEntity _ -> error $ show ext -- TODO: handle oexp + ObjectEntity _ -> error $ show ext -- no GCIs for now SimpleEntity (Entity l UnsolvedEntity i) -> if i `elem` Map.keys vMap then -- TODO: tests that it's an individual! @@ -662,7 +662,7 @@ solveFrameBit impSyms vMap fbit = -- trace ("fbit:" ++ show fbit) $ -- trace ("solved lft:" ++ show (ObjectBit aopes')) $ return (ListFrameBit mr $ ObjectBit aopes', used') DataBit adpes -> error "nyi" - IndividualSameOrDifferent ainds -> return (fbit, Set.empty) + IndividualSameOrDifferent ainds -> return (fbit, Set.fromList $ map (\ai -> Entity Nothing NamedIndividual $ snd ai) ainds) ObjectCharacteristics achars -> return (fbit, Set.empty) IndividualFacts afacts -> do let (afacts', used') = foldl (\(afs, usyms) (a, af) -> do From 2672d59e7b4634deba926f469d7aa1fd179aa9cc Mon Sep 17 00:00:00 2001 From: mcodescu Date: Tue, 9 Jun 2020 16:35:09 +0200 Subject: [PATCH 31/33] analysis of missing arguments, lists not yet handled right --- OWL2/StaticAnalysis.hs | 120 ++++++++++++++++++++++++++++++++--- Static/AnalysisStructured.hs | 2 +- 2 files changed, 112 insertions(+), 10 deletions(-) diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index 2232155894..cd46be3bed 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -703,7 +703,7 @@ solveClassExpression impSyms vMap (annos, cexp) = ObjectComplementOf cexp -> let ((_a, cexp'), u) = solveClassExpression impSyms vMap ([], cexp) in (ObjectComplementOf cexp', u) VarExpression _ -> error $ "should get a class expression but instead got " ++ show cexp - ObjectOneOf indivs -> error "nyi" + ObjectOneOf indivs -> (cexp, Set.fromList $ map (\i -> Entity Nothing NamedIndividual i) indivs) ObjectValuesFrom q opexp cexp -> let ((_, cexp'), u1) = solveClassExpression impSyms vMap ([], cexp) ((_, opexp'), u2) = solveObjPropExpression impSyms vMap ([], opexp) in (ObjectValuesFrom q opexp' cexp', Set.union u1 u2) @@ -739,8 +739,8 @@ solveObjPropExpression impSyms vMap (annos, opexp) = else (ObjectProp i, Set.singleton $ Entity Nothing ObjectProperty i) in ((annos, opexp'), used) -solveIndividual :: Set.Set Entity -> PatternVarMap -> (Annotations, IndExpression) -> ((Annotations, IndExpression), Set.Set Entity) -solveIndividual _ _ _ = error "nyi" +-- solveIndividual :: Set.Set Entity -> PatternVarMap -> (Annotations, IndExpression) -> ((Annotations, IndExpression), Set.Set Entity) +-- solveIndividual _ _ _ = error "nyi" -- TODO: -- write a method that solves a data property expression etc. @@ -853,7 +853,6 @@ instantiateAnno subst var anno = _ -> return anno --- TODO: this needs an aux that gives back a list of classexpressions! instantiateClassExpression :: GSubst -> PatternVarMap -> (Annotations, ClassExpression) -> Result (Annotations, ClassExpression) instantiateClassExpression subst var (annos, cexp) = case cexp of @@ -871,7 +870,7 @@ instantiateClassExpression subst var (annos, cexp) = ObjectComplementOf cexp0 -> do (_, cexp') <- instantiateClassExpression subst var ([], cexp0) return (annos, ObjectComplementOf cexp') - ObjectOneOf indivs -> error "nyi" + ObjectOneOf indivs -> error "nyi" -- TODO: instantiate individuals! ObjectValuesFrom q opexp cexp0 -> do (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) (_, cexp') <- instantiateClassExpression subst var ([], cexp0) @@ -967,8 +966,111 @@ deleteSymbolsFrames delSyms fs = do deleteSymbolsFrame :: Set.Set Entity -> Frame -> Result [Frame] deleteSymbolsFrame delSyms f@(Frame ext fBits) = case ext of - ClassEntity (VarExpression (MVar b i)) -> - if Set.member i $ Set.map (\x -> idToIRI $ entityToId x) delSyms --TODO: handle lists + ClassEntity (VarExpression (MVar b i)) -> -- lists can't occur here + if Set.member i $ Set.map (\x -> idToIRI $ entityToId x) delSyms then return [] - else return [f] - _ -> return [f] -- error $ "nyi: " ++ show ext + else do + fBits' <- mapM (deleteSymbolsFrameBit delSyms) fBits + return [Frame ext $ concat fBits'] + _ -> do + fBits' <- mapM (deleteSymbolsFrameBit delSyms) fBits + return [Frame ext $ concat fBits'] + +deleteSymbolsFrameBit :: Set.Set Entity -> FrameBit -> Result [FrameBit] +deleteSymbolsFrameBit delSyms fbit = + case fbit of + ListFrameBit mr lfb -> + case lfb of + ExpressionBit acexps -> do + let acexps' = filter (\ac -> classExpNoDeletedSymbol delSyms $ snd ac) acexps + case acexps' of + [] -> return [] + _ -> return [ListFrameBit mr $ ExpressionBit acexps'] + ObjectBit aopexps -> do + let aopexps' = filter (\ao -> objPropExpNoDeletedSymbol delSyms $ snd ao) aopexps + case aopexps' of + [] -> return [] + _ -> return [ListFrameBit mr $ ObjectBit aopexps'] + DataBit _adpexp -> return [] + IndividualSameOrDifferent ainds -> do + let ainds' = filter (\ai -> checkIRI delSyms NamedIndividual $ snd ai ) ainds + case ainds' of + [] -> return [] + _ -> return [ListFrameBit mr $ IndividualSameOrDifferent ainds'] + IndividualFacts afacts -> do + let afacts' = filter (\af -> factNoDelSym delSyms $ snd af) afacts + case afacts' of + [] -> return [] + _ -> return [ListFrameBit mr $ IndividualFacts afacts'] + _ -> return [fbit] + AnnFrameBit annos afb -> + case afb of + ClassDisjointUnion cexps -> do + let cexps' = filter (classExpNoDeletedSymbol delSyms) cexps + if length cexps' == length cexps then return [fbit] + else return [] + ClassHasKey opexps dpexps -> return [] -- TODO: no data properties yet + ObjectSubPropertyChain opexps -> do + let opexps' = filter (objPropExpNoDeletedSymbol delSyms) opexps + if length opexps' == length opexps then return [fbit] else return [] + _ -> return [fbit] + +factNoDelSym :: Set.Set Entity -> Fact -> Bool +factNoDelSym delSyms fact = + case fact of + ObjectPropertyFact _pn ope i -> + let + x = objPropExpNoDeletedSymbol delSyms ope + y = checkIRI delSyms NamedIndividual i + in x && y + _ -> False -- TODO: for now! + +checkIRI :: Set.Set Entity -> EntityType -> IRI -> Bool +checkIRI delSyms ck c = + case Set.toList $ Set.filter (\(Entity _ ik i) -> i == c && ik == ck) delSyms of + [] -> True + _ -> False + +classExpNoDeletedSymbol :: Set.Set Entity -> ClassExpression -> Bool +classExpNoDeletedSymbol delSyms cexp = trace ("checking:" ++ show cexp) $ + case cexp of + Expression c -> checkIRI delSyms Class c + UnsolvedClass c -> checkIRI delSyms Class c -- TODO: is this possible? + VarExpression (MVar b c) -> if b then True + else checkIRI delSyms Class c + ObjectJunction _jt cexps -> foldl (\a b -> a && b) True $ map (classExpNoDeletedSymbol delSyms) cexps + ObjectComplementOf cexp' -> classExpNoDeletedSymbol delSyms cexp' + ObjectOneOf inds -> foldl (\a b -> a && b) True $ map (checkIRI delSyms NamedIndividual) inds + ObjectValuesFrom _qt opexp cexp' -> + let + x = classExpNoDeletedSymbol delSyms cexp' + y = objPropExpNoDeletedSymbol delSyms opexp + in x && y + ObjectHasValue opexp ind -> + let + x = objPropExpNoDeletedSymbol delSyms opexp + y = checkIRI delSyms NamedIndividual ind + in x && y + ObjectHasSelf opexp -> + objPropExpNoDeletedSymbol delSyms opexp + ObjectCardinality (Cardinality _ _ opexp mcexp ) -> + let + x = case mcexp of + Nothing -> True + Just cexp' -> classExpNoDeletedSymbol delSyms cexp' + y = objPropExpNoDeletedSymbol delSyms opexp + in x && y + {- TODO: add these after handling dpexp! + | DataValuesFrom QuantifierType DataPropertyExpression DataRange + | DataHasValue DataPropertyExpression Literal + | DataCardinality (Cardinality DataPropertyExpression DataRange) -} + _ -> False + +objPropExpNoDeletedSymbol :: Set.Set Entity -> ObjectPropertyExpression -> Bool +objPropExpNoDeletedSymbol delSyms opexp = + case opexp of + ObjectProp op -> checkIRI delSyms ObjectProperty op + ObjectInverseOf opexp' -> objPropExpNoDeletedSymbol delSyms opexp' + ObjectPropertyVar b i -> if b then True else checkIRI delSyms ObjectProperty i + UnsolvedObjProp i -> checkIRI delSyms ObjectProperty i + diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 57014a4368..bfdd97abf6 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -1452,7 +1452,7 @@ removeMissingOptionalSymbols lg libEnv ln missingNodes vMap bodySig = do Basic_spec (G_basic_spec blid bsp) rg -> do let delSyms' = map (coerceSymbol lid blid) delSyms bsp' <- delete_symbols_macro blid (Set.fromList delSyms') bsp - return $ Basic_spec (G_basic_spec blid bsp') rg + trace ("\n\n\nspec after deleting symbols: " ++ show bsp') $ return $ Basic_spec (G_basic_spec blid bsp') rg _ -> error "only basic specs for now" vMap' = foldl (\f s -> Map.delete ( idToIRI $ sym_name lid s) f) vMap delSyms bodySig' <- case getBody bodySig of From 80c0fd0622abfc16e0baa4bef5bf8b8358dc777a Mon Sep 17 00:00:00 2001 From: mcodescu Date: Fri, 2 Oct 2020 17:17:01 +0200 Subject: [PATCH 32/33] made skale submission and book chapter go through --- Common/Id.hs | 6 +- Logic/Logic.hs | 4 ++ OWL2/Logic_OWL2.hs | 1 + OWL2/StaticAnalysis.hs | 57 +++++++++++---- Static/AnalysisStructured.hs | 132 +++++++++++++++++++++++++++------- Syntax/Parse_AS_Structured.hs | 49 ++++++++++--- 6 files changed, 197 insertions(+), 52 deletions(-) diff --git a/Common/Id.hs b/Common/Id.hs index c7b290e58d..cf9bfe577f 100644 --- a/Common/Id.hs +++ b/Common/Id.hs @@ -310,9 +310,9 @@ showSepList s f l = case l of -- | shows a compound list showIds :: [Id] -> ShowS -showIds is = noShow (null is) $ showString "_" - . showSepList (showString "_") showId is - . showString "" +showIds is = noShow (null is) $ showString "[" + . showSepList (showString ",") showId is + . showString "]" -- | shows an 'Id', puts final places behind a compound list showId :: Id -> ShowS diff --git a/Logic/Logic.hs b/Logic/Logic.hs index b746423e5d..2a74886bfa 100644 --- a/Logic/Logic.hs +++ b/Logic/Logic.hs @@ -494,6 +494,10 @@ class ( Syntax lid basic_spec symbol symb_items symb_map_items convertTheory :: lid -> Maybe ((sign, [Named sentence]) -> basic_spec) convertTheory _ = Nothing + -- | convert a pair of symbols to symb_map_items + convertSymbols :: lid -> Maybe(symbol -> symbol -> symb_map_items) + convertSymbols _ = Nothing + {- ----------------------- amalgamation --------------------------- Computation of colimits of signature diagram. Indeed, it suffices to compute a cocone that is weakly amalgamable diff --git a/OWL2/Logic_OWL2.hs b/OWL2/Logic_OWL2.hs index 8495a446a1..1c0d4e009b 100644 --- a/OWL2/Logic_OWL2.hs +++ b/OWL2/Logic_OWL2.hs @@ -159,6 +159,7 @@ instance StaticAnalysis OWL2 OntologyDocument Axiom stat_symb_items OWL2 s = return . statSymbItems s stat_symb_map_items OWL2 = statSymbMapItems convertTheory OWL2 = Just convertBasicTheory + convertSymbols OWL2 = Just convertEntities empty_signature OWL2 = emptySign signature_union OWL2 = uniteSign intersection OWL2 = intersectSign diff --git a/OWL2/StaticAnalysis.hs b/OWL2/StaticAnalysis.hs index cd46be3bed..54a1cfc4de 100644 --- a/OWL2/StaticAnalysis.hs +++ b/OWL2/StaticAnalysis.hs @@ -605,11 +605,11 @@ solveSymbols impSyms vMap (OntologyDocument pd (Ontology n is as fs)) = do diffSyms = Set.difference usedSyms (Set.union declSyms $ Set.union impSyms varSyms) -- each used symbol must be declared, imported or variable -- TODO: this test should take into account imports. Commented out for now because of structuring! - if Set.null diffSyms then - return $ OntologyDocument pd $ Ontology n is as fs' - else error $ "undeclared symbols in the body of the pattern. impSyms:" ++ show impSyms ++ - " declSyms:" ++ show declSyms ++ " usedSyms:" ++ show usedSyms ++ - " varSyms:" ++ show varSyms ++ " diffSyms:" ++ show diffSyms + --if Set.null diffSyms then + return $ OntologyDocument pd $ Ontology n is as fs' + --else error $ "undeclared symbols in the body of the pattern. impSyms:" ++ show impSyms ++ + -- " declSyms:" ++ show declSyms ++ " usedSyms:" ++ show usedSyms ++ + -- " varSyms:" ++ show varSyms ++ " diffSyms:" ++ show diffSyms -- solving symbols for each frame, also keep track of declared and used symbols @@ -788,7 +788,7 @@ instantiateFrame subst var (Frame ext fBits) = do let j = Map.findWithDefault (error "instantiateFrame") (i, "Individual") subst return $ SimpleEntity $ Entity Nothing NamedIndividual $ getIRIVal j else fail $ "unknown individual variable: " ++ show i - Misc _ -> error $ show ext + Misc _ -> return ext -- TODO: check if ok! error $ show ext fBits' <- mapM (instantiateFrameBit subst var) fBits return $ Frame ext' fBits' @@ -807,10 +807,18 @@ instantiateFrameBit subst var fbit = DataBit adpexps -> error $ show lfb IndividualSameOrDifferent aindivs -> trace ("subst:" ++ show subst ++ " var:" ++ show var ++ " fbit:" ++ show fbit) $ return $ ListFrameBit mr $ IndividualSameOrDifferent $ - map (\(a,i) -> if (i,"Individual")`elem` Map.keys subst then - let j = Map.findWithDefault (error "instantiateFrameBit") (i, "Individual") subst - in (a, instParamName subst $ getIRIVal j) - else (a, instParamName subst $ i)) aindivs + concatMap (\(a,i) -> if (i,"Individual")`elem` Map.keys subst then + let j = Map.findWithDefault (error "instantiateFrameBit, individual") (i, "Individual") subst + in [(a, instParamName subst $ getIRIVal j)] + else if (i,"list") `elem` Map.keys subst then + let j = Map.findWithDefault (error "instantiateFrameBit, list") (i, "list") subst + in case j of + ListVal k inds -> + if (k == "Individual") || null inds then + map (\x -> (a,x)) inds + else error $ "expected a list of individuals but got a list of "++ show k ++"s instead" + _ -> error $ "expected a list of individuals but got " ++ show j + else [(a, instParamName subst $ i)]) aindivs --TODO: this is wrong and needs to be fixed, we get a list and this should be concatenated ObjectCharacteristics _achars -> return fbit DataPropRange _ -> return fbit IndividualFacts afacts -> do @@ -870,7 +878,9 @@ instantiateClassExpression subst var (annos, cexp) = ObjectComplementOf cexp0 -> do (_, cexp') <- instantiateClassExpression subst var ([], cexp0) return (annos, ObjectComplementOf cexp') - ObjectOneOf indivs -> error "nyi" -- TODO: instantiate individuals! + ObjectOneOf indivs -> do + indivs' <- mapM (instIndiv subst var) indivs + return (annos, ObjectOneOf $ concat indivs') -- TODO: instantiate individuals! ObjectValuesFrom q opexp cexp0 -> do (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) (_, cexp') <- instantiateClassExpression subst var ([], cexp0) @@ -919,7 +929,9 @@ instClassExprAux subst var (annos, cexp) = ObjectComplementOf cexp0 -> do (_, cexp') <- instantiateClassExpression subst var ([], cexp0) return [(annos, ObjectComplementOf cexp')] - ObjectOneOf indivs -> error "nyi" + ObjectOneOf indivs -> do + indivs' <- mapM (instIndiv subst var) indivs + return [(annos, ObjectOneOf $ concat indivs')] ObjectValuesFrom q opexp cexp0 -> do (_, opexp') <- instantiateObjectPropertyExpression subst var ([], opexp) (_, cexp') <- instantiateClassExpression subst var ([], cexp0) @@ -952,6 +964,22 @@ instantiateObjectPropertyExpression subst var (annos, obexp) = return (annos, ObjectProp v) UnsolvedObjProp _ -> error $ "unsolved object property at instantiation: " ++ show obexp +instIndiv :: GSubst -> PatternVarMap -> IRI -> Result [IRI] +instIndiv subst var i = + if (i, "Individual") `elem` Map.keys subst then + let j = Map.findWithDefault (error "instIndiv") (i, "Individual") subst + in case j of + PlainVal v -> return [v] + _ -> error $ "expected plain value but got " ++ show j + else if (i, "list") `elem` Map.keys subst then + let j = Map.findWithDefault (error "instIndiv") (i, "list") subst + in case j of + ListVal k vs -> trace ("j:" ++ show j) $ if k == "Individual" then return vs else + if null vs then return vs else error $ "expected a list of individuals but got a list of "++ show k ++ "s instead" + _ -> error "plain value when expecting a list" + else return [instParamName subst i] + + -- delete symbols from a solved macro, for optional parameters deleteSymbolsMacro :: Set.Set Entity -> OntologyDocument -> Result OntologyDocument deleteSymbolsMacro delSyms (OntologyDocument pd (Ontology n is as fs)) = do @@ -1074,3 +1102,8 @@ objPropExpNoDeletedSymbol delSyms opexp = ObjectPropertyVar b i -> if b then True else checkIRI delSyms ObjectProperty i UnsolvedObjProp i -> checkIRI delSyms ObjectProperty i +convertEntities :: Entity -> Entity -> SymbMapItems +convertEntities sym1 sym2 = + if entityKind sym1 == entityKind sym2 then + SymbMapItems (EntityType $ entityKind sym1) [(cutIRI sym1, Just $ cutIRI sym2)] + else error $ "kind mismatch:" ++ show sym1 ++ " " ++ show sym2 diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index bfdd97abf6..20414d9d71 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -530,7 +530,7 @@ anaSpecAux conser addSyms optNodes lg anaSpecTop conser addSyms lg libEnv ln dg nsig name opts eo (item asp) rg return (Group (replaceAnnoted sp' asp) pos, nsig', dg') - Spec_inst spname' afitargs mImp pos0 -> do -- trace ("\n\n**** ana spec inst *** " ++ show spname' ++ " afitargs:" ++ show afitargs) $ do + Spec_inst spname' afitargs mImp pos0 -> trace ("\n\n**** ana spec inst *** " ++ show spname' ++ " afitargs:" ++ show afitargs) $ do spname <- expCurieR (globalAnnos dg) eo spname' let pos = if null afitargs then iriPos spname else pos0 adjustPos pos $ case lookupGlobalEnvDG spname dg of @@ -542,7 +542,7 @@ anaSpecAux conser addSyms optNodes lg (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs -- let body' = getBody bodySig (Logic cl) <- lookupCurrentLogic "anaGmaps" lg - (dg2, lastParamAndNewNodes, spB) <- -- trace ("calling instMacro on " ++ show spname' ++ "[" ++ show afitargs ++ "] subst:" ++ show subst ++ " gm':" ++ show gm') $ + (dg2, lastParamAndNewNodes, spB) <- --trace ("calling instMacro on " ++ show spname' ++ "[" ++ show afitargs ++ "] subst:" ++ show subst ++ " gm':" ++ show gm') $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig (dgI, allPrevDefs) <- unionNodes lg dg2 (makeName $ mkIRI "TESTNAME") $ nub lastParamAndNewNodes --the body should extend the last argument @@ -552,7 +552,8 @@ anaSpecAux conser addSyms optNodes lg --incl <- ginclusion lg (getSig nsig') (getSig nsig'') --let dg3 = insLink dg'' incl globalDef SeeTarget (getNode nsig') (getNode nsig'') -- trace ("sp':" ++ show sp' ++ " nsig'':" ++ show nsig'' ++ "dg3:"++ show (labEdges $ dgBody dg3)) $ - trace ("dg3:"++ show (length $ nodes $ dgBody dg3) ++ " theorem links:" ++ show (length $ filter (\(_,_, dglab) -> isGlobalThm $ dgl_type dglab) $ labEdges $ dgBody dg3)) $ + trace ("dg3:"++ show (length $ nodes $ dgBody dg3) ++ " theorem links:" ++ show (length $ filter (\(_,_, dglab) -> isGlobalThm $ dgl_type dglab) $ labEdges $ dgBody dg3)) $ + trace ("afitargs':" ++ show afitargs') $ return (Spec_inst spname' afitargs' mImp pos0, nsig'', dg3) -- was nsig'' else if la == 0 then error "arguments missing in instantiation" else if lp == 0 then error "pattern without arguments" @@ -1447,13 +1448,41 @@ removeMissingOptionalSymbols lg libEnv ln missingNodes vMap bodySig = do let delSyms = concatMap (\n -> let gs = getSig n in case gs of G_sign slid (ExtSign _ syms) _ -> map (\x -> coerceSymbol slid lid x) $ Set.toList syms ) missingNodes + + delSymsName = map (\x -> idToIRI $ sym_name lid x) delSyms removeSymbolsFromSpec sp = case sp of Basic_spec (G_basic_spec blid bsp) rg -> do let delSyms' = map (coerceSymbol lid blid) delSyms bsp' <- delete_symbols_macro blid (Set.fromList delSyms') bsp - trace ("\n\n\nspec after deleting symbols: " ++ show bsp') $ return $ Basic_spec (G_basic_spec blid bsp') rg - _ -> error "only basic specs for now" + -- trace ("\n\n\nspec after deleting symbols: " ++ show bsp') $ + return $ Basic_spec (G_basic_spec blid bsp') rg + Spec_inst _ fArgs _ _ -> trace ("delSyms:" ++ show delSyms ++ "\nsp:" ++ show sp) $ do + let missingArgs = filter (\fa -> case item fa of + Fit_spec fs _ _ -> + case item fs of + NormalVariable x -> x `elem` delSymsName + _ -> False -- TODO: should we cover other cases too? + _ -> False) fArgs + case missingArgs of + [] -> return sp + _ -> do + let esig = empty_signature lid + bsp = case convertTheory lid of + Nothing -> error "can't convert empty theory to basic spec" + Just f -> f (esig, []) + return $ Basic_spec (G_basic_spec lid bsp) nullRange + Extension asps rg -> do + asps' <- mapM (\asp -> do + sp' <- removeSymbolsFromSpec $ item asp + return $ asp{item = sp'}) asps + return $ Extension asps' rg + Union asps rg -> do + asps' <- mapM (\asp -> do + sp' <- removeSymbolsFromSpec $ item asp + return $ asp{item = sp'}) asps + return $ Union asps' rg + _ -> error $ "only some specs for now:" ++ show sp vMap' = foldl (\f s -> Map.delete ( idToIRI $ sym_name lid s) f) vMap delSyms bodySig' <- case getBody bodySig of Spec_pattern asp -> do @@ -1467,7 +1496,7 @@ anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibN -> MaybeNode -> MaybeNode -> MaybeNode -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = --trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1478,7 +1507,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 Fit_spec asp gm r -> case item asp of NormalVariable i -> error "vars should be solved by now!" - UnsolvedName i rg -> trace ("solving an unsolved name in inst arg:" ++ "tks:" ++ show (getTokens $ iriPath i) ++ " cmps:" ++ show (getComps $ iriPath i)) $ + UnsolvedName i rg -> -- trace ("solving an unsolved name in inst arg:" ++ "tks:" ++ show (getTokens $ iriPath i) ++ " cmps:" ++ show (getComps $ iriPath i)) $ -- TODO: here we must also pass the parameter, so we can check its symbols -- 1. if i is the name of a spec entry in globalEnv -- solve to Spec_inst i [] Nothing nullRange @@ -1506,14 +1535,14 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 _ -> error $ "argument mismatch in instantiation. parameter: " ++ show par0 ++ "\n argument: " ++ show arg0 else do case par0 of - SingleParamInfo b pNSig -> trace "here" $ + SingleParamInfo b pNSig -> case getSig pNSig of G_sign lid (ExtSign _ newDecls) _ -> case Set.toList newDecls of [sym] -> do let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (iriPath $ instParamName subst0 i))) nullRange - (arg2, dg1, (gmor, nsigA)) <- trace ("================ calling anaFitArg. pNSig:" ++ show pNSig ++ " arg1:" ++ show arg1 ++ " mgm0:" ++ show mgm0) $ + (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. pNSig:" ++ show pNSig ++ " arg1:" ++ show arg1 ++ " mgm0:" ++ show mgm0) $ anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 subst0 arg1 -- try: only extend previous morphism if the pattern is local! case gmor of @@ -1569,7 +1598,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- where x is the unique symbol declared in the param -- 3. otherwise, i is a new symbol of same kind as x -- and the substitution maps x to i - OntoList aspecs -> do + OntoList aspecs -> trace ("asp:" ++ show asp) $ do -- 1. generate a temporary param for the template node of the param list -- SingleParamInfo False n where the signature of the head of the param list is JustNode n let (par1, tailName) = case par0 of @@ -1585,7 +1614,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- but update prevSig name spname subst0 mgm0 par0 arg0; fold the dgs; store the nodes of args let emptyOntoName = mkIRI "empty" (aspecs', aNodes, subst1, dg1) <- - foldM (\(anaSpecs, specNodes, substI, aDg) crtSp -> -- trace ("crtSp:" ++ show crtSp) $ + foldM (\(anaSpecs, specNodes, substI, aDg) crtSp -> trace ("crtSp:" ++ show crtSp) $ case item crtSp of UnsolvedName x _ | x == emptyOntoName -> return (anaSpecs, specNodes, substI, aDg) _ -> do @@ -1636,14 +1665,65 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 in Map.insert (tailName, "list") (ListVal k iriList) subst1 _ -> subst1 - -- trace ("subst1:" ++ show subst1 ++ "subst2:" ++ show subst2 ++ "\nmgm0:" ++ show mgm0) $ - return (arg0, dg2, JustNode argUnion, subst2, mgm0) + l <- lookupCurrentLogic "fit string" lg + mgm1 <- extendWithSubst l mgm0 $ concatMap (\((x,k), y) -> case y of + PlainVal z -> [((x,k), (z,k))] + _ -> []) $ Map.toList subst2 + trace ("subst1:" ++ show subst1 ++ "subst2:" ++ show subst2 ++ "\nmgm0:" ++ show mgm1 ++ "\narg0:" ++ show arg0 ++ "\naspecs':" ++ show aspecs') $ + return (arg0, dg2, JustNode argUnion, subst2, mgm0) -- 5. instantiate the body with the substitution and add a link from the united arguments to the body - -- this should happen elsewhere! - _ -> -- trace ("itm:" ++ (show $ item arg0) ) $ + -- this should happen elsewhere! + Spec_inst spn afitargs miri rg -> do + -- 0. instantiate arguments using the subst + let afitargs' = map (\a -> case item a of + Fit_spec annosp gmaps aRg -> + case item annosp of + UnsolvedName n _ -> a {item = Fit_spec (emptyAnno $ UnsolvedName (instParamName subst0 n) nullRange) gmaps aRg} + NormalVariable x -> + let xKeys = filter (\(k, _) -> k == x) $ Map.keys subst0 + in case xKeys of + [] -> error $ "variable not found:" ++ show x + (y,k):_ -> let val = Map.findWithDefault (error "not possible") (y,k) subst0 + in case val of + PlainVal v -> a{item = Fit_spec (emptyAnno $ UnsolvedName v nullRange) gmaps aRg} + _ -> error "only plainval for now" + _ -> a + _ -> a) afitargs + -- 1. analyze the spec + (newSp, newNode, dg1) <- anaSpecAux None False False lg -- TODO: check if False False is right + libEnv ln dg0 isig name opts eo (Spec_inst spn afitargs' miri rg) nullRange --TODO: check if isig is right + -- 2. generate morphism from sig of par0 to the sig of the instantiated spec + (Logic crtLid) <- lookupCurrentLogic "spec_inst" lg + (parsig, pnode) <- case par0 of + SingleParamInfo _ ns -> return (getSig ns, ns) + _ -> error "expected single param info" + let tarsig = getSig newNode + ssig <- case parsig of + G_sign plid psig _ -> coerceSign plid crtLid "coercesign 1" psig + tsig <- case tarsig of + G_sign tlid tsig _ -> coerceSign tlid crtLid "coercesign 2" tsig + mor <- induced_from_to_morphism crtLid Map.empty ssig tsig + -- 3. add the morphism to the dgraph as a theorem link + let dg2 = insLink dg1 (gEmbed $ mkG_morphism crtLid mor) globalThm DGImpliesLink (getNode pnode) $ getNode newNode + -- 4. the subst should be extended with the symbol map of the morphism + morAsMap = symmap_of crtLid mor + isyms = case isig of + EmptyNode _ -> Set.empty + JustNode inode -> case getSig inode of + G_sign ilid (ExtSign sigI _) _ -> + Set.map (coerceSymbol ilid crtLid) $ symset_of ilid sigI + subst1 = foldl (\f (ssym, tsym) -> + let (sn, sk) = (idToIRI $ sym_name crtLid ssym, symKind crtLid ssym) + tn = idToIRI $ sym_name crtLid tsym + in Map.insert (sn, sk) (PlainVal tn) f) + subst0 $ filter (\(x,_) -> not $ Set.member x isyms) $ Map.toList morAsMap + -- 5. return (the argument, the last dgraph, JustNode the node of the spec, the subst, ?the morphism?) + return (arg0, dg2, JustNode newNode, subst1, Just $ mkG_morphism crtLid mor) + -- error $ "subst0:" ++ show subst0 ++ " after:" ++ show subst1 + _ -> trace ("itm:" ++ (show $ item arg0) ++ "\npar0:" ++ show par0) $ error "only unsolved names for now" _ -> trace ("itm:" ++ (show $ item arg0)) $ - error "only fit_spec for now" + error "only fit_spec for now" instantiateMacro :: LogicGraph -> LibEnv ->HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode @@ -1681,8 +1761,8 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr let lastNode = case crtNSig of JustNode x -> x _ -> error "no last param of a pattern, should not happend" - trace ("bsp':" ++ show bsp') $ - return (crtDG, [lastNode], asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) + --trace ("bsp':" ++ show bsp') $ + return (crtDG, [lastNode], asp0{item = Basic_spec (G_basic_spec lid bsp') rg}) Group asp1 rg -> do (dg2, ns2, asp2) <- instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM asp1 return (dg2, ns2, asp0{item = Group asp2 rg}) @@ -1697,7 +1777,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr return (dg', ns ++ ns', as ++ [a']) ) (crtDG, [], []) asps -- trace ("ns1:" ++ show ns1) $ return (dg1, ns1, asp0{item = Union asps1 rg}) - Spec_inst sn afitargs _ _ -> trace ("\n\nspec_inst:" ++ show (item asp0) ++ " crtSubst:" ++ show crtSubst) $ + Spec_inst sn afitargs _ _ -> trace ("\n\nin instmacro spec_inst:" ++ show (item asp0) ++ " crtSubst:" ++ show crtSubst) $ do -- here afitargs must be instantiated if they are variables!!! let snEntry = Map.findWithDefault (error $ "unknown pattern:" ++ show sn) sn $ globalEnv crtDG case snEntry of @@ -1711,7 +1791,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr esig' <- coercePlainSign ilid crtLid "coerceSign in anaPatternInstArgs" esig let emor = ide esig' return $ Just $ G_morphism crtLid emor startMorId - let solveVars aFitArg = trace ("solving:" ++ show aFitArg) $ + let solveVars aFitArg = --trace ("solving:" ++ show aFitArg) $ case item aFitArg of Fit_spec asp1 gm rg -> case item asp1 of @@ -1732,7 +1812,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr UnsolvedName i _ -> if i == mkIRI "empty" then error "empty list as argument in instantiation of pattern nyi" else ([], aFitArg) - NormalVariable i -> trace ("normal variable: " ++ show i ++ " crtSubst:" ++ show crtSubst ++ " vars:" ++ show vars ++ " pMap:" ++ show pMap) $ + NormalVariable i -> -- trace ("normal variable: " ++ show i ++ " crtSubst:" ++ show crtSubst ++ " vars:" ++ show vars ++ " pMap:" ++ show pMap) $ if i `elem` Map.keys vars then let (b, k) = Map.findWithDefault (error "notPossible") i vars val = Map.findWithDefault (error "variable not mapped") (i,k) crtSubst @@ -1784,7 +1864,7 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr _ -> False _ -> False) afitargs0 newVars = concatMap fst solved - zipped = trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ + zipped = --trace ("~~~~~~~~~~~~~newVars:"++ show newVars) $ zip pParams afitargs0 -- TODO: allow optionals in locals!!!! -- TODO: if isLocal start with subst1 else start with empty subst? (Logic lid) <- lookupCurrentLogic "macro" lg @@ -1792,16 +1872,16 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr bsp = case convertTheory lid of Just f -> f th _ -> error "cannot convert theory" - if not $ null aitems then trace "1" $ + if not $ null aitems then instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars crtGM $ asp0{item = Basic_spec (G_basic_spec lid bsp) nullRange} -- error "empty list as argument, not yet done" - else trace "2" $ do - gmor' <- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ + else do + gmor' <- -- trace ("~~~~~~~~~~~~~zipped:"++ show zipped) $ if isLocal then case crtGM of Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm else return idImps -- TODO: old variant: extendWithSubst l idImps newVars - (afitargs', dg', nsig', subst', gm') <- trace ("~~~~~~~~~~~~~crtSubst:"++ show crtSubst ++ " gmor':" ++ show gmor') $ + (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~crtSubst:"++ show crtSubst ++ " gmor':" ++ show gmor') $ foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 diff --git a/Syntax/Parse_AS_Structured.hs b/Syntax/Parse_AS_Structured.hs index 6516362206..d7e5bbbcd0 100644 --- a/Syntax/Parse_AS_Structured.hs +++ b/Syntax/Parse_AS_Structured.hs @@ -525,7 +525,7 @@ groupSpecAux withImport l flag = do c <- cBraceT return $ Group a $ catRange [b, c] <|> do - n <- hetIRI l + n <- hetIRI l -- TODO: here we should try to parse a compoundIRI and if that fails, try to parse a spec inst! {- (f, ps) <- fitArgs l flag mi <- if withImport then optionMaybe (hetIRI l) else return Nothing case f of @@ -559,18 +559,22 @@ fitArg l flag = do _ <- lookAhead $ try semiT <|> try cBracketT return $ Missing_arg nullRange fa <- annoParser emptyParam - trace ("**** just scanned 1: " ++ show fa) $ return (fa, nullRange) - <|> do + -- trace ("**** just scanned 1: " ++ show fa) $ + return (fa, nullRange) + <|> do -- b <- oBracketT fa <- annoParser $ fitString l flag -- c <- cBracketT - trace ("**** just scanned 2: " ++ show fa) $ return (fa, nullRange) - <|> do + -- trace ("**** just scanned 2: " ++ show fa) $ + return (fa, nullRange) + <|> do fa <- annoParser $ fittingArg l flag - trace ("**** just scanned 3: " ++ show fa) $ return (fa, nullRange) - <|> do + -- trace ("**** just scanned 3: " ++ show fa) $ + return (fa, nullRange) + <|> do s <- scanString - trace ("**** just scanned 4: " ++ s) $ return (Annoted (Fit_string (mkIRI s) nullRange) nullRange [][], nullRange) + -- trace ("**** just scanned 4: " ++ s) $ + return (Annoted (Fit_string (mkIRI s) nullRange) nullRange [][], nullRange) <|> do _b <- oBracketT (aspecs, _) <- separatedBy (iParser l flag) commaT @@ -579,9 +583,32 @@ fitArg l flag = do iParser :: LogicGraph -> Bool -> AParser st (Annoted SPEC) iParser l flag = do - i <- compoundIriCurie - _ <- option () skip - trace ("tks:" ++ show (getTokens $ iriPath i) ++ " cmps:" ++ show (getComps $ iriPath i)) $ return $ Annoted (UnsolvedName i nullRange) nullRange [][] + i <- hetIRI l --compoundIriCurie + asp <- option (Annoted (UnsolvedName i nullRange) nullRange [][]) $ + do + b <- oBracketT -- after a bracket, check if there's a separator. If there's a comma, it's a compound id. If theres a ;, it's a spec_inst. + fstI <- compoundIriCurie --hetIRI l + asp' <- + do + _ <- commaT + (ts, ps) <- mixId ([],[]) ([],[]) `separatedBy` commaT + c <- cBracketT + _ <- option () skip + return $ Annoted (UnsolvedName (i {iriPath = addComponents (iriPath i) ((iriPath fstI):ts, toRange b ps c)}) nullRange) nullRange [] [] + <|> do + _ <- semiT + (fas, _) <- (fitArg l flag) `separatedBy` semiT + let (f, _ps) = unzip fas + _c <- cBracketT + _ <- option () skip + let fstArg = Annoted (Fit_spec (Annoted (UnsolvedName fstI nullRange) nullRange [][]) [] nullRange) nullRange [][] + inst = Spec_inst i (fstArg:f) Nothing nullRange + return $ Annoted inst nullRange [] [] + <|> do + _ <- cBracketT + return $ Annoted (UnsolvedName i{iriPath = addComponents (iriPath i) ([iriPath fstI], nullRange)} nullRange) nullRange [][] + return asp' + return asp <|> aSpec l flag fitString :: LogicGraph -> Bool -> AParser st FIT_ARG From fcdf704ba248d2f972d7d573d4091d89a2d84a32 Mon Sep 17 00:00:00 2001 From: mcodescu Date: Sat, 5 Dec 2020 10:53:41 +0100 Subject: [PATCH 33/33] allow missing args in instantiations in pattern bodies too --- Static/AnalysisStructured.hs | 101 ++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 36 deletions(-) diff --git a/Static/AnalysisStructured.hs b/Static/AnalysisStructured.hs index 20414d9d71..da83d3fb64 100644 --- a/Static/AnalysisStructured.hs +++ b/Static/AnalysisStructured.hs @@ -539,15 +539,17 @@ anaSpecAux conser addSyms optNodes lg case (length afitargs, length params) of (la, lp) -> do if (la == lp) && la > 0 then do - (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs + crtL@(Logic cl) <- lookupCurrentLogic "anaGmaps" lg + (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst) <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig Nothing (EmptyNode crtL) afitargs -- no previous morphism and sig, therefore nothing -- let body' = getBody bodySig - (Logic cl) <- lookupCurrentLogic "anaGmaps" lg (dg2, lastParamAndNewNodes, spB) <- --trace ("calling instMacro on " ++ show spname' ++ "[" ++ show afitargs ++ "] subst:" ++ show subst ++ " gm':" ++ show gm') $ instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname subst vMap' gm' bodySig (dgI, allPrevDefs) <- unionNodes lg dg2 (makeName $ mkIRI "TESTNAME") $ nub lastParamAndNewNodes --the body should extend the last argument (sp', nsig'', dg3) <- -- trace ("spB:" ++ show spB) $ - anaSpecTop conser addSyms lg libEnv ln dgI (JustNode allPrevDefs) (makeName $ addSuffixToIRI "_source" $ getName name) opts eo (item spB) nullRange + anaSpecTop conser addSyms lg libEnv ln dgI (JustNode allPrevDefs) + name -- (makeName $ addSuffixToIRI "_source" $ getName name) + opts eo (item spB) nullRange -- TODO: nsig' should be the node of instantiateMacro!!! --incl <- ginclusion lg (getSig nsig') (getSig nsig'') --let dg3 = insLink dg'' incl globalDef SeeTarget (getNode nsig') (getNode nsig'') @@ -1361,9 +1363,10 @@ anaAllFitArgs lg libEnv opts eo ln dg nsig name spname anaPatternInstArgs :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode - -> NodeName -> IRI -> PatternSig -> [Annoted FIT_ARG] + -> NodeName -> IRI -> PatternSig -> Maybe G_morphism -> MaybeNode -> [Annoted FIT_ARG] -> Result ([Annoted FIT_ARG], PatternSig, DGraph, NodeSig, Maybe G_morphism, GSubst) -anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSig local imps params vMap body) afitargs = do +anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname + psig@(PatternSig local imps params vMap body) mgm crtNode afitargs = trace ("in anaPatternInstArgs name:" ++ show name) $ do l@(Logic crtLid) <- lookupCurrentLogic "anaPatternInstArgs" lg -- before the arguments are analysed, we have to go through their list -- and check if any Missing_arg nullRange appears @@ -1380,53 +1383,68 @@ anaPatternInstArgs lg libEnv opts eo ln dg isig csig name spname psig@(PatternSi esig' <- coercePlainSign ilid crtLid "coerceSign in anaPatternInstArgs" esig let emor = ide esig' return $ Just $ G_morphism crtLid emor startMorId + let startMor = case mgm of + Nothing -> idImps + Just _ -> mgm (missingNodes, zipped', _, dgP) <- - foldM (\(ns, ls, lastParam, dg0) (p,a) -> + foldM (\(ns, ls, lastParam, dg0) ((p,a), i) -> case item a of Missing_arg _ -> -- trace ("p:" ++ show p) $ case p of SingleParamInfo True parSig -> return (ns ++ [parSig], ls, lastParam, dg0) _ -> fail $ "unexpected missing argument for non-optional parameter:" ++ show p _ -> do --TODO: remove missing symbols only! if there were missing arguments before! - (dg1, newParam, p') <- removeMissingSymbolsParam lg libEnv ln dg0 lastParam ns p + (dg1, newParam, p') <- removeMissingSymbolsParam lg libEnv ln dg0 lastParam ns name i p -- don't return p, add a new node in the DG extending the previous argument that removes all symbols and sentences from dgn_theory p -- that include symbols from ns return $ (ns, ls ++ [(p',a)], newParam, dg1) ) - ([], [], isig, dg) zipped + ([], [], isig, dg) $ zip zipped [1..] (afitargs', dg', nsig', subst, gm') <- --trace ("zipped':" ++ (show $ map (\x -> case x of -- SingleParamInfo _ xs -> dgn_theory $ labDG dgP $ getNode xs -- _ -> error "nyi") $ map fst zipped')) $ - foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do + foldM (\(args0, dg0, nsig0, subst0, gm0) ((par0, arg0), idx ) -> do (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("\nFOLD\nsubst0:" ++ show subst0 ++ "\ngm0:" ++ show gm0) $ - anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname subst0 gm0 par0 arg0 + anaPatternInstArg lg libEnv opts eo ln dg0 isig csig nsig0 name spname idx + subst0 gm0 par0 arg0 let nsig2 = case nsig1 of -- this is a trick to make lists work! EmptyNode _ -> nsig0 _ -> nsig1 return (args0 ++ [arg1], dg1, nsig2, subst1, gm1)) - ([], dgP, EmptyNode l, Map.empty, idImps) $ zipped' + ([], dgP, crtNode, Map.empty, startMor) $ zip zipped' [1..] let lastParamSig = case nsig' of EmptyNode _ -> error "should not happen" JustNode x -> x case missingNodes of [] -> return (afitargs', psig, dg', lastParamSig, gm', subst) _ -> do - (vMap', body') <- removeMissingOptionalSymbols lg libEnv ln missingNodes vMap body + (vMap', body') <- removeMissingOptionalSymbolsBody lg libEnv ln missingNodes vMap body return (afitargs', PatternSig local imps (map fst zipped') vMap' body', dg', lastParamSig, gm', subst) -removeMissingSymbolsParam :: LogicGraph -> LibEnv -> LibName -> DGraph -> MaybeNode -> [NodeSig] -> PatternParamInfo -> Result (DGraph, MaybeNode, PatternParamInfo) -removeMissingSymbolsParam lg libEnv ln dg lastParam ns p = do +removeMissingSymbolsParam :: LogicGraph -> LibEnv -> LibName -> DGraph -> MaybeNode -> + [NodeSig] -> NodeName -> Int -> PatternParamInfo -> + Result (DGraph, MaybeNode, PatternParamInfo) +removeMissingSymbolsParam lg libEnv ln dg lastParam ns name idx p = do + -- shortcut: if ns is empty, return (dg, JustNode psig, p) case p of - SingleParamInfo optFlag psig -> do - let gth = dgn_theory $ labDG dg $ getNode psig - case gth of - G_theory lid syn (ExtSign sig nIsyms) sid sens tid -> do + SingleParamInfo optFlag psig -> + if null ns then return (dg, JustNode psig, p) + else do + let gth = dgn_theory $ labDG dg $ getNode psig + case gth of + G_theory lid syn (ExtSign sig nIsyms) sid sens tid -> do let delSyms = concatMap (\n -> let gs = getSig n in case gs of - G_sign slid (ExtSign _ syms) _ -> map (\x -> coerceSymbol slid lid x) $ Set.toList syms ) ns + G_sign slid (ExtSign _ syms) _ -> + map (\x -> coerceSymbol slid lid x) $ Set.toList syms + ) ns mor <- cogenerated_sign lid (Set.fromList delSyms) sig - let sens' = OMap.fromList $ filter (\(_, y) -> Set.null $ Set.intersection (Set.fromList delSyms) $ Set.fromList $ symsOfSen lid sig $ sentence y) $ OMap.toList sens + let sens' = OMap.fromList $ + filter (\(_, y) -> Set.null $ Set.intersection + (Set.fromList delSyms) $ + Set.fromList $ symsOfSen lid sig $ sentence y) + $ OMap.toList sens gth' = G_theory lid syn (ExtSign (dom mor) nIsyms) sid sens' tid - newNode = newInfoNodeLab (makeName $ mkIRI "NewParam") + newNode = newInfoNodeLab (incBy idx $ extName "Formal" name) (newNodeInfo DGFormalParams) gth' newNodeNr = getNewNodeDG dg dg' = changesDGH dg [InsertNode (newNodeNr, newNode)] @@ -1441,9 +1459,9 @@ removeMissingSymbolsParam lg libEnv ln dg lastParam ns p = do return (dg'', JustNode newParNode, SingleParamInfo optFlag newParNode) _ -> return (dg, lastParam, p) -- don't remove from lists yet -removeMissingOptionalSymbols :: LogicGraph -> LibEnv -> LibName -> [NodeSig] -> PatternVarMap -> LocalOrSpecSig +removeMissingOptionalSymbolsBody :: LogicGraph -> LibEnv -> LibName -> [NodeSig] -> PatternVarMap -> LocalOrSpecSig -> Result (PatternVarMap, LocalOrSpecSig) -removeMissingOptionalSymbols lg libEnv ln missingNodes vMap bodySig = do +removeMissingOptionalSymbolsBody lg libEnv ln missingNodes vMap bodySig = do Logic lid <- lookupCurrentLogic "removeMissingOptionalSymbols" lg let delSyms = concatMap (\n -> let gs = getSig n in case gs of @@ -1488,15 +1506,15 @@ removeMissingOptionalSymbols lg libEnv ln missingNodes vMap bodySig = do Spec_pattern asp -> do sp' <- removeSymbolsFromSpec $ item asp return $ SpecSig $ Spec_pattern $ asp{item = sp'} - Local_pattern locals asp -> error "2" + Local_pattern locals asp -> trace ("body:" ++ show bodySig) $ error "2" -- trace ("body:" ++ show bodySig ++" body':"++ show bodySig') $ return (vMap', bodySig') anaPatternInstArg :: LogicGraph -> LibEnv -> HetcatsOpts -> ExpOverrides -> LibName -> DGraph -> MaybeNode -> MaybeNode -> MaybeNode - -> NodeName -> IRI -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG + -> NodeName -> IRI -> Int -> GSubst -> Maybe G_morphism -> PatternParamInfo -> Annoted FIT_ARG -> Result (Annoted FIT_ARG, DGraph, MaybeNode, GSubst, Maybe G_morphism) -anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 mgm0 par0 arg0 = --trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ +anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname idx subst0 mgm0 par0 arg0 = --trace ("***** arg0 in argInst:" ++ show arg0 ++ " subst0:" ++ show subst0 ++ " par0 in argInst:" ++ show par0) $ case item arg0 of Fit_string s r -> case par0 of @@ -1518,7 +1536,8 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 let arg1 = Fit_spec (emptyAnno $ Spec_inst i [] Nothing nullRange) [] nullRange l <- lookupCurrentLogic "fit string" lg -- empty node was isig in next line - (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pSig opts (extName "Arg" name) eo csig prevSig mgm0 subst0 arg1 + (arg2, dg1, (gmor, nsigA)) <- anaFitArg lg libEnv ln dg0 spname isig pSig opts (incBy idx $ extName "Arg" name) + eo csig prevSig mgm0 subst0 arg1 case gmor of G_morphism lid mor _ -> do -- trace ("arg2:"++ show arg2 ++ " gmor:" ++ show gmor ++ " nsigA:"++ show nsigA) $ do let smap = symmap_of lid mor @@ -1543,7 +1562,8 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 let noCtxOrNoMatch = do let arg1 = Fit_new (G_symbol lid sym) (G_symbol lid (rename_symbol lid sym (iriPath $ instParamName subst0 i))) nullRange (arg2, dg1, (gmor, nsigA)) <- -- trace ("================ calling anaFitArg. pNSig:" ++ show pNSig ++ " arg1:" ++ show arg1 ++ " mgm0:" ++ show mgm0) $ - anaFitArg lg libEnv ln dg0 spname isig pNSig opts (extName "Arg" name) eo csig prevSig mgm0 subst0 arg1 + anaFitArg lg libEnv ln dg0 spname isig pNSig opts (incBy idx $ extName "Arg" name) + eo csig prevSig mgm0 subst0 arg1 -- try: only extend previous morphism if the pattern is local! case gmor of G_morphism glid mor _ -> do -- trace ("gmor:" ++ show gmor) $ do @@ -1614,14 +1634,14 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 -- but update prevSig name spname subst0 mgm0 par0 arg0; fold the dgs; store the nodes of args let emptyOntoName = mkIRI "empty" (aspecs', aNodes, subst1, dg1) <- - foldM (\(anaSpecs, specNodes, substI, aDg) crtSp -> trace ("crtSp:" ++ show crtSp) $ + foldM (\(anaSpecs, specNodes, substI, aDg) (crtSp, idx) -> trace ("crtSp:" ++ show crtSp) $ case item crtSp of UnsolvedName x _ | x == emptyOntoName -> return (anaSpecs, specNodes, substI, aDg) _ -> do (crtSp', aDg', argNode, aSubst, aMor) <- anaPatternInstArg lg libEnv opts eo ln aDg isig csig prevSig -- TODO: check that prevSig is fine here - name spname -- TODO: give proper names + name spname idx -- TODO: give proper names subst0 mgm0 -- TODO: check that this is ok par1 $ @@ -1631,7 +1651,7 @@ anaPatternInstArg lg libEnv opts eo ln dg0 isig csig prevSig name spname subst0 specNodes ++ [argNode], if substI == Map.empty then aSubst else substI, -- only interested in the first substitution. TODO: add it to mgm0? aDg') - ) ([], [], Map.empty, dg0) aspecs + ) ([], [], Map.empty, dg0) $ zip aspecs [1..] -- 3. unite the resulting nodes (dg2, argUnion) <- unionNodes lg dg1 (makeName $ mkIRI "UnionNode") $ concatMap (\aN -> case aN of JustNode x -> [x] @@ -1881,19 +1901,28 @@ instMacroAux lg libEnv opts eo ln crtDG imp crtNSig name spname crtSubst vars cr Nothing -> extendWithSubst l idImps newVars Just agm -> return $ Just agm else return idImps -- TODO: old variant: extendWithSubst l idImps newVars - (afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~crtSubst:"++ show crtSubst ++ " gmor':" ++ show gmor') $ - foldM (\(args0, dg0, nsig0, subst0, gm0) (par0, arg0) -> do + -- TODO: by using anaPatternInstArg instead of anaPatternInstArgs we lose missing arguments! + -- can we replace it? + -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + {-(afitargs', dg', nsig', subst', gm') <- -- trace ("~~~~~~~~~~~~~crtSubst:"++ show crtSubst ++ " gmor':" ++ show gmor') $ + foldM (\(args0, dg0, nsig0, subst0, gm0) ((par0, arg0), idx) -> do (arg1, dg1, nsig1, subst1, gm1) <- -- trace ("subst0 in spec_inst:" ++ show subst0 ++ " nsig0:" ++ show nsig0) $ anaPatternInstArg lg libEnv opts eo ln dg0 imp (EmptyNode l) nsig0 -- TODO: context is always empty now - name spname subst0 gm0 par0 arg0 + name spname idx subst0 gm0 par0 arg0 --trace ("$$$after analysis nsig':" ++ show nsig1 ++ " gm1:" ++ show gm1) $ return (args0 ++ [arg1], dg1, nsig1, subst1, gm1)) ([], crtDG, crtNSig, crtSubst, gmor') -- the last argument node should not be EmptyNode, but the target of gmor'. Try with nsig? - zipped + $ zip zipped [1..]-} + (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', gm', subst') <- + anaPatternInstArgs lg libEnv opts eo ln crtDG imp (EmptyNode l) name spname patSig gmor' crtNSig afitargs0 -- (afitargs', patSig'@(PatternSig _local _imp' _params' vMap' bodySig), dg', nsig', subst') <- anaPatternInstArgs lg libEnv opts eo ln dg imp nsig name spname patSig afitargs --trace ("\n\n\n recursive call:" ++ show subst' ++ " nsig':" ++ show nsig') $ - instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' crtSubst) vars gm' pBody + -- TODO: revert below if above + -- instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' crtSubst) vars gm' pBody + instantiateMacro lg libEnv opts eo ln dg' imp (JustNode nsig') name spname + (Map.union subst' crtSubst) (Map.union vMap' vars) -- TODO: is this fine or add vars? + gm' bodySig -- instantiateMacro lg libEnv opts eo ln dg' imp nsig' name spname (Map.union subst' subst) vars gm' pBody -- error $ "spec_inst:" ++ show sn ++ " args:" ++ show afitargs ++ " vars:" ++ show vars ++ " subst:" ++ show subst -- 1. afitargs should give raise to signature morphisms from the nodes of the params to the nodes of the args