Skip to content

Commit

Permalink
Remove resolved implementation from scope, closes #96
Browse files Browse the repository at this point in the history
  • Loading branch information
owickstrom committed May 17, 2016
1 parent a88f16d commit e216bef
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 21 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ endif

.PHONY: clean
clean:
stack clean
rm -rf build

.PHONY: test
Expand Down
19 changes: 19 additions & 0 deletions regression-test/src/protocols/chainedimpl.oden
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package protocols/chainedimpl/main

protocol Foo(a) {
Foo : a -> a
}

protocol Bar(b) {
Bar : b -> b
}

impl Bar(int) {
Bar(n) = n
}

impl Foo(int) {
Foo(n) = Bar::Bar(n)
}

main() = println(Foo::Foo(1))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
5 changes: 3 additions & 2 deletions src/Oden/Compiler/Resolution.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ resolveInExpr' = traverseExpr traversal


resolveInDefinition' :: TypedDefinition -> Resolve TypedDefinition
resolveInDefinition' = \case
resolveInDefinition' =
\case
Definition si name (scheme, expr) -> do
expr' <- resolveInExpr' expr
return (Definition si name (scheme, expr'))
Expand All @@ -122,7 +123,7 @@ resolveInDefinition' = \case
return (ProtocolDefinition si name protocol)
Implementation si implementation -> do
resolved <- resolveInImplementation implementation
modify (Set.insert resolved)
modify (Set.insert resolved . Set.delete implementation)
return (Implementation si resolved)


Expand Down
11 changes: 7 additions & 4 deletions src/Oden/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,14 @@ generalize env expr = (scheme, expr)
where quantifiers = map (TVarBinding $ Metadata Missing) (Set.toList $ ftv expr `Set.difference` ftv env)
scheme = Forall (Metadata $ getSourceInfo expr) quantifiers (collectConstraints expr) (typeOf expr)

instantiateMethod :: Protocol -> ProtocolMethod -> Infer (TypedMethodReference, Type)
instantiateMethod (Protocol _ protocolName' param _) (ProtocolMethod _ methodName (Forall _ qs cs methodType)) = do
instantiateMethod :: Metadata SourceInfo
-> Protocol
-> ProtocolMethod
-> Infer (TypedMethodReference, Type)
instantiateMethod constraintSi (Protocol _ protocolName' param _) (ProtocolMethod _ methodName (Forall _ qs cs methodType)) = do
headTypeVariables <- mapM (freshTypeForFreeVar (getSourceInfo param)) (Set.toList (ftv param))
subst <- Substitution.fromList <$> ((headTypeVariables ++) <$> mapM freshTypeForBinding qs)
let constraint = apply subst (ProtocolConstraint (Metadata Missing) protocolName' param)
let constraint = apply subst (ProtocolConstraint constraintSi protocolName' param)
constrainedType = TConstrained (Set.insert constraint cs) (apply subst methodType)
return (Typed.Unresolved protocolName' methodName constraint, constrainedType)
where
Expand Down Expand Up @@ -426,7 +429,7 @@ infer = \case
MethodReference si (NamedMethodReference protocol method) Untyped -> do
protocolType' <- lookupProtocol si protocol
method' <- findMethod (unwrap si) protocolType' method
(ref, methodType) <- instantiateMethod protocolType' method'
(ref, methodType) <- instantiateMethod si protocolType' method'
return (MethodReference si ref methodType)

ForeignFnApplication (Metadata si) _ _ _ ->
Expand Down
22 changes: 13 additions & 9 deletions src/Oden/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ code settings d =
escape settings [1, 34] <> contents <> escape settings [0]
where contents = backtick <> d <> backtick

instance Pretty SourceInfo where
pretty e =
case e of
SourceInfo pos ->
text (fileName pos)
<> colon <> int (line pos)
<> colon <> int (column pos)
Predefined -> empty
Missing -> empty

formatSourceInfo :: (MonadReader OutputSettings m, OdenOutput e) => e -> m Doc
formatSourceInfo e =
case sourceInfo e of
Just (SourceInfo pos) ->
return $ text (fileName pos)
<> colon <> int (line pos)
<> colon <> int (column pos)
<> colon
Just Predefined -> return empty
Just Missing -> return empty
Nothing -> return empty
Just si -> return (pretty si <> colon <> space)
Nothing -> return empty

formatOutputType :: (MonadReader OutputSettings m, OdenOutput e) => e -> m Doc
formatOutputType e = do
Expand All @@ -68,7 +72,7 @@ format e = do
t <- formatOutputType e
wl <- wikiLink e
return (vcat [
pos <+> t <+> header e s,
pos <> t <+> header e s,
indent 2 (details e s),
wl
])
Expand Down
18 changes: 14 additions & 4 deletions src/Oden/Output/Compiler/Resolution.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
module Oden.Output.Compiler.Resolution where

import Text.PrettyPrint.Leijen

import Oden.Compiler.Resolution
import Oden.Core.ProtocolImplementation
import Oden.Metadata
import Oden.Output
import Oden.Pretty ()
import Oden.Pretty ()

instance OdenOutput ResolutionError where
outputType _ = Error
Expand All @@ -30,10 +33,17 @@ instance OdenOutput ResolutionError where
NoMatchingImplementationInScope _ _ _ allImpls ->
vcat (text "The following implementations are in scope:" : map pretty allImpls)
MultipleMatchingImplementationsInScope _ impls ->
vcat (text "The following implementations matched:" : map pretty impls)
vcat (text "The following implementations matched:" : concatMap printImpl impls)
where
printImpl impl@(ProtocolImplementation (Metadata si) _ _ _) =
[ empty
, pretty impl
, text "defined at" <+> pretty si
, empty
]


sourceInfo =
\case
NoMatchingImplementationInScope si _ _ _ -> Just si
MultipleMatchingImplementationsInScope si _ -> Just si

2 changes: 1 addition & 1 deletion src/Oden/Predefined.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ types = [
universe :: TypedPackage
universe =
TypedPackage
(PackageDeclaration (Metadata Missing) [])
(PackageDeclaration (Metadata Predefined) [])
[]
(concat [ map toProtocolDef protocols
, map toForeignDef foreignFns
Expand Down
2 changes: 1 addition & 1 deletion src/Oden/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ instance Pretty Poly.Protocol where

instance (Pretty r, Pretty t, Pretty m) => Pretty (MethodImplementation (Expr r t m)) where
pretty (MethodImplementation _ methodName expr) =
vcat [ pretty methodName <+> equals <+> pretty (typeOf expr)
vcat [ pretty methodName <+> colon <+> pretty (typeOf expr)
, prettyDefinition methodName expr
]

Expand Down

0 comments on commit e216bef

Please sign in to comment.