Commit f9161736 authored by Nicolas Lenz's avatar Nicolas Lenz

Restructure project

parent 8d442b32
......@@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: a51038a57a549df7c527a6c89f16e4bb448017a0a2ee08be5d89074747639e34
-- hash: 8be90f2031d0309ad5effdfd1bccabf1108f825457ce34543279ee39e5ac9515
name: katrin
version: 0.0.0
......@@ -26,6 +26,8 @@ source-repository head
library
exposed-modules:
Katrin
Katrin.Algebra
Katrin.Core
other-modules:
Paths_katrin
hs-source-dirs:
......
......@@ -2,8 +2,10 @@
module Katrin where
import Prelude hiding (String) -- so I don't accidentally use String instead of Text
import Katrin.Core
import Katrin.Algebra
import Prelude hiding (String) -- so I don't accidentally use String instead of Text
import Data.Text (Text)
import qualified Data.Text as Text
......@@ -26,93 +28,6 @@ foldListExample :: Int
foldListExample = foldList (ListAlg 0 (+)) (Cons 1 $ Cons 2 $ Cons 42 Nil) -- = 1 + 2 + 42 = 45
-- | Main data type for a Katrin library consisting of a list of type definitions and a list of definitions.
data Katrin = Katrin {typeDefs :: [TypeDef], defs :: [Def]} deriving (Show, Read, Eq)
-- | Type for an identifier of a value, like an integer, a function, a type etc.
newtype ValueId = ValueId Text deriving (Show, Read, Eq)
-- | Type for an identifier of a constructor.
newtype ConstrId = ConstrId Text deriving (Show, Read, Eq)
-- | A type definition, consisting of an identifier for the type and the corresponding type expression.
data TypeDef = TypeDef {
typeId :: ValueId,
typeExp :: TypeExp
} deriving (Show, Read, Eq)
-- | A type expression, consisting of stuff.
-- TODO explain that. I'm too lazy right now.
data TypeExp = ProductType ConstrId [ValueId] | SumType TypeExp TypeExp
deriving (Show, Read, Eq)
data Def = Def {
expId :: ValueId,
expType :: Exp,
exp :: Exp
} deriving (Show, Read, Eq)
data Exp =
Literal ConstrId
| Application {function :: Exp, argument :: Exp}
| Variable {symbol :: ValueId}
| Lambda {inPattern :: Exp, outExp :: Exp}
deriving (Show, Read, Eq)
-- | Algebra / grammar for Katrin.
data KatrinAlg katrin valueId constrId typeDef def typeExp exp = KatrinAlg {
katrinAlg :: [typeDef] -> [def] -> katrin,
valueIdAlg :: Text -> valueId,
constrIdAlg :: Text -> constrId,
typeDefAlg :: valueId -> typeExp -> typeDef,
productTypeAlg :: constrId -> [valueId] -> typeExp,
sumTypeAlg :: typeExp -> typeExp -> typeExp,
defAlg :: valueId -> exp -> exp -> def,
literalAlg :: constrId -> exp,
applicationAlg :: exp -> exp -> exp,
variableAlg :: valueId -> exp,
lambdaAlg :: exp -> exp -> exp
}
-- Catamorphic fold functions for Katrin.
foldKatrin :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> Katrin -> katrin
foldKatrin alg (Katrin tds ds) = katrinAlg alg (foldTypeDef alg <$> tds) (foldDef alg <$> ds)
foldValueId :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> ValueId -> valueId
foldValueId alg (ValueId t) = valueIdAlg alg t
foldConstrId :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> ConstrId -> constrId
foldConstrId alg (ConstrId t) = constrIdAlg alg t
foldTypeDef :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> TypeDef -> typeDef
foldTypeDef alg (TypeDef vid te) = typeDefAlg alg (foldValueId alg vid) (foldTypeExp alg te)
foldTypeExp :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> TypeExp -> typeExp
foldTypeExp alg (ProductType cid vids) = productTypeAlg alg (foldConstrId alg cid) (foldValueId alg <$> vids)
foldTypeExp alg (SumType te1 te2) = sumTypeAlg alg (foldTypeExp alg te1) (foldTypeExp alg te2)
foldDef :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> Def -> def
foldDef alg (Def vid e1 e2) = defAlg alg (foldValueId alg vid) (foldExp alg e1) (foldExp alg e2)
foldExp :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> Exp -> exp
foldExp alg (Literal cid) = literalAlg alg (foldConstrId alg cid)
foldExp alg (Application e1 e2) = applicationAlg alg (foldExp alg e1) (foldExp alg e2)
foldExp alg (Variable vid) = variableAlg alg (foldValueId alg vid)
foldExp alg (Lambda e1 e2) = lambdaAlg alg (foldExp alg e1) (foldExp alg e2)
-- | Term algebra for Katrin. `foldKatrin katrinAlgTerm` folds a Katrin term into a, well, Katrin term.
-- Useful for compilation into a Katrin term and for testing.
katrinAlgTerm :: KatrinAlg Katrin ValueId ConstrId TypeDef Def TypeExp Exp
katrinAlgTerm = KatrinAlg {
katrinAlg = Katrin, valueIdAlg = ValueId, constrIdAlg = ConstrId,
typeDefAlg = TypeDef, productTypeAlg = ProductType, sumTypeAlg = SumType,
defAlg = Def, literalAlg = Literal, applicationAlg = Application,
variableAlg = Variable, lambdaAlg = Lambda
}
-- | An example type definition for natural numbers.
-- Corresponds to "data Nat = One | Succ Nat".
......
module Katrin.Algebra (KatrinAlg, foldKatrin, katrinAlgTerm) where
import Katrin.Core
import Data.Text (Text)
-- | Algebraic signature (or context-free grammar) for Katrin.
data KatrinAlg katrin valueId constrId typeDef def typeExp exp = KatrinAlg {
katrinAlg :: [typeDef] -> [def] -> katrin,
valueIdAlg :: Text -> valueId,
constrIdAlg :: Text -> constrId,
typeDefAlg :: valueId -> typeExp -> typeDef,
productTypeAlg :: constrId -> [valueId] -> typeExp,
sumTypeAlg :: typeExp -> typeExp -> typeExp,
defAlg :: valueId -> exp -> exp -> def,
literalAlg :: constrId -> exp,
applicationAlg :: exp -> exp -> exp,
variableAlg :: valueId -> exp,
lambdaAlg :: exp -> exp -> exp
}
---- CATAMORPHIC FOLD FUNCTIONS FOR KATRIN ----
foldKatrin :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> Katrin -> katrin
foldKatrin alg (Katrin tds ds) = katrinAlg alg (foldTypeDef alg <$> tds) (foldDef alg <$> ds)
foldValueId :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> ValueId -> valueId
foldValueId alg (ValueId t) = valueIdAlg alg t
foldConstrId :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> ConstrId -> constrId
foldConstrId alg (ConstrId t) = constrIdAlg alg t
foldTypeDef :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> TypeDef -> typeDef
foldTypeDef alg (TypeDef vid te) = typeDefAlg alg (foldValueId alg vid) (foldTypeExp alg te)
foldTypeExp :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> TypeExp -> typeExp
foldTypeExp alg (ProductType cid vids) = productTypeAlg alg (foldConstrId alg cid) (foldValueId alg <$> vids)
foldTypeExp alg (SumType te1 te2) = sumTypeAlg alg (foldTypeExp alg te1) (foldTypeExp alg te2)
foldDef :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> Def -> def
foldDef alg (Def vid e1 e2) = defAlg alg (foldValueId alg vid) (foldExp alg e1) (foldExp alg e2)
foldExp :: KatrinAlg katrin valueId constrId typeDef def typeExp exp -> Exp -> exp
foldExp alg (Literal cid) = literalAlg alg (foldConstrId alg cid)
foldExp alg (Application e1 e2) = applicationAlg alg (foldExp alg e1) (foldExp alg e2)
foldExp alg (Variable vid) = variableAlg alg (foldValueId alg vid)
foldExp alg (Lambda e1 e2) = lambdaAlg alg (foldExp alg e1) (foldExp alg e2)
---- ALGEBRAS FOR KATRIN ----
-- | Term algebra for Katrin. `foldKatrin katrinAlgTerm` folds a Katrin term into a, well, Katrin term.
-- Useful for compilation into a Katrin term and for testing.
katrinAlgTerm :: KatrinAlg Katrin ValueId ConstrId TypeDef Def TypeExp Exp
katrinAlgTerm = KatrinAlg {
katrinAlg = Katrin, valueIdAlg = ValueId, constrIdAlg = ConstrId,
typeDefAlg = TypeDef, productTypeAlg = ProductType, sumTypeAlg = SumType,
defAlg = Def, literalAlg = Literal, applicationAlg = Application,
variableAlg = Variable, lambdaAlg = Lambda
}
module Katrin.Core where
import Data.Text (Text)
-- | Main data type for a Katrin library consisting of a list of type definitions and a list of definitions.
data Katrin = Katrin {typeDefs :: [TypeDef], defs :: [Def]} deriving (Show, Read, Eq)
-- | Type for an identifier of a value, like an integer, a function, a type etc.
newtype ValueId = ValueId Text deriving (Show, Read, Eq)
-- | Type for an identifier of a constructor.
newtype ConstrId = ConstrId Text deriving (Show, Read, Eq)
-- | A type definition, consisting of an identifier for the type and the corresponding type expression.
data TypeDef = TypeDef {
typeId :: ValueId,
typeExp :: TypeExp
} deriving (Show, Read, Eq)
-- | A type expression, consisting of stuff.
-- TODO explain that. I'm too lazy right now.
data TypeExp = ProductType ConstrId [ValueId] | SumType TypeExp TypeExp
deriving (Show, Read, Eq)
data Def = Def {
expId :: ValueId,
expType :: Exp,
exp :: Exp
} deriving (Show, Read, Eq)
data Exp =
Literal ConstrId
| Application {function :: Exp, argument :: Exp}
| Variable {symbol :: ValueId}
| Lambda {inPattern :: Exp, outExp :: Exp}
deriving (Show, Read, Eq)
......@@ -3,7 +3,8 @@ module Main where
import Test.Tasty
import Test.Tasty.QuickCheck
import Test.QuickCheck.Instances
import Katrin
import Katrin.Core
import Katrin.Algebra
import Data.Text
main :: IO ()
......
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