git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge remote-tracking branch 'joneshf/hierarchy'

Conflicts: purescript.cabal

commitf2dc269eeb5066f31a627bf1fcb77f72ffc2b5f6 authorPhil Freeman <paf31@cantab.net> dateTue 15 Apr 2014, 10:37 (13 years ago) parent770351a112c29b
7 files changed+322−46
docgen/Main.hs+17−8 docs/source/prelude.rst+97−19 hierarchy/Main.hs+103 prelude/README.md+89−19 prelude/images/Prelude.png prelude/updateDocs.sh+7 purescript.cabal+9
docgen/Main.hs+17−8
@@ -14,17 +14,17 @@
1414
1515module Main where
1616
17−import qualified Language.PureScript as P
18−import System.Console.CmdTheLine
1917import Control.Applicative
2018import Control.Monad
2119import Control.Monad.Writer
22−import System.Exit (exitSuccess, exitFailure)
23−import qualified System.IO.UTF8 as U
24−import qualified Paths_purescript as Paths
25−import Data.Version (showVersion)
26−import Data.List
2720import Data.Function (on)
21+import Data.List
22+import Data.Version (showVersion)
23+import qualified Language.PureScript as P
24+import qualified Paths_purescript as Paths
25+import qualified System.IO.UTF8 as U
26+import System.Console.CmdTheLine
27+import System.Exit (exitSuccess, exitFailure)
2828
2929docgen :: FilePath -> IO ()
3030docgen input = do
@@ -61,6 +61,7 @@ renderModules ms = do
6161renderModule :: P.Module -> Docs
6262renderModule (P.Module moduleName ds exps) =
6363 let exported = filter (isExported exps) ds
64+ hasTypeclasses = any isTypeClassDeclaration ds
6465 in do
6566 headerLevel 2 $ "Module " ++ P.runModuleName moduleName
6667 spacer
@@ -70,6 +71,9 @@ renderModule (P.Module moduleName ds exps) =
7071 spacer
7172 headerLevel 3 "Type Classes"
7273 spacer
74+ when hasTypeclasses $ do
75+ renderTypeclassImage moduleName
76+ spacer
7377 renderTopLevel exps (filter isTypeClassDeclaration exported)
7478 spacer
7579 headerLevel 3 "Type Class Instances"
@@ -83,7 +87,7 @@ renderModule (P.Module moduleName ds exps) =
8387
8488isExported :: Maybe [P.DeclarationRef] -> P.Declaration -> Bool
8589isExported Nothing _ = True
86−isExported _ (P.TypeInstanceDeclaration _ _ _ _ _) = True
90+isExported _ P.TypeInstanceDeclaration{} = True
8791isExported exps (P.PositionedDeclaration _ d) = isExported exps d
8892isExported (Just exps) decl = any (matches decl) exps
8993 where
@@ -108,6 +112,11 @@ renderTopLevel exps decls = forM_ (sortBy (compare `on` getName) decls) $ \decl
108112 renderDeclaration 4 exps decl
109113 spacer
110114
115+renderTypeclassImage :: P.ModuleName -> Docs
116+renderTypeclassImage name =
117+ let name' = P.runModuleName name
118+ in tell ["![" ++ name' ++ "](images/" ++ name' ++ ".png)"]
119+
111120renderDeclaration :: Int -> Maybe [P.DeclarationRef] -> P.Declaration -> Docs
112121renderDeclaration n _ (P.TypeDeclaration ident ty) =
113122 atIndent n $ show ident ++ " :: " ++ P.prettyPrintType ty
docs/source/prelude.rst+97−19
@@ -17,16 +17,25 @@ Types
1717Type Classes
1818~~~~~~~~~~~~
1919
20+.. figure:: images/Prelude.png
21+ :alt: Prelude
22+
23+ Prelude
2024::
2125
2226 class Alternative f where
2327 empty :: forall a. f a
2428 (<|>) :: forall a. f a -> f a -> f a
2529
26− class Applicative f where
30+ class (Apply f) <= Applicative f where
2731 pure :: forall a. a -> f a
32+
33+ class (Functor f) <= Apply f where
2834 (<*>) :: forall a b. f (a -> b) -> f a -> f b
2935
36+ class (Apply m) <= Bind m where
37+ (>>=) :: forall a b. m a -> (a -> m b) -> m b
38+
3039 class Bits b where
3140 (&) :: b -> b -> b
3241 (|) :: b -> b -> b
@@ -41,9 +50,8 @@ Type Classes
4150 (||) :: b -> b -> b
4251 not :: b -> b
4352
44− class Category a where
53+ class (Semigroupoid a) <= Category a where
4554 id :: forall t. a t t
46− (<<<) :: forall b c d. a c d -> a b c -> a b d
4755
4856 class Eq a where
4957 (==) :: a -> a -> Prim.Boolean
@@ -52,9 +60,7 @@ Type Classes
5260 class Functor f where
5361 (<$>) :: forall a b. (a -> b) -> f a -> f b
5462
55− class Monad m where
56− return :: forall a. a -> m a
57− (>>=) :: forall a b. m a -> (a -> m b) -> m b
63+ class (Applicative m, Bind m) <= Monad m where
5864
5965 class Num a where
6066 (+) :: a -> a -> a
@@ -64,9 +70,15 @@ Type Classes
6470 (%) :: a -> a -> a
6571 negate :: a -> a
6672
67− class Ord a where
73+ class (Eq a) <= Ord a where
6874 compare :: a -> a -> Ordering
6975
76+ class Semigroup a where
77+ (<>) :: a -> a -> a
78+
79+ class Semigroupoid a where
80+ (<<<) :: forall b c d. a c d -> a b c -> a b d
81+
7082 class Show a where
7183 show :: a -> Prim.String
7284
@@ -75,8 +87,6 @@ Type Class Instances
7587
7688::
7789
78− instance applicativeFromMonad :: (Monad m) => Applicative m
79−
8090 instance bitsNumber :: Bits Prim.Number
8191
8292 instance boolLikeBoolean :: BoolLike Prim.Boolean
@@ -89,14 +99,20 @@ Type Class Instances
8999
90100 instance eqNumber :: Eq Prim.Number
91101
92− instance eqString :: Eq Prim.String
102+ instance eqOrdering :: Eq Ordering
93103
94− instance functorFromApplicative :: (Applicative f) => Functor f
104+ instance eqString :: Eq Prim.String
95105
96106 instance numNumber :: Num Prim.Number
97107
98108 instance ordNumber :: Ord Prim.Number
99109
110+ instance semigroupString :: Semigroup Prim.String
111+
112+ instance semigroupoidArr :: Semigroupoid Prim.Function
113+
114+ instance showArray :: (Show a) => Show [a]
115+
100116 instance showBoolean :: Show Prim.Boolean
101117
102118 instance showNumber :: Show Prim.Number
@@ -110,13 +126,13 @@ Values
110126
111127::
112128
113− (!!) :: forall a. [a] -> Prim.Number -> a
114−
115129 (#) :: forall a b. a -> (a -> b) -> b
116130
117131 ($) :: forall a b. (a -> b) -> a -> b
118132
119− (++) :: Prim.String -> Prim.String -> Prim.String
133+ (++) :: forall s. (Semigroup s) => s -> s -> s
134+
135+ (:) :: forall a. a -> [a] -> [a]
120136
121137 (<) :: forall a. (Ord a) => a -> a -> Prim.Boolean
122138
@@ -126,7 +142,11 @@ Values
126142
127143 (>=) :: forall a. (Ord a) => a -> a -> Prim.Boolean
128144
129− (>>>) :: forall a b c d. (Category a) => a b c -> a c d -> a b d
145+ (>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
146+
147+ ap :: forall m a b. (Monad m) => m (a -> b) -> m a -> m b
148+
149+ asTypeOf :: forall a. a -> a -> a
130150
131151 boolAnd :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
132152
@@ -134,10 +154,18 @@ Values
134154
135155 boolOr :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
136156
157+ concatString :: Prim.String -> Prim.String -> Prim.String
158+
159+ cons :: forall a. a -> [a] -> [a]
160+
137161 const :: forall a b. a -> b -> a
138162
139163 flip :: forall a b c. (a -> b -> c) -> b -> a -> c
140164
165+ liftA1 :: forall f a b. (Applicative f) => (a -> b) -> f a -> f b
166+
167+ liftM1 :: forall m a b. (Monad m) => (a -> b) -> m a -> m b
168+
141169 numAdd :: Prim.Number -> Prim.Number -> Prim.Number
142170
143171 numAnd :: Prim.Number -> Prim.Number -> Prim.Number
@@ -166,14 +194,37 @@ Values
166194
167195 numZshr :: Prim.Number -> Prim.Number -> Prim.Number
168196
169− on :: forall a b c. (b -> b -> c) -> (a -> b) -> a -> a -> c
170−
171197 refEq :: forall a. a -> a -> Prim.Boolean
172198
173199 refIneq :: forall a. a -> a -> Prim.Boolean
174200
201+ return :: forall m a. (Monad m) => a -> m a
202+
203+ showArrayImpl :: forall a. (a -> Prim.String) -> [a] -> Prim.String
204+
175205 showNumberImpl :: Prim.Number -> Prim.String
176206
207+ showStringImpl :: Prim.String -> Prim.String
208+
209+Module Data.Function
210+--------------------
211+
212+Types
213+~~~~~
214+
215+Type Classes
216+~~~~~~~~~~~~
217+
218+Type Class Instances
219+~~~~~~~~~~~~~~~~~~~~
220+
221+Values
222+~~~~~~
223+
224+::
225+
226+ on :: forall a b c. (b -> b -> c) -> (a -> b) -> a -> a -> c
227+
177228Module Data.Eq
178229--------------
179230
@@ -202,6 +253,25 @@ Values
202253
203254 liftRef :: forall a b. (a -> a -> b) -> Ref a -> Ref a -> b
204255
256+Module Prelude.Unsafe
257+---------------------
258+
259+Types
260+~~~~~
261+
262+Type Classes
263+~~~~~~~~~~~~
264+
265+Type Class Instances
266+~~~~~~~~~~~~~~~~~~~~
267+
268+Values
269+~~~~~~
270+
271+::
272+
273+ unsafeIndex :: forall a. [a] -> Prim.Number -> a
274+
205275Module Control.Monad.Eff
206276------------------------
207277
@@ -222,6 +292,14 @@ Type Class Instances
222292
223293::
224294
295+ instance applicativeEff :: Applicative (Eff e)
296+
297+ instance applyEff :: Apply (Eff e)
298+
299+ instance bindEff :: Bind (Eff e)
300+
301+ instance functorEff :: Functor (Eff e)
302+
225303 instance monadEff :: Monad (Eff e)
226304
227305Values
@@ -229,13 +307,13 @@ Values
229307
230308::
231309
232− bindEff :: forall e a b. Eff e a -> (a -> Eff e b) -> Eff e b
310+ bindE :: forall e a b. Eff e a -> (a -> Eff e b) -> Eff e b
233311
234312 forE :: forall e. Prim.Number -> Prim.Number -> (Prim.Number -> Eff e { }) -> Eff e { }
235313
236314 foreachE :: forall e a. [a] -> (a -> Eff e { }) -> Eff e { }
237315
238− retEff :: forall e a. a -> Eff e a
316+ returnE :: forall e a. a -> Eff e a
239317
240318 runPure :: forall a. Pure a -> a
241319
hierarchy/Main.hsnew file+103
@@ -0,0 +1,103 @@
1+-----------------------------------------------------------------------------
2+--
3+-- Module : Main
4+-- Copyright : (c) Hardy Jones 2014
5+-- License : MIT
6+--
7+-- Maintainer : Hardy Jones <jones3.hardy@gmail.com>
8+-- Stability : experimental
9+-- Portability :
10+--
11+-- |
12+-- Generate Directed Graphs of PureScript TypeClasses
13+--
14+-----------------------------------------------------------------------------
15+
16+module Main where
17+
18+import Control.Applicative ((<*>), (<$>))
19+import Control.Monad (unless)
20+
21+import Data.List (intercalate,nub,sort)
22+import Data.Foldable (for_)
23+import Data.Version (showVersion)
24+
25+import System.Console.CmdTheLine
26+import System.Directory (createDirectoryIfMissing)
27+import System.FilePath ((</>))
28+import System.Exit (exitFailure, exitSuccess)
29+
30+import Text.Parsec (ParseError)
31+
32+import qualified Language.PureScript as P
33+import qualified Paths_purescript as Paths
34+import qualified System.IO.UTF8 as U
35+
36+newtype SuperMap = SuperMap { unSuperMap :: Either P.ProperName (P.ProperName, P.ProperName) }
37+ deriving Eq
38+
39+instance Show SuperMap where
40+ show (SuperMap (Left sub)) = show sub
41+ show (SuperMap (Right (super, sub))) = show super ++ " -> " ++ show sub
42+
43+instance Ord SuperMap where
44+ compare (SuperMap s) (SuperMap s') = getCls s `compare` getCls s'
45+ where
46+ getCls = either id snd
47+
48+runModuleName :: P.ModuleName -> String
49+runModuleName (P.ModuleName pns) = intercalate "_" (P.runProperName `map` pns)
50+
51+readInput :: FilePath -> IO (Either ParseError [P.Module])
52+readInput p = do
53+ text <- U.readFile p
54+ return $ P.runIndentParser p P.parseModules text
55+
56+compile :: FilePath -> Maybe FilePath -> IO ()
57+compile input mOutput = do
58+ modules <- readInput input
59+ case modules of
60+ Left err -> U.print err >> exitFailure
61+ Right ms -> do
62+ for_ ms $ \(P.Module moduleName decls _) ->
63+ let name = runModuleName moduleName
64+ tcs = filter P.isTypeClassDeclaration decls
65+ supers = sort . nub . filter (not . null) $ fmap superClasses tcs
66+ prologue = "digraph " ++ name ++ " {\n"
67+ body = intercalate "\n" (concatMap (fmap (\s -> " " ++ show s ++ ";")) supers)
68+ epilogue = "\n}"
69+ hier = prologue ++ body ++ epilogue
70+ in unless (null supers) $ case mOutput of
71+ Just output -> do
72+ createDirectoryIfMissing True output
73+ U.writeFile (output </> name) hier
74+ Nothing -> U.putStrLn hier
75+ exitSuccess
76+
77+superClasses :: P.Declaration -> [SuperMap]
78+superClasses (P.TypeClassDeclaration sub _ supers@(_:_) _) =
79+ fmap (\(P.Qualified _ super, _) -> SuperMap (Right (super, sub))) supers
80+superClasses (P.TypeClassDeclaration sub _ _ _) = [SuperMap (Left sub)]
81+superClasses (P.PositionedDeclaration _ decl) = superClasses decl
82+superClasses _ = []
83+
84+outputFile :: Term (Maybe FilePath)
85+outputFile = value $ opt Nothing $ (optInfo [ "o", "output" ])
86+ { optDoc = "The output directory" }
87+
88+inputFile :: Term FilePath
89+inputFile = value $ pos 0 "main.purs" $ posInfo
90+ { posDoc = "The input file to generate a hierarchy from" }
91+
92+term :: Term (IO ())
93+term = compile <$> inputFile <*> outputFile
94+
95+termInfo :: TermInfo
96+termInfo = defTI
97+ { termName = "hierarchy"
98+ , version = showVersion Paths.version
99+ , termDoc = "Creates a GraphViz directed graph of PureScript TypeClasses"
100+ }
101+
102+main :: IO ()
103+main = run (term, termInfo)
prelude/README.md+89−19
@@ -11,14 +11,21 @@
1111
1212### Type Classes
1313
14+![Prelude](images/Prelude.png)
15+
1416 class Alternative f where
1517 empty :: forall a. f a
1618 (<|>) :: forall a. f a -> f a -> f a
1719
18− class Applicative f where
20+ class (Apply f) <= Applicative f where
1921 pure :: forall a. a -> f a
22+
23+ class (Functor f) <= Apply f where
2024 (<*>) :: forall a b. f (a -> b) -> f a -> f b
2125
26+ class (Apply m) <= Bind m where
27+ (>>=) :: forall a b. m a -> (a -> m b) -> m b
28+
2229 class Bits b where
2330 (&) :: b -> b -> b
2431 (|) :: b -> b -> b
@@ -33,9 +40,8 @@
3340 (||) :: b -> b -> b
3441 not :: b -> b
3542
36− class Category a where
43+ class (Semigroupoid a) <= Category a where
3744 id :: forall t. a t t
38− (<<<) :: forall b c d. a c d -> a b c -> a b d
3945
4046 class Eq a where
4147 (==) :: a -> a -> Prim.Boolean
@@ -44,9 +50,7 @@
4450 class Functor f where
4551 (<$>) :: forall a b. (a -> b) -> f a -> f b
4652
47− class Monad m where
48− return :: forall a. a -> m a
49− (>>=) :: forall a b. m a -> (a -> m b) -> m b
53+ class (Applicative m, Bind m) <= Monad m where
5054
5155 class Num a where
5256 (+) :: a -> a -> a
@@ -56,17 +60,21 @@
5660 (%) :: a -> a -> a
5761 negate :: a -> a
5862
59− class Ord a where
63+ class (Eq a) <= Ord a where
6064 compare :: a -> a -> Ordering
6165
66+ class Semigroup a where
67+ (<>) :: a -> a -> a
68+
69+ class Semigroupoid a where
70+ (<<<) :: forall b c d. a c d -> a b c -> a b d
71+
6272 class Show a where
6373 show :: a -> Prim.String
6474
6575
6676### Type Class Instances
6777
68− instance applicativeFromMonad :: (Monad m) => Applicative m
69−
7078 instance bitsNumber :: Bits Prim.Number
7179
7280 instance boolLikeBoolean :: BoolLike Prim.Boolean
@@ -79,14 +87,20 @@
7987
8088 instance eqNumber :: Eq Prim.Number
8189
82− instance eqString :: Eq Prim.String
90+ instance eqOrdering :: Eq Ordering
8391
84− instance functorFromApplicative :: (Applicative f) => Functor f
92+ instance eqString :: Eq Prim.String
8593
8694 instance numNumber :: Num Prim.Number
8795
8896 instance ordNumber :: Ord Prim.Number
8997
98+ instance semigroupString :: Semigroup Prim.String
99+
100+ instance semigroupoidArr :: Semigroupoid Prim.Function
101+
102+ instance showArray :: (Show a) => Show [a]
103+
90104 instance showBoolean :: Show Prim.Boolean
91105
92106 instance showNumber :: Show Prim.Number
@@ -98,13 +112,13 @@
98112
99113### Values
100114
101− (!!) :: forall a. [a] -> Prim.Number -> a
102−
103115 (#) :: forall a b. a -> (a -> b) -> b
104116
105117 ($) :: forall a b. (a -> b) -> a -> b
106118
107− (++) :: Prim.String -> Prim.String -> Prim.String
119+ (++) :: forall s. (Semigroup s) => s -> s -> s
120+
121+ (:) :: forall a. a -> [a] -> [a]
108122
109123 (<) :: forall a. (Ord a) => a -> a -> Prim.Boolean
110124
@@ -114,7 +128,11 @@
114128
115129 (>=) :: forall a. (Ord a) => a -> a -> Prim.Boolean
116130
117− (>>>) :: forall a b c d. (Category a) => a b c -> a c d -> a b d
131+ (>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
132+
133+ ap :: forall m a b. (Monad m) => m (a -> b) -> m a -> m b
134+
135+ asTypeOf :: forall a. a -> a -> a
118136
119137 boolAnd :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
120138
@@ -122,10 +140,18 @@
122140
123141 boolOr :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
124142
143+ concatString :: Prim.String -> Prim.String -> Prim.String
144+
145+ cons :: forall a. a -> [a] -> [a]
146+
125147 const :: forall a b. a -> b -> a
126148
127149 flip :: forall a b c. (a -> b -> c) -> b -> a -> c
128150
151+ liftA1 :: forall f a b. (Applicative f) => (a -> b) -> f a -> f b
152+
153+ liftM1 :: forall m a b. (Monad m) => (a -> b) -> m a -> m b
154+
129155 numAdd :: Prim.Number -> Prim.Number -> Prim.Number
130156
131157 numAnd :: Prim.Number -> Prim.Number -> Prim.Number
@@ -154,14 +180,34 @@
154180
155181 numZshr :: Prim.Number -> Prim.Number -> Prim.Number
156182
157− on :: forall a b c. (b -> b -> c) -> (a -> b) -> a -> a -> c
158−
159183 refEq :: forall a. a -> a -> Prim.Boolean
160184
161185 refIneq :: forall a. a -> a -> Prim.Boolean
162186
187+ return :: forall m a. (Monad m) => a -> m a
188+
189+ showArrayImpl :: forall a. (a -> Prim.String) -> [a] -> Prim.String
190+
163191 showNumberImpl :: Prim.Number -> Prim.String
164192
193+ showStringImpl :: Prim.String -> Prim.String
194+
195+
196+## Module Data.Function
197+
198+### Types
199+
200+
201+### Type Classes
202+
203+
204+### Type Class Instances
205+
206+
207+### Values
208+
209+ on :: forall a b c. (b -> b -> c) -> (a -> b) -> a -> a -> c
210+
165211
166212## Module Data.Eq
167213
@@ -184,6 +230,22 @@
184230 liftRef :: forall a b. (a -> a -> b) -> Ref a -> Ref a -> b
185231
186232
233+## Module Prelude.Unsafe
234+
235+### Types
236+
237+
238+### Type Classes
239+
240+
241+### Type Class Instances
242+
243+
244+### Values
245+
246+ unsafeIndex :: forall a. [a] -> Prim.Number -> a
247+
248+
187249## Module Control.Monad.Eff
188250
189251### Types
@@ -198,18 +260,26 @@
198260
199261### Type Class Instances
200262
263+ instance applicativeEff :: Applicative (Eff e)
264+
265+ instance applyEff :: Apply (Eff e)
266+
267+ instance bindEff :: Bind (Eff e)
268+
269+ instance functorEff :: Functor (Eff e)
270+
201271 instance monadEff :: Monad (Eff e)
202272
203273
204274### Values
205275
206− bindEff :: forall e a b. Eff e a -> (a -> Eff e b) -> Eff e b
276+ bindE :: forall e a b. Eff e a -> (a -> Eff e b) -> Eff e b
207277
208278 forE :: forall e. Prim.Number -> Prim.Number -> (Prim.Number -> Eff e { }) -> Eff e { }
209279
210280 foreachE :: forall e a. [a] -> (a -> Eff e { }) -> Eff e { }
211281
212− retEff :: forall e a. a -> Eff e a
282+ returnE :: forall e a. a -> Eff e a
213283
214284 runPure :: forall a. Pure a -> a
215285
prelude/images/Prelude.pngnew file

Binary file not shown.

prelude/updateDocs.sh+7
@@ -1,2 +1,9 @@
11../dist/build/docgen/docgen prelude.purs > README.md
2+../dist/build/hierarchy/hierarchy prelude.purs -o prelude
3+mkdir -p images
4+for f in prelude/*; do
5+ BASE=$(basename "$f")
6+ dot -Tpng -o images/"$BASE".png "$f"
7+done
8+rm -r prelude
29pandoc -o ../docs/source/prelude.rst README.md
purescript.cabal+9
@@ -125,6 +125,15 @@ executable docgen
125125 other-modules:
126126 ghc-options: -Wall -O2
127127
128+executable hierarchy
129+ build-depends: base >=4 && <5, cmdtheline -any, purescript -any, utf8-string -any,
130+ process -any, mtl -any, parsec -any, filepath -any, directory -any
131+ main-is: Main.hs
132+ buildable: True
133+ hs-source-dirs: hierarchy
134+ other-modules:
135+ ghc-options: -Wall -O2
136+
128137test-suite tests
129138 build-depends: base >=4 && <5, containers -any, directory -any,
130139 filepath -any, mtl -any, parsec -any, purescript -any, syb >= 0.4.1 && < 0.5,