1 {-# LANGUAGE PatternGuards #-}
10 | Conc [Expr] -- Conc [] is Id
19 | And BoolExpr BoolExpr
20 | AllZipWith String String BoolExpr Expr Expr
21 | Condition Expr Expr Typ BoolExpr BoolExpr
22 | UnpackPair String String Expr Bool Typ BoolExpr
23 | UnCond String Bool Typ BoolExpr
24 | TypeVarInst Int BoolExpr
31 unpackPair = UnpackPair
33 allZipWith v1 v2 rel e1 e2 | Just v1' <- defFor v1 rel =
34 e1 `equal` app (app Map (lambda v2 v1')) e2
35 | Just v2' <- defFor v2 rel =
36 app (app Map (lambda v1 v2')) e1 `equal` e2
38 AllZipWith v1 v2 rel e1 e2
40 defFor v (e1 `Equal` e2) | (Var v) == e1 = Just e2
41 | (Var v) == e2 = Just e1
44 app Map (Conc []) = Conc []
48 unCond v b t (Equal l r) | (Just l') <- isApplOn v l
49 , (Just r') <- isApplOn v r =
50 if hasVar v l' || hasVar v r'
51 then UnCond v b t (Equal l' r')
53 unCond v b t e = UnCond v b t e
55 lambda v e | (Just e') <- isApplOn v e, not (hasVar v e') = e'
56 | Var v == e = Conc []
57 | otherwise = Lambda v e
60 conc f (Conc fs) = Conc (f:fs)
64 isApplOn v (Var _) = Nothing
65 isApplOn v (App f (Var v')) | v == v' = Just (Conc [f])
66 isApplOn v (App f e) | (Just inner) <- isApplOn v e = Just (conc f inner)
67 isApplOn _ _ = Nothing
69 hasVar v (Var v') = v == v'
70 hasVar v (App e1 e2) = hasVar v e1 && hasVar v e2
71 hasVar v (Conc es) = any (hasVar v) es
72 hasVar v (Lambda _ e) = hasVar v e
75 isTuple (TPair _ _) = True
88 instance Show Expr where
89 showsPrec d (Var s) = showString s
90 showsPrec d (App e1 e2) = showParen (d>10) $
91 showsPrec 10 e1 . showChar ' ' . showsPrec 11 e2
92 showsPrec d (Conc []) = showString "id"
93 showsPrec d (Conc [e]) = showsPrec d e
94 showsPrec d (Conc es) = showParen (d>9) $
95 showIntercalate (showString " . ") (map (showsPrec 10) es)
96 showsPrec d (Lambda v e) = showParen True $
101 showsPrec _ (Pair e1 e2) = showParen True $
105 showsPrec _ Map = showString "map"
107 showIntercalate i [] = id
108 showIntercalate i [x] = x
109 showIntercalate i (x:xs) = x . i . showIntercalate i xs
111 instance Show BoolExpr where
112 show (Equal e1 e2) = showsPrec 9 e1 $
115 show (And be1 be2) = show be1 ++
118 show (AllZipWith v1 v2 be e1 e2) =
129 showsPrec 11 e1 "" ++
132 show (Condition v1 v2 t be1 be2) =
134 showsPrec 11 v1 "" ++
136 arrowInstType False t ++
138 showsPrec 11 v2 "" ++
140 arrowInstType True t ++
142 (if be1 /= BETrue then indent 2 (show be1) ++ "==>\n" else "") ++
144 show (UnpackPair v1 v2 e b t be) =
155 show (UnCond v1 b t be1) =
162 show (TypeVarInst i be) =
176 indent n = unlines . map (replicate n ' ' ++) . lines
178 arrowInstType :: Bool -> Typ -> String
179 arrowInstType b = ait 0
182 ait _ (TVar (TypVar i)) | not b = "t" ++ show (2*i-1)
183 | b = "t" ++ show (2*i)
184 ait d (Arrow t1 t2) = paren (d>9) $
185 ait 10 t1 ++ " -> " ++ ait 9 t2
186 ait d (List t) = "[" ++ ait 0 t ++ "]"
187 ait d (TEither t1 t2) = "Either " ++ ait 11 t1 ++
189 ait d (TPair t1 t2) = "(" ++ ait 0 t1 ++ "," ++ ait 0 t2 ++ ")"
191 paren b p = if b then "(" ++ p ++ ")" else p