summaryrefslogtreecommitdiff
path: root/src/GF/Command/TreeOperations.hs
blob: 0489dd23f458c6d2b85c12cee12c1d024f15615e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module GF.Command.TreeOperations (
  treeOp,
  allTreeOps
  ) where

import GF.Compile.TypeCheck
import PGF
import PGF.Data

import Data.List

type TreeOp = [Expr] -> [Expr]

treeOp :: PGF -> String -> Maybe TreeOp
treeOp pgf f = fmap snd $ lookup f $ allTreeOps pgf

allTreeOps :: PGF -> [(String,(String,TreeOp))]
allTreeOps pgf = [
   ("compute",("compute by using semantic definitions (def)",
      map (compute pgf))),
   ("paraphrase",("paraphrase by using semantic definitions (def)",
      nub . concatMap (paraphrase pgf))),
   ("smallest",("sort trees from smallest to largest, in number of nodes",
      smallest))
  ]

smallest :: [Expr] -> [Expr]
smallest = sortBy (\t u -> compare (size t) (size u)) where
  size t = case t of
    EAbs _ e -> size e + 1
    EApp e1 e2 -> size e1 + size e2 + 1
    _ -> 1