1 {-# LANGUAGE PatternGuards, DeriveDataTypeable #-}
10 import Data.Generics hiding (typeOf)
11 import Data.Generics.Schemes
13 data TypedExpr = TypedExpr
16 } deriving (Eq, Typeable, Data)
18 typedLeft, typedRight :: Expr -> Typ -> TypedExpr
19 typedLeft e t = TypedExpr e (instType False t)
20 typedRight e t = TypedExpr e (instType True t)
25 deriving (Eq, Typeable, Data)
31 | Conc [Expr] -- Conc [] is Id
32 | Lambda TypedExpr Expr
36 deriving (Eq, Typeable, Data)
38 data LambdaBE = CurriedEquals Typ
39 | LambdaBE TypedExpr TypedExpr BoolExpr
40 deriving (Eq, Typeable, Data)
44 | And [BoolExpr] -- And [] is True
45 | AllZipWith LambdaBE Expr Expr
46 | AndEither LambdaBE LambdaBE Expr Expr
47 | Condition [TypedExpr] BoolExpr BoolExpr
48 | UnpackPair TypedExpr TypedExpr TypedExpr BoolExpr
49 | TypeVarInst Bool Int BoolExpr
50 deriving (Eq, Typeable, Data)
54 -- | Try eta reduction
55 equal :: TypedExpr -> TypedExpr -> BoolExpr
56 equal te1 te2 | typeOf te1 /= typeOf te2 = error "Type mismatch in equal"
57 | otherwise = equal' (unTypeExpr te1) (unTypeExpr te2)
59 equal' :: Expr -> Expr -> BoolExpr
60 equal' e1 e2 | (Just (lf,lv)) <- isFunctionApplication e1
61 , (Just (rf,rv)) <- isFunctionApplication e2
64 -- This makes it return True...
66 | otherwise = Equal e1 e2
68 -- | If e is of the type (app f1 (app f2 .. (app fn x)..)),
69 -- return Just (f1 . f2. ... . fn, x)
70 isFunctionApplication :: Expr -> Maybe (Expr, Expr)
71 isFunctionApplication (App f e') | (Just (inner,v)) <- isFunctionApplication e'
72 = Just (conc f inner, v)
75 isFunctionApplication _ = Nothing
78 -- | If both bound variables are just functions, we can replace this
80 unpackPair :: TypedExpr -> TypedExpr -> TypedExpr -> BoolExpr -> BoolExpr
81 unpackPair v1 v2 te be | Just subst1 <- findReplacer v1 be
82 , Just subst2 <- findReplacer v2 be
83 = subst1. subst2 $ (pair v1 v2 `equal` te) `aand` be
85 -- | If the whole tuple is a function, we can replace this
87 unpackPair v1 v2 te be | Just subst <- findReplacer (pair v1 v2) be
88 = subst $ (pair v1 v2 `equal` te) `aand` be
90 -- | Nothing to optimize
91 unpackPair v1 v2 te be = UnpackPair v1 v2 te be
93 pair :: TypedExpr -> TypedExpr -> TypedExpr
94 pair (TypedExpr e1 t1) (TypedExpr e2 t2) = TypedExpr (Pair e1 e2) (TPair t1 t2)
96 lambdaBE :: TypedExpr -> TypedExpr -> BoolExpr -> LambdaBE
97 lambdaBE v1 v2 rel | typeOf v1 == typeOf v2
98 , rel == v1 `equal` v2 = CurriedEquals (typeOf v1)
99 | otherwise = LambdaBE v1 v2 rel
101 andEither :: LambdaBE -> LambdaBE -> TypedExpr -> TypedExpr -> BoolExpr
102 andEither (CurriedEquals _) (CurriedEquals _) e1 e2 = e1 `equal` e2
103 andEither lbe1 lbe2 e1 e2 | Just f1 <- arg1IsFunc lbe1
104 , Just f2 <- arg1IsFunc lbe2
105 = e1 `equal` eitherE f1 f2 e2
106 | Just f1 <- arg2IsFunc lbe1
107 , Just f2 <- arg2IsFunc lbe2
108 = eitherE f1 f2 e1 `equal` e2
110 = AndEither lbe1 lbe2 (unTypeExpr e1) (unTypeExpr e2)
112 arg1IsFunc (CurriedEquals t) = Just $ TypedExpr (Conc []) (Arrow t t)
113 arg1IsFunc (LambdaBE v1 v2 rel) | Just v1' <- defFor v1 rel
114 = Just (lambda v2 v1')
115 | otherwise = Nothing
117 arg2IsFunc (CurriedEquals t) = Just $ TypedExpr (Conc []) (Arrow t t)
118 arg2IsFunc (LambdaBE v1 v2 rel) | Just v2' <- defFor v2 rel
119 = Just (lambda v1 v2')
120 | otherwise = Nothing
122 allZipWith :: TypedExpr -> TypedExpr -> BoolExpr -> TypedExpr -> TypedExpr -> BoolExpr
123 allZipWith v1 v2 rel e1 e2 | Just v1' <- defFor v1 rel =
124 e1 `equal` amap (lambda v2 v1') e2
125 | Just v2' <- defFor v2 rel =
126 amap (lambda v1 v2') e1 `equal` e2
128 AllZipWith (LambdaBE v1 v2 rel) (unTypeExpr e1) (unTypeExpr e2)
130 eitherE :: TypedExpr -> TypedExpr -> TypedExpr -> TypedExpr
131 eitherE f1 f2 e | Arrow lt1 lt2 <- typeOf f1
132 , Arrow rt1 rt2 <- typeOf f2
133 , TEither lt rt <- typeOf e
136 = let tEither = TypedExpr EitherMap (Arrow (typeOf f1) (Arrow (typeOf f2) (Arrow (typeOf e) (TEither lt2 rt2))))
137 in app (app (app tEither f1) f2) e
138 | otherwise = error $ "Type error in eitherE\n" ++ show (f1, f2, e)
140 amap :: TypedExpr -> TypedExpr -> TypedExpr
141 amap tf tl | Arrow t1 t2 <- typeOf tf
142 , List t <- typeOf tl
144 = let tMap = TypedExpr Map (Arrow (Arrow t1 t2) (Arrow (List t1) (List t2)))
145 in app (app tMap tf) tl
146 | otherwise = error "Type error in map"
148 aand :: BoolExpr -> BoolExpr -> BoolExpr
149 aand (And xs) (And ys) = And (xs ++ ys)
150 aand (And xs) y = And (xs ++ [y])
151 aand x (And ys) = And ([x] ++ ys)
152 aand x y = And ([x,y])
157 -- | Optimize a forall condition
158 condition :: [TypedExpr] -> BoolExpr -> BoolExpr -> BoolExpr
160 condition [] cond concl | cond == beTrue
162 -- float out conditions on the right
163 condition vars cond (Condition vars' cond' concl')
164 = condition (vars ++ vars') (cond `aand` cond') concl'
166 -- Try to find variables that are functions of other variables, and remove them
167 condition vars cond concl | True -- set to false to disable
168 , ((vars',cond',concl'):_) <- mapMaybe try vars
169 = condition vars' cond' concl'
170 -- A variable which can be replaced
171 where try v | Just subst <- findReplacer v cond
172 = -- trace ("Tested " ++ show v ++ ", can be replaced") $
173 Just (delete v vars, subst cond, subst concl)
175 -- A variable with can be removed
176 | not (unTypeExpr v `occursIn` cond || unTypeExpr v `occursIn` concl)
177 = -- trace ("Tested " ++ show v ++ ", can be reased") $
178 Just (delete v vars, cond, concl)
180 -- Nothing to do with this variable
182 = -- trace ("Tested " ++ show v ++ " without success") $
185 -- Nothing left to optizmize
186 condition vars cond concl = Condition vars cond concl
188 -- | Replaces a Term in a BoolExpr
189 replaceTermBE :: Expr -> Expr -> BoolExpr -> BoolExpr
190 replaceTermBE d r = go
191 where go (e1 `Equal` e2) | d == e1 && r == e2 = beTrue
192 | d == e2 && r == e1 = beTrue
193 | otherwise = go' e1 `equal'` go' e2
194 go (And es) = foldr aand beTrue (map go es)
195 go (AllZipWith lbe e1 e2)
196 = AllZipWith (goL lbe) (go' e1) (go' e2)
197 go (AndEither lbe1 lbe2 e1 e2)
198 = AndEither (goL lbe1) (goL lbe2) (go' e1) (go' e2)
199 go (Condition vs cond concl)
200 = condition vs (go cond) (go concl)
201 go (UnpackPair v1 v2 e be)
202 = unpackPair v1 v2 (go' e) (go be)
203 go (TypeVarInst _ _ _) = error "TypeVarInst not expected here"
205 go' :: Data a => a -> a
206 go' = replaceExpr d r
208 goL (CurriedEquals t) = (CurriedEquals t)
209 goL (LambdaBE v1 v2 be) = lambdaBE v1 v2 (go be)
211 replaceExpr :: Data a => Expr -> Expr -> a -> a
212 replaceExpr d r = everywhere (mkT go)
213 where go e | e == d = r
216 -- | Is inside the term a definition for the variable?
217 findReplacer :: TypedExpr -> BoolExpr -> Maybe (BoolExpr -> BoolExpr)
218 findReplacer tv be = findReplacer' (unTypeExpr tv) be
220 -- | Find a definition, and return a substitution
221 findReplacer' :: Expr -> BoolExpr -> Maybe (BoolExpr -> BoolExpr)
222 -- For combined types, look up the components
223 findReplacer' (Pair x y) e | Just (delX) <- findReplacer' x e
224 , Just (delY) <- findReplacer' y e
226 -- Find the definition
227 findReplacer' e (e1 `Equal` e2) | e == e1 = Just (replaceTermBE e e2)
228 | e == e2 = Just (replaceTermBE e e1)
229 findReplacer' e (And es) = listToMaybe (mapMaybe (findReplacer' e) es)
230 -- assuming no two definitions can exist
231 findReplacer' _ _ = Nothing
233 -- | Is inside the term a definition for the variable?
234 defFor :: TypedExpr -> BoolExpr -> Maybe (TypedExpr)
235 defFor tv be | Just (e') <- defFor' (unTypeExpr tv) be
236 = Just (TypedExpr e' (typeOf tv))
237 | otherwise = Nothing
239 -- | Find a definition, and return it along the definition remover
240 defFor' :: Expr -> BoolExpr -> Maybe (Expr)
241 defFor' e (e1 `Equal` e2) | e == e1 = Just (e2)
242 | e == e2 = Just (e1)
243 defFor' e (And es) | [d] <- mapMaybe (defFor' e) es -- exactly one definition
245 defFor' _ _ = Nothing
247 app :: TypedExpr -> TypedExpr -> TypedExpr
248 app te1 te2 | Arrow t1 t2 <- typeOf te1
251 = TypedExpr (app' (unTypeExpr te1) (unTypeExpr te2)) t2
252 where app' Map (Conc []) = Conc [] -- map id = id
253 app' (Conc []) v = v -- id x = x
255 app te1 te2 | otherwise = error $ "Type mismatch in app: " ++
256 show te1 ++ " " ++ show te2
258 lambda :: TypedExpr -> TypedExpr -> TypedExpr
259 lambda tv e = TypedExpr inner (Arrow (typeOf tv) (typeOf e))
260 where inner | (Just e') <- isApplOn (unTypeExpr tv) (unTypeExpr e)
261 , not (unTypeExpr tv `occursIn` e')
264 | otherwise = Lambda tv (unTypeExpr e)
266 conc :: Expr -> Expr -> Expr
267 conc (Conc xs) (Conc ys) = Conc (xs ++ ys)
268 conc (Conc xs) y = Conc (xs ++ [y])
269 conc x (Conc ys) = Conc ([x] ++ ys)
270 conc x y = Conc ([x,y])
273 -- Specialization of g'
278 isApplOn :: Expr -> Expr -> Maybe Expr
279 isApplOn e e' | e == e' = Nothing
280 isApplOn e (App f e') | e == e' = Just (Conc [f])
281 isApplOn e (App f e') | (Just inner) <- isApplOn e e' = Just (conc f inner)
282 isApplOn _ _ = Nothing
284 occursIn :: (Typeable a, Data a1, Eq a) => a -> a1 -> Bool
285 e `occursIn` e' = not (null (listify (==e) e'))
287 isTuple :: Typ -> Bool
288 isTuple (TPair _ _) = True
301 instance Show EVar where
303 show (FromTypVar i) = "g" ++ show i
304 show (FromParam i b) = "x" ++ show i ++ if b then "'" else ""
307 instance Show Expr where
308 showsPrec _ (Var v) = showsPrec 11 v
309 showsPrec d (App e1 e2) = showParen (d>10) $
310 showsPrec 10 e1 . showChar ' ' . showsPrec 11 e2
311 showsPrec _ (Conc []) = showString "id"
312 showsPrec d (Conc [e]) = showsPrec d e
313 showsPrec d (Conc es) = showParen (d>9) $
314 showIntercalate (showString " . ") (map (showsPrec 10) es)
315 showsPrec _ (Lambda tv e) = showParen True $
320 showsPrec _ (Pair e1 e2) = showParen True $
324 showsPrec _ Map = showString "map"
325 showsPrec _ EitherMap = showString "eitherMap"
327 showIntercalate :: ShowS -> [ShowS] -> ShowS
328 showIntercalate _ [] = id
329 showIntercalate _ [x] = x
330 showIntercalate i (x:xs) = x . i . showIntercalate i xs
332 instance Show TypedExpr where
333 showsPrec d (TypedExpr e t) =
337 showString (showTypePrec 0 t)
339 instance Show LambdaBE where
340 show (CurriedEquals _) =
342 show (LambdaBE v1 v2 be) =
345 showsPrec 11 (unTypeExpr v1) "" ++
347 showsPrec 11 (unTypeExpr v2) "" ++
352 instance Show BoolExpr where
353 show (Equal e1 e2) = showsPrec 9 e1 $
356 show (And []) = "True"
357 show (And bes) = intercalate " && " $ map show bes
358 show (AllZipWith lbe e1 e2) =
362 showsPrec 11 e1 "" ++
365 show (AndEither lbe1 lbe2 e1 e2) =
371 showsPrec 11 e1 "" ++
374 show (Condition tvars be1 be2) =
376 intercalate ", " (map show tvars) ++
378 (if be1 /= beTrue then indent 2 (show be1) ++ "==>\n" else "") ++
380 show (UnpackPair v1 v2 e be) =
389 show (TypeVarInst strict i be) =
395 (if strict then "strict " else "") ++
405 indent :: Int -> String -> String
406 indent n = unlines . map (replicate n ' ' ++) . lines
408 showTypePrec :: Int -> Typ -> String
409 showTypePrec _ Int = "Int"
410 showTypePrec _ (TVar (TypVar i)) = "a"++show i
411 showTypePrec _ (TVar (TypInst i b)) | not b = "t" ++ show (2*i-1)
412 | b = "t" ++ show (2*i)
413 showTypePrec d (Arrow t1 t2) = paren (d>9) $
414 showTypePrec 10 t1 ++
417 showTypePrec _ (List t) = "[" ++ showTypePrec 0 t ++ "]"
418 showTypePrec _ (TEither t1 t2) = "Either " ++ showTypePrec 11 t1 ++
419 " " ++ showTypePrec 11 t2
420 showTypePrec _ (TPair t1 t2) = "(" ++ showTypePrec 0 t1 ++
421 "," ++ showTypePrec 0 t2 ++ ")"
422 showTypePrec _ t = error $ "Did not expect to show " ++ show t
424 paren :: Bool -> String -> String
425 paren b p = if b then "(" ++ p ++ ")" else p