Grothendieck.hs revision bba8f93f73808355208a81d9d3446be84b37c93d
f4505a64a089693012a3f5c3b1f12a82cd7a2a5aKlaus Luettich{-# OPTIONS -fallow-overlapping-instances -fallow-incoherent-instances #-}
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder{- |
c63ebf815c8a874525cf18670ad74847f7fc7b26Christian MaederModule : $Header$
c092fcac4b8f5c524c22ca579189c4487c13edf7Christian MaederCopyright : (c) Till Mossakowski, and Uni Bremen 2002-2004
fefee7e1dee1ee5f0768a03a4abae88d1ca2c3fdRazvan PascanuLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
75a6279dbae159d018ef812185416cf6df386c10Till Mossakowski
3f69b6948966979163bdfe8331c38833d5d90ecdChristian MaederMaintainer : till@tzi.de
75a6279dbae159d018ef812185416cf6df386c10Till MossakowskiStability : provisional
75a6279dbae159d018ef812185416cf6df386c10Till MossakowskiPortability : non-portable (overlapping instances, dynamics, existentials)
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder
c092fcac4b8f5c524c22ca579189c4487c13edf7Christian MaederThe Grothendieck logic is defined to be the
dda5ab793f1615c1ba1dcaa97a4346b0878da6b1Christian Maeder heterogeneous logic over the logic graph.
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder This will be the logic over which the data
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder structures and algorithms for specification in-the-large
da955132262baab309a50fdffe228c9efe68251dCui Jian are built.
dda5ab793f1615c1ba1dcaa97a4346b0878da6b1Christian Maeder
dda5ab793f1615c1ba1dcaa97a4346b0878da6b1Christian Maeder This module heavily works with existential types, see
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder <http://haskell.org/hawiki/ExistentialTypes> and chapter 7 of /Heterogeneous
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder specification and the heterogeneous tool set/
0799b5dc3f06d2640e66e9ab54b8b217348fd719Christian Maeder (<http://www.tzi.de/~till/papers/habil.ps>).
0799b5dc3f06d2640e66e9ab54b8b217348fd719Christian Maeder
43b4c41fbb07705c9df321221ab9cb9832460407Christian Maeder References:
a1c6679d00e15a949730ab640159e0adc5b0e3e7Christian Maeder
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski R. Diaconescu:
fa388aea9cef5f9734fec346159899a74432ce26Christian Maeder Grothendieck institutions
3c4e64e0b4361a24275ee8f308fa965ab1e52f2eHeng Jiang J. applied categorical structures 10, 2002, p. 383-402.
fa388aea9cef5f9734fec346159899a74432ce26Christian Maeder
0799b5dc3f06d2640e66e9ab54b8b217348fd719Christian Maeder T. Mossakowski:
59fddbdd745a05adeaf655b617ab8da947903832Christian Maeder Comorphism-based Grothendieck institutions.
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowski In K. Diks, W. Rytter (Eds.), Mathematical foundations of computer science,
cfaa17b0d2ed0a663f0a4f18b23f98ab876c2f40Christian Maeder LNCS 2420, pp. 593-604
f77f29e84b3f6e791c82e61b13fbf76582bedd2fChristian Maeder
dda5ab793f1615c1ba1dcaa97a4346b0878da6b1Christian Maeder T. Mossakowski:
f77f29e84b3f6e791c82e61b13fbf76582bedd2fChristian Maeder Heterogeneous specification and the heterogeneous tool set.
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder
c0c2380bced8159ff0297ece14eba948bd236471Christian Maeder Todo:
da333ffa6336cf59a4071fcddad358c5eafd3e61Sonja Gröning compComorphism: cancellation of id comorphisms if target sublogic is
c0c2380bced8159ff0297ece14eba948bd236471Christian Maeder not increased
5b50a0fb9834540caf04471389df975fbd356ca3Razvan Pascanu gWeaklyAmalgamableCocone
6352f3c31da3043783a13be6594aacb2147378baRazvan Pascanu Transportability for heterogeneous morphisms
16281c97e3e04f9f88c806d7da79feb106dbee2aTill Mossakowski-}
66a774f13272fde036481edd2298081ab3d04678Razvan Pascanu
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maedermodule Logic.Grothendieck where
74885352ea11b26253d453af28dc904dadc4d530Christian Maeder
dda5ab793f1615c1ba1dcaa97a4346b0878da6b1Christian Maederimport Logic.Logic
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Logic.Prover
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Logic.Comorphism
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Logic.Morphism
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Logic.Coerce
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Common.PrettyPrint
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Common.Lib.Pretty
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport qualified Data.Graph.Inductive.Tree as Tree
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport qualified Common.Lib.Map as Map
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport qualified Common.Lib.Set as Set
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Common.Result
8955ea64c61a6e8b96b7696021863afd1a75f6daRazvan Pascanuimport Common.DynamicUtils
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Data.Dynamic
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport qualified Data.List as List
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Data.Maybe
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederimport Control.Monad
fefee7e1dee1ee5f0768a03a4abae88d1ca2c3fdRazvan Pascanu
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder------------------------------------------------------------------
fefee7e1dee1ee5f0768a03a4abae88d1ca2c3fdRazvan Pascanu--"Grothendieck" versions of the various parts of type class Logic
fefee7e1dee1ee5f0768a03a4abae88d1ca2c3fdRazvan Pascanu------------------------------------------------------------------
fefee7e1dee1ee5f0768a03a4abae88d1ca2c3fdRazvan Pascanu
ee1c7c5796832536932d7b06cbfb1ca13f9a0d7bMartin Kühl-- | Grothendieck basic specifications
ee1c7c5796832536932d7b06cbfb1ca13f9a0d7bMartin Kühldata G_basic_spec = forall lid sublogics
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder basic_spec sentence symb_items symb_map_items
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder sign morphism symbol raw_symbol proof_tree .
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder Logic lid sublogics
c0c2380bced8159ff0297ece14eba948bd236471Christian Maeder basic_spec sentence symb_items symb_map_items
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder sign morphism symbol raw_symbol proof_tree =>
c0c2380bced8159ff0297ece14eba948bd236471Christian Maeder G_basic_spec lid basic_spec
fa388aea9cef5f9734fec346159899a74432ce26Christian Maeder
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederinstance PrettyPrint G_basic_spec where
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder printText0 ga (G_basic_spec _ s) = printText0 ga s
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder
fa388aea9cef5f9734fec346159899a74432ce26Christian Maeder-- | Grothendieck signatures
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederdata G_sign = forall lid sublogics
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder basic_spec sentence symb_items symb_map_items
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder sign morphism symbol raw_symbol proof_tree .
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder Logic lid sublogics
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder basic_spec sentence symb_items symb_map_items
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder sign morphism symbol raw_symbol proof_tree =>
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder G_sign lid sign
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian MaedertyconG_sign :: TyCon
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian MaedertyconG_sign = mkTyCon "Logic.Grothendieck.G_sign"
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederinstance Typeable G_sign where
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder typeOf _ = mkTyConApp tyconG_sign []
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederinstance Eq G_sign where
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder (G_sign i1 sigma1) == (G_sign i2 sigma2) =
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder coerceSign i1 i2 "Eq G_sign" sigma1 == Just sigma2
f77f29e84b3f6e791c82e61b13fbf76582bedd2fChristian Maeder
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder-- | prefer a faster subsignature test if possible
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederis_subgsign :: G_sign -> G_sign -> Bool
f1dec6898638ba1131a9fadbc4d1544c93dfabb0Klaus Luettichis_subgsign (G_sign i1 sigma1) (G_sign i2 sigma2) =
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder maybe False (is_subsig i1 sigma1) $ coerceSign i2 i1 "is_subgsign" sigma2
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maederinstance PrettyPrint G_sign where
f77f29e84b3f6e791c82e61b13fbf76582bedd2fChristian Maeder printText0 ga (G_sign _ s) = printText0 ga s
langNameSig :: G_sign -> String
langNameSig (G_sign lid _) = language_name lid
-- | Grothendieck signature lists
data G_sign_list = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_sign_list lid [sign]
-- | Grothendieck extended signatures
data G_ext_sign = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_ext_sign lid sign (Set.Set symbol)
-- | Grothendieck symbols
data G_symbol = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_symbol lid symbol
instance PrettyPrint G_symbol where
printText0 ga (G_symbol _ s) = printText0 ga s
instance Eq G_symbol where
(G_symbol i1 s1) == (G_symbol i2 s2) =
language_name i1 == language_name i2 && coerceSymbol i1 i2 s1 == s2
-- | Grothendieck symbol lists
data G_symb_items_list = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_symb_items_list lid [symb_items]
instance PrettyPrint G_symb_items_list where
printText0 ga (G_symb_items_list _ l) =
fsep $ punctuate comma $ map (printText0 ga) l
instance Eq G_symb_items_list where
(G_symb_items_list i1 s1) == (G_symb_items_list i2 s2) =
coerceSymbItemsList i1 i2 "Eq G_symb_items_list" s1 == Just s2
-- | Grothendieck symbol maps
data G_symb_map_items_list = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_symb_map_items_list lid [symb_map_items]
instance PrettyPrint G_symb_map_items_list where
printText0 ga (G_symb_map_items_list _ l) =
fsep $ punctuate comma $ map (printText0 ga) l
instance Eq G_symb_map_items_list where
(G_symb_map_items_list i1 s1) == (G_symb_map_items_list i2 s2) =
coerceSymbMapItemsList i1 i2 "Eq G_symb_map_items_list" s1 == Just s2
-- | Grothendieck diagrams
data G_diagram = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_diagram lid (Tree.Gr sign morphism)
-- | Grothendieck sublogics
data G_sublogics = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_sublogics lid sublogics
tyconG_sublogics :: TyCon
tyconG_sublogics = mkTyCon "Logic.Grothendieck.G_sublogics"
instance Typeable G_sublogics where
typeOf _ = mkTyConApp tyconG_sublogics []
instance Show G_sublogics where
show (G_sublogics lid sub) = case sublogic_names lid sub of
[] -> error "show G_sublogics"
h : _ -> show lid ++ "." ++ h
instance Eq G_sublogics where
(G_sublogics lid1 l1) == (G_sublogics lid2 l2) =
language_name lid1 == language_name lid2
&& coerceSublogic lid1 lid2 l1 == l2
instance Ord G_sublogics where
compare (G_sublogics lid1 l1) (G_sublogics lid2 l2) =
compare (coerceSublogic lid1 lid2 l1) l2
-- | Homogeneous Grothendieck signature morphisms
data G_morphism = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_morphism lid morphism
----------------------------------------------------------------
-- Existential types for the logic graph
----------------------------------------------------------------
--- Comorphisms ----
-- | Existential type for comorphisms
data AnyComorphism = forall cid lid1 sublogics1
basic_spec1 sentence1 symb_items1 symb_map_items1
sign1 morphism1 symbol1 raw_symbol1 proof_tree1
lid2 sublogics2
basic_spec2 sentence2 symb_items2 symb_map_items2
sign2 morphism2 symbol2 raw_symbol2 proof_tree2 .
Comorphism cid
lid1 sublogics1 basic_spec1 sentence1
symb_items1 symb_map_items1
sign1 morphism1 symbol1 raw_symbol1 proof_tree1
lid2 sublogics2 basic_spec2 sentence2
symb_items2 symb_map_items2
sign2 morphism2 symbol2 raw_symbol2 proof_tree2 =>
Comorphism cid
instance Eq AnyComorphism where
Comorphism cid1 == Comorphism cid2 =
constituents cid1 == constituents cid2
-- need to be refined, using comorphism translations !!!
instance Show AnyComorphism where
show (Comorphism cid) =
language_name cid
++" : "++language_name (sourceLogic cid)
++" -> "++language_name (targetLogic cid)
tyconAnyComorphism :: TyCon
tyconAnyComorphism = mkTyCon "Logic.Grothendieck.AnyComorphism"
instance Typeable AnyComorphism where
typeOf _ = mkTyConApp tyconAnyComorphism []
-- | compute the identity comorphism for a logic
idComorphism :: AnyLogic -> AnyComorphism
idComorphism (Logic lid) = Comorphism (IdComorphism lid (top_sublogic lid))
-- | Test whether a comporphism is the identity
isIdComorphism :: AnyComorphism -> Bool
isIdComorphism (Comorphism cid) =
constituents cid == []
-- | Compose comorphisms
compComorphism :: Monad m => AnyComorphism -> AnyComorphism -> m AnyComorphism
compComorphism (Comorphism cid1) (Comorphism cid2) =
let l1 = targetLogic cid1
l2 = sourceLogic cid2 in
if language_name l1 == language_name l2 then
if coerceSublogic l1 l2 (targetSublogic cid1) <= sourceSublogic cid2
then {- case (isIdComorphism cm1,isIdComorphism cm2) of
(True,_) -> return cm2
(_,True) -> return cm1
_ -> -} return $ Comorphism (CompComorphism cid1 cid2)
else fail ("Sublogic mismatch in composition of "++language_name cid1++
" and "++language_name cid2)
else fail ("Logic mismatch in composition of "++language_name cid1++
" and "++language_name cid2)
--- Morphisms ---
-- | Existential type for morphisms
data AnyMorphism = forall cid lid1 sublogics1
basic_spec1 sentence1 symb_items1 symb_map_items1
sign1 morphism1 symbol1 raw_symbol1 proof_tree1
lid2 sublogics2
basic_spec2 sentence2 symb_items2 symb_map_items2
sign2 morphism2 symbol2 raw_symbol2 proof_tree2 .
Morphism cid
lid1 sublogics1 basic_spec1 sentence1
symb_items1 symb_map_items1
sign1 morphism1 symbol1 raw_symbol1 proof_tree1
lid2 sublogics2 basic_spec2 sentence2
symb_items2 symb_map_items2
sign2 morphism2 symbol2 raw_symbol2 proof_tree2 =>
Morphism cid
{-
instance Eq AnyMorphism where
Morphism cid1 == Morphism cid2 =
constituents cid1 == constituents cid2
-- need to be refined, using morphism translations !!!
instance Show AnyMorphism where
show (Morphism cid) =
language_name cid
++" : "++language_name (sourceLogic cid)
++" -> "++language_name (targetLogic cid)
-}
tyconAnyMorphism :: TyCon
tyconAnyMorphism = mkTyCon "Logic.Grothendieck.AnyMorphism"
instance Typeable AnyMorphism where
typeOf _ = mkTyConApp tyconAnyMorphism []
-- | Logic graph
data LogicGraph = LogicGraph {
logics :: Map.Map String AnyLogic,
comorphisms :: Map.Map String AnyComorphism,
inclusions :: Map.Map (String,String) AnyComorphism,
unions :: Map.Map (String,String) (AnyComorphism,AnyComorphism)
}
emptyLogicGraph :: LogicGraph
emptyLogicGraph = LogicGraph Map.empty Map.empty Map.empty Map.empty
-- | find a logic in a logic graph
lookupLogic :: Monad m => String -> String -> LogicGraph -> m AnyLogic
lookupLogic error_prefix logname logicGraph =
case Map.lookup logname (logics logicGraph) of
Nothing -> fail (error_prefix++" in LogicGraph logic \""
++logname++"\" unknown")
Just lid -> return lid
-- | union to two logics
logicUnion :: LogicGraph -> AnyLogic -> AnyLogic -> Result (AnyComorphism,AnyComorphism)
logicUnion lg l1@(Logic lid1) l2@(Logic lid2) =
case logicInclusion lg l1 l2 of
Result _ (Just c) -> return (c,idComorphism l2)
_ -> case logicInclusion lg l2 l1 of
Result _ (Just c) -> return (idComorphism l1,c)
_ -> case Map.lookup (ln1,ln2) (unions lg) of
Just u -> return u
Nothing -> case Map.lookup (ln2,ln1) (unions lg) of
Just (c2,c1) -> return (c1,c2)
Nothing -> fail ("Union of logics "++ln1++" and "++ln2++" does not exist")
where ln1 = language_name lid1
ln2 = language_name lid2
-- | split list at separator elements, avoid empty sublists
splitBy :: Eq a => a -> [a] -> [[a]]
splitBy x xs = let (l, r) = break (==x) xs in
(if null l then [] else [l]) ++ (if null r then [] else splitBy x $ tail r)
-- suffix "By" usually indicates a (a -> a -> Bool) argument instead of Eq
-- | find a comorphism in a logic graph
lookupComorphism :: Monad m => String -> LogicGraph -> m AnyComorphism
lookupComorphism coname logicGraph = do
let nameList = splitBy ';' coname
cs <- sequence $ map lookupN nameList
case cs of
c:cs1 -> foldM compComorphism c cs1
_ -> fail ("Illgegal comorphism name: "++coname)
where
lookupN name =
case name of
'i':'d':'_':logic -> do
let mainLogic = takeWhile (/= '.') logic
l <- maybe (fail ("Cannot find Logic "++mainLogic)) return
$ Map.lookup mainLogic (logics logicGraph)
return $ idComorphism l
_ -> maybe (fail ("Cannot find logic comorphism "++name)) return
$ Map.lookup name (comorphisms logicGraph)
------------------------------------------------------------------
-- The Grothendieck signature category
------------------------------------------------------------------
-- | Grothendieck signature morphisms
data GMorphism = forall cid lid1 sublogics1
basic_spec1 sentence1 symb_items1 symb_map_items1
sign1 morphism1 symbol1 raw_symbol1 proof_tree1
lid2 sublogics2
basic_spec2 sentence2 symb_items2 symb_map_items2
sign2 morphism2 symbol2 raw_symbol2 proof_tree2 .
Comorphism cid
lid1 sublogics1 basic_spec1 sentence1
symb_items1 symb_map_items1
sign1 morphism1 symbol1 raw_symbol1 proof_tree1
lid2 sublogics2 basic_spec2 sentence2
symb_items2 symb_map_items2
sign2 morphism2 symbol2 raw_symbol2 proof_tree2 =>
GMorphism cid sign1 morphism2
instance Eq GMorphism where
GMorphism cid1 sigma1 mor1 == GMorphism cid2 sigma2 mor2
= Comorphism cid1 == Comorphism cid2
&& coerceSign (sourceLogic cid1) (sourceLogic cid2)
"Eq GMorphism.coerceSign" sigma1 == Just sigma2
&& coerceMorphism (targetLogic cid1) (targetLogic cid2)
"Eq GMorphism.coerceMorphism" mor1 == Just mor2
isHomogeneous :: GMorphism -> Bool
isHomogeneous (GMorphism cid _ _) =
isIdComorphism (Comorphism cid)
data Grothendieck = Grothendieck deriving Show
instance Language Grothendieck
instance PrettyPrint GMorphism where
printText0 ga (GMorphism cid s m) =
ptext (show cid)
<+> -- ptext ":" <+> ptext (show (sourceLogic cid)) <+>
-- ptext "->" <+> ptext (show (targetLogic cid)) <+>
ptext "(" <+> printText0 ga s <+> ptext ")"
$$
printText0 ga m
instance Category Grothendieck G_sign GMorphism where
ide _ (G_sign lid sigma) =
GMorphism (IdComorphism lid (top_sublogic lid)) sigma (ide lid sigma)
comp _
(GMorphism r1 sigma1 mor1)
(GMorphism r2 _sigma2 mor2) =
do let lid2 = targetLogic r1
lid3 = sourceLogic r2
lid4 = targetLogic r2
mor1' <- coerceMorphism lid2 lid3 "Grothendieck.comp" mor1
mor1'' <- map_morphism r2 mor1'
mor <- comp lid4 mor1'' mor2
return (GMorphism (CompComorphism r1 r2) sigma1 mor)
dom _ (GMorphism r sigma _mor) =
G_sign (sourceLogic r) sigma
cod _ (GMorphism r _sigma mor) =
G_sign lid2 (cod lid2 mor)
where lid2 = targetLogic r
legal_obj _ (G_sign lid sigma) = legal_obj lid sigma
legal_mor _ (GMorphism r sigma mor) =
legal_mor lid2 mor &&
case maybeResult $ map_sign r sigma of
Just (sigma',_) -> sigma' == cod lid2 mor
Nothing -> False
where lid2 = targetLogic r
-- | Embedding of homogeneous signature morphisms as Grothendieck sig mors
gEmbed :: G_morphism -> GMorphism
gEmbed (G_morphism lid mor) =
GMorphism (IdComorphism lid (top_sublogic lid)) (dom lid mor) mor
-- | Embedding of comorphisms as Grothendieck sig mors
gEmbedComorphism :: AnyComorphism -> G_sign -> Result GMorphism
gEmbedComorphism (Comorphism cid) (G_sign lid sig) = do
sig' <- coerceSign lid (sourceLogic cid) "gEmbedComorphism" sig
(sigTar,_) <- map_sign cid sig'
let lidTar = targetLogic cid
return (GMorphism cid sig'(ide lidTar sigTar))
-- | heterogeneous union of two Grothendieck signatures
gsigUnion :: LogicGraph -> G_sign -> G_sign -> Result G_sign
gsigUnion lg gsig1@(G_sign lid1 sigma1) gsig2@(G_sign lid2 sigma2) =
if language_name lid1 == language_name lid2
then homogeneousGsigUnion gsig1 gsig2
else do
(Comorphism cid1,Comorphism cid2) <- logicUnion lg (Logic lid1) (Logic lid2)
let lidS1 = sourceLogic cid1
lidS2 = sourceLogic cid2
lidT1 = targetLogic cid1
lidT2 = targetLogic cid2
sigma1' <- coerceSign lid1 lidS1 "Union of signaturesa" sigma1
sigma2' <- coerceSign lid2 lidS2 "Union of signaturesb" sigma2
(sigma1'',_) <- map_sign cid1 sigma1' -- where to put axioms???
(sigma2'',_) <- map_sign cid2 sigma2' -- where to put axioms???
sigma2''' <- coerceSign lidT2 lidT1 "Union of signaturesc" sigma2''
sigma3 <- signature_union lidT1 sigma1'' sigma2'''
return (G_sign lidT1 sigma3)
-- | homogeneous Union of two Grothendieck signatures
homogeneousGsigUnion :: G_sign -> G_sign -> Result G_sign
homogeneousGsigUnion (G_sign lid1 sigma1) (G_sign lid2 sigma2) = do
sigma2' <- coerceSign lid2 lid1 "Union of signaturesd" sigma2
sigma3 <- signature_union lid1 sigma1 sigma2'
return (G_sign lid1 sigma3)
-- | union of a list of Grothendieck signatures
gsigManyUnion :: LogicGraph -> [G_sign] -> Result G_sign
gsigManyUnion _ [] =
fail "union of emtpy list of signatures"
gsigManyUnion lg (gsigma : gsigmas) =
foldM (gsigUnion lg) gsigma gsigmas
-- | homogeneous Union of a list of morphisms
homogeneousMorManyUnion :: [G_morphism] -> Result G_morphism
homogeneousMorManyUnion [] =
fail "homogeneous union of emtpy list of morphisms"
homogeneousMorManyUnion (gmor : gmors) =
foldM ( \ (G_morphism lid2 mor2) (G_morphism lid1 mor1) -> do
mor1' <- coerceMorphism lid1 lid2 "homogeneousMorManyUnion" mor1
mor <- morphism_union lid2 mor1' mor2
return (G_morphism lid2 mor)) gmor gmors
-- | inclusion between two logics
logicInclusion :: LogicGraph -> AnyLogic -> AnyLogic -> Result AnyComorphism
logicInclusion logicGraph l1@(Logic lid1) (Logic lid2) =
let ln1 = language_name lid1
ln2 = language_name lid2 in
if ln1==ln2 then
return (idComorphism l1)
else case Map.lookup (ln1,ln2) (inclusions logicGraph) of
Just (Comorphism i) ->
return (Comorphism i)
Nothing ->
fail ("No inclusion from "++ln1++" to "++ln2++" found")
-- | inclusion morphism between two Grothendieck signatures
ginclusion :: LogicGraph -> G_sign -> G_sign -> Result GMorphism
ginclusion logicGraph (G_sign lid1 sigma1) (G_sign lid2 sigma2) = do
Comorphism i <- logicInclusion logicGraph (Logic lid1) (Logic lid2)
sigma1' <- coerceSign lid1 (sourceLogic i) "Inclusion of signatures" sigma1
(sigma1'',_) <- map_sign i sigma1'
sigma2' <- coerceSign lid2 (targetLogic i) "Inclusion of signatures" sigma2
mor <- inclusion (targetLogic i) sigma1'' sigma2'
return (GMorphism i sigma1' mor)
-- | Composition of two Grothendieck signature morphisms
-- | with itermediate inclusion
compInclusion :: LogicGraph -> GMorphism -> GMorphism -> Result GMorphism
compInclusion lg mor1 mor2 = do
incl <- ginclusion lg (cod Grothendieck mor1) (dom Grothendieck mor2)
mor <- comp Grothendieck mor1 incl
comp Grothendieck mor mor2
-- | Composition of two Grothendieck signature morphisms
-- | with itermediate homogeneous inclusion
compHomInclusion :: GMorphism -> GMorphism -> Result GMorphism
compHomInclusion mor1 mor2 = compInclusion emptyLogicGraph mor1 mor2
-- | Find all (composites of) comorphisms starting from a given logic
findComorphismPaths :: LogicGraph -> G_sublogics -> [AnyComorphism]
findComorphismPaths lg (G_sublogics lid sub) =
List.nub $ map fst $ iterateComp (0::Int) [(idc,[idc])]
where
idc = Comorphism (IdComorphism lid sub)
coMors = Map.elems $ comorphisms lg
-- compute possible compositions, but only up to depth 3
iterateComp n l = -- (l::[(AnyComorphism,[AnyComorphism])]) =
if n>3 || l==newL then newL else iterateComp (n+1) newL
where
newL = List.nub (l ++ (concat (map extend l)))
-- extend comorphism list in all directions, but no cylces
extend (coMor,comps) =
let addCoMor c =
case compComorphism coMor c of
Nothing -> Nothing
Just c1 -> Just (c1,c:comps)
in catMaybes $ map addCoMor $ filter (not . (`elem` comps)) $ coMors
-- | check transpotability of Grothendieck signature morphisms
-- | (currently returns false for heterogeneous morphisms)
isTransportable :: GMorphism -> Bool
isTransportable (GMorphism cid _ mor) =
isIdComorphism (Comorphism cid) && is_transportable (targetLogic cid) mor
------------------------------------------------------------------
-- Provers
------------------------------------------------------------------
-- | provers and consistency checkers for specific logics
data G_prover = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_prover lid (Prover sign sentence proof_tree)
coerceProver ::
(Logic lid1 sublogics1 basic_spec1 sentence1 symb_items1 symb_map_items1
sign1 morphism1 symbol1 raw_symbol1 proof_tree1,
Logic lid2 sublogics2 basic_spec2 sentence2 symb_items2 symb_map_items2
sign2 morphism2 symbol2 raw_symbol2 proof_tree2,
Monad m) => lid1 -> lid2 -> String -> Prover sign1 sentence1 proof_tree1
-> m (Prover sign2 sentence2 proof_tree2)
coerceProver l1 l2 msg m1 = primCoerce l1 l2 msg m1
data G_cons_checker = forall lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree .
Logic lid sublogics
basic_spec sentence symb_items symb_map_items
sign morphism symbol raw_symbol proof_tree =>
G_cons_checker lid (ConsChecker sign sentence morphism proof_tree)
coerceConsChecker ::
(Logic lid1 sublogics1 basic_spec1 sentence1 symb_items1 symb_map_items1
sign1 morphism1 symbol1 raw_symbol1 proof_tree1,
Logic lid2 sublogics2 basic_spec2 sentence2 symb_items2 symb_map_items2
sign2 morphism2 symbol2 raw_symbol2 proof_tree2,
Monad m) => lid1 -> lid2 -> String
-> ConsChecker sign1 sentence1 morphism1 proof_tree1
-> m (ConsChecker sign2 sentence2 morphism2 proof_tree2)
coerceConsChecker l1 l2 msg m1 = primCoerce l1 l2 msg m1