mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 19:42:50 -06:00
pattern matching optimization; trace of fun in compilation with -v
This commit is contained in:
@@ -34,6 +34,14 @@ import GF.Infra.Option
|
|||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Data.List
|
import Data.List
|
||||||
|
|
||||||
|
import Debug.Trace
|
||||||
|
|
||||||
|
|
||||||
|
-- conditional trace
|
||||||
|
|
||||||
|
prtIf :: (Print a) => Bool -> a -> a
|
||||||
|
prtIf b t = if b then trace (" " ++ prt t) t else t
|
||||||
|
|
||||||
-- experimental evaluation, option to import
|
-- experimental evaluation, option to import
|
||||||
oEval = iOpt "eval"
|
oEval = iOpt "eval"
|
||||||
|
|
||||||
@@ -113,10 +121,13 @@ evalResInfo oopts gr (c,info) = case info of
|
|||||||
|
|
||||||
evalCncInfo ::
|
evalCncInfo ::
|
||||||
Options -> SourceGrammar -> Ident -> Ident -> (Ident,Info) -> Err (Ident,Info)
|
Options -> SourceGrammar -> Ident -> Ident -> (Ident,Info) -> Err (Ident,Info)
|
||||||
evalCncInfo opts gr cnc abs (c,info) = errIn ("optimizing" +++ prt c) $ case info of
|
evalCncInfo opts gr cnc abs (c,info) = do
|
||||||
|
|
||||||
|
seq (prtIf (oElem beVerbose opts) c) $ return ()
|
||||||
|
|
||||||
|
errIn ("optimizing" +++ prt c) $ case info of
|
||||||
|
|
||||||
CncCat ptyp pde ppr -> do
|
CncCat ptyp pde ppr -> do
|
||||||
|
|
||||||
pde' <- case (ptyp,pde) of
|
pde' <- case (ptyp,pde) of
|
||||||
(Yes typ, Yes de) ->
|
(Yes typ, Yes de) ->
|
||||||
liftM yes $ pEval ([(strVar, typeStr)], typ) de
|
liftM yes $ pEval ([(strVar, typeStr)], typ) de
|
||||||
|
|||||||
@@ -141,9 +141,11 @@ computeTermOpt rec gr = comp where
|
|||||||
return $ S t' v' -- if v' is not canonical
|
return $ S t' v' -- if v' is not canonical
|
||||||
|
|
||||||
S t v -> do
|
S t v -> do
|
||||||
t' <- comp g t
|
t' <- comp g t
|
||||||
v' <- comp g v
|
v' <- comp g v
|
||||||
case t' of
|
case v' of
|
||||||
|
FV vs -> mapM (\c -> comp g (S t' c)) vs >>= returnC . variants
|
||||||
|
_ -> case t' of
|
||||||
FV ccs -> mapM (\c -> comp g (S c v')) ccs >>= returnC . variants
|
FV ccs -> mapM (\c -> comp g (S c v')) ccs >>= returnC . variants
|
||||||
|
|
||||||
T _ [(PV IW,c)] -> comp g c --- an optimization
|
T _ [(PV IW,c)] -> comp g c --- an optimization
|
||||||
@@ -152,21 +154,21 @@ computeTermOpt rec gr = comp where
|
|||||||
T _ [(PV z,c)] -> comp (ext z v' g) c --- another optimization
|
T _ [(PV z,c)] -> comp (ext z v' g) c --- another optimization
|
||||||
T _ [(PT _ (PV z),c)] -> comp (ext z v' g) c
|
T _ [(PT _ (PV z),c)] -> comp (ext z v' g) c
|
||||||
|
|
||||||
|
-- course-of-values table: look up by index, no pattern matching needed
|
||||||
V ptyp ts -> do
|
V ptyp ts -> do
|
||||||
vs <- allParamValues gr ptyp
|
vs <- allParamValues gr ptyp
|
||||||
ps <- mapM term2patt vs
|
case lookup v' (zip vs [0 .. length vs - 1]) of
|
||||||
let cc = zip ps ts
|
Just i -> comp g $ ts !! i
|
||||||
case v' of
|
_ -> return $ S t' v' -- if v' is not canonical
|
||||||
FV vs -> mapM (\c -> comp g (S t' c)) vs >>= returnC . variants
|
|
||||||
_ -> case matchPattern cc v' of
|
|
||||||
Ok (c,g') -> comp (g' ++ g) c
|
|
||||||
_ | isCan v' -> prtBad ("missing case" +++ prt v' +++ "in") t
|
|
||||||
_ -> return $ S t' v' -- if v' is not canonical
|
|
||||||
|
|
||||||
T _ cc -> case v' of
|
T (TComp _) cs -> do
|
||||||
FV vs -> mapM (\c -> comp g (S t' c)) vs >>= returnC . variants
|
case term2patt v' of
|
||||||
_ -> case matchPattern cc v' of
|
Ok p' -> case lookup p' cs of
|
||||||
|
Just u -> comp g u
|
||||||
|
_ -> return $ S t' v' -- if v' is not canonical
|
||||||
|
_ -> return $ S t' v'
|
||||||
|
|
||||||
|
T _ cc -> case matchPattern cc v' of
|
||||||
Ok (c,g') -> comp (g' ++ g) c
|
Ok (c,g') -> comp (g' ++ g) c
|
||||||
_ | isCan v' -> prtBad ("missing case" +++ prt v' +++ "in") t
|
_ | isCan v' -> prtBad ("missing case" +++ prt v' +++ "in") t
|
||||||
_ -> return $ S t' v' -- if v' is not canonical
|
_ -> return $ S t' v' -- if v' is not canonical
|
||||||
|
|||||||
@@ -29,8 +29,11 @@ import Control.Monad
|
|||||||
|
|
||||||
matchPattern :: [(Patt,Term)] -> Term -> Err (Term, Substitution)
|
matchPattern :: [(Patt,Term)] -> Term -> Err (Term, Substitution)
|
||||||
matchPattern pts term =
|
matchPattern pts term =
|
||||||
errIn ("trying patterns" +++ unwords (intersperse "," (map (prt . fst) pts))) $
|
if not (isInConstantForm term)
|
||||||
findMatch [([p],t) | (p,t) <- pts] [term]
|
then prtBad "variables occur in" term
|
||||||
|
else
|
||||||
|
errIn ("trying patterns" +++ unwords (intersperse "," (map (prt . fst) pts))) $
|
||||||
|
findMatch [([p],t) | (p,t) <- pts] [term]
|
||||||
|
|
||||||
testOvershadow :: [Patt] -> [Term] -> Err [Patt]
|
testOvershadow :: [Patt] -> [Term] -> Err [Patt]
|
||||||
testOvershadow pts vs = do
|
testOvershadow pts vs = do
|
||||||
@@ -54,14 +57,15 @@ tryMatch (p,t) = do
|
|||||||
t' <- termForm t
|
t' <- termForm t
|
||||||
trym p t'
|
trym p t'
|
||||||
where
|
where
|
||||||
|
isInConstantFormt = True -- tested already
|
||||||
trym p t' =
|
trym p t' =
|
||||||
case (p,t') of
|
case (p,t') of
|
||||||
(PVal _ i, (_,Val _ j,_))
|
(PVal _ i, (_,Val _ j,_))
|
||||||
| i == j -> return []
|
| i == j -> return []
|
||||||
| otherwise -> Bad $ "no match of values"
|
| otherwise -> Bad $ "no match of values"
|
||||||
(_,(x,Empty,y)) -> trym p (x,K [],y) -- because "" = [""] = []
|
(_,(x,Empty,y)) -> trym p (x,K [],y) -- because "" = [""] = []
|
||||||
(PV IW, _) | isInConstantForm t -> return [] -- optimization with wildcard
|
(PV IW, _) | isInConstantFormt -> return [] -- optimization with wildcard
|
||||||
(PV x, _) | isInConstantForm t -> return [(x,t)]
|
(PV x, _) | isInConstantFormt -> return [(x,t)]
|
||||||
(PString s, ([],K i,[])) | s==i -> return []
|
(PString s, ([],K i,[])) | s==i -> return []
|
||||||
(PInt s, ([],EInt i,[])) | s==i -> return []
|
(PInt s, ([],EInt i,[])) | s==i -> return []
|
||||||
(PFloat s,([],EFloat i,[])) | s==i -> return [] --- rounding?
|
(PFloat s,([],EFloat i,[])) | s==i -> return [] --- rounding?
|
||||||
|
|||||||
Reference in New Issue
Block a user