summaryrefslogtreecommitdiff
path: root/src/runtime/haskell/PGF/Generate.hs
blob: 797e5e2296b4d232e7d9d2665a2528eafa09811e (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
module PGF.Generate 
         ( generateAll,         generateAllDepth
         , generateFrom,        generateFromDepth
         , generateRandom,      generateRandomDepth
         , generateRandomFrom,  generateRandomFromDepth
         
         , RandomSelector(..)
         ) where

import PGF.CId
import PGF.Data
import PGF.Expr
import PGF.Macros
import PGF.TypeCheck
import PGF.Probabilistic

import qualified Data.Map as Map
import qualified Data.IntMap as IntMap
import Control.Monad
import System.Random

-- | Generates an exhaustive possibly infinite list of
-- abstract syntax expressions.
generateAll :: PGF -> Type -> [Expr]
generateAll pgf ty = generateAllDepth pgf ty Nothing

-- | A variant of 'generateAll' which also takes as argument
-- the upper limit of the depth of the generated expression.
generateAllDepth :: PGF -> Type -> Maybe Int -> [Expr]
generateAllDepth pgf ty dp = generate () pgf ty dp

-- | Generates a list of abstract syntax expressions
-- in a way similar to 'generateAll' but instead of
-- generating all instances of a given type, this
-- function uses a template. 
generateFrom :: PGF -> Expr -> [Expr]
generateFrom pgf ex = generateFromDepth pgf ex Nothing

-- | A variant of 'generateFrom' which also takes as argument
-- the upper limit of the depth of the generated subexpressions.
generateFromDepth :: PGF -> Expr -> Maybe Int -> [Expr]
generateFromDepth pgf e dp = generateForMetas False pgf (\ty -> generateAllDepth pgf ty dp) e

-- | Generates an infinite list of random abstract syntax expressions.
-- This is usefull for tree bank generation which after that can be used
-- for grammar testing.
generateRandom :: RandomGen g => RandomSelector g -> PGF -> Type -> [Expr]
generateRandom sel pgf ty = 
    generate sel pgf ty Nothing

-- | A variant of 'generateRandom' which also takes as argument
-- the upper limit of the depth of the generated expression.
generateRandomDepth :: RandomGen g => RandomSelector g -> PGF -> Type -> Maybe Int -> [Expr]
generateRandomDepth sel pgf ty dp = generate sel pgf ty dp

-- | Random generation based on template
generateRandomFrom :: RandomGen g => RandomSelector g -> PGF -> Expr -> [Expr]
generateRandomFrom sel pgf e = 
  generateForMetas True pgf (\ty -> generate sel pgf ty Nothing) e

-- | Random generation based on template with a limitation in the depth.
generateRandomFromDepth :: RandomGen g => RandomSelector g -> PGF -> Expr -> Maybe Int -> [Expr]
generateRandomFromDepth sel pgf e dp = 
  generateForMetas True pgf (\ty -> generate sel pgf ty dp) e



-- generic algorithm for filling holes in a generator
-- for random, should be breadth-first, since otherwise first metas always get the same
-- value when a list is generated
generateForMetas :: Bool -> PGF -> (Type -> [Expr]) -> Expr -> [Expr]
generateForMetas breadth pgf gen exp = case exp of
  EApp f (EMeta _) -> [EApp g a | g <- gener f, a <- genArg g]
  EApp f x | breadth -> [EApp g a | (g,a) <- zip (gener f) (gener x)]
  EApp f x           -> [EApp g a | g <- gener f, a <- gener x]
  _ -> if breadth then repeat exp else [exp]
 where
  gener    = generateForMetas breadth pgf gen
  genArg f = case inferExpr pgf f of
    Right (_,DTyp ((_,_,ty):_) _ _) -> gen ty
    _ -> []


------------------------------------------------------------------------------
-- The main generation algorithm

generate :: Selector sel => sel -> PGF -> Type -> Maybe Int -> [Expr]
generate sel pgf ty dp =
  [value2expr (funs (abstract pgf),lookupMeta ms) 0 v |
        (ms,v) <- runGenM (prove (abstract pgf) emptyScope (TTyp [] ty) dp) sel IntMap.empty]

prove :: Selector sel => Abstr -> Scope -> TType -> Maybe Int -> GenM sel MetaStore Value
prove abs scope tty@(TTyp env (DTyp [] cat es)) dp = do
  (fn,DTyp hypos cat es) <- clauses cat
  case dp of
    Just 0 | not (null hypos) -> mzero
    _                         -> return ()
  (env,args) <- mkEnv [] hypos
  runTcM abs (eqType scope (scopeSize scope) 0 (TTyp env (DTyp [] cat es)) tty)
  vs <- mapM descend args
  return (VApp fn vs)
  where
    clauses cat =
      do fn <- select abs cat
         case Map.lookup fn (funs abs) of
           Just (ty,_,_) -> return (fn,ty)
           Nothing       -> mzero

    mkEnv env []                = return (env,[])
    mkEnv env ((bt,x,ty):hypos) = do
      (env,arg) <- if x /= wildCId
                    then do i <- runTcM abs (newMeta scope (TTyp env ty))
                            let v = VMeta i env []
                            return (v : env,Right v)
                    else return (env,Left (TTyp env ty))
      (env,args) <- mkEnv env hypos
      return (env,(bt,arg):args)

    descend (bt,arg) = do let dp' = fmap (flip (-) 1) dp
                          v <- case arg of
                                 Right v  -> return v
                                 Left tty -> prove abs scope tty dp'
                          v <- case bt of
                                 Implicit -> return (VImplArg v)
                                 Explicit -> return v
                          return v


------------------------------------------------------------------------------
-- Generation Monad

newtype GenM sel s a = GenM {unGen :: sel -> s -> Choice sel s a}
data Choice sel s a = COk sel s a
                    | CFail
                    | CBranch (Choice sel s a) (Choice sel s a)

instance Monad (GenM sel s) where
  return x = GenM (\sel s -> COk sel s x)
  f >>= g  = GenM (\sel s -> iter (unGen f sel s))
    where
      iter (COk sel s x)   = unGen (g x) sel s
      iter (CBranch b1 b2) = CBranch (iter b1) (iter b2)
      iter CFail           = CFail
  fail _   = GenM (\sel s -> CFail)

instance Selector sel => MonadPlus (GenM sel s) where
  mzero = GenM (\sel s -> CFail)
  mplus f g = GenM (\sel s -> let (sel1,sel2) = splitSelector sel
                              in CBranch (unGen f sel1 s) (unGen g sel2 s))

runGenM :: GenM sel s a -> sel -> s -> [(s,a)]
runGenM f sel s = toList (unGen f sel s) []
  where
    toList (COk sel s x)   xs = (s,x) : xs
    toList (CFail)         xs = xs
    toList (CBranch b1 b2) xs = toList b1 (toList b2 xs)

runTcM :: Abstr -> TcM a -> GenM sel MetaStore a
runTcM abs f = GenM (\sel ms ->
  case unTcM f abs ms of
    Ok ms a -> COk sel ms a
    Fail _  -> CFail)


------------------------------------------------------------------------------
-- Selectors

class Selector sel where
  splitSelector :: sel -> (sel,sel)
  select        :: Abstr -> CId -> GenM sel s CId

instance Selector () where
  splitSelector sel = (sel,sel)
  select abs cat = GenM (\sel s -> case Map.lookup cat (cats abs) of
                                     Just (_,fns) -> iter s fns
                                     Nothing      -> CFail)
    where
      iter s []       = CFail
      iter s (fn:fns) = CBranch (COk () s fn) (iter s fns)

-- | The random selector data type is used to specify the random number generator
-- and the distribution among the functions with the same result category.
-- The distribution is even for 'RandSel' and weighted for 'WeightSel'.
data RandomSelector g = RandSel   g
                      | WeightSel g Probabilities

instance RandomGen g => Selector (RandomSelector g) where
  splitSelector (RandSel g)         = let (g1,g2) = split g
                                      in (RandSel g1, RandSel g2)
  splitSelector (WeightSel g probs) = let (g1,g2) = split g
                                      in (WeightSel g1 probs, WeightSel g2 probs)

  select abs cat = GenM (\sel s -> case sel of
                                     RandSel g         -> case Map.lookup cat (cats abs) of
                                                            Just (_,fns) -> do_rand   g s (length fns) fns
                                                            Nothing      -> CFail
                                     WeightSel g probs -> case Map.lookup cat (catProbs probs) of
                                                            Just fns     -> do_weight g s 1.0 fns
                                                            Nothing      -> CFail)
    where
      do_rand g s n []  = CFail
      do_rand g s n fns = let n'        = n-1
                              (i,g')    = randomR (0,n') g
                              (g1,g2)   = split g'
                              (fn,fns') = pick i fns
                          in CBranch (COk (RandSel g1) s fn) (do_rand g2 s n' fns')

      do_weight g s p []  = CFail
      do_weight g s p fns = let (d,g')    = randomR (0.0,p) g
                                (g1,g2)   = split g'
                                (p',fn,fns') = hit d fns
                            in CBranch (COk (RandSel g1) s fn) (do_weight g2 s (p-p') fns')

      pick :: Int -> [a] -> (a,[a])
      pick 0 (x:xs) = (x,xs)
      pick n (x:xs) = let (x',xs') = pick (n-1) xs
                      in (x',x:xs')

      hit :: Double -> [(Double,a)] -> (Double,a,[(Double,a)])
      hit d (px@(p,x):xs)
        | d < p     = (p,x,xs)
        | otherwise = let (p',x',xs') = hit (d-p) xs
                      in (p,x',px:xs')