diff --git a/explore/lower-your-guards/Setup.hs b/explore/lower-your-guards/Setup.hs index 9a994af6..e8ef27db 100644 --- a/explore/lower-your-guards/Setup.hs +++ b/explore/lower-your-guards/Setup.hs @@ -1,2 +1,3 @@ import Distribution.Simple + main = defaultMain diff --git a/explore/lower-your-guards/src/Annotated.hs b/explore/lower-your-guards/src/Annotated.hs index 50ecf4d9..70def529 100644 --- a/explore/lower-your-guards/src/Annotated.hs +++ b/explore/lower-your-guards/src/Annotated.hs @@ -3,8 +3,8 @@ module Annotated where import qualified GuardTree as G -import qualified Uncovered as U import MatchInfo +import qualified Uncovered as U data Ant where Grhs :: U.RefinementType -> Int -> Ant @@ -17,4 +17,5 @@ annotated ref gdt = case gdt of G.Guarded (var, g) t -> case g of G.GMatch k args -> annotated (ref `U.liftAndLit` varInfo (Match k args)) t G.GWas new -> annotated (ref `U.liftAndLit` varInfo (WasOriginally new)) t - where varInfo = U.Info var + where + varInfo = U.Info var diff --git a/explore/lower-your-guards/src/GuardTree.hs b/explore/lower-your-guards/src/GuardTree.hs index 865c6650..bfbee784 100644 --- a/explore/lower-your-guards/src/GuardTree.hs +++ b/explore/lower-your-guards/src/GuardTree.hs @@ -6,9 +6,9 @@ import Control.Monad (replicateM, zipWithM) import Data.List.NonEmpty (NonEmpty (..)) import qualified Data.List.NonEmpty as NE import qualified Fresh as F +import MatchInfo import qualified Parse as P import qualified Types as Ty -import MatchInfo data Gdt where Grhs :: Int -> Gdt @@ -32,11 +32,11 @@ desugarClauses args clauses = do desugarClause :: [F.VarID] -> (Int, P.Clause) -> F.Fresh Gdt desugarClause args (i, P.Clause pat typeIn _) = do let x1 = head args -- we only suport 1 arg for this toy lyg - guards <- desugarMatch (x1,typeIn) pat + guards <- desugarMatch (x1, typeIn) pat return $ foldr Guarded (Grhs i) guards desugarMatch :: TypedVar -> P.Pattern -> F.Fresh [(TypedVar, Guard)] -desugarMatch var@(_,ty) pat = do +desugarMatch var@(_, ty) pat = do case pat of P.PWild -> return [] P.PVar name -> do diff --git a/explore/lower-your-guards/src/Inhabitants.hs b/explore/lower-your-guards/src/Inhabitants.hs index 254fbc4c..f18231d1 100644 --- a/explore/lower-your-guards/src/Inhabitants.hs +++ b/explore/lower-your-guards/src/Inhabitants.hs @@ -45,9 +45,9 @@ accessableRedundant ant args = case ant of -- do a linear scan from right to left lookupVar :: TypedVar -> [ConstraintFor] -> TypedVar lookupVar x = foldr getNextId x - where - getNextId (x', MatchInfo (WasOriginally y)) | x' == x = const y - getNextId _ = id + where + getNextId (x', MatchInfo (WasOriginally y)) | x' == x = const y + getNextId _ = id alistLookup :: (Eq a) => a -> [(a, b)] -> [b] alistLookup a = map snd . filter ((== a) . fst) @@ -101,10 +101,10 @@ findVarInhabitants var nref@(_, cns) = if null posNrefs then Poss.retSingle $ IPNot [] else Poss.anyOf <$> forM posNrefs (findVarInhabitants var) - where - constraintsOnX = onVar var cns - posMatch = listToMaybe $ mapMaybe (\case MatchInfo (Match k ys) -> Just (k, ys); _ -> Nothing) constraintsOnX - negMatch = mapMaybe (\case MatchInfo (Not k) -> Just k; _ -> Nothing) constraintsOnX + where + constraintsOnX = onVar var cns + posMatch = listToMaybe $ mapMaybe (\case MatchInfo (Match k ys) -> Just (k, ys); _ -> Nothing) constraintsOnX + negMatch = mapMaybe (\case MatchInfo (Not k) -> Just k; _ -> Nothing) constraintsOnX normalize :: NormRefType -> U.Formula -> F.Fresh (S.Set NormRefType) normalize nref (f1 `U.And` f2) = do @@ -155,8 +155,8 @@ addConstraintHelper nref@(ctx, cns) cf@(origX, c) = case c of else do let (noX', withX') = partition ((/= origX) . fst) cns addConstraints (ctx, noX' ++ [cf]) (substituteVarIDs origY origX withX') - where - added = (ctx, cns ++ [cf]) + where + added = (ctx, cns ++ [cf]) ----- ----- Helper functions for adding constraints: diff --git a/explore/lower-your-guards/src/Main.hs b/explore/lower-your-guards/src/Main.hs index a2ea3852..c75727da 100644 --- a/explore/lower-your-guards/src/Main.hs +++ b/explore/lower-your-guards/src/Main.hs @@ -147,5 +147,5 @@ niceInhabPattern (I.IPNot nots) = "(Not " ++ niceList (map Ty.dcName nots) ++ ") niceList :: [Text] -> String niceList as = concat $ tail $ concatMap comma as - where - comma x = [", ", T.unpack x] + where + comma x = [", ", T.unpack x] diff --git a/explore/lower-your-guards/src/MatchTree.hs b/explore/lower-your-guards/src/MatchTree.hs index 6fb02e39..f2b4da09 100644 --- a/explore/lower-your-guards/src/MatchTree.hs +++ b/explore/lower-your-guards/src/MatchTree.hs @@ -73,21 +73,21 @@ treeMinus t1 t2 = case (t1, t2) of (Pair _ _, Either _ _) -> error "type error6" (Pair a b, Pair c d) -> map mkPairL aMinusC ++ map mkPairR bMinusD ++ both - where - mkPairL aSubC = Pair aSubC d - mkPairR bSubD = Pair c bSubD - both = [Pair aSubC bSubD | aSubC <- aMinusC, bSubD <- bMinusD] - c' = treeIntersect a c - d' = treeIntersect b d - aMinusC = concatMap (a \\) c' - bMinusD = concatMap (b \\) d' - -- [Pair d' aSubC, Pair c' bSubD, Pair aSubC bSubD] + where + mkPairL aSubC = Pair aSubC d + mkPairR bSubD = Pair c bSubD + both = [Pair aSubC bSubD | aSubC <- aMinusC, bSubD <- bMinusD] + c' = treeIntersect a c + d' = treeIntersect b d + aMinusC = concatMap (a \\) c' + bMinusD = concatMap (b \\) d' + -- [Pair d' aSubC, Pair c' bSubD, Pair aSubC bSubD] (Either a b, Either c d) -> [Either left right] - where - -- l = foldr a (flip map (\\)) c - left = concat [x \\ y | x <- a, y <- c] - right = concat [x \\ y | x <- b, y <- d] + where + -- l = foldr a (flip map (\\)) c + left = concat [x \\ y | x <- a, y <- c] + right = concat [x \\ y | x <- b, y <- d] treeIntersect :: MatchTree -> MatchTree -> [MatchTree] treeIntersect m1 m2 = case (m1, m2) of @@ -105,20 +105,20 @@ treeIntersect m1 m2 = case (m1, m2) of (Either _ _, Pair _ _) -> error "type error5" (Pair _ _, Either _ _) -> error "type error6" (Pair a b, Pair c d) -> error "pairs" - -- map mkPairL (a \\ c) ++ map mkPairR (b \\ d) ++ both - -- where - -- mkPairL aSubC = Pair aSubC d - -- mkPairR bSubD = Pair c bSubD - -- both = [Pair aSubC bSubD | aSubC <- a \\ c, bSubD <- b \\ d] - -- c' = statIntersect a c - -- d' = statIntersect b d + -- map mkPairL (a \\ c) ++ map mkPairR (b \\ d) ++ both + -- where + -- mkPairL aSubC = Pair aSubC d + -- mkPairR bSubD = Pair c bSubD + -- both = [Pair aSubC bSubD | aSubC <- a \\ c, bSubD <- b \\ d] + -- c' = statIntersect a c + -- d' = statIntersect b d (Either a b, Either c d) -> error "eithers" - -- [Either left right] - -- where - -- -- l = foldr a (flip map (\\)) c - -- left = concat [x \\ y | x <- a, y <- c] - -- right = concat [x \\ y | x <- b, y <- d] - + +-- [Either left right] +-- where +-- -- l = foldr a (flip map (\\)) c +-- left = concat [x \\ y | x <- a, y <- c] +-- right = concat [x \\ y | x <- b, y <- d] -- (a-c)*(b-d) + c*(b-d) + (a-c)*d diff --git a/explore/lower-your-guards/src/Parse.hs b/explore/lower-your-guards/src/Parse.hs index 7f62341e..6c2b0e50 100644 --- a/explore/lower-your-guards/src/Parse.hs +++ b/explore/lower-your-guards/src/Parse.hs @@ -105,9 +105,9 @@ pDataConsMatch typeIn = pPattern :: Ty.Type -> Parser Pattern pPattern typeIn = choice - [ symbol "_" $> PWild, - pDataConsMatch typeIn, - pName <&> PVar + [ symbol "_" $> PWild + , pDataConsMatch typeIn + , pName <&> PVar ] pClause :: Text -> Ty.Type -> Parser Clause @@ -133,15 +133,15 @@ pFn = do pType :: Parser Ty.Type pType = choice - [ Ty.int <$ lexeme (string "Int"), - Ty.bool <$ lexeme (string "Bool"), - Ty.throol <$ lexeme (string "Throol"), - do + [ Ty.int <$ lexeme (string "Int") + , Ty.bool <$ lexeme (string "Bool") + , Ty.throol <$ lexeme (string "Throol") + , do _ <- symbol "," l <- pType r <- pType - return $ Ty.pair l r, - do + return $ Ty.pair l r + , do _ <- lexeme (string "Either") l <- pType r <- pType diff --git a/explore/lower-your-guards/src/Play.hs b/explore/lower-your-guards/src/Play.hs index dad78cf1..9ff61c5e 100644 --- a/explore/lower-your-guards/src/Play.hs +++ b/explore/lower-your-guards/src/Play.hs @@ -1,8 +1,8 @@ module Play where thing :: (Int, Bool) -> () -thing (n,True) = () -thing (0,n) = () +thing (n, True) = () +thing (0, n) = () triple :: (Int, Int, Int) -> () triple (7, 5, 3) = () @@ -11,19 +11,19 @@ triple2 :: (Int, (Int, Int)) -> () triple2 (7, (5, 3)) = () foo :: (Either Int Bool, Int) -> Bool -foo (Left 1 , 2) = True -foo (Right False , n) = True -foo (Right True , n) = True -foo (Left 3 , n) = True -foo (Left 3 , n) = True +foo (Left 1, 2) = True +foo (Right False, n) = True +foo (Right True, n) = True +foo (Left 3, n) = True +foo (Left 3, n) = True foo2 :: (Either Int Bool, Int) -> Bool -foo2 (Left 1 , n) = True -foo2 (Right False , n) = True +foo2 (Left 1, n) = True +foo2 (Right False, n) = True foo3 :: (Either Bool Bool, Bool) -> Bool -foo3 (Left True , b) = True -foo3 (Right False , b) = True +foo3 (Left True, b) = True +foo3 (Right False, b) = True foo4 :: (Int, Int) -> Bool foo4 (1, n) = True @@ -42,24 +42,25 @@ foo7 (1, 2, 3) = False foo8 :: (Either Int Bool, Int) -> () foo8 (Left 10, 2) = () + -- foo8 (Right True, 5) = () data Pat where - Base :: Pat - Kon :: Pat -> Pat - DonKon :: Pat -> Pat -> Pat - deriving (Show, Eq) + Base :: Pat + Kon :: Pat -> Pat + DonKon :: Pat -> Pat -> Pat + deriving (Show, Eq) timelineSplitter :: Int -> [Pat] timelineSplitter p = do - case p of - 0 -> return Base - 2 -> [Base, Base] - 4 -> do - a <- timelineSplitter (p - 1) - b <- timelineSplitter (p - 2) - - return $ DonKon a b - _ -> do - n <- timelineSplitter (p - 1) - return $ Kon n + case p of + 0 -> return Base + 2 -> [Base, Base] + 4 -> do + a <- timelineSplitter (p - 1) + b <- timelineSplitter (p - 2) + + return $ DonKon a b + _ -> do + n <- timelineSplitter (p - 1) + return $ Kon n diff --git a/explore/lower-your-guards/src/Possibilities.hs b/explore/lower-your-guards/src/Possibilities.hs index 359e0618..96232977 100644 --- a/explore/lower-your-guards/src/Possibilities.hs +++ b/explore/lower-your-guards/src/Possibilities.hs @@ -55,10 +55,10 @@ retSingle i = return $ Possibilities [i] -- cartesian product of multiple sets allCombinations :: [Possibilities a] -> Possibilities [a] allCombinations = foldr prod nil - where - -- note, nil /= mempty - -- VERY important - nil = Possibilities [[]] + where + -- note, nil /= mempty + -- VERY important + nil = Possibilities [[]] prod :: Possibilities a -> Possibilities [a] -> Possibilities [a] prod (Possibilities xs) (Possibilities yss) = Possibilities [x : ys | x <- xs, ys <- yss] diff --git a/explore/lower-your-guards/src/Types.hs b/explore/lower-your-guards/src/Types.hs index 24f21d0c..81f25aec 100644 --- a/explore/lower-your-guards/src/Types.hs +++ b/explore/lower-your-guards/src/Types.hs @@ -1,4 +1,5 @@ {-# OPTIONS_GHC -Wno-missing-export-lists #-} + module Types where import Data.Text (Text) @@ -8,17 +9,17 @@ data TypeConstructor = TBool | TPair | TEither | TInt | TThrool deriving (Show, Eq, Ord) data Type = Type - { typeCons :: TypeConstructor, - dataCons :: Maybe [DataConstructor] + { typeCons :: TypeConstructor + , dataCons :: Maybe [DataConstructor] } deriving (Eq, Ord) instance Show Type where - show Type { typeCons = t } = "Type: " ++ show t + show Type {typeCons = t} = "Type: " ++ show t data DataConstructor = DataConstructor - { dcIdent :: DataConName, - dcTypes :: [Type] + { dcIdent :: DataConName + , dcTypes :: [Type] } deriving (Eq, Ord) @@ -36,34 +37,37 @@ instance Show DataConstructor where show dc = "(\'" ++ T.unpack (dcName dc) ++ "\' <" ++ (show . length $ dcTypes dc) ++ ">)" intCon :: Int -> DataConstructor -intCon i = DataConstructor { dcIdent = NameInt i, dcTypes = []} +intCon i = DataConstructor {dcIdent = NameInt i, dcTypes = []} bool :: Type bool = Type - { typeCons = TBool, - dataCons = Just - [ DataConstructor {dcIdent = NameText "True", dcTypes = []}, - DataConstructor {dcIdent = NameText "False", dcTypes = []} + { typeCons = TBool + , dataCons = + Just + [ DataConstructor {dcIdent = NameText "True", dcTypes = []} + , DataConstructor {dcIdent = NameText "False", dcTypes = []} ] } throol :: Type throol = Type - { typeCons = TThrool, - dataCons = Just - [ DataConstructor {dcIdent = NameText "Foo", dcTypes = []}, - DataConstructor {dcIdent = NameText "Bar", dcTypes = []}, - DataConstructor {dcIdent = NameText "Baz", dcTypes = []} + { typeCons = TThrool + , dataCons = + Just + [ DataConstructor {dcIdent = NameText "Foo", dcTypes = []} + , DataConstructor {dcIdent = NameText "Bar", dcTypes = []} + , DataConstructor {dcIdent = NameText "Baz", dcTypes = []} ] } pair :: Type -> Type -> Type pair a b = Type - { typeCons = TPair, - dataCons = Just + { typeCons = TPair + , dataCons = + Just [ DataConstructor {dcIdent = NameText ",", dcTypes = [a, b]} ] } @@ -71,17 +75,18 @@ pair a b = either :: Type -> Type -> Type either a b = Type - { typeCons = TEither, - dataCons = Just - [ DataConstructor {dcIdent = NameText "Left", dcTypes = [a]}, - DataConstructor {dcIdent = NameText "Right", dcTypes = [b]} + { typeCons = TEither + , dataCons = + Just + [ DataConstructor {dcIdent = NameText "Left", dcTypes = [a]} + , DataConstructor {dcIdent = NameText "Right", dcTypes = [b]} ] } int :: Type int = Type - { typeCons = TInt, - -- int is an opaque type + { typeCons = TInt + , -- int is an opaque type dataCons = Nothing } diff --git a/explore/lower-your-guards/src/UA.hs b/explore/lower-your-guards/src/UA.hs index 96bef30c..b70d1825 100644 --- a/explore/lower-your-guards/src/UA.hs +++ b/explore/lower-your-guards/src/UA.hs @@ -31,8 +31,8 @@ ua nrefs gdt = case gdt of (n', u) <- ua n t n'' <- addLitMulti nrefs $ varInfo (Not k) return (n'' ++ n', u) - where - varInfo = U.Info var + where + varInfo = U.Info var addLitMulti :: [I.NormRefType] -> U.Literal -> F.Fresh [I.NormRefType] addLitMulti [] _ = return [] diff --git a/explore/lower-your-guards/src/Uncovered.hs b/explore/lower-your-guards/src/Uncovered.hs index 00cd3f01..875025f0 100644 --- a/explore/lower-your-guards/src/Uncovered.hs +++ b/explore/lower-your-guards/src/Uncovered.hs @@ -29,12 +29,12 @@ uncovered r g = case g of uncovered (uncovered r t1) t2 G.Guarded (var, guard) t -> case guard of G.GMatch dataCon ys -> noMatch `union` matchedPath - where - noMatch = r `liftAndLit` varInfo (Not dataCon) - matchedPath = uncovered (r `liftAndLit` varInfo (Match dataCon ys)) t + where + noMatch = r `liftAndLit` varInfo (Not dataCon) + matchedPath = uncovered (r `liftAndLit` varInfo (Match dataCon ys)) t G.GWas old -> uncovered (r `liftAndLit` varInfo (WasOriginally old)) t - where - varInfo = Info var + where + varInfo = Info var liftAndLit :: RefinementType -> Literal -> RefinementType liftAndLit (cont, form) f = (cont, form `And` Literal f)