Changeset 12698

Show
Ignore:
Timestamp:
23.10.2009 10:50:12 (4 weeks ago)
Author:
maeder
Message:

cleaned up

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/CASL/Utils.hs

    r12268 r12698  
    2424 
    2525import CASL.AS_Basic_CASL 
     26import CASL.Sign (mkVarTerm) 
    2627import CASL.Fold 
    2728 
     
    5556type FreshVARMap f = Map.Map VAR (TERM f) 
    5657 
    57 -- | build_vMap constructs a mapping between a list of old variables and 
     58-- | buildVMap constructs a mapping between a list of old variables and 
    5859-- corresponding fresh variables based on two lists of VAR_DECL 
    59 build_vMap :: [VAR_DECL] -> [VAR_DECL] -> FreshVARMap f 
    60 build_vMap vDecls f_vDecls = 
    61     Map.fromList (concat (zipWith toTups vDecls f_vDecls)) 
     60buildVMap :: [VAR_DECL] -> [VAR_DECL] -> FreshVARMap f 
     61buildVMap vDecls fVDecls = 
     62    Map.fromList (concat (zipWith toTups vDecls fVDecls)) 
    6263    where toTups (Var_decl vars1 sor1 _) (Var_decl vars2 sor2 _) = 
    6364            if sor1 == sor2 then zipWith (toTup sor1) vars1 vars2 
    64             else error "CASL.Utils.build_vMap" 
    65           toTup s v1 v2 = (v1,toVarTerm v2 s) 
     65            else error "CASL.Utils.buildVMap" 
     66          toTup s v1 v2 = (v1, mkVarTerm (v2, s)) 
    6667 
    6768-- | specialised lookup in FreshVARMap that ensures that the VAR with 
    6869-- the correct type (SORT) is replaced 
    69 lookup_vMap :: VAR -> SORT -> FreshVARMap f -> Maybe (TERM f) 
    70 lookup_vMap v s = 
     70lookupVMap :: VAR -> SORT -> FreshVARMap f -> Maybe (TERM f) 
     71lookupVMap v s = 
    7172    maybe Nothing 
    7273          (\ t@(Qual_var _ s' _) -> if s==s' then Just t else Nothing) 
     
    7475 
    7576-- | specialized delete that deletes all shadowed variables 
    76 delete_vMap :: [VAR_DECL] -> FreshVARMap f -> FreshVARMap f 
    77 delete_vMap vDecls m = foldr Map.delete m 
     77deleteVMap :: [VAR_DECL] -> FreshVARMap f -> FreshVARMap f 
     78deleteVMap vDecls m = foldr Map.delete m 
    7879  $ concatMap (\ (Var_decl vs _ _) -> vs) vDecls 
    7980 
     
    8182replaceVarsRec m mf = (mapRecord mf) 
    8283     { foldQual_var = \ qv v s _ -> 
    83            fromMaybe qv $ lookup_vMap v s m 
     84           fromMaybe qv $ lookupVMap v s m 
    8485     , foldQuantification = \ (Quantification q vs f ps) _ _ _ _ -> 
    85                let nm = delete_vMap vs m 
    86                in Quantification q vs (replace_varsF nm mf f) ps 
     86               let nm = deleteVMap vs m 
     87               in Quantification q vs (replaceVarsF nm mf f) ps 
    8788     } 
    8889 
    89 -- | replace_vars replaces all Qual_var occurences that are supposed 
     90-- | replaceVars replaces all Qual_var occurences that are supposed 
    9091-- to be replaced according to the FreshVARMap 
    91 replace_varsF :: FreshVARMap f 
     92replaceVarsF :: FreshVARMap f 
    9293             -> (f -> f) 
    9394             -- ^ this function replaces Qual_var in ExtFORMULA 
    9495             -> FORMULA f -> FORMULA f 
    95 replace_varsF m = foldFormula . replaceVarsRec m 
     96replaceVarsF m = foldFormula . replaceVarsRec m 
    9697 
    9798codeOutUniqueRecord :: (f -> f) -> (f -> f) -> Record f (FORMULA f) (TERM f) 
     
    103104              if sor1 == sor2 then zipWith (eqFor sor1) vars1 vars2 
    104105              else error "codeOutUniqueRecord1" 
    105             eqFor s v1 v2 = Strong_equation (toSortTerm (toVarTerm v1 s)) 
    106                                           (toSortTerm (toVarTerm v2 s)) 
     106            eqFor s v1 v2 = Strong_equation (mkVarTerm (v1, s)) 
     107                                          (mkVarTerm (v2, s)) 
    107108                                          nullRange 
    108             -- fresh_vars produces new variables based on a list 
     109            -- freshVars produces new variables based on a list 
    109110            -- of defined variables 
    110111            -- args: (1) set of already used variable names 
    111112            --       (2) list of variables 
    112             fresh_vars = mapAccumL fresh_var 
    113             fresh_var accSet (Var_decl vars sor _) = 
     113            freshVars = mapAccumL freshVar 
     114            freshVar accSet (Var_decl vars sor _) = 
    114115              let accSet' = Set.union (Set.fromList vars) accSet 
    115116                  (accSet'',vars') = mapAccumL nVar accSet' vars 
     
    120121                                 [genVar v (i :: Int) | i<-[1..]]) 
    121122              in (Set.insert v' aSet,v') 
    122             (vSet', vDecl')  = fresh_vars Set.empty vDecl 
    123             (_vSet'', vDecl'')  = fresh_vars vSet' vDecl 
    124             f'_rep_x = replace_varsF (build_vMap vDecl vDecl') rf f' 
    125             f'_rep_y = replace_varsF (build_vMap vDecl vDecl'') rf f' 
     123            (vSet', vDecl')  = freshVars Set.empty vDecl 
     124            (_vSet'', vDecl'')  = freshVars vSet' vDecl 
     125            f'_rep_x = replaceVarsF (buildVMap vDecl vDecl') rf f' 
     126            f'_rep_y = replaceVarsF (buildVMap vDecl vDecl'') rf f' 
    126127            allForm = Quantification Universal (vDecl'++vDecl'') 
    127128                           (Implication 
     
    171172              \ (Membership t s ps) _ _ _ -> 
    172173                  either (codeOutConditionalF fun) id 
    173                     (mkSingleTermF (\ x y -> Membership x s y) t ps) 
     174                    (mkSingleTermF (\ x -> Membership x s) t ps) 
    174175          , foldDefinedness = 
    175176              \ (Definedness t ps) _ _ -> 
     
    184185codeOutCondPredication phi@(Predication _ ts _) = 
    185186    maybe (Right phi) (Left . constructExpansion phi) 
    186           (listToMaybe (catMaybes (map findConditionalT ts))) 
     187          $ listToMaybe $ mapMaybe findConditionalT ts 
    187188codeOutCondPredication _ = error "CASL.Utils: Predication expected" 
    188189 
     
    204205mkEquationAtom cons t1 t2 ps = 
    205206    maybe (Right phi) (Left . constructExpansion phi) 
    206           (listToMaybe (catMaybes (map findConditionalT [t1,t2]))) 
     207          $ listToMaybe $ mapMaybe findConditionalT [t1, t2] 
    207208    where phi = cons t1 t2 ps 
    208209 
     
    283284substEqPreds :: Set.Set PRED_SYMB -> (f -> f) -> FORMULA f -> FORMULA f 
    284285substEqPreds eqPredSet = foldFormula . eqSubstRecord eqPredSet 
    285  
    286 -- | adds Sorted_term to a Qual_var term 
    287 toSortTerm :: TERM f -> TERM f 
    288 toSortTerm t@(Qual_var _ s ps) = Sorted_term t s ps 
    289 toSortTerm _ = error "CASL2TopSort.toSortTerm: can only handle Qual_var" 
    290  
    291 -- | generates from a variable and sort an Qual_var term 
    292 toVarTerm :: VAR -> SORT -> TERM f 
    293 toVarTerm vs s = Qual_var vs s nullRange 
  • trunk/Comorphisms/CASL2TopSort.inline.hs

    r12401 r12698  
    3131import CASL.Logic_CASL 
    3232import CASL.AS_Basic_CASL 
    33 import CASL.Utils 
    3433import CASL.Sign 
    3534import CASL.Morphism 
     
    241240    where p_axs = 
    242241          -- generate argument restrictions for predicates 
    243            Map.foldWithKey (\ pName set al -> 
    244               al ++ Map.foldWithKey 
     242           Map.foldWithKey (\ pName set -> 
     243              (++ Map.foldWithKey 
    245244                             (\ sl -> genArgRest 
    246245                                      (genSenName "p" pName $ length sl) 
    247246                                      (genPredication pName) 
    248247                                      sl) 
    249                                     [] (mkProfMapPred subSortMap set)) 
     248                                    [] (mkProfMapPred subSortMap set))) 
    250249                   [] pMap 
    251250          hi_axs = 
     
    324323genArgRest sen_name genProp sl spl fs = 
    325324    let vars = genVars sl 
    326         mquant = genQuantification (genProp sl $ map toSortTerm vars) vars spl 
     325        mquant = genQuantification (genProp sl vars) vars spl 
    327326    in maybe fs (\ quant -> mapNamed (const quant) (makeNamed "" sen_name) 
    328327                             : fs) mquant 
     
    345344 
    346345genVars :: [SORT] -> [TERM f] 
    347 genVars = zipWith toVarTerm varSymbs 
     346genVars = map mkVarTerm . zip varSymbs 
    348347    where varSymbs = map mkSimpleId 
    349348            (map (: []) "xyzuwv" ++ map (\ i -> 'v' : show i) [(1::Int)..]) 
     
    459458          relativize Universal vdl f1 = 
    460459              if null vdl then f1 
    461               else Implication (mkVarPreds vdl) f1 True nullRange 
     460              else mkImpl (mkVarPreds vdl) f1 
    462461          -- existential or unique-existential? then use conjuction 
    463462          relativize _ vdl f1 = 
    464463              if null vdl then f1 
    465               else Conjunction [mkVarPreds vdl,f1] nullRange 
    466           mkVarPreds [v] = mkVarPred v 
    467           mkVarPreds vdl = Conjunction (map mkVarPred vdl) nullRange 
    468           mkVarPred (Var_decl [v] s _) = mkVarPred1 s v 
    469           mkVarPred (Var_decl vs s _) = 
    470               Conjunction (map (mkVarPred1 s) vs) nullRange 
     464              else conjunct [mkVarPreds vdl, f1] 
     465          mkVarPreds = conjunct . map mkVarPred 
     466          mkVarPred (Var_decl vs s _) = conjunct $ map (mkVarPred1 s) vs 
    471467          mkVarPred1 s v = 
    472468              let sTop = lkupTop subSortMap s 
     
    490486    Conditional t1 f t2 pl -> 
    491487        Conditional (mapTerm ssMap t1) (mapSen1 ssMap f) (mapTerm ssMap t2) pl 
    492     _ -> 
    493         error "CASL2TopSort.mapTerm" 
     488    _ -> error "CASL2TopSort.mapTerm" 
    494489    where lTop = lkupTop ssMap 
    495490          updateOP_SYMB (Op_name _) = 
     
    511506                    [xs@(x : _)] -> return $ genQuant x $ genImpl xs 
    512507                    ((x : _) : _) -> return $ genQuant x 
    513                         $ Conjunction (map genImpl groupedInjOps) nullRange 
     508                        $ conjunct $ map genImpl groupedInjOps 
    514509                    _ -> error "CASL2TopSort.genEitherAxiom.groupedInjOps" 
    515510               else Result [Diag Error 
     
    529524          sameTarget x1 x2 = compTarget x1 x2 == EQ 
    530525          lTop = lkupTop ssMap 
    531           varName = mkSimpleId "x" 
    532           mkVarTerm qon = 
    533               Sorted_term (Qual_var varName s nullRange) s nullRange 
    534               where s = lTop $ resultSort qon 
    535           mkVarDecl qon = 
    536               Var_decl [varName] (lTop (resultSort qon)) nullRange 
    537           genQuant qon f = Quantification Universal [mkVarDecl qon] f nullRange 
    538           genImpl []       = error "No OP_SYMB found" 
    539           genImpl xs@(x:_) = 
    540               assert (lTop (resultSort x) == lTop (argSort x)) 
    541               (if resultSort x == lTop (resultSort x) 
    542                then genDisj xs 
    543                else Implication (genProp x) (genDisj xs) True nullRange) 
    544           genProp qon = genPredication (lPredName (resultSort qon)) 
    545                                        [lTop (resultSort qon)] 
    546                                        [mkVarTerm qon] 
     526          mkXVarTerm = toQualVar . mkXVarDecl 
     527              -- I do not think this term needs to be a sorted term 
     528          mkXVarDecl = mkVarDeclStr "x" . lTop . resultSort 
     529          genQuant qon f = mkForall [mkXVarDecl qon] f nullRange 
     530          genImpl xs = case xs of 
     531            x : _ -> let 
     532              rSrt = resultSort x 
     533              ltSrt = lTop rSrt 
     534              disjs = genDisj xs 
     535              in if ltSrt == lTop (argSort x) then 
     536                   if rSrt == ltSrt then disjs else mkImpl (genProp x) disjs 
     537                 else  error "CASL2TopSort.genEitherAxiom.genImpl" 
     538            _ -> error "CASL2TopSort.genEitherAxiom.genImpl No OP_SYMB found" 
     539          genProp qon = let rSrt = resultSort qon in 
     540              genPredication (lPredName rSrt) [lTop rSrt] [mkXVarTerm qon] 
    547541          lPredName s = fromMaybe (error $ 
    548542              "CASL2TopSort.genEitherAxiom: No PRED_NAME for \"" 
     
    550544          genDisj qons = Disjunction (map genPred qons) nullRange 
    551545          genPred qon = genPredication (lPredName $ argSort qon) 
    552               [lTop $ resultSort qon] [mkVarTerm qon] 
     546              [lTop $ resultSort qon] [mkXVarTerm qon]