checkPlaces :: [SORT] -> Id -> [Diagnosis]
if let n = placeCount i in n == 0 || n == length args then []
else [mkDiag Error "wrong number of places" i]
addOp :: OpType -> Id -> State (Sign f e) ()
do checkSorts (opRes ty : opArgs ty)
check = addDiags $ checkPlaces (opArgs ty) i
store = do put e { opMap = addOpTo i ty m }
addDiags [mkDiag Hint "redeclared op" i]
Partial -> if
Set.member ty {opKind = Total} l then
addDiags [mkDiag Warning "partially redeclared" i]
addDiags [mkDiag Hint "redeclared as total" i]
addAssocOp :: OpType -> Id -> State (Sign f e) ()
put e { assocOps = addOpTo i ty $ assocOps e }
updateExtInfo :: (e -> Result e) -> State (Sign f e) ()
let re = upd $ extendedInfo s
Just e -> put s { extendedInfo = e }
addOpTo :: Id -> OpType -> OpMap -> OpMap
Total -> let vp = v { opKind = Partial } in
addPred :: PredType -> Id -> State (Sign f e) ()
do checkSorts $ predArgs ty
addDiags [mkDiag Hint "redeclared pred" i]
addDiags $ checkPlaces (predArgs ty) i
addAssocs :: GlobalAnnos -> Sign f e -> GlobalAnnos
_ -> m ) (assoc_annos ga) (
Map.keys $ assocOps e) }
formulaIds :: Sign f e ->
Set.Set Id
formulaIds e = let ops = allOpIds e in
allPredIds :: Sign f e ->
Set.Set Id
addSentences :: [Named (FORMULA f)] -> State (Sign f e) ()
put e { sentences = reverse ds ++ sentences e }
-- * traversing all data types of the abstract syntax
ana_BASIC_SPEC :: Resolver f => Min f e
-> Ana b f e -> Ana s f e -> GlobalAnnos
-> BASIC_SPEC b s f -> State (Sign f e) (BASIC_SPEC b s f)
ana_BASIC_SPEC mef ab as ga (Basic_spec al) = fmap Basic_spec $
mapAnM (ana_BASIC_ITEMS mef ab as ga) al
-- looseness of a datatype
data GenKind = Free | Generated | Loose deriving (Show, Eq, Ord)
mkForall :: [VAR_DECL] -> FORMULA f -> [Pos] -> FORMULA f
mkForall vl f ps = if null vl then f else
Quantification Universal vl f ps
unionGenAx :: [GenAx] -> GenAx
unionGenAx = foldr ( \ (s1, r1, f1) (s2, r2, f2) ->
ana_BASIC_ITEMS :: Resolver f => Min f e
-> Ana b f e -> Ana s f e -> GlobalAnnos
-> BASIC_ITEMS b s f -> State (Sign f e) (BASIC_ITEMS b s f)
ana_BASIC_ITEMS mef ab as ga bi =
Sig_items sis -> fmap Sig_items $
ana_SIG_ITEMS mef as ga Loose sis
do let sorts = map (( \ (Datatype_decl s _ _) -> s) . item) al
mapAnM (ana_DATATYPE_DECL Free) al
toSortGenAx ps True $ getDataGenSig al
do (gs,ul) <- ana_Generated mef as ga al
toSortGenAx ps False $ unionGenAx gs
Local_var_axioms il afs ps ->
put e { envDiags = vds } -- restore with shadowing warnings
let preds = allPredIds sign
newGa = addAssocs ga sign
(es, resFs, anaFs) = foldr ( \ f (dss, ress, anas) ->
let Result ds m = anaForm mef newGa ops preds sign
Nothing -> (ds ++ dss, ress, anas)
(ds ++ dss, f {item = resF} : ress,
fufs = map (mapAn (\ f -> let
in stripQuant $ mkForall (vs ++ il) f ps))
sens = map ( \ a -> NamedSen (getRLabel a) True $ item a) fufs
return $ Local_var_axioms il resFs ps
newGa <- gets $ addAssocs ga
let (es, resFs, anaFs) = foldr ( \ f (dss, ress, anas) ->
let Result ds m = anaForm mef newGa ops preds sign
Nothing -> (ds ++ dss, ress, anas)
(ds ++ dss, f {item = resF} : ress,
fufs = map (mapAn (\ f -> let
in stripQuant $ mkForall vs f ps)) anaFs
sens = map ( \ a -> NamedSen (getRLabel a) True $ item a) fufs
return $ Axiom_items resFs ps
Ext_BASIC_ITEMS b -> fmap Ext_BASIC_ITEMS $ ab ga b
mapAn :: (a -> b) -> Annoted a -> Annoted b
mapAn f an = replaceAnnoted (f $ item an) an
toSortGenAx :: [Pos] -> Bool -> GenAx -> State (Sign f e) ()
toSortGenAx ps isFree (sorts, rel, ops) = do
opSyms = map ( \ c -> let ide = compId c in Qual_op_name ide
(toOP_TYPE $ compType c) $ posOfId ide) $
Set.toList ops
injSyms = map ( \ (s, t) -> let p = posOfId s in
resType _ (Op_name _) = False
resType s (Qual_op_name _ t _) = res_OP_TYPE t ==s
getIndex s = maybe (-1) id $ findIndex (==s) sortList
addIndices os@(Qual_op_name _ t _) =
(os,map getIndex $ args_OP_TYPE t)
Constraint s (map addIndices $ filter (resType s)
constrs = map collectOps sortList
f = Sort_gen_ax constrs isFree
addDiags[Diag Error "missing generated sort" ps]
showSepList (showString "_") showId sortList "")
ana_SIG_ITEMS :: Resolver f => Min f e
-> Ana s f e -> GlobalAnnos -> GenKind
-> SIG_ITEMS s f -> State (Sign f e) (SIG_ITEMS s f)
ana_SIG_ITEMS mef as ga gk si =
do ul <- mapM (ana_SORT_ITEM mef ga) al
return $ Sort_items ul ps
do ul <- mapM (ana_OP_ITEM mef ga) al
do ul <- mapM (ana_PRED_ITEM mef ga) al
return $ Pred_items ul ps
do let sorts = map (( \ (Datatype_decl s _ _) -> s) . item) al
mapAnM (ana_DATATYPE_DECL gk) al
Ext_SIG_ITEMS s -> fmap Ext_SIG_ITEMS $ as ga s
ana_Generated :: Resolver f => Min f e
-> Ana s f e -> GlobalAnnos -> [Annoted (SIG_ITEMS s f)]
-> State (Sign f e) ([GenAx], [Annoted (SIG_ITEMS s f)])
ana_Generated mef as ga al = do
ul <- mapAnM (ana_SIG_ITEMS mef as ga Generated) al
return (map (getGenSig . item) ul, ul)
getGenSig :: SIG_ITEMS s f -> GenAx
getGenSig si = case si of
Sort_items al _ -> unionGenAx $ map (getGenSorts . item) al
Datatype_items dl _ -> getDataGenSig dl
isConsAlt :: ALTERNATIVE -> Bool
getDataGenSig :: [Annoted DATATYPE_DECL] -> GenAx
let alts = concatMap (( \ (Datatype_decl s al _) ->
map ( \ a -> (s, item a)) al) . item) dl
(realAlts, subs) = partition (isConsAlt . snd) alts
let (i, ty, _) = getConsType s a
in Component i ty) realAlts
rel = foldr ( \ (t, a) r ->
getGenSorts :: SORT_ITEM f -> GenAx
let (sorts, rel) = case si of
, foldr ( \ s r -> foldr ( \ t ->
getOps :: OP_ITEM f ->
Set.Set Component
Op_defn i par _ _ ->
Set.single $ Component i $ toOpType $ headToType par
ana_SORT_ITEM :: Resolver f => Min f e
-> GlobalAnnos -> Annoted (SORT_ITEM f)
-> State (Sign f e) (Annoted (SORT_ITEM f))
ana_SORT_ITEM mef ga asi =
Subsort_defn sub v super af ps ->
addVars (Var_decl [v] super ps)
newGa <- gets $ addAssocs ga
let Result ds mf = anaForm mef newGa ops preds sign $ item af
lab = if null lb then getRLabel asi else lb
Nothing -> return asi { item = Subsort_decl [sub] super ps}
addSentences[NamedSen lab True $
mkForall [Var_decl [v] super pv]
(Membership (Qual_var v super pv) sub p)
return asi { item = Subsort_defn sub v super
mapM_ ( \ i -> mapM_ (addSubsort i) il) il
ana_OP_ITEM :: Resolver f => Min f e -> GlobalAnnos -> Annoted (OP_ITEM f)
-> State (Sign f e) (Annoted (OP_ITEM f))
ul <- mapM (ana_OP_ATTR mef ga oty ops) il
if null $ filter ( \ i -> case i of
else mapM_ (addAssocOp oty) ops
return aoi {item = Op_decl ops ty (catMaybes ul) ps}
do let ty = headToType ohd
lab = if null lb then getRLabel aoi else lb
vs = map (\ (Arg_decl v s qs) -> (Var_decl v s qs)) args
arg = concatMap ( \ (Var_decl v s qs) ->
map ( \ j -> Qual_var j s qs) v) vs
newGa <- gets $ addAssocs ga
let Result ds mt = anaTerm mef newGa ops preds sign
(res_OP_TYPE ty) ps $ item at
Nothing -> return aoi { item = Op_decl [i] ty [] ps }
addSentences [NamedSen lab True $ mkForall vs
(Application (Qual_op_name i ty p) arg ps)
return aoi {item = Op_defn i ohd at { item = resT } ps }
headToType :: OP_HEAD -> OP_TYPE
headToType (Op_head k args r ps) = Op_type k (sortsOfArgs args) r ps
sortsOfArgs :: [ARG_DECL] -> [SORT]
sortsOfArgs = concatMap ( \ (Arg_decl l s _) -> map (const s) l)
ana_OP_ATTR :: Resolver f => Min f e -> GlobalAnnos
-> OpType -> [Id] -> (OP_ATTR f)
-> State (Sign f e) (Maybe (OP_ATTR f))
ana_OP_ATTR mef ga ty ois oa = do
[t1,t2] | t1 == t2 -> case oa of
Comm_op_attr -> return ()
_ -> if t1 == rty then return ()
else addDiags [Diag Error
"result sort must be equal to argument sorts" q]
_ -> addDiags [Diag Error
"expecting two arguments of equal sort" q]
newGa <- gets $ addAssocs ga
let Result ds mt = anaTerm mef newGa ops preds
Nothing -> return Nothing
addSentences $ map (makeUnit True anaT ty) ois
addSentences $ map (makeUnit False anaT ty) ois
return $ Just $ Unit_op_attr resT
let ns = map mkSimpleId ["x", "y", "z"]
vs = map ( \ v -> Var_decl [v] rty q) ns
[v1, v2, v3] = map ( \ v -> Qual_var v rty q) ns
makeAssoc i = let p = posOfId i
qi = Qual_op_name i sty p in
NamedSen ("ga_assoc_" ++ showId i "") True $
(Application qi [v1, Application qi [v2, v3] p] p)
(Application qi [Application qi [v1, v2] p, v3] p) p) p
addSentences $ map makeAssoc ois
let ns = map mkSimpleId ["x", "y"]
vs = zipWith ( \ v t -> Var_decl [v] t
$ concatMap posOfId atys) ns atys
makeComm i = let p = posOfId i
qi = Qual_op_name i sty p in
NamedSen ("ga_comm_" ++ showId i "") True $
(Application qi (reverse args) p) p) p
addSentences $ map makeComm ois
makeIdem i = let p = posOfId i in
NamedSen ("ga_idem_" ++ showId i "") True $
(Application (Qual_op_name i sty p) [qv, qv] p)
addSentences $ map makeIdem ois
makeUnit :: Bool -> TERM f -> OpType -> Id -> Named (FORMULA f)
let lab = "ga_" ++ (if b then "right" else "left") ++ "_unit_"
rargs = if b then args else reverse args
in NamedSen lab True $ mkForall [Var_decl [v] vty q]
(Application (Qual_op_name i (toOP_TYPE ty) p) rargs p)
ana_PRED_ITEM :: Resolver f => Min f e
-> GlobalAnnos -> Annoted (PRED_ITEM f)
-> State (Sign f e) (Annoted (PRED_ITEM f))
ana_PRED_ITEM mef ga ap =
do mapM (addPred $ toPredType ty) preds
Pred_defn i phd@(Pred_head args rs) at ps ->
lab = if null lb then getRLabel ap else lb
ty = Pred_type (sortsOfArgs args) rs
vs = map (\ (Arg_decl v s qs) -> (Var_decl v s qs)) args
arg = concatMap ( \ (Var_decl v s qs) ->
map ( \ j -> Qual_var j s qs) v) vs
addPred (toPredType ty) i
newGa <- gets $ addAssocs ga
let Result ds mt = anaForm mef newGa ops preds sign $ item at
Nothing -> return ap {item = Pred_decl [i] ty ps}
addSentences [NamedSen lab True $
(Equivalence (Predication (Qual_pred_name i ty p)
return ap {item = Pred_defn i phd at { item = resF } ps}
-- full function type of a selector (result sort is component sort)
data Component = Component { compId :: Id, compType :: OpType }
instance Eq Component where
Component i1 t1 == Component i2 t2 =
(i1, opArgs t1, opRes t1) == (i2, opArgs t2, opRes t2)
instance Ord Component where
Component i1 t1 <= Component i2 t2 =
(i1, opArgs t1, opRes t1) <= (i2, opArgs t2, opRes t2)
instance PrettyPrint Component where
printText0 ga (Component i ty) =
printText0 ga i <+> colon <> printText0 ga ty
instance PosItem Component where
get_pos = get_pos . compId
-- | return list of constructors
ana_DATATYPE_DECL :: GenKind -> DATATYPE_DECL -> State (Sign f e) [Component]
ana_DATATYPE_DECL gk (Datatype_decl s al _) =
do ul <- mapM (ana_ALTERNATIVE s . item) al
let constr = catMaybes ul
if null constr then return ()
else do addDiags $ checkUniqueness cs
wrongConstr = filter ((totalSels /=) . snd) constr
addDiags $ map ( \ (c, _) -> mkDiag Error
("total selectors '" ++ showSepList (showString ",")
"'\n must appear in alternative") c) wrongConstr
(alts, subs) = partition isConsAlt allts
sbs = concatMap getAltSubsorts subs
comps = map (getConsType s) alts
ttrips = map (( \ (a, vs, t, ses) -> (a, vs, t, catSels ses))
sels = concatMap ( \ (_, _, _, ses) -> ses) ttrips
addSentences $ map makeInjective
$ filter ( \ (_, _, ces) -> not $ null ces)
addSentences $ makeDisjSubsorts s sbs
addSentences $ concatMap ( \ c -> map (makeDisjToSort c) sbs)
addSentences $ makeDisjoint comps
addSentences $ catMaybes $ concatMap
map (makeUndefForm ses) ttrips) sels
makeDisjSubsorts :: SORT -> [SORT] -> [Named (FORMULA f)]
makeDisjSubsorts d subs = case subs of
s : rs -> map (makeDisjSubsort s) rs ++ makeDisjSubsorts d rs
makeDisjSubsort :: SORT -> SORT -> Named (FORMULA f)
makeDisjSubsort s1 s2 = let
in NamedSen ("ga_disjoint_sorts_" ++ showId s1 "_"
mkForall [v] (Negation (Conjunction [
Membership qv s1 p1, Membership qv s2 p2] p) p) p
makeDisjToSort :: (Id, OpType, [COMPONENTS]) -> SORT -> Named (FORMULA f)
let (c, v, t, _) = selForms1 "X" a
NamedSen ("ga_disjoint_" ++ showId c "_sort_"
mkForall v (Negation (Membership t s p) p) p
makeInjective :: (Id, OpType, [COMPONENTS]) -> Named (FORMULA f)
let (c, v1, t1, _) = selForms1 "X" a
(_, v2, t2, _) = selForms1 "Y" a
in NamedSen ("ga_injective_" ++ showId c "") True $
(Equivalence (Strong_equation t1 t2 p)
(let ces = zipWith ( \ w1 w2 -> Strong_equation
(toQualVar w1) (toQualVar w2) p) v1 v2
in if isSingle ces then head ces else Conjunction ces p)
makeDisjoint :: [(Id, OpType, [COMPONENTS])] -> [Named (FORMULA f)]
makeDisjoint l = case l of
c : cs -> map (makeDisj c) cs ++ makeDisjoint cs
makeDisj :: (Id, OpType, [COMPONENTS]) -> (Id, OpType, [COMPONENTS])
let (c1, v1, t1, _) = selForms1 "X" a1
(c2, v2, t2, _) = selForms1 "Y" a2
p = posOfId c1 ++ posOfId c2
in NamedSen ("ga_disjoint_" ++ showId c1 "_" ++ showId c2 "") True
(Negation (Strong_equation t1 t2 p) p) p
catSels :: [(Maybe Id, OpType)] -> [(Id, OpType)]
catSels = map ( \ (m, t) -> (fromJust m, t)) .
filter ( \ (m, _) -> isJust m)
makeUndefForm :: (Id, OpType) -> (Id, [VAR_DECL], TERM f, [(Id, OpType)])
-> Maybe (Named (FORMULA f))
makeUndefForm (s, ty) (i, vs, t, sels) =
if any ( \ (se, ts) -> s == se && opRes ts == opRes ty ) sels
Just $ NamedSen ("ga_selector_undef_" ++ showId s "_"
(Application (Qual_op_name s (toOP_TYPE ty) p) [t] p)
getAltSubsorts :: ALTERNATIVE -> [SORT]
getAltSubsorts c = case c of
getConsType :: SORT -> ALTERNATIVE -> (Id, OpType, [COMPONENTS])
let getConsTypeAux (part, i, il) =
(i, OpType part (concatMap
(map (opRes . snd) . getCompType s) il) s, il)
Subsorts _ _ -> error "getConsType"
Alt_construct k a l _ -> getConsTypeAux (k, a, l)
getCompType :: SORT -> COMPONENTS -> [(Maybe Id, OpType)]
getCompType s (Cons_select k l cs _) =
map (\ i -> (Just i, OpType k [s] cs)) l
getCompType s (Sort cs) = [(Nothing, OpType Partial [s] cs)]
genSelVars :: String -> Int -> [OpType] -> [VAR_DECL]
genSelVars str n (ty:rs) =
Var_decl [mkSelVar str n] (opRes ty) [] : genSelVars str (n+1) rs
mkSelVar :: String -> Int -> Token
mkSelVar str n = mkSimpleId (str ++ show n)
makeSelForms :: Int -> (Id, [VAR_DECL], TERM f, [(Maybe Id, OpType)])
makeSelForms _ (_, _, _, []) = []
makeSelForms n (i, vs, t, (mi, ty):rs) =
Just j -> let p = posOfId j
[NamedSen ("ga_selector_" ++ showId j "") True
(Application (Qual_op_name j (toOP_TYPE ty) p) [t] p)
(Qual_var (mkSelVar "X" n) rty q) p) p]
) ++ makeSelForms (n+1) (i, vs, t, rs)
selForms1 :: String -> (Id, OpType, [COMPONENTS])
-> (Id, [VAR_DECL], TERM f, [(Maybe Id, OpType)])
selForms1 str (i, ty, il) =
let cs = concatMap (getCompType (opRes ty)) il
vs = genSelVars str 1 $ map snd cs
in (i, vs, Application (Qual_op_name i (toOP_TYPE ty) [])
(map toQualVar vs) [], cs)
toQualVar :: VAR_DECL -> TERM f
toQualVar (Var_decl v s ps) =
if isSingle v then Qual_var (head v) s ps else error "toQualVar"
selForms :: (Id, OpType, [COMPONENTS]) -> [Named (FORMULA f)]
selForms = makeSelForms 1 . selForms1 "X"
-- | return the constructor and the set of total selectors
ana_ALTERNATIVE :: SORT -> ALTERNATIVE
-> State (Sign f e) (Maybe (Component,
Set.Set Component))
do mapM_ (addSubsort s) ss
_ -> do let cons@(i, ty, il) = getConsType s c
ul <- mapM (ana_COMPONENTS s) il
let ts = concatMap fst ul
addDiags $ checkUniqueness (ts ++ concatMap snd ul)
addSentences $ selForms cons
-- | return total and partial selectors
ana_COMPONENTS :: SORT -> COMPONENTS
-> State (Sign f e) ([Component], [Component])
sels <- mapM ( \ (mi, ty) ->
Nothing -> return Nothing
return $ Just $ Component i ty) cs
return $ partition ((==Total) . opKind . compType) $ catMaybes sels
resultToState :: (a -> Result a) -> a -> State (Sign f e) a
-- wrap it all up for a logic
type Ana b f e = GlobalAnnos -> b -> State (Sign f e) b
class (PrettyPrint f, PosItem f) => Resolver f where
putParen :: f -> f -- ^ put parenthesis around mixfix terms
mixResolve :: MixResolve f -- ^ resolve mixfix terms
checkMix :: (f -> Bool) -- ^ check if a formula extension has been
-- analysed completely by mixfix resolution
putInj :: f -> f -- ^ insert injections
anaForm :: Resolver f => Min f e -> GlobalAnnos ->
Set.Set Id ->
Set.Set Id
-> Sign f e -> (FORMULA f) -> Result (FORMULA f, FORMULA f)
anaForm mef ga ops preds sign f = do
resF <- resolveFormula putParen mixResolve ga ops preds f
anaF <- minExpFORMULA mef ga sign
$ assert (noMixfixF checkMix resF) resF
anaTerm :: Resolver f => Min f e -> GlobalAnnos ->
Set.Set Id ->
Set.Set Id
-> Sign f e -> SORT -> [Pos] -> (TERM f) -> Result (TERM f, TERM f)
anaTerm mef ga ops preds sign srt pos t = do
resT <- resolveMixfix putParen mixResolve ga ops preds t
anaT <- oneExpTerm mef ga sign
$ assert (noMixfixT checkMix resT) $ Sorted_term resT srt pos
basicAnalysis :: Resolver f
=> Min f e -- ^ type analysis of f
-> Ana b f e -- ^ static analysis of basic item b
-> Ana s f e -- ^ static analysis of signature item s
-> (e -> e -> e) -- ^ difference of signature extension e
-> (BASIC_SPEC b s f, Sign f e, GlobalAnnos)
-> Result (BASIC_SPEC b s f, Sign f e, Sign f e, [Named (FORMULA f)])
basicAnalysis mef anab anas dif (bs, inSig, ga) =
let (newBs, accSig) = runState (ana_BASIC_SPEC mef anab anas ga bs)
ds = reverse $ envDiags accSig
sents = reverse $ sentences accSig
cleanSig = accSig { envDiags = [], sentences = [], varMap =
Map.empty }
diff = diffSig cleanSig inSig
{ extendedInfo = dif (extendedInfo accSig) $ extendedInfo inSig }
in Result ds $ Just (newBs, diff, cleanSig, sents)
basicCASLAnalysis :: (BASIC_SPEC () () (), Sign () (), GlobalAnnos)
-> Result (BASIC_SPEC () () (), Sign () (),
Sign () (), [Named (FORMULA ())])
basicAnalysis (const $ const return) (const return) (const return) const
instance Resolver () where
mixResolve = const $ const return