packages feed

compilation 0.0.0.1 → 0.0.0.2

raw patch · 9 files changed

+397/−104 lines, 9 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Control.Compilation.Compile: Compile :: (State a -> (State a, b)) -> Compile a b
- Control.Compilation.Compile: State :: FreshIndex -> Indentation -> (Maybe ModuleName) -> NestingDepth -> a -> State a
- Control.Compilation.Compile: data Compile a b
- Control.Compilation.Compile: data State a
- Control.Compilation.Compile: depth :: Compile a Integer
- Control.Compilation.Compile: empState :: a -> State a
- Control.Compilation.Compile: extract :: Compile a () -> a -> a
- Control.Compilation.Compile: fresh :: Compile a String
- Control.Compilation.Compile: freshWithPrefix :: String -> Compile a String
- Control.Compilation.Compile: getModule :: Compile a (Maybe String)
- Control.Compilation.Compile: indent :: Compile String ()
- Control.Compilation.Compile: instance Monad (Compile a)
- Control.Compilation.Compile: nest :: Compile a ()
- Control.Compilation.Compile: newline :: Compile String ()
- Control.Compilation.Compile: newlines :: Int -> Compile String ()
- Control.Compilation.Compile: nothing :: Compile a ()
- Control.Compilation.Compile: raw :: String -> Compile String ()
- Control.Compilation.Compile: setModule :: String -> Compile a ()
- Control.Compilation.Compile: space :: Compile String ()
- Control.Compilation.Compile: spaces :: Int -> Compile String ()
- Control.Compilation.Compile: string :: String -> Compile String ()
- Control.Compilation.Compile: type FreshIndex = Integer
- Control.Compilation.Compile: type Indentation = String
- Control.Compilation.Compile: type ModuleName = String
- Control.Compilation.Compile: type NestingDepth = Integer
- Control.Compilation.Compile: unindent :: Compile String ()
- Control.Compilation.Compile: unnest :: Compile a ()
+ Control.Compilation: Compilation :: (State a -> (State a, b)) -> Compilation a b
+ Control.Compilation: Error :: String -> Compilation a b
+ Control.Compilation: State :: FreshIndex -> Indentation -> (Maybe ModuleName) -> NestingDepth -> a -> State a
+ Control.Compilation: class StateExtension a
+ Control.Compilation: data Compilation a b
+ Control.Compilation: data State a
+ Control.Compilation: error :: String -> Compilation a ()
+ Control.Compilation: extract :: StateExtension a => Compilation a b -> a
+ Control.Compilation: extractFromState :: StateExtension a => a -> Compilation a b -> a
+ Control.Compilation: get :: StateExtension a => Compilation a a
+ Control.Compilation: initial :: StateExtension a => a
+ Control.Compilation: instance StateExtension ()
+ Control.Compilation: instance StateExtension a => Monad (Compilation a)
+ Control.Compilation: nothing :: Compilation a ()
+ Control.Compilation: set :: StateExtension a => a -> Compilation a ()
+ Control.Compilation: type Compile a b = Compilation a b
+ Control.Compilation: type FreshIndex = Integer
+ Control.Compilation: type Indentation = String
+ Control.Compilation: type ModuleName = String
+ Control.Compilation: type NestingDepth = Integer
+ Control.Compilation.Environment: addEnv :: Environment a b => String -> b -> Compilation a ()
+ Control.Compilation.Environment: class StateExtension a => Environment a b where addEnv v x = do { s :: a <- get; env :: Env b <- return $ project s; set $ inject ((v, x) : env) s } popEnv = do { s :: a <- get; env :: Env b <- return $ project s; set $ inject (tail env) s } dropEnv n = do { s :: a <- get; env :: Env b <- return $ project s; set $ inject (drop n env) s } lookupEnv v = do { s :: a <- get; env :: Env b <- return $ project s; return $ lookup v env } setEnv env = do { s :: a <- get; set $ inject env s } getEnv = do { s :: a <- get; env :: Env b <- return $ project s; return $ env }
+ Control.Compilation.Environment: dropEnv :: Environment a b => Int -> Compilation a ()
+ Control.Compilation.Environment: getEnv :: Environment a b => Compilation a (Env b)
+ Control.Compilation.Environment: inject :: Environment a b => Env b -> a -> a
+ Control.Compilation.Environment: lookupEnv :: Environment a b => String -> Compilation a (Maybe b)
+ Control.Compilation.Environment: popEnv :: Environment a b => Compilation a ()
+ Control.Compilation.Environment: project :: Environment a b => a -> Env b
+ Control.Compilation.Environment: setEnv :: Environment a b => Env b -> Compilation a ()
+ Control.Compilation.Environment: type Env a = [(String, a)]
+ Control.Compilation.Fresh: class StateExtension a => Fresh a where freshInteger = do { s <- get; i <- return $ project s; set $ inject (i + 1) s; return $ i } freshString = do { i <- freshInteger; return $ show i } freshStringWithPrefix prefix = do { s <- freshString; return $ prefix ++ s } freshWithPrefix = freshStringWithPrefix fresh = freshString fresh_ = freshStringWithPrefix freshes i = do { ns <- mapM (\ _ -> fresh) [0 .. i - 1]; return $ [show n | n <- ns] } freshes_ prefix i = do { ns <- mapM (\ _ -> fresh) [0 .. i - 1]; return $ [prefix ++ show n | n <- ns] }
+ Control.Compilation.Fresh: fresh :: Fresh a => Compilation a String
+ Control.Compilation.Fresh: freshInteger :: Fresh a => Compilation a Integer
+ Control.Compilation.Fresh: freshString :: Fresh a => Compilation a String
+ Control.Compilation.Fresh: freshStringWithPrefix :: Fresh a => String -> Compilation a String
+ Control.Compilation.Fresh: freshWithPrefix :: Fresh a => String -> Compilation a String
+ Control.Compilation.Fresh: fresh_ :: Fresh a => String -> Compilation a String
+ Control.Compilation.Fresh: freshes :: Fresh a => Integer -> Compilation a [String]
+ Control.Compilation.Fresh: freshes_ :: Fresh a => String -> Integer -> Compilation a [String]
+ Control.Compilation.Fresh: inject :: Fresh a => Integer -> a -> a
+ Control.Compilation.Fresh: project :: Fresh a => a -> Integer
+ Control.Compilation.Module: class StateExtension a => Module a where setModule m = do { s <- get; set $ inject m s } getModule = do { s <- get; return $ project s }
+ Control.Compilation.Module: getModule :: Module a => Compilation a String
+ Control.Compilation.Module: inject :: Module a => String -> a -> a
+ Control.Compilation.Module: project :: Module a => a -> String
+ Control.Compilation.Module: setModule :: Module a => String -> Compilation a ()
+ Control.Compilation.Sequence: class StateExtension a => Sequence a b where nest xs = do { s :: a <- get; xss :: [[b]] <- return $ project s; set $ inject (xs : xss) s } unnest = do { s :: a <- get; xs :: [[b]] <- return $ project s; set $ inject (tail $ xs) s; return $ head $ project s } depth = do { s <- get; xss :: [[b]] <- return $ project s; return $ toInteger $ length xss }
+ Control.Compilation.Sequence: depth :: Sequence a b => Compilation a Integer
+ Control.Compilation.Sequence: inject :: Sequence a b => [[b]] -> a -> a
+ Control.Compilation.Sequence: nest :: Sequence a b => [b] -> Compilation a ()
+ Control.Compilation.Sequence: project :: Sequence a b => a -> [[b]]
+ Control.Compilation.Sequence: unnest :: Sequence a b => Compilation a [b]
+ Control.Compilation.String: indent :: Compilation String ()
+ Control.Compilation.String: newline :: Compilation String ()
+ Control.Compilation.String: newlines :: Int -> Compilation String ()
+ Control.Compilation.String: raw :: String -> Compilation String ()
+ Control.Compilation.String: space :: Compilation String ()
+ Control.Compilation.String: spaces :: Int -> Compilation String ()
+ Control.Compilation.String: string :: String -> Compilation String ()
+ Control.Compilation.String: unindent :: Compilation String ()

Files

+ Control/Compilation.hs view
@@ -0,0 +1,83 @@+----------------------------------------------------------------
+--
+-- | Compilation
+--   Monad and combinators for quickly assembling simple
+--   compilers.
+--
+-- @Control\/Compilation.hs@
+--
+--   A generic compilation monad for quickly assembling simple
+--   compilers.
+--
+
+----------------------------------------------------------------
+--
+
+module Control.Compilation
+  where
+
+----------------------------------------------------------------
+-- | Data types, class declarations, and class memberships.
+
+type FreshIndex = Integer
+type Indentation = String
+type ModuleName = String
+type NestingDepth = Integer
+
+data State a =
+  State
+    FreshIndex
+    Indentation
+    (Maybe ModuleName)
+    NestingDepth
+    a     -- State data structure.
+
+class StateExtension a where
+  initial :: a
+
+type Compile a b = Compilation a b
+data Compilation a b = 
+    Compilation (State a -> (State a, b))
+  | Error String
+
+-- | Standard state monad definition.
+instance StateExtension a => Monad (Compilation a) where
+  return x = Compilation (\s -> (s, x))
+  (>>=) fc1 fc2 = 
+    case fc1 of
+      Compilation c1 ->
+        Compilation $
+          (\state ->
+            let (state', r) = c1 state
+                Compilation c2 = fc2 r
+            in c2 state'
+          )
+      Error err -> Error err
+
+-- | Default memberships.
+
+instance StateExtension () where
+  initial = ()
+
+----------------------------------------------------------------
+-- | Generic combinators and functions.
+
+extract :: StateExtension a => Compilation a b -> a
+extract (Compilation c) = let (State _ _ _ _ r, _) = c (State 0 "" Nothing 0 initial) in r
+
+extractFromState :: StateExtension a => a -> Compilation a b -> a
+extractFromState s (Compilation c) = let (State _ _ _ _ r, _) = c (State 0 "" Nothing 0 s) in r
+
+nothing :: Compilation a ()
+nothing = Compilation $ \s -> (s, ())
+
+get :: StateExtension a => Compilation a a
+get = Compilation $ \(State f i m n s) -> (State f i m n s, s)
+
+set :: StateExtension a => a -> Compilation a ()
+set s = Compilation $ \(State f i m n _) -> (State f i m n s, ())
+
+error :: String -> Compilation a ()
+error err = Error err
+
+--eof
− Control/Compilation/Compile.hs
@@ -1,102 +0,0 @@-----------------------------------------------------------------
---
--- Compilation
--- Monads for quickly assembling simple compilers.
---
--- Control/Compilation/Compile.hs
---   A generic compilation monad for quickly assembling simple
---   compilers.
---
-
-----------------------------------------------------------------
--- Haskell implementation of a simple compilation monad.
-
-module Control.Compilation.Compile
-  where
-
-----------------------------------------------------------------
--- Data types and class memberships.
-
-type FreshIndex = Integer
-type Indentation = String
-type ModuleName = String
-type NestingDepth = Integer
-
-data State a = 
-  State FreshIndex Indentation (Maybe ModuleName) NestingDepth a
-
-empState :: a -> State a
-empState s = State 0 "" Nothing 0 s
-
-data Compile a b = 
-  Compile (State a -> (State a, b))
-
--- Standard state monad definition.
-instance Monad (Compile a) where
-  return x = Compile (\s -> (s, x))
-  (>>=) (Compile c1) fc2 = Compile $
-    (\state ->
-      let (state', r) = c1 state
-          Compile c2 = fc2 r
-      in c2 state'
-    )
-
-----------------------------------------------------------------
--- Generic combinators and functions.
-
-extract :: Compile a () -> a -> a
-extract (Compile c) o = let (State _ _ _ _ r, _) = c (empState o) in r
-
-nothing :: Compile a ()
-nothing = Compile $ \s -> (s, ())
-
-fresh :: Compile a String
-fresh = Compile $ \(State f i m n s) -> (State (f+1) i m n s, show f)
-
-freshWithPrefix :: String -> Compile a String
-freshWithPrefix p = Compile $ \(State f i m n s) -> (State (f+1) i m n s, p ++ show f)
-
-setModule :: String -> Compile a ()
-setModule m = Compile $ \(State f i _ n s) -> (State f i (Just m) n s, ())
-
-getModule :: Compile a (Maybe String)
-getModule = Compile $ \(State f i m n s) -> (State f i m n s, m)
-
-nest :: Compile a ()
-nest = Compile $ \(State f i m n s) -> (State f i m (n+1) s, ())
-
-unnest :: Compile a ()
-unnest = Compile $ \(State f i m n s) -> (State f i m (n-1) s, ())
-
-depth :: Compile a Integer
-depth = Compile $ \(State f i m n s) -> (State f i m n s, n)
-
-----------------------------------------------------------------
--- Combinators and functions for compiling directly into a raw
--- ASCII string.
-
-indent :: Compile String ()
-indent = Compile $ \(State f i m n s) -> (State f ("  " ++ i) m n s, ())
-
-unindent :: Compile String ()
-unindent = Compile $ \(State f i m n s) -> (State f (drop (min (length i) 2) i) m n s, ())
-
-space :: Compile String ()
-space = Compile $ \(State f i m n s) -> (State f i m n (s ++ " "), ())
-
-spaces :: Int -> Compile String ()
-spaces k = Compile $ \(State f i m n s) -> (State f i m n (s ++ (take k $ repeat ' ')), ())
-
-newline :: Compile String ()
-newline = Compile $ \(State f i m n s) -> (State f i m n (s ++ "\n" ++ i), ())
-
-newlines :: Int -> Compile String ()
-newlines k = Compile $ \(State f i m n s) -> (State f i m n (s ++ (take k $ repeat '\n') ++ i), ())
-
-string :: String -> Compile String ()
-string s' = Compile $ \(State f i m n s) -> (State f i m n (s ++ s'), ())
-
-raw :: String -> Compile String ()
-raw = string
-
---eof
+ Control/Compilation/Environment.hs view
@@ -0,0 +1,71 @@+----------------------------------------------------------------
+--
+-- | Compilation
+--   Monad and combinators for quickly assembling simple
+--   compilers.
+--
+-- @Control\/Compilation\/Environment.hs@
+--
+--   State extension class and combinators for implementations
+--   of a state that support an environment (i.e., lookup table
+--   or dictionary) data structure or structures.
+--
+
+----------------------------------------------------------------
+--
+
+{-# LANGUAGE MultiParamTypeClasses, ScopedTypeVariables #-}
+
+module Control.Compilation.Environment
+  where
+
+import Control.Compilation
+
+----------------------------------------------------------------
+-- | Type synonyms.
+
+type Env a = [(String, a)]
+
+----------------------------------------------------------------
+-- | State extension class definition.
+
+class StateExtension a => Environment a b where
+  project :: a -> Env b
+  inject :: Env b -> a -> a
+
+  addEnv :: String -> b -> Compilation a ()
+  addEnv v x =
+    do s :: a <- get
+       env :: Env b <- return $ project s
+       set $ inject ((v,x):env) s
+
+  popEnv :: Compilation a ()
+  popEnv =
+    do s :: a <- get
+       env :: Env b <- return $ project s
+       set $ inject (tail env) s
+
+  dropEnv :: Int -> Compilation a ()
+  dropEnv n =
+    do s :: a <- get
+       env :: Env b <- return $ project s
+       set $ inject (drop n env) s
+
+  lookupEnv :: String -> Compilation a (Maybe b)
+  lookupEnv v =
+    do s :: a <- get
+       env :: Env b <- return $ project s
+       return $ lookup v env
+
+  setEnv :: Env b -> Compilation a ()
+  setEnv env =
+    do s :: a <- get
+       set $ inject env s
+
+  getEnv :: Compilation a (Env b)
+  getEnv =
+    do s :: a <- get
+       env :: Env b <- return $ project s
+       return $ env
+
+--eof
+ Control/Compilation/Fresh.hs view
@@ -0,0 +1,66 @@+----------------------------------------------------------------
+--
+-- | Compilation
+--   Monad and combinators for quickly assembling simple
+--   compilers.
+--
+-- @Control\/Compilation\/Fresh.hs@
+--
+--   State extension class and combinators for implementations
+--   of a state that support generation of fresh (i.e., unique)
+--   values (integers and strings).
+--
+
+----------------------------------------------------------------
+--
+
+module Control.Compilation.Fresh
+  where
+
+import Control.Compilation
+
+----------------------------------------------------------------
+-- | State extension class definition, including combinators
+--   and convenient synonyms.
+
+class StateExtension a => Fresh a where
+  project :: a -> Integer
+  inject :: Integer -> a -> a
+
+  freshInteger :: Compilation a Integer
+  freshInteger =
+    do s <- get
+       i <- return $ project s
+       set $ inject (i+1) s
+       return $ i
+
+  freshString :: Compilation a String
+  freshString =
+    do i <- freshInteger
+       return $ show i
+
+  freshStringWithPrefix :: String -> Compilation a String
+  freshStringWithPrefix prefix =
+    do s <- freshString
+       return $ prefix ++ s
+
+  freshWithPrefix :: String -> Compilation a String
+  freshWithPrefix = freshStringWithPrefix
+
+  fresh :: Compilation a String
+  fresh = freshString
+
+  fresh_ :: String -> Compilation a String
+  fresh_ = freshStringWithPrefix
+
+  freshes :: Integer -> Compilation a [String]
+  freshes i =
+    do ns <- mapM (\_ -> fresh) [0..i-1]
+       return $ [show n | n <- ns]
+
+  freshes_ :: String -> Integer -> Compilation a [String]
+  freshes_ prefix i =
+    do ns <- mapM (\_ -> fresh) [0..i-1]
+       return $ [prefix ++ show n | n <- ns]
+
+--eof
+ Control/Compilation/Module.hs view
@@ -0,0 +1,38 @@+----------------------------------------------------------------
+--
+-- | Compilation
+--   Monad and combinators for quickly assembling simple
+--   compilers.
+--
+-- @Control\/Compilation\/Module.hs@
+--
+--   State extension class and combinators for implementations
+--   of a state that support module name specification.
+--
+
+----------------------------------------------------------------
+--
+
+module Control.Compilation.Module
+  where
+
+import Control.Compilation
+
+----------------------------------------------------------------
+-- | State extension class definition, including combinators.
+
+class StateExtension a => Module a where
+  project :: a -> String
+  inject :: String -> a -> a
+
+  setModule :: String -> Compilation a ()
+  setModule m =
+    do s <- get
+       set $ inject m s
+
+  getModule :: Compilation a String
+  getModule =
+    do s <- get
+       return $ project s
+
+--eof
+ Control/Compilation/Sequence.hs view
@@ -0,0 +1,53 @@+----------------------------------------------------------------
+--
+-- | Compilation
+--   Monad and combinators for quickly assembling simple
+--   compilers.
+--
+-- @Control\/Compilation\/Sequence.hs@
+--
+--   A generic compilation monad for quickly assembling simple
+--   compilers for target languages that are primarily
+--   sequences of instructions (possibly with nesting, e.g., 
+--   loop constructs or procedures).
+--
+
+----------------------------------------------------------------
+--
+
+{-# LANGUAGE MultiParamTypeClasses, ScopedTypeVariables #-}
+
+module Control.Compilation.Sequence
+  where
+
+import Control.Compilation
+
+----------------------------------------------------------------
+-- | State extension class definition, and combinators for
+--   compiling into a sequence (possibly with nested blocks)
+--   of instructions.
+
+class StateExtension a => Sequence a b where
+  project :: a -> [[b]]
+  inject :: [[b]] -> a -> a
+
+  nest :: [b] -> Compilation a ()
+  nest xs =
+    do s :: a <- get
+       xss :: [[b]] <- return $ project s
+       set $ inject (xs : xss) s
+
+  unnest :: Compilation a [b]
+  unnest =
+    do s :: a <- get
+       xs :: [[b]] <- return $ project s
+       set $ inject (tail $ xs) s
+       return $ head $ project s
+
+  depth :: Compilation a Integer
+  depth =
+    do s <- get
+       xss :: [[b]] <- return $ project s
+       return $ toInteger $ length xss
+
+--eof
+ Control/Compilation/String.hs view
@@ -0,0 +1,51 @@+----------------------------------------------------------------
+--
+-- | Compilation
+--   Monad and combinators for quickly assembling simple
+--   compilers.
+--
+-- @Control\/Compilation\/String.hs@
+--
+--   A generic compilation monad and combinators for quickly
+--   assembling simple compilers that emit an ASCII string
+--   representation of the target language (well-suited for
+--   direct syntax translators).
+--
+
+----------------------------------------------------------------
+--
+
+module Control.Compilation.String
+  where
+  
+import Control.Compilation
+
+----------------------------------------------------------------
+-- | Combinators and functions for compiling directly into a raw
+--   ASCII string.
+
+indent :: Compilation String ()
+indent = Compilation $ \(State f i m n s) -> (State f ("  " ++ i) m n s, ())
+
+unindent :: Compilation String ()
+unindent = Compilation $ \(State f i m n s) -> (State f (drop (min (length i) 2) i) m n s, ())
+
+space :: Compilation String ()
+space = Compilation $ \(State f i m n s) -> (State f i m n (s ++ " "), ())
+
+spaces :: Int -> Compilation String ()
+spaces k = Compilation $ \(State f i m n s) -> (State f i m n (s ++ (take k $ repeat ' ')), ())
+
+newline :: Compilation String ()
+newline = Compilation $ \(State f i m n s) -> (State f i m n (s ++ "\n" ++ i), ())
+
+newlines :: Int -> Compilation String ()
+newlines k = Compilation $ \(State f i m n s) -> (State f i m n (s ++ (take k $ repeat '\n') ++ i), ())
+
+string :: String -> Compilation String ()
+string s' = Compilation $ \(State f i m n s) -> (State f i m n (s ++ s'), ())
+
+raw :: String -> Compilation String ()
+raw = string
+
+--eof
+ Control/Compilation/Tree.hs view
@@ -0,0 +1,27 @@+----------------------------------------------------------------
+--
+-- | Compilation
+--   Monad and combinators for quickly assembling simple
+--   compilers.
+--
+-- @Control\/Compilation\/Tree.hs@
+--
+--   A generic compilation monad for quickly assembling simple
+--   compilers for target languages that are primarily
+--   expression trees.
+--
+
+----------------------------------------------------------------
+--
+
+module Control.Compilation.Tree
+  where
+
+import Control.Compilation
+
+----------------------------------------------------------------
+-- Combinator definitions.
+
+() = ()
+
+--eof
compilation.cabal view
@@ -1,5 +1,5 @@ Name:              compilation
-Version:           0.0.0.1
+Version:           0.0.0.2
 Cabal-Version:     >= 1.6
 License:           GPL-3
 License-File:      LICENSE
@@ -11,7 +11,13 @@ Build-Type:        Simple
 
 Library
-  Exposed-Modules: Control.Compilation.Compile
+  Exposed-Modules: Control.Compilation,
+                   Control.Compilation.Fresh,
+                   Control.Compilation.Module,
+                   Control.Compilation.Environment,
+                   Control.Compilation.String,
+                   Control.Compilation.Sequence,
+                   Control.Compilation.Tree
   Build-Depends:   base >= 3 && < 5, MissingH
 
 Source-Repository head