summaryrefslogtreecommitdiff
path: root/src/compiler/GF/Command/TreeOperations.hs
diff options
context:
space:
mode:
authorHans Leiss <leiss@cis.uni-muenchen.de>2025-02-19 12:59:43 +0100
committerHans Leiss <leiss@cis.uni-muenchen.de>2025-02-19 12:59:43 +0100
commit5776b567a2aa78902bf5e4b40cf0bbb69bdbde9a (patch)
tree104ae1a696209c7281927dc2e09589e547b5c210 /src/compiler/GF/Command/TreeOperations.hs
parent643617ccc4faafa659aa16fae2870ac68a8f394b (diff)
Reactivate the gf-shell command 'pt -transfer'
Diffstat (limited to 'src/compiler/GF/Command/TreeOperations.hs')
-rw-r--r--src/compiler/GF/Command/TreeOperations.hs23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/compiler/GF/Command/TreeOperations.hs b/src/compiler/GF/Command/TreeOperations.hs
index fc0e6616d..7497eb7e8 100644
--- a/src/compiler/GF/Command/TreeOperations.hs
+++ b/src/compiler/GF/Command/TreeOperations.hs
@@ -5,6 +5,8 @@ module GF.Command.TreeOperations (
) where
import PGF(Expr,PGF,CId,compute,mkApp,unApp,unapply,unMeta,exprSize,exprFunctions)
+import PGF.Data(Expr(EApp,EFun))
+import PGF.TypeCheck(inferExpr)
import Data.List
type TreeOp = [Expr] -> [Expr]
@@ -16,15 +18,17 @@ allTreeOps :: PGF -> [(String,(String,Either TreeOp (CId -> TreeOp)))]
allTreeOps pgf = [
("compute",("compute by using semantic definitions (def)",
Left $ map (compute pgf))),
+ ("transfer",("apply this transfer function to all maximal subtrees of suitable type",
+ Right $ \f -> map (transfer pgf f))), -- HL 12/24, modified from gf-3.3
("largest",("sort trees from largest to smallest, in number of nodes",
Left $ largest)),
- ("nub",("remove duplicate trees",
+ ("nub\t",("remove duplicate trees",
Left $ nub)),
("smallest",("sort trees from smallest to largest, in number of nodes",
Left $ smallest)),
("subtrees",("return all fully applied subtrees (stopping at abstractions), by default sorted from the largest",
Left $ concatMap subtrees)),
- ("funs",("return all fun functions appearing in the tree, with duplications",
+ ("funs\t",("return all fun functions appearing in the tree, with duplications",
Left $ \es -> [mkApp f [] | e <- es, f <- exprFunctions e]))
]
@@ -48,3 +52,18 @@ subtrees :: Expr -> [Expr]
subtrees t = t : case unApp t of
Just (f,ts) -> concatMap subtrees ts
_ -> [] -- don't go under abstractions
+
+-- Apply transfer function f:C -> D to all maximal subtrees s:C of tree e and replace
+-- these s by the values of f(s). This modifies the 'simple-minded transfer' of gf-3.3.
+-- If applied to strict subtrees s of e, better use with f:C -> C only. HL 12/2024
+
+transfer :: PGF -> CId -> Expr -> Expr
+transfer pgf f e = case inferExpr pgf (appf e) of
+ Left _err -> case e of
+ EApp g a -> EApp (transfer pgf f g) (transfer pgf f a)
+ _ -> e
+ Right _ty -> case (compute pgf (appf e)) of
+ v | v /= (appf e) -> v
+ _ -> e -- default case of f, or f has no computation rule
+ where
+ appf = EApp (EFun f)