Skip to content
This repository has been archived by the owner on Feb 10, 2020. It is now read-only.

Commit

Permalink
Named type class instances
Browse files Browse the repository at this point in the history
  • Loading branch information
paf31 committed Mar 5, 2014
1 parent 17ccc92 commit 8b6c26e
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 180 deletions.
5 changes: 5 additions & 0 deletions RELEASE-0.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## Breaking Changes

- Support for blocks has been removed. (paf31)
- Type class instances must now be named (paf31)

instance showNumber :: Show Number where
...

### New Features

Expand All @@ -24,6 +28,7 @@
- PSCI history is in XDG config (joneshf)
- PSCI allows loading of modules from ~ paths (joneshf)
- PSCI can accept a list of modules to load on start from the command line (paf31)
- Type class instances are now named, to enable easier interop with Javascript (paf31)

### Bug Fixes

Expand Down
6 changes: 3 additions & 3 deletions docgen/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ renderDeclaration n (P.TypeSynonymDeclaration name args ty) = do
renderDeclaration n (P.TypeClassDeclaration name args ds) = do
atIndent n $ "class " ++ P.runProperName name ++ " " ++ unwords args ++ " where"
mapM_ (renderDeclaration (n + 2)) ds
renderDeclaration n (P.TypeInstanceDeclaration constraints name tys _) = do
renderDeclaration n (P.TypeInstanceDeclaration name constraints className tys _) = do
let constraintsText = case constraints of
[] -> ""
cs -> "(" ++ intercalate "," (map (\(pn, tys') -> show pn ++ " (" ++ unwords (map (("(" ++) . (++ ")") . P.prettyPrintType) tys') ++ ")") cs) ++ ") => "
atIndent n $ constraintsText ++ "instance " ++ show name ++ " " ++ unwords (map (("(" ++) . (++ ")") . P.prettyPrintType) tys)
atIndent n $ constraintsText ++ "instance " ++ show name ++ " :: " ++ show className ++ " " ++ unwords (map (("(" ++) . (++ ")") . P.prettyPrintType) tys)
renderDeclaration _ _ = return ()

getName :: P.Declaration -> String
Expand All @@ -116,7 +116,7 @@ getName (P.DataDeclaration name _ _) = P.runProperName name
getName (P.ExternDataDeclaration name _) = P.runProperName name
getName (P.TypeSynonymDeclaration name _ _) = P.runProperName name
getName (P.TypeClassDeclaration name _ _) = P.runProperName name
getName (P.TypeInstanceDeclaration _ name _ _) = show name
getName (P.TypeInstanceDeclaration name _ _ _ _) = show name
getName _ = error "Invalid argument to getName"

isValueDeclaration :: P.Declaration -> Bool
Expand Down
2 changes: 1 addition & 1 deletion examples/passing/Applicative.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Applicative f where

data Maybe a = Nothing | Just a

instance Applicative Maybe where
instance applicativeMaybe :: Applicative Maybe where
pure = Just
(<*>) (Just f) (Just a) = Just (f a)
(<*>) _ _ = Nothing
Expand Down
2 changes: 1 addition & 1 deletion examples/passing/ArrayType.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Functor f where

foreign import (:) :: forall a. a -> [a] -> [a]

instance Functor [] where
instance functorArray :: Functor [] where
fmap _ [] = []
fmap f (x:xs) = f x : fmap f xs

Expand Down
2 changes: 1 addition & 1 deletion examples/passing/Do.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Prelude

data Maybe a = Nothing | Just a

instance Prelude.Monad Maybe where
instance monadMaybe :: Prelude.Monad Maybe where
return = Just
(>>=) Nothing _ = Nothing
(>>=) (Just a) f = f a
Expand Down
2 changes: 1 addition & 1 deletion examples/passing/DottedModules.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Main where
import Prelude
import Some.Module.Name

instance Some.Module.Name.Foo String where
instance fooString :: Some.Module.Name.Foo String where
foo s = s

unwrap (Text x) = x
Expand Down
2 changes: 1 addition & 1 deletion examples/passing/EmptyTypeClass.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ head (x:xs) = x

module AllowPartialFns where

instance EmptyTypeClass.Partial
instance allowPartials :: EmptyTypeClass.Partial

module Main where

Expand Down
8 changes: 4 additions & 4 deletions examples/passing/MPTCs.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import Data.Array
class NullaryTypeClass where
greeting :: String

instance NullaryTypeClass where
instance nullaryTypeClass :: NullaryTypeClass where
greeting = "Hello, World!"

class Coerce a b where
coerce :: a -> b

instance Coerce a a where
instance coerceRefl :: Coerce a a where
coerce a = a

instance (Prelude.Show a) => Coerce a String where
instance coerceShow :: (Prelude.Show a) => Coerce a String where
coerce = show

instance (Coerce a b) => Coerce [a] [b] where
instance coerceArray :: (Coerce a b) => Coerce [a] [b] where
coerce = map coerce

main = Debug.Trace.print $ coerce [greeting] :: [String]
4 changes: 2 additions & 2 deletions examples/passing/MonadState.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ data State s a = State (s -> Tuple s a)

runState s (State f) = f s

instance Prelude.Monad (State s) where
instance monadState :: Prelude.Monad (State s) where
return a = State $ \s -> Tuple s a
(>>=) f g = State $ \s -> let (Tuple s1 a) = runState s f in
runState s1 (g a)

instance MonadState s (State s) where
instance monadStateState :: MonadState s (State s) where
get = State (\s -> Tuple s s)
put s = State (\_ -> Tuple s {})

Expand Down
4 changes: 2 additions & 2 deletions examples/passing/RuntimeScopeIssue.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class A a where
class B a where
b :: a -> Boolean

instance A Number where
instance aNumber :: A Number where
a 0 = true
a n = b (n - 1)

instance B Number where
instance bNumber :: B Number where
b 0 = false
b n = a (n - 1)

Expand Down
10 changes: 5 additions & 5 deletions examples/passing/TypeClasses.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ test8 = \_ -> show $ "testing"

data Data a = Data a

instance (Prelude.Show a) => Prelude.Show (Data a) where
instance showData :: (Prelude.Show a) => Prelude.Show (Data a) where
show (Data a) = "Data (" ++ show a ++ ")"

test3 = \_ -> show (Data "testing")

instance Prelude.Monad Data where
instance monadData :: Prelude.Monad Data where
return = Data
(>>=) (Data a) f = f a

data Maybe a = Nothing | Just a

instance Prelude.Monad Maybe where
instance monadMaybe :: Prelude.Monad Maybe where
return = Just
(>>=) Nothing _ = Nothing
(>>=) (Just a) f = f a
Expand All @@ -42,13 +42,13 @@ module TypeClasses2 where
import Prelude
import TypeClasses

instance (Prelude.Show a) => Prelude.Show [a] where
instance showArray :: (Prelude.Show a) => Prelude.Show [a] where
show [] = "[]"
show (x:xs) = show x ++ ", " ++ show xs

test6 = \_ -> show ["testing"]

instance Prelude.Monad ((->) r) where
instance monadFunction :: Prelude.Monad ((->) r) where
return a r = a
(>>=) f g r = g (f r) r

Expand Down
2 changes: 1 addition & 1 deletion examples/passing/TypeClassesInOrder.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Prelude
class Foo a where
foo :: a -> String

instance Foo String where
instance fooString :: Foo String where
foo s = s

main = Debug.Trace.trace $ foo "Done"
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Main where
import Prelude
import Data.Either

instance Prelude.Functor (Either a) where
instance functorEither :: Prelude.Functor (Either a) where
(<$>) _ (Left x) = Left x
(<$>) f (Right y) = Right (f y)

Expand Down
Loading

0 comments on commit 8b6c26e

Please sign in to comment.