mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 19:42:50 -06:00
judgement construction after parsing
This commit is contained in:
@@ -3,16 +3,11 @@ module GF.Devel.Judgements where
|
|||||||
import GF.Devel.Terms
|
import GF.Devel.Terms
|
||||||
import GF.Infra.Ident
|
import GF.Infra.Ident
|
||||||
|
|
||||||
import GF.Data.Operations
|
|
||||||
|
|
||||||
import Control.Monad
|
|
||||||
import Data.Map
|
|
||||||
|
|
||||||
data Judgement = Judgement {
|
data Judgement = Judgement {
|
||||||
jform :: JudgementForm, -- cat fun oper param
|
jform :: JudgementForm, -- cat fun oper param
|
||||||
jtype :: Type, -- context type type type
|
jtype :: Type, -- context type type constructors
|
||||||
jdef :: Term, -- lindef def - values
|
jdef :: Term, -- lindef def - values
|
||||||
jlin :: Term, -- lincat lin def constructors
|
jlin :: Term, -- lincat lin def -
|
||||||
jprintname :: Term -- printname printname - -
|
jprintname :: Term -- printname printname - -
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,44 +18,3 @@ data JudgementForm =
|
|||||||
| JParam
|
| JParam
|
||||||
deriving Eq
|
deriving Eq
|
||||||
|
|
||||||
-- constructing judgements from parse tree
|
|
||||||
|
|
||||||
emptyJudgement :: JudgementForm -> Judgement
|
|
||||||
emptyJudgement form = Judgement form meta meta meta meta where
|
|
||||||
meta = Meta 0
|
|
||||||
|
|
||||||
absCat :: Context -> Judgement
|
|
||||||
absCat co = (emptyJudgement JCat) {jtype = Sort "Type"} ---- works for empty co
|
|
||||||
|
|
||||||
absFun :: Type -> Judgement
|
|
||||||
absFun ty = (emptyJudgement JFun) {jtype = ty}
|
|
||||||
|
|
||||||
cncCat :: Type -> Judgement
|
|
||||||
cncCat ty = (emptyJudgement JCat) {jlin = ty}
|
|
||||||
|
|
||||||
cncFun :: Term -> Judgement
|
|
||||||
cncFun tr = (emptyJudgement JFun) {jlin = tr}
|
|
||||||
|
|
||||||
resOperType :: Type -> Judgement
|
|
||||||
resOperType ty = (emptyJudgement JOper) {jtype = ty}
|
|
||||||
|
|
||||||
resOperDef :: Term -> Judgement
|
|
||||||
resOperDef tr = (emptyJudgement JOper) {jlin = tr}
|
|
||||||
|
|
||||||
resOper :: Type -> Term -> Judgement
|
|
||||||
resOper ty tr = (emptyJudgement JOper) {jtype = ty, jlin = tr}
|
|
||||||
|
|
||||||
-- unifying contents of judgements
|
|
||||||
|
|
||||||
unifyJudgement :: Judgement -> Judgement -> Err Judgement
|
|
||||||
unifyJudgement old new = do
|
|
||||||
testErr (jform old == jform new) "different judment forms"
|
|
||||||
[jty,jde,jli,jpri] <- mapM unifyField [jtype,jdef,jlin,jprintname]
|
|
||||||
return $ old{jtype = jty, jdef = jde, jlin = jli, jprintname = jpri}
|
|
||||||
where
|
|
||||||
unifyField field = unifyTerm (field old) (field new)
|
|
||||||
unifyTerm oterm nterm = case (oterm,nterm) of
|
|
||||||
(Meta _,t) -> return t
|
|
||||||
(t,Meta _) -> return t
|
|
||||||
_ -> testErr (nterm == oterm) "incompatible fields" >> return nterm
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ module GF.Devel.Lookup where
|
|||||||
|
|
||||||
import GF.Devel.Modules
|
import GF.Devel.Modules
|
||||||
import GF.Devel.Judgements
|
import GF.Devel.Judgements
|
||||||
|
import GF.Devel.Macros
|
||||||
import GF.Devel.Terms
|
import GF.Devel.Terms
|
||||||
import GF.Infra.Ident
|
import GF.Infra.Ident
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ lookupJForm = lookupJField jform
|
|||||||
lookupCatContext :: GF -> Ident -> Ident -> Err Context
|
lookupCatContext :: GF -> Ident -> Ident -> Err Context
|
||||||
lookupCatContext gf m c = do
|
lookupCatContext gf m c = do
|
||||||
ty <- lookupJField jtype gf m c
|
ty <- lookupJField jtype gf m c
|
||||||
return [] ---- context of ty
|
return $ contextOfType ty
|
||||||
|
|
||||||
lookupFunType :: GF -> Ident -> Ident -> Err Term
|
lookupFunType :: GF -> Ident -> Ident -> Err Term
|
||||||
lookupFunType = lookupJField jtype
|
lookupFunType = lookupJField jtype
|
||||||
@@ -35,10 +36,24 @@ lookupLin = lookupJField jlin
|
|||||||
lookupLincat :: GF -> Ident -> Ident -> Err Term
|
lookupLincat :: GF -> Ident -> Ident -> Err Term
|
||||||
lookupLincat = lookupJField jlin
|
lookupLincat = lookupJField jlin
|
||||||
|
|
||||||
|
lookupOperType :: GF -> Ident -> Ident -> Err Term
|
||||||
|
lookupOperType = lookupJField jtype
|
||||||
|
|
||||||
|
lookupOperDef :: GF -> Ident -> Ident -> Err Term
|
||||||
|
lookupOperDef = lookupJField jlin
|
||||||
|
|
||||||
|
lookupParams :: GF -> Ident -> Ident -> Err [(Ident,Context)]
|
||||||
|
lookupParams gf m c = do
|
||||||
|
ty <- lookupJField jtype gf m c
|
||||||
|
return [(k,contextOfType t) | (k,t) <- contextOfType ty]
|
||||||
|
|
||||||
|
lookupParamConstructor :: GF -> Ident -> Ident -> Err Type
|
||||||
|
lookupParamConstructor = lookupJField jlin
|
||||||
|
|
||||||
lookupParamValues :: GF -> Ident -> Ident -> Err [Term]
|
lookupParamValues :: GF -> Ident -> Ident -> Err [Term]
|
||||||
lookupParamValues gf m c = do
|
lookupParamValues gf m c = do
|
||||||
j <- lookupJudgement gf m c
|
d <- lookupJField jdef gf m c
|
||||||
case jdef j of
|
case d of
|
||||||
V _ ts -> return ts
|
V _ ts -> return ts
|
||||||
_ -> raise "no parameter values"
|
_ -> raise "no parameter values"
|
||||||
|
|
||||||
|
|||||||
66
src/GF/Devel/MkJudgements.hs
Normal file
66
src/GF/Devel/MkJudgements.hs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
module GF.Devel.MkJudgements where
|
||||||
|
|
||||||
|
import GF.Devel.Macros
|
||||||
|
import GF.Devel.Judgements
|
||||||
|
import GF.Devel.Terms
|
||||||
|
import GF.Infra.Ident
|
||||||
|
|
||||||
|
import GF.Data.Operations
|
||||||
|
|
||||||
|
import Control.Monad
|
||||||
|
import Data.Map
|
||||||
|
|
||||||
|
-- constructing judgements from parse tree
|
||||||
|
|
||||||
|
emptyJudgement :: JudgementForm -> Judgement
|
||||||
|
emptyJudgement form = Judgement form meta meta meta meta where
|
||||||
|
meta = Meta 0
|
||||||
|
|
||||||
|
absCat :: Context -> Judgement
|
||||||
|
absCat co = (emptyJudgement JCat) {jtype = mkProd co typeType}
|
||||||
|
|
||||||
|
absFun :: Type -> Judgement
|
||||||
|
absFun ty = (emptyJudgement JFun) {jtype = ty}
|
||||||
|
|
||||||
|
cncCat :: Type -> Judgement
|
||||||
|
cncCat ty = (emptyJudgement JCat) {jlin = ty}
|
||||||
|
|
||||||
|
cncFun :: Term -> Judgement
|
||||||
|
cncFun tr = (emptyJudgement JFun) {jlin = tr}
|
||||||
|
|
||||||
|
resOperType :: Type -> Judgement
|
||||||
|
resOperType ty = (emptyJudgement JOper) {jtype = ty}
|
||||||
|
|
||||||
|
resOperDef :: Term -> Judgement
|
||||||
|
resOperDef tr = (emptyJudgement JOper) {jlin = tr}
|
||||||
|
|
||||||
|
resOper :: Type -> Term -> Judgement
|
||||||
|
resOper ty tr = (emptyJudgement JOper) {jtype = ty, jlin = tr}
|
||||||
|
|
||||||
|
-- param m.p = c g is encoded as p : (ci : gi -> EData) -> Type
|
||||||
|
-- we use EData instead of m.p to make circularity check easier
|
||||||
|
resParam :: Ident -> Ident -> [(Ident,Context)] -> Judgement
|
||||||
|
resParam m p cos = (emptyJudgement JParam) {
|
||||||
|
jtype = mkProd [(c,mkProd co EData) | (c,co) <- cos] typeType
|
||||||
|
}
|
||||||
|
|
||||||
|
-- to enable constructor type lookup:
|
||||||
|
-- create an oper for each constructor m.p = c g, as c : g -> m.p = EData
|
||||||
|
paramConstructors :: Ident -> Ident -> [(Ident,Context)] -> [(Ident,Judgement)]
|
||||||
|
paramConstructors m p cs =
|
||||||
|
[(c,resOper (mkProd co (QC m p)) EData) | (c,co) <- cs]
|
||||||
|
|
||||||
|
-- unifying contents of judgements
|
||||||
|
|
||||||
|
unifyJudgement :: Judgement -> Judgement -> Err Judgement
|
||||||
|
unifyJudgement old new = do
|
||||||
|
testErr (jform old == jform new) "different judment forms"
|
||||||
|
[jty,jde,jli,jpri] <- mapM unifyField [jtype,jdef,jlin,jprintname]
|
||||||
|
return $ old{jtype = jty, jdef = jde, jlin = jli, jprintname = jpri}
|
||||||
|
where
|
||||||
|
unifyField field = unifyTerm (field old) (field new)
|
||||||
|
unifyTerm oterm nterm = case (oterm,nterm) of
|
||||||
|
(Meta _,t) -> return t
|
||||||
|
(t,Meta _) -> return t
|
||||||
|
_ -> testErr (nterm == oterm) "incompatible fields" >> return nterm
|
||||||
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
module GF.Devel.Terms where
|
module GF.Devel.Terms where
|
||||||
|
|
||||||
import GF.Data.Str
|
|
||||||
import GF.Infra.Ident
|
import GF.Infra.Ident
|
||||||
import GF.Infra.Option ---
|
|
||||||
import GF.Infra.Modules
|
import GF.Infra.Modules
|
||||||
|
|
||||||
import GF.Data.Operations
|
import GF.Data.Operations
|
||||||
|
|||||||
Reference in New Issue
Block a user