GitLab will be upgraded on June 2nd 2020 at 2.00 pm (AEDT) to 3.00 pm (AEDT) due to Critical Security Patch Availability. During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to local Gitlab admin team.

Commit f0cabf7d authored by nathyong's avatar nathyong

Add initial Kappa representation

parent 7eff9168
......@@ -20,6 +20,7 @@ library
, Compiler.Mu.FromSTG
, Compiler.Mu.Types
, Compiler.Mu.PprMu
, Compiler.Mu.Kappa
, Language.Haskell.GHC.Simple.Extra
build-depends: base >= 4.7 && < 5
, mtl
......
-- |
-- Module : Compiler.Mu.Kappa
-- Copyright : Nathan Yong 2016
--
-- Maintainer : nathan.yong@anu.edu.au
-- Stability : experimental
-- Portability : unknown
--
-- Kappa is a letter that comes before Mu, and is not Lambda.
--
-- Within the Kappa translation process, it is assumed that all names are
-- globally unique (!).
--
{-# LANGUAGE OverloadedStrings #-}
module Compiler.Mu.Kappa (
-- * Data types
Bundle (..)
, Top (..)
, Procedure (..)
, Expression (..)
, Const (..)
, Name (..)
-- * Classes
, HasType (..)
) where
import Data.ByteString (ByteString)
import Data.Map (Map)
import Data.Maybe (fromJust)
import qualified Data.Map as M
import DataCon (DataCon)
------------------------------------------------------------------- * Data types
-- | A collection of Kappa top-level declarations.
--
data Bundle = Bundle (Map Name Top)
-- | Corresponds to top-level STG Binding, or top-level Cmm Closure.
--
data Top
-- | A top-level closure that yields a value. Examples include functions,
-- function applications, and complex data types.
= ClosureTop
{ ktName :: Name
, ktType :: Name
, ktArity :: !Int
, ktMainProc :: Procedure
, ktOtherProcs :: [Procedure]
}
-- | A type constructor. May include references to other (GHC internals)
-- data constructors.
| TypeTop
{ ktName :: Name
, ktArity :: !Int
, ktDataCons :: [DataCon]
}
-- | 'Procedure's are different from functions and closures by having the
-- following guarantees:
--
-- * They are strict in evaluation (although it may push a lazy closure)
-- * They are not recursive (although it may push a reference by name to
-- itself)
--
data Procedure = Procedure
{ kpName :: Name
, kpArity :: !Int
, kpUpvalues :: [Name]
, kpBody :: Expression
}
-- | An 'Expression' is anything that can be reduced to a value.
--
data Expression
-- | Call a Haskell function, with arguments
= Call Name [Name]
-- | Branch to different values, depending on the result of an expression.
| Switch Expression [(Const, Expression)]
-- | Emit code to push a closure to a particular value.
| MkClosure Name Expression Expression
-- | A literal value
| Literal Const
-- | A primitive operation. Will translate into a Mu primitive operation.
| PrimOps Name Name
-- | Kappa constants
--
data Const
= KInt !Int
| KDouble !Double
| TopClosure Name
deriving Eq
-- | Kappa names can be either local or global.
--
data Name
= GlobalName ByteString
| LocalName ByteString
deriving (Eq, Ord)
---------------------------------------------------------------------- * Classes
-- | Things which have an observable type, perhaps within the context of
-- a Bundle (which you can lookup).
--
class HasType a where
-- | Obtain the name of something, perhaps using the context of a particular
-- bundle.
typeOf :: a -> Bundle -> Name
instance HasType Const where
typeOf (KInt _) _ = GlobalName "Mu.Int"
typeOf (KDouble _) _ = GlobalName "Mu.Double"
typeOf (TopClosure n) (Bundle m) = ktName $ fromJust (M.lookup n m)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment