Overload.hs revision e0030988dd4c3abcfc90f643873d6ee44c899cc0
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian MaederModule : $Header$
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian MaederDescription : Overload resolution
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian MaederCopyright : (c) Martin Kuehl, T. Mossakowski, C. Maeder, 2004-2007
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian MaederLicense : GPLv2 or higher, see LICENSE.txt
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian MaederMaintainer : Christian.Maeder@dfki.de
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian MaederStability : provisional
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian MaederPortability : portable
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian MaederOverload resolution (injections are inserted separately)
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder Follows Sect. III:3.3 of the CASL Reference Manual.
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder The algorthim is from:
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder Till Mossakowski, Kolyang, Bernd Krieg-Brueckner:
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder Static semantic analysis and theorem proving for CASL.
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder 12th Workshop on Algebraic Development Techniques, Tarquinia 1997,
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder LNCS 1376, p. 333-348
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder ( minExpFORMULA
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder , minExpFORMULAeq
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder , isUnambiguous
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder , minimalSupers
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder , maximalSubs
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder , haveCommonSupersorts
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder , haveCommonSubsorts
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder , keepMinimals1
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder , keepMinimals
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maederimport CASL.ToDoc (FormExtension)
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maederimport qualified Common.Lib.MapSet as MapSet
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maederimport qualified Common.Lib.Rel as Rel
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maederimport qualified Data.Map as Map
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maederimport qualified Data.Set as Set
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder-- | the type of the type checking function of extensions
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maedertype Min f e = Sign f e -> f -> Result f
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian MaedermkSorted :: TermExtension f => Sign f e -> TERM f -> SORT -> Range -> TERM f
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian MaedermkSorted sign t s r = let nt = Sorted_term t s r in case optTermSort t of
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder Nothing -> nt
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder Just srt -> if leqSort sign s srt then t else nt
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder{- ----------------------------------------------------------
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder - Minimal expansion of a formula -
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder Expand a given formula by typing information.
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder * For non-atomic formulae, recurse through subsentences.
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder * For trival atomic formulae, no expansion is neccessary.
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder * For atomic formulae, the following cases are implemented:
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder + Predication is handled by the dedicated expansion function
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder 'minExpFORMULApred'.
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder + Existl_equation and Strong_equation are handled by the dedicated
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder expansion function 'minExpFORMULAeq'.
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder + Definedness is handled by expanding the subterm.
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder + Membership is handled like Cast
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder---------------------------------------------------------- -}
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder :: (FormExtension f, TermExtension f)
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder => Min f e -> Sign f e -> FORMULA f -> Result (FORMULA f)
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian MaederminExpFORMULA mef sign formula = let sign0 = sign { envDiags = [] } in
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder case formula of
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder Quantification q vars f pos -> do
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder -- add 'vars' to signature
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder let sign' = execState (mapM_ addVars vars) sign0
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder Result (envDiags sign') $ Just ()
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder f' <- minExpFORMULA mef sign' f
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder return (Quantification q vars f' pos)
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder Junction j fs pos -> do
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder fs' <- mapR (minExpFORMULA mef sign) fs
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder return (Junction j fs' pos)
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder Relation f1 c f2 pos ->
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder joinResultWith (\ f1' f2' -> Relation f1' c f2' pos)
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder (minExpFORMULA mef sign f1) $ minExpFORMULA mef sign f2
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder Negation f pos -> do
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder f' <- minExpFORMULA mef sign f
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder return (Negation f' pos)
38824a7dba4f7d82532afec67e0b594a5af5d76bChristian Maeder Predication (Pred_name ide) terms pos
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder -> minExpFORMULApred mef sign ide Nothing terms pos
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder Predication (Qual_pred_name ide ty pos1) terms pos2
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder -> minExpFORMULApred mef sign ide (Just $ toPredType ty)
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder terms (pos1 `appRange` pos2)
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder Equation term1 e term2 pos
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder -> minExpFORMULAeq mef sign (`Equation` e) term1 term2 pos
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder Definedness term pos -> do
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder t <- oneExpTerm mef sign term
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder return (Definedness t pos)
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder Membership term srt pos -> do
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder ts <- minExpTerm mef sign term
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder let fs = map (concatMap ( \ t ->
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder Membership (mkSorted sign t c pos) srt pos)
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder $ maybe [srt] (minimalSupers sign srt)
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder $ optTermSort t)) ts
7fe976d9f9c4af1aa7636c568d9919859523de0aChristian Maeder isUnambiguous "" (globAnnos sign) formula fs pos
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder ExtFORMULA f -> fmap ExtFORMULA $ mef sign f
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder QuantOp o ty f -> do
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder let sign' = sign0 { opMap = addOpTo o (toOpType ty) $ opMap sign0 }
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder Result (envDiags sign') $ Just ()
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder f' <- minExpFORMULA mef sign' f
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder return $ QuantOp o ty f'
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder QuantPred p ty f -> do
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder let pm = predMap sign0
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder sign' = sign0
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder { predMap = MapSet.insert p (toPredType ty) pm }
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder Result (envDiags sign') $ Just ()
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder f' <- minExpFORMULA mef sign' f
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder return $ QuantPred p ty f'
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder Mixfix_formula term -> do
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder t <- oneExpTerm mef sign term
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder let Result ds mt = adjustPos (getRangeSpan term) $ termToFormula t
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder appendDiags ds
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder Nothing -> mkError "not a formula" term
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder Just f -> return f
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder _ -> return formula -- do not fail even for unresolved cases
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder-- | test if a term can be uniquely resolved
13140d161d2d2d11d87283d01d57ee3a738a833dChristian MaederoneExpTerm :: (FormExtension f, TermExtension f)
preds' = Set.filter ((nargs ==) . length . predArgs) $
MapSet.lookup ide $ predMap sign
$ Rel.leqClasses (leqP' sign) preds'
Just ty -> [[ty] | Set.member ty preds']
minExpTermVar sign tok ms = case Map.lookup tok $ varMap sign of
ops' = Set.filter ( \ o -> length (opArgs o) == nargs) $
MapSet.lookup ide $ opMap sign
$ Rel.leqClasses (leqF' sign) ops'
Just ty -> [[ty] | Set.member ty ops' ||
Set.member (mkTotal ty) ops' ]
if Set.member s2 l1 then if b then [s2] else [s1] else
if Set.member s1 l2 then if b then [s1] else [s2] else
else Set.intersection (subsortsOf s1 sign)
b = Set.member s1 l2 in
if Set.member s2 l1 then