on 8b7e4e4 help sign in
project-m36/src/lib/ProjectM36/AtomFunction.hs
34 lines 1.9 kB history raw
1module ProjectM36.AtomFunction where
2import ProjectM36.Base
3import ProjectM36.Error
4import qualified ProjectM36.Attribute as A
5import qualified Data.HashSet as HS
7foldAtomFuncType :: AtomType -> AtomType -> [AtomType]
8foldAtomFuncType foldType returnType = [RelationAtomType (A.attributesFromList [Attribute "_" foldType]), returnType]
10atomFunctionForName :: AtomFunctionName -> AtomFunctions -> Either RelationalError AtomFunction
11atomFunctionForName funcName funcSet = if HS.null foundFunc then
12 Left $ NoSuchTupleExprFunctionError funcName
13 else
14 Right $ head $ HS.toList foundFunc
15 where
16 foundFunc = HS.filter (\(AtomFunction name _ _) -> name == funcName) funcSet
18-- | Create a junk named atom function for use with searching for an already existing function in the AtomFunctions HashSet.
19emptyAtomFunction :: AtomFunctionName -> AtomFunction
20emptyAtomFunction name = AtomFunction { atomFuncName = name,
21 atomFuncType = [AnyAtomType, AnyAtomType],
22 atomFuncBody = AtomFunctionBody Nothing (\(x:_) -> x) }
25-- | AtomFunction constructor for compiled-in functions.
26compiledAtomFunction :: AtomFunctionName -> [AtomType] -> AtomFunctionBodyType -> AtomFunction
27compiledAtomFunction name aType body = AtomFunction { atomFuncName = name,
28 atomFuncType = aType,
29 atomFuncBody = AtomFunctionBody Nothing body }
31--the atom function really should offer some way to return an error
32evalAtomFunction :: AtomFunction -> [Atom] -> Atom
33evalAtomFunction func args = case atomFuncBody func of
34 (AtomFunctionBody _ f) -> f args