Skip to content

Replace case of with do blocks for Either #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 006_hindley_milner.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ type scheme.

```haskell
runInfer :: Infer (Subst, Type) -> Either TypeError Scheme
runInfer m = case evalState (runExceptT m) initUnique of
Left err -> Left err
Right res -> Right $ closeOver res
runInfer m = do
res <- evalState (runExceptT m) initUnique
return (closeOver res)
```

Substitution
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Contributors
* Christian Sievers
* Franklin Chen
* Jake Taylor
* Oskar Wickström
12 changes: 6 additions & 6 deletions chapter7/poly/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ data TypeError
| GenericTypeError

runInfer :: Infer (Subst, Type) -> Either TypeError Scheme
runInfer m = case evalState (runExceptT m) initUnique of
Left err -> Left err
Right res -> Right $ closeOver res
runInfer m = do
res <- evalState (runExceptT m) initUnique
return (closeOver res)

closeOver :: (Map.Map TVar Type, Type) -> Scheme
closeOver (sub, ty) = normalize sc
Expand Down Expand Up @@ -190,9 +190,9 @@ inferExpr env = runInfer . infer env

inferTop :: TypeEnv -> [(String, Expr)] -> Either TypeError TypeEnv
inferTop env [] = Right env
inferTop env ((name, ex):xs) = case (inferExpr env ex) of
Left err -> Left err
Right ty -> inferTop (extend env (name, ty)) xs
inferTop env ((name, ex):xs) = do
ty <- (inferExpr env ex)
inferTop (extend env (name, ty)) xs

normalize :: Scheme -> Scheme
normalize (Forall ts body) = Forall (fmap snd ord) (normtype body)
Expand Down
27 changes: 12 additions & 15 deletions chapter7/poly_constraints/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,18 @@ runInfer env m = runExcept $ evalRWST m env initInfer

-- | Solve for the toplevel type of an expression in a given environment
inferExpr :: Env -> Expr -> Either TypeError Scheme
inferExpr env ex = case runInfer env (infer ex) of
Left err -> Left err
Right (ty, cs) -> case runSolve cs of
Left err -> Left err
Right subst -> Right $ closeOver $ apply subst ty
inferExpr env ex = do
(ty, cs) <- runInfer env (infer ex)
subst <- runSolve cs
return (closeOver (apply subst ty))

-- | Return the internal constraints used in solving for the type of an expression
constraintsExpr :: Env -> Expr -> Either TypeError ([Constraint], Subst, Type, Scheme)
constraintsExpr env ex = case runInfer env (infer ex) of
Left err -> Left err
Right (ty, cs) -> case runSolve cs of
Left err -> Left err
Right subst -> Right $ (cs, subst, ty, sc)
where
sc = closeOver $ apply subst ty
constraintsExpr env ex = do
(ty, cs) <- runInfer env (infer ex)
subst <- runSolve cs
let sc = closeOver $ apply subst ty
return (cs, subst, ty, sc)

-- | Canonicalize and return the polymorphic toplevel type.
closeOver :: Type -> Scheme
Expand Down Expand Up @@ -217,9 +214,9 @@ infer expr = case expr of

inferTop :: Env -> [(String, Expr)] -> Either TypeError Env
inferTop env [] = Right env
inferTop env ((name, ex):xs) = case (inferExpr env ex) of
Left err -> Left err
Right ty -> inferTop (extend env (name, ty)) xs
inferTop env ((name, ex):xs) = do
ty <- (inferExpr env ex)
inferTop (extend env (name, ty)) xs

normalize :: Scheme -> Scheme
normalize (Forall _ body) = Forall (map snd ord) (normtype body)
Expand Down
16 changes: 8 additions & 8 deletions chapter7/poly_constraints/poly.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ extra-source-files: README.md
cabal-version: >=1.10

executable poly
build-depends:
base >= 4.6 && <4.7
, pretty >= 1.1 && <1.2
, parsec >= 3.1 && <3.2
, text >= 1.2 && <1.3
, containers >= 0.5 && <0.6
, mtl >= 2.2 && <2.3
, transformers >= 0.4.2 && <0.5
build-depends:
base >= 4.6 && <= 4.9
, pretty >= 1.1 && < 1.2
, parsec >= 3.1 && < 3.2
, text >= 1.2 && < 1.3
, containers >= 0.5 && < 0.6
, mtl >= 2.2 && < 2.3
, transformers >= 0.4.2 && < 0.5
, repline >= 0.1.2.0
default-language: Haskell2010
main-is: Main.hs