Overload.hs revision 0cf33146cad74eb01c5617581a3f569967ce3cb1
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht Module : $Header$
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht Copyright : (c) Martin Kuehl, T. Mossakowski, C. Maeder, Uni Bremen 2004
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht Licence : similar to LGPL, see HetCATS/LICENCE.txt or LIZENZ.txt
da4b55f4795a4b585f513eaceb67cda10485febfChristian Maeder Maintainer : hets@tzi.de
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht Stability : provisional
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht Portability : portable
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht Overload resolution
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht Follows Sect. III:3.3 of the CASL Reference Manual.
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht The algorthim is from:
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht Till Mossakowski, Kolyang, Bernd Krieg-Brueckner:
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht Static semantic analysis and theorem proving for CASL.
4bf72807172000becf65e11bd225efc1dfd99713Simon Ulbricht 12th Workshop on Algebraic Development Techniques, Tarquinia 1997,
94968509d2764786208bd34b59a93c7cbe3aa6dbSimon Ulbricht LNCS 1376, p. 333-348
846ef0914b29a4806ca0444c116fd3cf267c4fb7Christian Maederimport qualified Common.Lib.Map as Map
ce07f3639c04fc3457da387c0dfd9ec01dbf05c4Christian Maederimport qualified Common.Lib.Set as Set
e4d1479434761dc3eb8d17b6c75de4eb24866f0bSimon Ulbrichtimport qualified Common.AS_Annotation as Named
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbrichtimport qualified Common.Id as Id
846ef0914b29a4806ca0444c116fd3cf267c4fb7Christian Maeder - calculate global information from signature and pass it through
e4d1479434761dc3eb8d17b6c75de4eb24866f0bSimon Ulbricht - recurse through sentences until atomic sentences are reached
e4d1479434761dc3eb8d17b6c75de4eb24866f0bSimon Ulbricht - calculate expansions of atomic sentences
036b2c3b35fbb0ad74a7490d6d23de606e88a841Simon Ulbricht * generalize 'is_unambiguous' (see 'choose') and make it available globally
036b2c3b35fbb0ad74a7490d6d23de606e88a841Simon Ulbricht * replace 'case' statements w/ pattern matching where possible
fcc4b0f2dadf063ebb8022737cb6e40fb9c4baa8Simon Ulbricht * generalize minExpFORMULA_pred/minExpTerm_op/minExpTerm_cond
fcc4b0f2dadf063ebb8022737cb6e40fb9c4baa8Simon Ulbricht * utilize Sets instead of Lists
59fa2ed5a4936e7e56f7164d8a274df68dd4160cSimon Ulbricht * generalize expansion for Qual_var and Sorted_term
fcc4b0f2dadf063ebb8022737cb6e40fb9c4baa8Simon Ulbricht * move structural logic to higher levels, as e.g. in
fcc4b0f2dadf063ebb8022737cb6e40fb9c4baa8Simon Ulbricht qualifyOps = map (map qualify_op) -- map should go somewhere above
fcc4b0f2dadf063ebb8022737cb6e40fb9c4baa8Simon Ulbricht * sweep op-like logic from minExpTerm_cond where it is unneeded
fe6a19b07759bc4190e88dda76a211d86bf32062Simon Ulbricht * generalize zipped_all
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht * generalize qualifyTerms or implement locally - too much structural force
59fa2ed5a4936e7e56f7164d8a274df68dd4160cSimon Ulbricht * gemeralize minimize_* or implement locally - needed only in one f. each
59fa2ed5a4936e7e56f7164d8a274df68dd4160cSimon Ulbricht * use more let/in constructs (instead of where) to simulate workflow
ce07f3639c04fc3457da387c0dfd9ec01dbf05c4Christian Maeder To find more items or pitfalls, search this file for:
036b2c3b35fbb0ad74a7490d6d23de606e88a841Simon Ulbricht-- | the type of the type checking function
036b2c3b35fbb0ad74a7490d6d23de606e88a841Simon Ulbrichttype Min f e = GlobalAnnos -> Sign f e -> f -> Result f
8221f726d2e0ca6f0df32ef8f88063b7a85b1cfeSimon Ulbricht{-----------------------------------------------------------
8221f726d2e0ca6f0df32ef8f88063b7a85b1cfeSimon Ulbricht - Overload Resolution -
8221f726d2e0ca6f0df32ef8f88063b7a85b1cfeSimon Ulbricht Apply the algorithm for overload resolution described in
8600e22385bce13c5d1048f7b955f9394a5d94d6Simon Ulbricht Till Mossakowski, Kolyang, Bernd Krieg-Brueckner:
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht Static semantic analysis and theorem proving for CASL.
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht 12th Workshop on Algebraic Development Techniques, Tarquinia 1997,
8221f726d2e0ca6f0df32ef8f88063b7a85b1cfeSimon Ulbricht LNCS 1376, p. 333-348
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht to all given formulae/sentences w.r.t. the given annotations and
9b59de1ee08232aa26d9d21d3bf99f8d1f68c45dChristian Maeder signature. All real work is done by 'minExpFORMULA', which is
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder applied to any given formula in turn.
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder-----------------------------------------------------------}
0223b75560eead55b7bbf11d18117a6819540983Christian MaederoverloadResolution :: (Eq f, PrettyPrint f) =>
9b59de1ee08232aa26d9d21d3bf99f8d1f68c45dChristian Maeder GlobalAnnos ->
3413b54d5439b4a66d6423cc134e1b9abb5bbe2fChristian Maeder [Named.Named (FORMULA f)] ->
9b59de1ee08232aa26d9d21d3bf99f8d1f68c45dChristian Maeder Result [Named.Named (FORMULA f)]
8221f726d2e0ca6f0df32ef8f88063b7a85b1cfeSimon Ulbricht -- neat type signature, eh?
a9ad67574dd71144680f8dedc285f6c4498a79f8Christian MaederoverloadResolution mef ga sign = mapM overload
a9ad67574dd71144680f8dedc285f6c4498a79f8Christian Maeder where overload sent = do let sent' = Named.sentence sent
a9ad67574dd71144680f8dedc285f6c4498a79f8Christian Maeder exp_sent <- minExpFORMULA mef ga sign sent'
8221f726d2e0ca6f0df32ef8f88063b7a85b1cfeSimon Ulbricht return sent { Named.sentence = exp_sent }
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht{-----------------------------------------------------------
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht - Minimal expansion of a formula -
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht Expand a given formula by typing information.
2fa75b8879de8c878475f16ab43161e0580f5430Simon Ulbricht * For non-atomic formulae, recurse through subsentences.
627e304eb081ce411768e08d3554d8efd52d4187Simon Ulbricht * For trival atomic formulae, no expansion is neccessary.
5d75e163c4134d97bba0ced346c3095d7150685cSimon Ulbricht * For atomic formulae, the following cases are implemented:
5d75e163c4134d97bba0ced346c3095d7150685cSimon Ulbricht + Predication is handled by the dedicated expansion function
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht 'minExpFORMULA_pred'.
fcc4b0f2dadf063ebb8022737cb6e40fb9c4baa8Simon Ulbricht + Existl_equation and Strong_equation are handled by the dedicated
fcc4b0f2dadf063ebb8022737cb6e40fb9c4baa8Simon Ulbricht expansion function 'minExpFORMULA_eq'.
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht + Definedness is handled by expanding the subterm.
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht + Membership is handled by expanding the subterm, followed by
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht checking the found expansions' appropriateness to the stated
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht + TODO: I don't know about 'Sort_gen_ax' and 'ExtFORMULA'.
a2cf22f16e226fcc85aa0801f001923ab2db49ddSimon Ulbricht-----------------------------------------------------------}
0223b75560eead55b7bbf11d18117a6819540983Christian MaederminExpFORMULA :: (Eq f, PrettyPrint f) =>
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder GlobalAnnos ->
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder (FORMULA f) ->
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder Result (FORMULA f)
0223b75560eead55b7bbf11d18117a6819540983Christian MaederminExpFORMULA mef ga sign formula = case formula of
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder --- Trivial atomic formula -> return sentence unmodified
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder True_atom _ -> return formula
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder False_atom _ -> return formula
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder Sort_gen_ax _ _ -> return formula
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder --- Non-atomic formula -> recurse through subsentences
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht Quantification q vars f pos -> do
08913787eb7dc05172d505d02b11545ffc7e1256Simon Ulbricht -- add 'vars' to signature
fe6a19b07759bc4190e88dda76a211d86bf32062Simon Ulbricht let (_, sign') = runState (mapM_ addVars vars) sign
fe6a19b07759bc4190e88dda76a211d86bf32062Simon Ulbricht -- expand subformula
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht f' <- minExpFORMULA mef ga sign' f
f665662391cc4b8fdc03e8bd082936cfedbce1a2Simon Ulbricht -- wrap 'Quantification' around expanded sentence
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht return (Quantification q vars f' pos)
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht Conjunction fs pos -> do
f665662391cc4b8fdc03e8bd082936cfedbce1a2Simon Ulbricht -- expand all subformulae
f665662391cc4b8fdc03e8bd082936cfedbce1a2Simon Ulbricht fs' <- mapM (minExpFORMULA mef ga sign) fs
f665662391cc4b8fdc03e8bd082936cfedbce1a2Simon Ulbricht -- wrap 'Conjunction' around expanded sentences
f665662391cc4b8fdc03e8bd082936cfedbce1a2Simon Ulbricht return (Conjunction fs' pos)
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht Disjunction fs pos -> do
e4d1479434761dc3eb8d17b6c75de4eb24866f0bSimon Ulbricht -- expand all subformulae
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht fs' <- mapM (minExpFORMULA mef ga sign) fs
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht -- wrap 'Disjunction' around expanded sentences
2fa75b8879de8c878475f16ab43161e0580f5430Simon Ulbricht return $ Disjunction fs' pos
2fa75b8879de8c878475f16ab43161e0580f5430Simon Ulbricht -- TODO: reminds of 'Conjunction'...
2fa75b8879de8c878475f16ab43161e0580f5430Simon Ulbricht Implication f1 f2 b pos -> do
2fa75b8879de8c878475f16ab43161e0580f5430Simon Ulbricht -- expand all subformulae
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht f1' <- minExpFORMULA mef ga sign f1
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht f2' <- minExpFORMULA mef ga sign f2
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht -- wrap 'Implication' around expanded sentences
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht return (Implication f1' f2' b pos)
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht Equivalence f1 f2 pos -> do
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht -- expand all subformulae
0e62ba92d48fb6b8251a0707d0c7e8358ac00a02Simon Ulbricht f1' <- minExpFORMULA mef ga sign f1
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht f2' <- minExpFORMULA mef ga sign f2
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht -- wrap 'Equivalence' around expanded sentences
9575d8e9e9211ccd22dbc9b86fa3e8941ee1d021Simon Ulbricht return (Equivalence f1' f2' pos)
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht -- TODO: reminds of 'Implication'...
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht Negation f pos -> do
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder -- expand subformula
9575d8e9e9211ccd22dbc9b86fa3e8941ee1d021Simon Ulbricht f' <- minExpFORMULA mef ga sign f
08913787eb7dc05172d505d02b11545ffc7e1256Simon Ulbricht -- wrap 'Negation' around expanded sentence
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder return (Negation f' pos)
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder -- TODO: reminds of ... all of the above
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder --- Atomic formula -> expand and check for ambiguities
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder Predication predicate terms pos
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder -- pass information to dedicated helper function
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder -> minExpFORMULA_pred mef ga sign predicate terms pos
0223b75560eead55b7bbf11d18117a6819540983Christian Maeder Existl_equation term1 term2 pos
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht -- pass information to dedicated helper function
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht -> minExpFORMULA_eq mef ga sign Existl_equation term1 term2 pos
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht Strong_equation term1 term2 pos
f665662391cc4b8fdc03e8bd082936cfedbce1a2Simon Ulbricht -- pass information to dedicated helper function
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht -> minExpFORMULA_eq mef ga sign Strong_equation term1 term2 pos
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht Definedness term pos -> do
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht -- expand subterm
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht t <- minExpTerm mef ga sign term
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht -- check expansions for ambiguity
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht t' <- is_unambiguous term t pos
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht -- wrap 'Definedness' around expanded term
c044cefcba5a9db7f8948b3778266971742b3dc6Simon Ulbricht return (Definedness t' pos)
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht Membership term sort pos -> do
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht -- expand subterm
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht t <- minExpTerm mef ga sign term
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht -- choose expansions that fulfill 'Membership' to the given 'sort'
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht t' <- let leq_term [] = False
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht leq_term (t1:_) = leq_SORT sign sort $ term_sort t1
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht in return $ filter leq_term t
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht -- check chosen expansions for ambiguity
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht t'' <- is_unambiguous term t' pos
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht -- wrap 'Membership' around expanded term
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht return (Membership t'' sort pos)
f665662391cc4b8fdc03e8bd082936cfedbce1a2Simon Ulbricht ExtFORMULA f -> fmap ExtFORMULA $ mef ga sign f
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht --- unknown formula -> signal error
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht _ -> error "minExpFORMULA: unexpected type of FORMULA: "
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht{-----------------------------------------------------------
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht - Check expanded terms for ambiguity -
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht Check, whether the given expansions contain ambiguity.
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht If so, return any of the equivalent expansions, otherwise, or in
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbricht case no correct expansions were found, signal an error.
a9ad67574dd71144680f8dedc285f6c4498a79f8Christian Maeder-----------------------------------------------------------}
776dc405f11bb5a86787cd05c1e539203e88759bSimon Ulbrichtis_unambiguous :: (Eq f, PrettyPrint f) =>
e46d78f7c6324ed9f1a191d46b6e5732e61e1835Simon Ulbricht [[TERM f]] ->
08913787eb7dc05172d505d02b11545ffc7e1256Simon Ulbricht Result (TERM f)
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht--- unambiguous expansion found -> return first expansion
7dc84ca1f3a253bcf947bd870f0303fffd37d3afSimon Ulbrichtis_unambiguous _ ((term:_):_) _ = return term
7dc84ca1f3a253bcf947bd870f0303fffd37d3afSimon Ulbricht-- TODO: pattern should read '((term:_):[])' to really filter ambiguities
627e304eb081ce411768e08d3554d8efd52d4187Simon Ulbricht--- no expansions found -> signal error
627e304eb081ce411768e08d3554d8efd52d4187Simon Ulbrichtis_unambiguous topterm [] pos
19988126590a72905215aef1d7a67c646d99bdadSimon Ulbricht = pplain_error (Unparsed_term "<error>" [])
19988126590a72905215aef1d7a67c646d99bdadSimon Ulbricht (ptext "No correct typing for " <+> (printText topterm))
19988126590a72905215aef1d7a67c646d99bdadSimon Ulbricht--- ambiguous expansions found -> signal error
19988126590a72905215aef1d7a67c646d99bdadSimon Ulbrichtis_unambiguous _ term pos
19988126590a72905215aef1d7a67c646d99bdadSimon Ulbricht = pplain_error (Unparsed_term "<error>" [])
19988126590a72905215aef1d7a67c646d99bdadSimon Ulbricht (ptext "Cannot disambiguate!\nPossible Expansions: "
19988126590a72905215aef1d7a67c646d99bdadSimon Ulbricht <+> (printText term))
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht{-----------------------------------------------------------
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht - Minimal expansion of a predication formula -
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht Expand a predicate application by typing information.
5f662be3a5a327b763dbc53e28836a04cfc3bd3aChristian Maeder 1. First expand all argument subterms.
5f662be3a5a327b763dbc53e28836a04cfc3bd3aChristian Maeder 2. Permute these expansions so we compute the set of tuples
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht { (C_1, ..., C_n) | (C_1, ..., C_n) \in
5f662be3a5a327b763dbc53e28836a04cfc3bd3aChristian Maeder minExpTerm(t_1) x ... x minExpTerm(t_n) }
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht where t_1, ..., t_n are the given argument terms.
857ab542e0b0bdf90e5a484ffe8df5a9c9c3e38fChristian Maeder 3. For each element of this set compute the set of possible profiles
19988126590a72905215aef1d7a67c646d99bdadSimon Ulbricht (as described on p. 339).
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht 3a. For each profile inject all arguments t_i (elements of the list
01bf5a978a5dd7aecf7dea0ee2e1046922c64fd2Simon Ulbricht t_1:s_1,...,t_n:s_n) that don't exactly match the expected type (s_i)
e46d78f7c6324ed9f1a191d46b6e5732e61e1835Simon Ulbricht so that they do. E.g. if t_j is of sort s_j' but sort s_j, replace
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht the term with the injection of t_j from s_j' to s_j.
95f75d053c19b9be988c73b7c866d9db57825efeSimon Ulbricht 4. Define an equivalence relation ~ on these profiles
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht (as described on p. 339).
454e349ad409df6c5fa9ba2b485243b8222dec41Simon Ulbricht 5. Separate each profile into equivalence classes by the relation ~
d3d09eed06d615a26a9c930966f29cf2c149b876Simon Ulbricht and take the unification of these sets.
d3d09eed06d615a26a9c930966f29cf2c149b876Simon Ulbricht 6. Transform each term in the minimized set into a qualified predication.
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht This process is analogous to expanding a function application (s. below),
454e349ad409df6c5fa9ba2b485243b8222dec41Simon Ulbricht just with the minimization step being left out.
1a088ae6e5ab1e717d720da7b517233286665073Christian Maeder-----------------------------------------------------------}
1a088ae6e5ab1e717d720da7b517233286665073Christian MaederminExpFORMULA_pred :: (Eq f, PrettyPrint f) =>
454e349ad409df6c5fa9ba2b485243b8222dec41Simon Ulbricht GlobalAnnos ->
857ab542e0b0bdf90e5a484ffe8df5a9c9c3e38fChristian Maeder Result (FORMULA f)
857ab542e0b0bdf90e5a484ffe8df5a9c9c3e38fChristian MaederminExpFORMULA_pred mef ga sign predicate terms pos = do
857ab542e0b0bdf90e5a484ffe8df5a9c9c3e38fChristian Maeder -- expand argument subterms (Step 1)
627e304eb081ce411768e08d3554d8efd52d4187Simon Ulbricht expansions <- mapM (minExpTerm mef ga sign) terms
627e304eb081ce411768e08d3554d8efd52d4187Simon Ulbricht -- permute expansions (Step 2)
857ab542e0b0bdf90e5a484ffe8df5a9c9c3e38fChristian Maeder permuted_exps <- return (permute expansions)
e46d78f7c6324ed9f1a191d46b6e5732e61e1835Simon Ulbricht -- convert each permutation to a profile (Step 3)
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht -- then inject arguments that don't match the predicate's expected type
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht profiles <- return $ -- map (map insert_injections) TODO: fix inlineAxioms
e46d78f7c6324ed9f1a191d46b6e5732e61e1835Simon Ulbricht map get_profile permuted_exps
e46d78f7c6324ed9f1a191d46b6e5732e61e1835Simon Ulbricht -- collect generated profiles into equivalence classes (Step 5)
e46d78f7c6324ed9f1a191d46b6e5732e61e1835Simon Ulbricht -- by the computed equivalence relation (Step 4)
e46d78f7c6324ed9f1a191d46b6e5732e61e1835Simon Ulbricht -- and unify them (also Step 5)
9deba6a2981f6b73fc57f27d525cabbb4f8bf484Simon Ulbricht p <- return $ concat
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht $ map (equivalence_Classes pred_eq)
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht -- choose profile if unambiguous, otherwise signal error
e46d78f7c6324ed9f1a191d46b6e5732e61e1835Simon Ulbricht p' <- choose p
042cf1b6c164c2b06bccafc8db6bf44134f3f0b8Simon Ulbricht -- transform chosen profile into a qualified predicate application
9deba6a2981f6b73fc57f27d525cabbb4f8bf484Simon Ulbricht return (qualify_pred p')
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht where -- the predicate's name
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht name :: PRED_NAME
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht name = pred_name predicate
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht -- predicates matching that name in the current environment
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht preds :: [PredType]
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht $ Map.findWithDefault Set.empty name $ predMap sign
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht -- extract a predicate's name from its definition
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht pred_name :: PRED_SYMB -> PRED_NAME
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht pred_name (Pred_name name') = name'
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht pred_name (Qual_pred_name name' _ _) = name'
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht -- if an unambiguous class of expansions is found, choose its head
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht -- otherwise signal an error
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht --choose :: [[(PredType, [TERM f])]] -> Result (PredType, [TERM f])
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht -- TODO: Signature throws an Error that I don't understand...
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht choose ((p:_):_) = return p
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht choose [] = pplain_error (PredType [], [])
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht (ptext "No correct typing for "
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht <+> printText (Predication predicate terms pos))
5f662be3a5a327b763dbc53e28836a04cfc3bd3aChristian Maeder choose ps = pplain_error (PredType [], terms)
5f662be3a5a327b763dbc53e28836a04cfc3bd3aChristian Maeder (ptext "Cannot disambiguate! Term: "
bcce4250f8aa524ddc0af7021a238e9fc2b8034aSimon Ulbricht <+> (printText (predicate, terms))
(Id.posOfId name)
type Eq_constructor f = TERM f -> TERM f -> [Id.Pos] -> FORMULA f
Expand all subterms w.r.t. the given environment, then find pairs of
[Id.Pos] ->
(Id.headPos pos)
(Id.headPos pos)
Id.SIMPLE_ID ->
ops' <- return $ Set.filter is_const_op
ops <- return (Set.image opRes ops')
cs <- return (Set.union vars ops)
$ Set.toList least
name = Id.simpleIdToId var
-- True if operation takes no arguments (i.e. op is a constant)
is_least_sort :: Set.Set SORT -> SORT -> Bool
| otherwise = (Application (Qual_op_name name (Total_op_type [] sort []) []) [] [], sort) -- should deal with partial constants as well!
[Id.Pos] ->
[Id.Pos] ->
so that they do. E.g. if t_j is of sort s_j' but sort s_j, replace
[Id.Pos] ->
minExpTerm_op _ _ sign (Op_name (Id.Id [tok] [] _)) [] _
-- transform minimized eq.classes into qualified function application terms
-- qualify all ops in a list of eq.classes of ops
-- TODO: check whether arg.types are equal
[Id.Pos] ->
[Id.Pos] ->
-- permute to get all possible combinations of eq.classes
-- minimize generated eq.classes
-- transform eq.classes into qualified conditional terms
have_supersort = not . Set.isEmpty . supersorts
supersorts :: [TERM f] -> Set.Set SORT
-- qualify all conditionals in a list of eq.classes
s <- Set.toList (supersorts c) ]
qualifyTerms :: [Id.Pos] -> [[(TERM f, SORT)]] -> [[TERM f]]
injName :: Id.Id
(Total_op_type [s1] s2 [Id.nullPos])
where results :: Set.Set SORT
results = Set.fromList (map (opRes . fst) ops_n_terms)
lesserSorts :: SORT -> Set.Set SORT
lesserSorts s = Set.intersection results (subsortsOf s sign)
leastSort s = Set.size (lesserSorts s) == 1
where sorts :: Set.Set SORT
sorts = Set.fromList (map snd terms_n_sort)
lesserSorts :: SORT -> Set.Set SORT
lesserSorts s = Set.intersection sorts (subsortsOf s sign)
leastSort s = Set.size (lesserSorts s) == 1
common_subsorts :: Sign f e -> [SORT] -> Set.Set SORT
in foldr1 Set.intersection
common_supersorts :: Sign f e -> [SORT] -> Set.Set SORT
common_supersorts _ [] = Set.empty
in foldr1 Set.intersection
have_common_subsorts s = (not . Set.isEmpty . common_subsorts s)
have_common_supersorts s = (not . Set.isEmpty . common_supersorts s)
leq_SORT sign s1 s2 = Set.member s2 (supersortsOf s1 sign)
geq_SORT sign s1 s2 = Set.member s2 (subsortsOf s1 sign)