1492N/A{-# LANGUAGE DeriveDataTypeable #-}
1492N/ADescription : The OMDoc Data Types
1492N/ACopyright : (c) Ewaryst Schulz, DFKI 2008
1492N/AMaintainer : Ewaryst.Schulz@dfki.de
1492N/ADatatypes for an intermediate OMDoc representation.
1492N/A OMDoc represented in 3 layers:
1492N/A 2. theory constitutive (axiom, symbol)
1492N/A 3. subelements (insort, ...) and OpenMath
1492N/A-- -------------------- Datatypes for Representation ----------------------
1492N/A-- | OMDoc root element with libname and a list of toplevel elements
1492N/Adata OMDoc = OMDoc String [TLElement] deriving (Show, Read, Eq, Ord)
1492N/A-- | Toplevel elements for OMDoc, theory with name, meta and content,
1492N/A-- view with from, to and morphism
1492N/Adata TLElement = TLTheory String (Maybe OMCD) [TCElement]
1492N/A | TLView String OMCD OMCD TCMorphism
deriving (Show, Read, Eq, Ord)
-- | Theory constitutive elements for OMDoc
-- | Symbol to represent sorts, constants, predicate symbols, etc.
TCSymbol String OMElement SymbolRole (Maybe OMElement)
-- | A notation for the given symbol
| TCNotation OMQualName String
-- | Import statements for referencing other theories
| TCImport String OMCD TCMorphism
-- | A comment, only for development purposes
deriving (Show, Read, Eq, Ord)
-- | return type for sentence translation (ADT or formula)
type TCorOMElement = Either TCElement OMElement
-- | Morphisms to specify signature mappings
type TCMorphism = [(OMName, OMImage)]
-- | The target type of a mapping is just an alias or an assignment to
type OMImage = Either String OMElement
-- | The flattened structure of an Algebraic Data Type
-- | A single sort given by name, type and a list of constructors
ADTSortDef String ADTType [OmdADT]
-- | A constructor given by its name and a list of arguments
| ADTConstr String [OmdADT]
-- | An argument with type and evtually a selector
| ADTArg OMElement (Maybe OmdADT)
-- | The selector has a name and is total (Yes) or partial (No)
| ADTSelector String Totality
-- | Insort elements point to other sortdefs and inherit their structure
deriving (Show, Read, Eq, Ord)
-- | Roles of the declared symbols can be object or type
data SymbolRole = Obj | Typ | Axiom | Theorem deriving (Eq, Ord)
-- | Type of the algebraic data type
data ADTType = Free | Generated deriving (Eq, Ord)
-- | Totality for selectors of an adt
data Totality = Yes | No deriving (Eq, Ord)
instance Show SymbolRole where
instance Show ADTType where
show Generated = "generated"
instance Show Totality where
instance Read SymbolRole where
readsPrec _ = readShow [Obj, Typ, Axiom, Theorem]
instance Read ADTType where
readsPrec _ = readShow [Free, Generated]
instance Read Totality where
readsPrec _ = readShow [Yes, No]
-- | Names used for OpenMath variables and symbols
data OMName = OMName { name :: String, path :: [String] }
deriving (Show, Read, Eq, Ord, Typeable)
-- of a type-annotated term
data OMAttribute = OMAttr OMElement OMElement
deriving (Show, Read, Eq, Ord)
-- | CD contains the reference to the content dictionary
-- and eventually the cdbase entry
data OMCD = CD [String] deriving (Show, Read, Eq, Ord)
type OMQualName = (OMCD, OMName)
-- | Elements for Open Math
-- | Attributed element needed for type annotations of elements
| OMATTT OMElement OMAttribute
-- | Application to a list of arguments,
-- first argument is usually the functionhead
-- | Bindersymbol, bound vars, body
| OMBIND OMElement [OMElement] OMElement
deriving (Show, Read, Eq, Ord)
nameToId = parseString parseAnnoId
nameToToken :: String -> Token
-- * Utils for Translation
type UniqName = (String, Int)
type NameMap a =
Map.Map a UniqName
-- | Mapping of symbols and sentence names to unique ids (used in export)
data SigMap a = SigMap (NameMap a) (NameMap String)
-- | Mapping of OMDoc names to hets strings, for signature creation,
-- and strings to symbols, for lookup in terms (used in import)
data SigMapI a = SigMapI { sigMapISymbs ::
Map.Map OMName a
, sigMapINotations ::
Map.Map OMName String }
sigMapSymbs :: SigMap a -> NameMap a
sigMapSymbs (SigMap sm _) = sm
cdFromList :: [String] -> OMCD
cdFromList ["", ""] = CD []
cdFromList ["", cd] = CD [cd]
cdFromList [base, cd] = CD [cd, base]
cdFromList _ = error "cdFromList: Malformed list. I need exactly 2 elements!"
cdIsEmpty :: OMCD -> Bool
cdIsEmpty cd = ["", ""] == cdToList cd
-- | The result list has always two elements: [base, modul]
cdToList :: OMCD -> [String]
cdToList (CD [cd, base]) = [base, cd]
cdToList (CD [cd]) = ["", cd]
cdToMaybeList :: OMCD -> [Maybe String]
cdToMaybeList (CD [cd, base]) = [Just base, Just cd]
cdToMaybeList (CD [cd]) = [Nothing, Just cd]
cdToMaybeList _ = [Nothing, Nothing]
-- * Name handling: encoding, decoding, unique names
-- | The closing paren + percent can be used neither in ordinary Hets-names
-- nor in sentence names hence it is used here for encodings.
-- | Special name encoding in order to be able to recognize these names
nameEncode :: String -- ^ the kind of the encoding, may not contain colons
-> [String] -- ^ the values to encode
nameEncode s l = concat [uniqPrefix, s, ":", intercalate uniqPrefix l]
-- | This invariant should hold:
-- @(x, l) = fromJust $ nameDecode $ nameEncode x l@
nameDecode :: String -> Maybe (String, [String])
case stripPrefix uniqPrefix s of
let (kind, r) = break (== ':') s'
then error $ "nameDecode: missing colon in " ++ s
else Just (kind, splitByList uniqPrefix $ tail r)
nameToString :: UniqName -> String
let s' = escapeURIString (\ c -> not $ elem c "/?%") s
in if i > 0 then nameEncode ("over_" ++ show i) [s'] else s'
-- | name of the theory constitutive element, error if not TCSymbol, TCNotation,
tcName :: TCElement -> OMName
TCSymbol s _ _ _ -> mkSimpleName s
TCNotation qn _ -> unqualName qn
TCImport s _ _ -> mkSimpleName s
_ -> error "tcName: No name for this value."
unqualName :: OMQualName -> OMName
omName :: UniqName -> OMName
omName = mkSimpleName . nameToString
mkSimpleName :: String -> OMName
mkSimpleName s = OMName s []
mkSimpleQualName :: UniqName -> OMQualName
mkSimpleQualName un = (CD [], omName un)
simpleOMS :: UniqName -> OMElement
simpleOMS = OMS . mkSimpleQualName
-- * Lookup utils for Import and Export
lookupNotation :: SigMapI a -> OMName -> String
lookupNotation smi = lookupNotationInMap $ sigMapINotations smi
lookupNotationInMap ::
Map.Map OMName String -> OMName -> String
instance Pretty OMName where