1 {-# LANGUAGE PatternGuards, FlexibleContexts #-}
7 import Control.Monad.Reader
9 test = putStrLn . show . freeTheorem . parseType
11 freeTheorem t = flip runReader freeVars $
12 freeTheorem' (typedLeft (Var "f") (unquantify t))
13 (typedRight (Var "f") (unquantify t))
15 freeVars = (map (:"") ['a'..])
18 freeTheorem' :: TypedExpr -> TypedExpr -> Typ -> Reader [String] BoolExpr
20 freeTheorem' e1 e2 Int = return $
23 freeTheorem' e1 e2 (TVar (TypVar i)) = return $
24 let tv = TypedExpr (Var ("g"++show i))
25 (Arrow (TVar (TypInst i False)) (TVar (TypInst i True)))
26 in equal (app tv e1) e2
28 freeTheorem' e1 e2 (Arrow t1 t2) | isTuple t1 = do
29 -- Create patterns for (possily nested) tuples and only give
30 -- the inner variables names
31 fillTuplevars False t1 $ \tve1 ->
32 fillTuplevars True t1 $ \tve2 -> do
33 cond <- freeTheorem' (tve1) (tve2) t1
34 concl <- freeTheorem' (app e1 tve1) (app e2 tve2) t2
35 return $ condition [tve1, tve2] cond concl
37 -- No tuple on the left hand side:
38 | otherwise = getSideVars t1 $ \(v1,v2) -> do
39 cond <- freeTheorem' v1 v2 t1
40 concl <- freeTheorem' (app e1 v1) (app e2 v2) t2
41 return $ condition [ v1, v2 ] cond concl
43 freeTheorem' e1 e2 (List t) = getSideVars t $ \(v1,v2) -> do
44 map <- freeTheorem' v1 v2 t
45 return $ allZipWith v1 v2 map e1 e2
48 freeTheorem' e1 e2 (TPair t1 t2) = getVars 4 $ \[x1,x2,y1,y2] -> do
49 concl1 <- freeTheorem' (Var x1) (Var y1) t1
50 concl2 <- freeTheorem' (Var x2) (Var y2) t2
51 return $ Condition x1 y1 t1 BETrue $
53 And (Equal e2 (Pair (Var y1) (Var y2)))
54 (Equal e1 (Pair (Var x1) (Var x2)))) $
58 freeTheorem' e1 e2 t@(TPair t1 t2)
59 | (Pair x1 x2) <- unTypeExpr e1
60 , (Pair y1 y2) <- unTypeExpr e2
61 = do concl1 <- freeTheorem'
65 concl2 <- freeTheorem'
69 return $ aand concl1 concl2
71 = getVars 4 $ \[x1',x2',y1',y2'] -> do
72 let x1 = TypedExpr (Var x1') tx1
73 x2 = TypedExpr (Var x2') tx2
74 y1 = TypedExpr (Var y1') ty1
75 y2 = TypedExpr (Var y2') ty2
76 concl1 <- freeTheorem' x1 y1 t1
77 concl2 <- freeTheorem' x2 y2 t2
78 return $ unpackPair x1 x2 e1 $
81 where (TPair tx1 tx2) = typeOf e1
82 (TPair ty1 ty2) = typeOf e2
84 freeTheorem' e1 e2 (AllStar (TypVar i) t) = TypeVarInst i `fmap` freeTheorem' e1 e2 t
85 freeTheorem' e1 e2 (All (TypVar i) t) = TypeVarInst i `fmap` freeTheorem' e1 e2 t
87 getVars :: (MonadReader [String] m) => Int -> ([String] -> m a) -> m a
88 getVars n a = asks (take n) >>= local (drop n) . a
90 getSideVars :: (MonadReader [String] m) => Typ -> ((TypedExpr,TypedExpr) -> m a) -> m a
91 getSideVars t a = getVars 2 $ \[v1,v2] -> a (typedLeft (Var v1) t, typedRight (Var v2) t)
93 fillTuplevars :: (MonadReader [String] m) => Bool -> Typ -> (TypedExpr -> m a) -> m a
94 fillTuplevars rightSide t@(TPair t1 t2) a = do
95 fillTuplevars rightSide t1 $ \ve1 ->
96 fillTuplevars rightSide t2 $ \ve2 ->
97 let pair = Pair (unTypeExpr ve1) (unTypeExpr ve2) in
98 a (TypedExpr pair (instType rightSide t))
99 fillTuplevars rightSide t a = getVars 1 $ \[s] ->
100 a (TypedExpr (Var s) (instType rightSide t))