mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-03 16:22:52 -06:00
overload resolution with value type, for experiment
This commit is contained in:
@@ -29,15 +29,15 @@ lin
|
|||||||
|
|
||||||
evax1 =
|
evax1 =
|
||||||
proof (by (ref (mkLabel ["the first axiom of evenness ,"])))
|
proof (by (ref (mkLabel ["the first axiom of evenness ,"])))
|
||||||
(mkS (pred (regA "even") (UsePN (regPN "zero")))) ;
|
(mkS (predA (regA "even") (UsePN (regPN "zero")))) ;
|
||||||
evax2 n c =
|
evax2 n c =
|
||||||
appendText c
|
appendText c
|
||||||
(proof (by (ref (mkLabel ["the second axiom of evenness ,"])))
|
(proof (by (ref (mkLabel ["the second axiom of evenness ,"])))
|
||||||
(mkS (pred (regA "odd") (appN2 (regN2 "successor") n)))) ;
|
(mkS (predA (regA "odd") (appN2 (regN2 "successor") n)))) ;
|
||||||
evax3 n c =
|
evax3 n c =
|
||||||
appendText c
|
appendText c
|
||||||
(proof (by (ref (mkLabel ["the third axiom of evenness ,"])))
|
(proof (by (ref (mkLabel ["the third axiom of evenness ,"])))
|
||||||
(mkS (pred (regA "even") (appN2 (regN2 "successor") n)))) ;
|
(mkS (predA (regA "even") (appN2 (regN2 "successor") n)))) ;
|
||||||
|
|
||||||
|
|
||||||
eqax1 =
|
eqax1 =
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ lincat
|
|||||||
lin
|
lin
|
||||||
ThmWithProof = theorem ;
|
ThmWithProof = theorem ;
|
||||||
|
|
||||||
Conj A = coord and_Conj A ;
|
Conj = coord and_Conj ;
|
||||||
Disj A B = coord or_Conj A B ;
|
Disj = coord or_Conj ;
|
||||||
Impl A B = coord ifthen_DConj A B ;
|
Impl = coord ifthen_DConj ;
|
||||||
|
|
||||||
Abs =
|
Abs =
|
||||||
mkS (pred have_V2 (mkNP we_Pron) (mkNP (mkDet IndefArt) contradiction_N)) ;
|
mkS (pred have_V2 (mkNP we_Pron) (mkNP (mkDet IndefArt) contradiction_N)) ;
|
||||||
@@ -28,21 +28,21 @@ lin
|
|||||||
Univ A B =
|
Univ A B =
|
||||||
AdvS
|
AdvS
|
||||||
(mkAdv for_Prep (mkNP all_Predet
|
(mkAdv for_Prep (mkNP all_Predet
|
||||||
(mkNP (mkDet (PlQuant IndefArt)) (mkCN A (symb B.$0)))))
|
(mkNP (mkDet (PlQuant IndefArt) NoNum NoOrd) (mkCN A (symb B.$0)))))
|
||||||
B ;
|
B ;
|
||||||
|
|
||||||
DisjIl A B a = proof a (proof afortiori (coord or_Conj A B)) ;
|
DisjIl A B a = proof a (proof afortiori (coord or_Conj A B)) ;
|
||||||
DisjIr A B b = proof b (proof afortiori (coord or_Conj A B)) ;
|
DisjIr A B b = proof b (proof afortiori (coord or_Conj A B)) ;
|
||||||
|
|
||||||
DisjE A B C c b1 b2 =
|
DisjE A B C c d e =
|
||||||
appendText
|
appendText
|
||||||
c
|
c
|
||||||
(appendText
|
(appendText
|
||||||
(appendText
|
(appendText
|
||||||
(cases (mkNum n2))
|
(cases (mkNum n2))
|
||||||
(proofs
|
(proofs
|
||||||
(appendText (assumption A) b1)
|
(appendText (assumption A) d)
|
||||||
(appendText (assumption B) b2)))
|
(appendText (assumption B) e)))
|
||||||
(proof therefore C)) ;
|
(proof therefore C)) ;
|
||||||
|
|
||||||
ImplI A B b =
|
ImplI A B b =
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ oper
|
|||||||
|
|
||||||
definition : Decls -> Object -> Object -> Section
|
definition : Decls -> Object -> Object -> Section
|
||||||
= \decl,a,b ->
|
= \decl,a,b ->
|
||||||
appendText decl (mkUtt (mkS (pred b a))) ;
|
appendText decl (mkText (mkPhr (mkUtt (mkS (pred b a)))) TEmpty) ;
|
||||||
|
|
||||||
theorem : Prop -> Proof -> Section
|
theorem : Prop -> Proof -> Section
|
||||||
= \prop,prf -> appendText (mkText (mkPhr prop) TEmpty) prf ;
|
= \prop,prf -> appendText (mkText (mkPhr prop) TEmpty) prf ;
|
||||||
@@ -64,17 +64,17 @@ oper
|
|||||||
= appendText ;
|
= appendText ;
|
||||||
|
|
||||||
cases : Num -> Branching
|
cases : Num -> Branching
|
||||||
= \nu ->
|
= \n ->
|
||||||
mkS (pred have_V2 (mkNP we_Pron) (mkNP (mkDet nu) case_N)) ;
|
mkS (pred have_V2 (mkNP we_Pron) (mkNP (mkDet n) case_N)) ;
|
||||||
|
|
||||||
by : Ref -> Adverb
|
by : Ref -> Adverb
|
||||||
= \h -> mkAdv by8means_Prep h ;
|
= \h -> C.mkPConj (mkAdv by8means_Prep h).s ;
|
||||||
therefore : Adverb
|
therefore : Adverb
|
||||||
= therefore_PConj ;
|
= therefore_PConj ;
|
||||||
afortiori : Adverb
|
afortiori : Adverb
|
||||||
= C.mkPConj ["a fortiori"] ;
|
= C.mkPConj ["a fortiori"] ;
|
||||||
hypothesis : Adverb
|
hypothesis : Adverb
|
||||||
= mkAdv by8means_Prep (mkNP (mkDet DefArt) hypothesis_N) ;
|
= C.mkPConj (mkAdv by8means_Prep (mkNP (mkDet DefArt) hypothesis_N)).s ;
|
||||||
|
|
||||||
ref : Label -> Ref
|
ref : Label -> Ref
|
||||||
= \h -> h ;
|
= \h -> h ;
|
||||||
|
|||||||
@@ -189,7 +189,10 @@ checkResInfo gr mo (c,info) = do
|
|||||||
ResOverload tysts -> chIn "overloading" $ do
|
ResOverload tysts -> chIn "overloading" $ do
|
||||||
tysts' <- mapM (uncurry $ flip check) tysts
|
tysts' <- mapM (uncurry $ flip check) tysts
|
||||||
let tysts2 = [(y,x) | (x,y) <- tysts']
|
let tysts2 = [(y,x) | (x,y) <- tysts']
|
||||||
checkUniq $ sort [map snd xs | (x,_) <- tysts2, Ok (xs,_) <- [typeFormCnc x]]
|
--- this can only be a partial guarantee, since matching
|
||||||
|
--- with value type is only possible if expected type is given
|
||||||
|
checkUniq $
|
||||||
|
sort [t : map snd xs | (x,_) <- tysts2, Ok (xs,t) <- [typeFormCnc x]]
|
||||||
return (c,ResOverload tysts2)
|
return (c,ResOverload tysts2)
|
||||||
|
|
||||||
ResParam (Yes (pcs,_)) -> chIn "parameter type" $ do
|
ResParam (Yes (pcs,_)) -> chIn "parameter type" $ do
|
||||||
@@ -395,7 +398,7 @@ inferLType gr trm = case trm of
|
|||||||
return (e,t')
|
return (e,t')
|
||||||
|
|
||||||
App f a -> do
|
App f a -> do
|
||||||
over <- getOverload trm
|
over <- getOverload gr Nothing trm
|
||||||
case over of
|
case over of
|
||||||
Just trty -> return trty
|
Just trty -> return trty
|
||||||
_ -> do
|
_ -> do
|
||||||
@@ -572,7 +575,11 @@ inferLType gr trm = case trm of
|
|||||||
PRep _ -> return $ typeTok
|
PRep _ -> return $ typeTok
|
||||||
_ -> infer (patt2term p) >>= return . snd
|
_ -> infer (patt2term p) >>= return . snd
|
||||||
|
|
||||||
getOverload t = case appForm t of
|
|
||||||
|
-- type inference: Nothing, type checking: Just t
|
||||||
|
-- the latter permits matching with value type
|
||||||
|
getOverload :: SourceGrammar -> Maybe Type -> Term -> Check (Maybe (Term,Type))
|
||||||
|
getOverload env@gr mt t = case appForm t of
|
||||||
(f@(Q m c), ts) -> case lookupOverload gr m c of
|
(f@(Q m c), ts) -> case lookupOverload gr m c of
|
||||||
Ok typs -> do
|
Ok typs -> do
|
||||||
ttys <- mapM infer ts
|
ttys <- mapM infer ts
|
||||||
@@ -580,31 +587,34 @@ inferLType gr trm = case trm of
|
|||||||
return $ Just v
|
return $ Just v
|
||||||
_ -> return Nothing
|
_ -> return Nothing
|
||||||
_ -> return Nothing
|
_ -> return Nothing
|
||||||
|
where
|
||||||
|
infer = inferLType env
|
||||||
matchOverload f typs ttys = do
|
matchOverload f typs ttys = do
|
||||||
let (tts,tys) = unzip ttys
|
let (tts,tys) = unzip ttys
|
||||||
case lookupOverloadInstance tys typs of
|
let vfs = lookupOverloadInstance tys typs
|
||||||
[((val,fun),_)] -> return (mkApp fun tts, val)
|
|
||||||
|
case [vf | vf@(v,f) <- vfs, elem mt [Nothing,Just v]] of
|
||||||
|
[(val,fun)] -> return (mkApp fun tts, val)
|
||||||
[] -> raise $ "no overload instance of" +++ prt f +++
|
[] -> raise $ "no overload instance of" +++ prt f +++
|
||||||
|
maybe [] (("when expecting" +++) . prtType) mt +++
|
||||||
"for" +++ unwords (map prtType tys) +++ "among" ++++
|
"for" +++ unwords (map prtType tys) +++ "among" ++++
|
||||||
unlines [unwords (map prtType ty) | (ty,_) <- typs]
|
unlines [unwords (map prtType ty) | (ty,_) <- typs]
|
||||||
---- ++++ "DEBUG" +++ unwords (map show tys) +++ ";"
|
---- ++++ "DEBUG" +++ unwords (map show tys) +++ ";"
|
||||||
---- ++++ unlines (map (show . fst) typs) ----
|
---- ++++ unlines (map (show . fst) typs) ----
|
||||||
vfs -> case filter snd vfs of
|
|
||||||
[((val,fun),_)] -> return (mkApp fun tts, val)
|
|
||||||
_ -> raise $ "ambiguous overloading of" +++ prt f +++
|
_ -> raise $ "ambiguous overloading of" +++ prt f +++
|
||||||
"for" +++ unwords (map prtType tys) ++++ "with alternatives" ++++
|
"for" +++ unwords (map prtType tys) ++++ "with alternatives" ++++
|
||||||
unlines [prtType ty | ((ty,_),_) <- vfs]
|
unlines [prtType ty | (ty,_) <- vfs]
|
||||||
|
|
||||||
---- TODO: accept subtypes
|
---- TODO: accept subtypes
|
||||||
---- TODO: use a trie
|
---- TODO: use a trie
|
||||||
lookupOverloadInstance tys typs =
|
lookupOverloadInstance tys typs =
|
||||||
[((mkFunType rest val, t),null rest) |
|
[(mkFunType rest val, t) |
|
||||||
(ty,(val,t)) <- typs,
|
(ty,(val,t)) <- typs,
|
||||||
let (pre,rest) = splitAt (length tys) ty,
|
let (pre,rest) = splitAt (length tys) ty,
|
||||||
pre == tys
|
pre == tys
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
checkLType :: SourceGrammar -> Term -> Type -> Check (Term, Type)
|
checkLType :: SourceGrammar -> Term -> Type -> Check (Term, Type)
|
||||||
checkLType env trm typ0 = do
|
checkLType env trm typ0 = do
|
||||||
|
|
||||||
@@ -625,6 +635,14 @@ checkLType env trm typ0 = do
|
|||||||
return $ (Abs x c', Prod x a b')
|
return $ (Abs x c', Prod x a b')
|
||||||
_ -> raise $ "product expected instead of" +++ prtType typ
|
_ -> raise $ "product expected instead of" +++ prtType typ
|
||||||
|
|
||||||
|
App f a -> do
|
||||||
|
over <- getOverload env (Just typ) trm
|
||||||
|
case over of
|
||||||
|
Just trty -> return trty
|
||||||
|
_ -> do
|
||||||
|
(trm',ty') <- infer trm
|
||||||
|
termWith trm' $ checkEq typ ty' trm'
|
||||||
|
|
||||||
T _ [] ->
|
T _ [] ->
|
||||||
prtFail "found empty table in type" typ
|
prtFail "found empty table in type" typ
|
||||||
T _ cs -> case typ of
|
T _ cs -> case typ of
|
||||||
|
|||||||
@@ -281,7 +281,7 @@ compileSourceModule opts env@(k,gr,can,eenv) mo@(i,mi) = do
|
|||||||
(mo3:_,warnings) <- putpp " type checking" $ ioeErr $ showCheckModule mos mo2
|
(mo3:_,warnings) <- putpp " type checking" $ ioeErr $ showCheckModule mos mo2
|
||||||
if null warnings then return () else putp warnings $ return ()
|
if null warnings then return () else putp warnings $ return ()
|
||||||
|
|
||||||
(k',mo3r:_) <- ioeErr $ refreshModule (k,mos) mo3
|
(k',mo3r:_) <- putpp " refreshing " $ ioeErr $ refreshModule (k,mos) mo3
|
||||||
|
|
||||||
(mo4,eenv') <-
|
(mo4,eenv') <-
|
||||||
---- if oElem "check_only" opts
|
---- if oElem "check_only" opts
|
||||||
|
|||||||
Reference in New Issue
Block a user