4752N/ADescription : create legal Isabelle mixfix identifier
4752N/ACopyright : (c) University of Cambridge, Cambridge, England
4752N/A adaption (c) Till Mossakowski, Uni Bremen 2002-2005
4752N/AMaintainer : Christian.Maeder@dfki.de
6982N/Atranslate 'Id' to Isabelle strings
4752N/A ( showIsaConstT, showIsaConstIT, showIsaTypeT, transConstStringT
6982N/A , mkIsaConstT, mkIsaConstIT, transString, isaPrelude, IsaPreludes
4752N/A------------------- Id translation functions -------------------
4752N/Adata IsaPreludes = IsaPreludes
4752N/A (HsHOLCF_thy, types holcfS), (MainHC_thy, types mainS),
4752N/A (Main_thy, types mainS), (HOLCF_thy, types holcfS)],
4752N/A [(HsHOL_thy, consts mainS),
4752N/A [pAppS, aptS, appS, defOpS, pairC]),
4752N/A (Main_thy, consts mainS), (HOLCF_thy, consts holcfS)]}
4752N/AgetAltTokenList :: String -> Int -> Id -> BaseSig -> [Token]
4752N/AgetAltTokenList newPlace over i@(Id ms cs qs) thy = let
4752N/A (fs, ps) = splitMixToken ms
4752N/A nonPlaces = filter (not . isPlace) fs
4752N/A o1 = if over2 && over == 0 then over + 1 else over
4752N/A newFs = if null fs || not over2 && over == 0 then fs else
4752N/A if o1 < 3 then replicate o1 '\'' else '_' : show o1]
4752N/A in getTokenList newPlace $ Id (newFs ++ ps) cs qs
4752N/AtoAltSyntax :: Bool -> Int -> GlobalAnnos -> Int -> Id -> BaseSig
4752N/AtoAltSyntax prd over ga n i thy = let
4752N/A minPrec = if prd then 42 else 52
4752N/A adjustPrec p = 2 * p + minPrec
4752N/A hd : tl = getAltTokenList newPlace over i thy
4752N/A convert = \ Token { tokStr = s } -> if s == newPlace then s
4752N/A Nothing -> let q = adjustPrec $ mx + 1 in (q : minL2 ++ [q], minPrec)
4752N/A else if begPlace i then let q = adjustPrec $ mx + 3 in (q : minL1 , q)
4752N/A else if endPlace i then let q = adjustPrec $ mx + 2 in (minL1 ++ [q], q)
4752N/A in if n < 0 || ni > 1 && ni /= n then Nothing
4752N/A else if n == 0 then Just $ AltSyntax ts [] maxPrio
4752N/A else if isMixfix i then Just $ AltSyntax
4752N/A ('(' : ts ++ ")") precList erg
4752N/A concat (replicate (n - 1) "_,/ ")
4752N/A ++ "_')") (replicate n 3) $ maxPrio - 1
4752N/A c : r -> (if elem c "_/'()" then '\'' : [c]
4752N/A else if elem c "\\\"" then '\\' : [c] else [c]) ++ quote r
4752N/AshowIsaT1 :: (String -> String) -> Id -> String
4752N/A in if null str then error "showIsaT1" else if
4752N/A elem (last str) "_" then str ++ "X" else str
4752N/AshowIsaConstT :: Id -> BaseSig -> String
4752N/AshowIsaConstT ide thy = showIsaT1 (transConstStringT thy) ide
4752N/A-- also pass number of arguments
4752N/AmkIsaConstT :: Bool -> GlobalAnnos -> Int -> Id -> BaseSig -> VName
4752N/AmkIsaConstT prd ga n ide = mkIsaConstVName 0 showIsaConstT prd ga n ide
4752N/AmkIsaConstVName :: Int -> (Id -> BaseSig -> String) -> Bool -> GlobalAnnos
4752N/A -> Int -> Id -> BaseSig -> VName
4752N/AmkIsaConstVName over f prd ga n ide thy =
4752N/A a = toAltSyntax prd over ga n ide thy
Just (AltSyntax as [] _) -> as == s
_ -> False then VName { new = s, altSyn = Nothing }
{ new = (if n < 0 || isMixfix ide || s /= show ide then id else ("X_" ++)) s
showIsaTypeT :: Id -> BaseSig -> String
showIsaTypeT ide thy = showIsaT1 (transTypeStringT thy) ide
-- | add a number for overloading
showIsaConstIT :: Id -> Int -> BaseSig -> String
showIsaConstIT ide i thy = showIsaConstT ide thy ++ "X" ++ show i
mkIsaConstIT :: Bool -> GlobalAnnos -> Int -> Id -> Int -> BaseSig -> VName
mkIsaConstIT prd ga n ide i =
mkIsaConstVName i ( \ ide' -> showIsaConstIT ide' i) prd ga n ide
{- | get the tokens of the alternative syntax that should not be used
getConstIsaToks :: Id -> Int -> BaseSig ->
Set.Set String
getConstIsaToks ide i thy = if i < 2 then
Set.union (getConstIsaToksAux ide 0 thy) (getConstIsaToksAux ide 1 thy)
else getConstIsaToksAux ide i thy
getConstIsaToksAux :: Id -> Int -> BaseSig ->
Set.Set String
getConstIsaToksAux ide i thy =
transIsaStringT m i s = let t = transStringAux False s in
then transIsaStringT m i $ "_" ++ s else t
transConstStringT :: BaseSig -> String -> String
transConstStringT = transIsaStringT $ preConsts isaPrelude
transTypeStringT :: BaseSig -> String -> String
transTypeStringT = transIsaStringT $ preTypes isaPrelude
-- | check for legal alphanumeric Isabelle characters
isIsaChar :: Char -> Bool
isIsaChar c = isAlphaNum c && isAscii c || elem c "_'"
-- | translate to a valid Isabelle string possibly non-injectively
transString :: String -> String
transString = transStringAux True
-- | if true don't try to be injective
transStringAux :: Bool -> String -> String
transStringAux b str = let
replaceChar1 d | not b && d == x = [x, x] -- code out existing X!
| otherwise = x : replaceChar d
"" -> error "transString"
c : s -> let l = replaceChar1 c in
(if isDigit c || elem c "_'" then [x, c]
else l) ++ concatMap replaceChar1 s
-- | injective replacement of special characters
replaceChar :: Char -> String
replaceChar c = if isIsaChar c then [c] else let n = ord c in
if n <= 32 || n >= 127 && n < 160 || n > 255 then "Slash_" ++ show n