Skip to content

poly_constraints: update build #112

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 2 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
16 changes: 8 additions & 8 deletions chapter7/poly_constraints/poly.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ cabal-version: >=1.10

executable poly
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
base
, pretty
, parsec
, text
, containers
, mtl
, transformers
, repline

other-modules:
Env
Expand Down
4 changes: 3 additions & 1 deletion chapter7/poly_constraints/src/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ fromList xs = TypeEnv (Map.fromList xs)
toList :: Env -> [(Name, Scheme)]
toList (TypeEnv env) = Map.toList env

instance Semigroup Env where
(<>) = merge

instance Monoid Env where
mempty = empty
mappend = merge
20 changes: 16 additions & 4 deletions chapter7/poly_constraints/src/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,23 @@ eval env expr = case expr of
return v

Op op a b -> do
VInt a' <- eval env a
VInt b' <- eval env b
let unwrap comp = do v <- eval env comp
case v of
VInt x -> pure x
_ -> error "eval: type error: non-VInt"
a' <- unwrap a
b' <- unwrap b
return $ (binop op) a' b'

Lam x body ->
return (VClosure x body env)

App fun arg -> do
VClosure x body clo <- eval env fun
let unwrap comp = do v <- eval env comp
case v of
VClosure x y z -> pure (x, y, z)
_ -> error "eval: type error: non-VClosure"
(x, body, clo) <- unwrap fun
argv <- eval env arg
let nenv = Map.insert x argv clo
eval nenv body
Expand All @@ -50,7 +58,11 @@ eval env expr = case expr of
eval nenv body

If cond tr fl -> do
VBool br <- eval env cond
let unwrap comp = do v <- eval env comp
case v of
VBool x -> pure x
_ -> error "eval: type error: non-VBool"
br <- unwrap cond
if br == True
then eval env tr
else eval env fl
Expand Down
2 changes: 1 addition & 1 deletion chapter7/poly_constraints/src/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Unifier = (Subst, [Constraint])
type Solve a = ExceptT TypeError Identity a

newtype Subst = Subst (Map.Map TVar Type)
deriving (Eq, Ord, Show, Monoid)
deriving (Eq, Ord, Show, Semigroup, Monoid)

class Substitutable a where
apply :: Subst -> a -> a
Expand Down
35 changes: 22 additions & 13 deletions chapter7/poly_constraints/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,21 @@ cmd source = exec True (L.pack source)
-------------------------------------------------------------------------------

-- :browse command
browse :: [String] -> Repl ()
browse :: String -> Repl ()
browse _ = do
st <- get
liftIO $ mapM_ putStrLn $ ppenv (tyctx st)

-- :load command
load :: [String] -> Repl ()
load :: String -> Repl ()
load args = do
contents <- liftIO $ L.readFile (unwords args)
contents <- liftIO $ L.readFile args
exec True contents

-- :type command
typeof :: [String] -> Repl ()
typeof args = do
typeof :: String -> Repl ()
typeof arg = do
st <- get
let arg = unwords args
case Env.lookup arg (tyctx st) of
Just val -> liftIO $ putStrLn $ ppsignature (arg, val)
Nothing -> exec False (L.pack arg)
Expand Down Expand Up @@ -134,8 +133,8 @@ comp n = do
let defs = Map.keys ctx
return $ filter (isPrefixOf n) (cmds ++ defs)

options :: [(String, [String] -> Repl ())]
options = [
opts :: [(String, String -> Repl ())]
opts = [
("load" , load)
, ("browse" , browse)
, ("quit" , quit)
Expand All @@ -149,9 +148,19 @@ options = [
completer :: CompleterStyle (StateT IState IO)
completer = Prefix (wordCompleter comp) defaultMatcher

shell :: Repl a -> IO ()
shell pre = flip evalStateT initState
$ evalRepl "Poly> " cmd options completer pre
shell :: Repl () -> IO ()
shell pre = flip evalStateT initState $
evalReplOpts $ ReplOpts
{ banner = const (pure "Poly> ")
, command = cmd
, options = opts
, prefix = Just ':'
, multilineCommand = Nothing
, tabComplete = completer
, initialiser = pre
, finaliser = pure Exit
}


-------------------------------------------------------------------------------
-- Toplevel
Expand All @@ -162,6 +171,6 @@ main = do
args <- getArgs
case args of
[] -> shell (return ())
[fname] -> shell (load [fname])
["test", fname] -> shell (load [fname] >> browse [] >> quit ())
[fname] -> shell (load fname)
["test", fname] -> shell (load fname >> browse "" >> quit ())
_ -> putStrLn "invalid arguments"
15 changes: 8 additions & 7 deletions chapter7/poly_constraints/src/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Type
import Syntax
import Infer

import Text.PrettyPrint
import Text.PrettyPrint hiding ((<>))
import qualified Data.Map as Map

parensIf :: Bool -> Doc -> Doc
Expand All @@ -45,7 +45,7 @@ instance Pretty Type where

instance Pretty Scheme where
ppr p (Forall [] t) = ppr p t
ppr p (Forall ts t) = text "forall" <+> hcat (punctuate space (map (ppr p) ts)) <> text "." <+> ppr p t
ppr p (Forall ts t) = text "forall" <+> (hcat (punctuate space (map (ppr p) ts)) <> (text "." <+> ppr p t))

instance Pretty Binop where
ppr _ Add = text "+"
Expand All @@ -56,15 +56,16 @@ instance Pretty Binop where
instance Pretty Expr where
ppr p (Var a) = ppr p a
ppr p (App a b) = parensIf (p > 0) $ ppr (p+1) a <+> ppr p b
ppr p (Lam a b) = text "\\" <> ppr p a <+> text "->" <+> ppr p b
ppr p (Let a b c) = text "let" <> ppr p a <+> text "=" <+> ppr p b <+> text "in" <+> ppr p c
ppr p (Lam a b) = text "\\" <> (ppr p a <+> text "->" <+> ppr p b)
ppr p (Let a b c) = text "let" <> (ppr p a <+> text "=" <+> ppr p b <+> text "in" <+> ppr p c)
ppr p (Lit a) = ppr p a
ppr p (Op o a b) = parensIf (p>0) $ ppr p a <+> ppr p o <+> ppr p b
ppr p (Fix a) = parensIf (p>0) $ text "fix" <> ppr p a
ppr p (If a b c) =
text "if" <> ppr p a <+>
text "then" <+> ppr p b <+>
text "else" <+> ppr p c
text "if" <>
(ppr p a <+>
text "then" <+> ppr p b <+>
text "else" <+> ppr p c)

instance Pretty Lit where
ppr _ (LInt i) = integer i
Expand Down