mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-16 22:52:50 -06:00
simplify the Ident type
This commit is contained in:
@@ -311,21 +311,20 @@ linTypeOfType :: Grammar -> ModuleName -> L Type -> Check ([Ident],Ident,Context
|
|||||||
linTypeOfType cnc m (L loc typ) = do
|
linTypeOfType cnc m (L loc typ) = do
|
||||||
let (ctxt,res_cat) = typeSkeleton typ
|
let (ctxt,res_cat) = typeSkeleton typ
|
||||||
val <- lookLin res_cat
|
val <- lookLin res_cat
|
||||||
lin_args <- mapM mkLinArg (zip [0..] ctxt)
|
lin_args <- mapM mkLinArg (zip [1..] ctxt)
|
||||||
let (args,arg_cats) = unzip lin_args
|
let (args,arg_cats) = unzip lin_args
|
||||||
return (arg_cats, snd res_cat, args, val)
|
return (arg_cats, snd res_cat, args, val)
|
||||||
where
|
where
|
||||||
mkLinArg (i,(n,mc@(m,cat))) = do
|
mkLinArg (i,(n,mc@(m,cat))) = do
|
||||||
val <- lookLin mc
|
val <- lookLin mc
|
||||||
let vars = mkRecType varLabel $ replicate n typeStr
|
let vars = mkRecType varLabel $ replicate n typeStr
|
||||||
symb = argIdent n cat i
|
|
||||||
rec <- if n==0 then return val else
|
rec <- if n==0 then return val else
|
||||||
errIn (render ("extending" $$
|
errIn (render ("extending" $$
|
||||||
nest 2 vars $$
|
nest 2 vars $$
|
||||||
"with" $$
|
"with" $$
|
||||||
nest 2 val)) $
|
nest 2 val)) $
|
||||||
plusRecType vars val
|
plusRecType vars val
|
||||||
return ((Explicit,symb,rec),cat)
|
return ((Explicit,varX i,rec),cat)
|
||||||
lookLin (_,c) = checks [ --- rather: update with defLinType ?
|
lookLin (_,c) = checks [ --- rather: update with defLinType ?
|
||||||
lookupLincat cnc m c >>= normalForm cnc
|
lookupLincat cnc m c >>= normalForm cnc
|
||||||
,return defLinType
|
,return defLinType
|
||||||
|
|||||||
@@ -18,8 +18,7 @@ module GF.Infra.Ident (-- ** Identifiers
|
|||||||
-- *** Normal identifiers (returned by the parser)
|
-- *** Normal identifiers (returned by the parser)
|
||||||
identS, identC, identW,
|
identS, identC, identW,
|
||||||
-- *** Special identifiers for internal use
|
-- *** Special identifiers for internal use
|
||||||
identV, identA, identAV,
|
identV,
|
||||||
argIdent, isArgIdent, getArgIndex,
|
|
||||||
varStr, varX, isWildIdent, varIndex,
|
varStr, varX, isWildIdent, varIndex,
|
||||||
-- *** Raw identifiers
|
-- *** Raw identifiers
|
||||||
RawIdent, rawIdentS, rawIdentC, ident2raw, prefixRawIdent,
|
RawIdent, rawIdentS, rawIdentC, ident2raw, prefixRawIdent,
|
||||||
@@ -52,9 +51,6 @@ data Ident =
|
|||||||
--
|
--
|
||||||
-- below this constructor: internal representation never returned by the parser
|
-- below this constructor: internal representation never returned by the parser
|
||||||
| IV {-# UNPACK #-} !RawIdent {-# UNPACK #-} !Int -- ^ /INTERNAL/ variable
|
| IV {-# UNPACK #-} !RawIdent {-# UNPACK #-} !Int -- ^ /INTERNAL/ variable
|
||||||
| IA {-# UNPACK #-} !RawIdent {-# UNPACK #-} !Int -- ^ /INTERNAL/ argument of cat at position
|
|
||||||
| IAV {-# UNPACK #-} !RawIdent {-# UNPACK #-} !Int {-# UNPACK #-} !Int -- ^ /INTERNAL/ argument of cat with bindings at position
|
|
||||||
--
|
|
||||||
deriving (Eq, Ord, Show, Read)
|
deriving (Eq, Ord, Show, Read)
|
||||||
|
|
||||||
-- | Identifiers are stored as UTF-8-encoded bytestrings.
|
-- | Identifiers are stored as UTF-8-encoded bytestrings.
|
||||||
@@ -83,8 +79,6 @@ ident2utf8 :: Ident -> UTF8.ByteString
|
|||||||
ident2utf8 i = case i of
|
ident2utf8 i = case i of
|
||||||
IC (Id s) -> s
|
IC (Id s) -> s
|
||||||
IV (Id s) n -> BS.append s (pack ('_':show n))
|
IV (Id s) n -> BS.append s (pack ('_':show n))
|
||||||
IA (Id s) j -> BS.append s (pack ('_':show j))
|
|
||||||
IAV (Id s) b j -> BS.append s (pack ('_':show b ++ '_':show j))
|
|
||||||
IW -> pack "_"
|
IW -> pack "_"
|
||||||
|
|
||||||
ident2raw :: Ident -> RawIdent
|
ident2raw :: Ident -> RawIdent
|
||||||
@@ -106,37 +100,14 @@ identW :: Ident
|
|||||||
prefixIdent :: String -> Ident -> Ident
|
prefixIdent :: String -> Ident -> Ident
|
||||||
prefixIdent pref = identC . Id . BS.append (pack pref) . ident2utf8
|
prefixIdent pref = identC . Id . BS.append (pack pref) . ident2utf8
|
||||||
|
|
||||||
-- normal identifier
|
|
||||||
-- ident s = IC s
|
|
||||||
|
|
||||||
identV :: RawIdent -> Int -> Ident
|
identV :: RawIdent -> Int -> Ident
|
||||||
identA :: RawIdent -> Int -> Ident
|
|
||||||
identAV:: RawIdent -> Int -> Int -> Ident
|
|
||||||
|
|
||||||
(identC, identV, identA, identAV, identW) =
|
(identC, identV, identW) =
|
||||||
(IC, IV, IA, IAV, IW)
|
(IC, IV, IW)
|
||||||
|
|
||||||
-- | to mark argument variables
|
|
||||||
argIdent :: Int -> Ident -> Int -> Ident
|
|
||||||
argIdent 0 (IC c) i = identA c i
|
|
||||||
argIdent b (IC c) i = identAV c b i
|
|
||||||
|
|
||||||
isArgIdent IA{} = True
|
|
||||||
isArgIdent IAV{} = True
|
|
||||||
isArgIdent _ = False
|
|
||||||
|
|
||||||
getArgIndex (IA _ i) = Just i
|
|
||||||
getArgIndex (IAV _ _ i) = Just i
|
|
||||||
getArgIndex (IC (Id bs))
|
|
||||||
| isDigit c =
|
|
||||||
-- (Just . read . unpack . snd . BS.spanEnd isDigit) bs -- not ok with UTF-8
|
|
||||||
(Just . read . reverse . takeWhile isDigit) s
|
|
||||||
where s@(c:_) = reverse (unpack bs)
|
|
||||||
getArgIndex x = Nothing
|
|
||||||
|
|
||||||
-- | used in lin defaults
|
-- | used in lin defaults
|
||||||
varStr :: Ident
|
varStr :: Ident
|
||||||
varStr = identA (rawIdentS "str") 0
|
varStr = identS "str"
|
||||||
|
|
||||||
-- | refreshing variables
|
-- | refreshing variables
|
||||||
varX :: Int -> Ident
|
varX :: Int -> Ident
|
||||||
|
|||||||
Reference in New Issue
Block a user