This library provides selector functions, test and update operations as well as some useful auxiliary functions for FlatCurry data terms. Most of the provided functions are based on general transformation functions that replace constructors with user-defined functions. For recursive datatypes the transformations are defined inductively over the term structure. This is quite usual for transformations on FlatCurry terms, so the provided functions can be used to implement specific transformations without having to explicitly state the recursion. Essentially, the tedious part of such transformations - descend in fairly complex term structures - is abstracted away, which hopefully makes the code more clear and brief.
Author: Sebastian Fischer
Version: November 2020
trProg
:: (String -> [String] -> [TypeDecl] -> [FuncDecl] -> [OpDecl] -> a) -> Prog -> a transform program |
progName
:: Prog -> String get name from program |
progImports
:: Prog -> [String] get imports from program |
progTypes
:: Prog -> [TypeDecl] get type declarations from program |
progFuncs
:: Prog -> [FuncDecl] get functions from program |
progOps
:: Prog -> [OpDecl] get infix operators from program |
updProg
:: (String -> String) -> ([String] -> [String]) -> ([TypeDecl] -> [TypeDecl]) -> ([FuncDecl] -> [FuncDecl]) -> ([OpDecl] -> [OpDecl]) -> Prog -> Prog update program |
updProgName
:: (String -> String) -> Prog -> Prog update name of program |
updProgImports
:: ([String] -> [String]) -> Prog -> Prog update imports of program |
updProgTypes
:: ([TypeDecl] -> [TypeDecl]) -> Prog -> Prog update type declarations of program |
updProgFuncs
:: ([FuncDecl] -> [FuncDecl]) -> Prog -> Prog update functions of program |
updProgOps
:: ([OpDecl] -> [OpDecl]) -> Prog -> Prog update infix operators of program |
allVarsInProg
:: Prog -> [Int] get all program variables (also from patterns) |
updProgExps
:: (Expr -> Expr) -> Prog -> Prog lift transformation on expressions to program |
rnmAllVarsInProg
:: (Int -> Int) -> Prog -> Prog rename programs variables |
updQNamesInProg
:: ((String,String) -> (String,String)) -> Prog -> Prog update all qualified names in program |
rnmProg
:: String -> Prog -> Prog rename program (update name of and all qualified names in program) |
trType
:: ((String,String) -> Visibility -> [(Int,Kind)] -> [ConsDecl] -> a) -> ((String,String) -> Visibility -> [(Int,Kind)] -> TypeExpr -> a) -> ((String,String) -> Visibility -> [(Int,Kind)] -> NewConsDecl -> a) -> TypeDecl -> a transform type declaration |
typeName
:: TypeDecl -> (String,String) get name of type declaration |
typeVisibility
:: TypeDecl -> Visibility get visibility of type declaration |
typeParams
:: TypeDecl -> [(Int,Kind)] get type parameters of type declaration |
typeConsDecls
:: TypeDecl -> [ConsDecl] get constructor declarations from type declaration |
typeSyn
:: TypeDecl -> TypeExpr get synonym of type declaration |
isTypeData
:: TypeDecl -> Bool is type declaration a basic data type? |
isTypeSyn
:: TypeDecl -> Bool is type declaration a type synonym? |
isTypeNew
:: TypeDecl -> Bool is type declaration a newtype? |
updType
:: ((String,String) -> (String,String)) -> (Visibility -> Visibility) -> ([(Int,Kind)] -> [(Int,Kind)]) -> ([ConsDecl] -> [ConsDecl]) -> (NewConsDecl -> NewConsDecl) -> (TypeExpr -> TypeExpr) -> TypeDecl -> TypeDecl update type declaration |
updTypeName
:: ((String,String) -> (String,String)) -> TypeDecl -> TypeDecl update name of type declaration |
updTypeVisibility
:: (Visibility -> Visibility) -> TypeDecl -> TypeDecl update visibility of type declaration |
updTypeParams
:: ([(Int,Kind)] -> [(Int,Kind)]) -> TypeDecl -> TypeDecl update type parameters of type declaration |
updTypeConsDecls
:: ([ConsDecl] -> [ConsDecl]) -> TypeDecl -> TypeDecl update constructor declarations of type declaration |
updTypeNewConsDecl
:: (NewConsDecl -> NewConsDecl) -> TypeDecl -> TypeDecl update newtype constructor declaration of type declaration |
updTypeSynonym
:: (TypeExpr -> TypeExpr) -> TypeDecl -> TypeDecl update synonym of type declaration |
updQNamesInType
:: ((String,String) -> (String,String)) -> TypeDecl -> TypeDecl update all qualified names in type declaration |
trCons
:: ((String,String) -> Int -> Visibility -> [TypeExpr] -> a) -> ConsDecl -> a transform constructor declaration |
consName
:: ConsDecl -> (String,String) get name of constructor declaration |
consArity
:: ConsDecl -> Int get arity of constructor declaration |
consVisibility
:: ConsDecl -> Visibility get visibility of constructor declaration |
consArgs
:: ConsDecl -> [TypeExpr] get arguments of constructor declaration |
updCons
:: ((String,String) -> (String,String)) -> (Int -> Int) -> (Visibility -> Visibility) -> ([TypeExpr] -> [TypeExpr]) -> ConsDecl -> ConsDecl update constructor declaration |
updConsName
:: ((String,String) -> (String,String)) -> ConsDecl -> ConsDecl update name of constructor declaration |
updConsArity
:: (Int -> Int) -> ConsDecl -> ConsDecl update arity of constructor declaration |
updConsVisibility
:: (Visibility -> Visibility) -> ConsDecl -> ConsDecl update visibility of constructor declaration |
updConsArgs
:: ([TypeExpr] -> [TypeExpr]) -> ConsDecl -> ConsDecl update arguments of constructor declaration |
updQNamesInConsDecl
:: ((String,String) -> (String,String)) -> ConsDecl -> ConsDecl update all qualified names in constructor declaration |
trNewCons
:: ((String,String) -> Visibility -> TypeExpr -> a) -> NewConsDecl -> a transform newtype constructor declaration |
newConsArg
:: NewConsDecl -> TypeExpr |
newConsName
:: NewConsDecl -> (String,String) |
newConsVisibility
:: NewConsDecl -> Visibility |
updNewCons
:: ((String,String) -> (String,String)) -> (Visibility -> Visibility) -> (TypeExpr -> TypeExpr) -> NewConsDecl -> NewConsDecl update newtype constructor declaration |
updNewConsName
:: ((String,String) -> (String,String)) -> NewConsDecl -> NewConsDecl update name of newtype constructor declaration |
updNewConsVisibility
:: (Visibility -> Visibility) -> NewConsDecl -> NewConsDecl update visibility of newtype constructor declaration |
updNewConsArg
:: (TypeExpr -> TypeExpr) -> NewConsDecl -> NewConsDecl update argument of newtype constructor declaration |
updQNamesInNewConsDecl
:: ((String,String) -> (String,String)) -> NewConsDecl -> NewConsDecl |
tVarIndex
:: TypeExpr -> Int get index from type variable |
domain
:: TypeExpr -> TypeExpr get domain from functional type |
range
:: TypeExpr -> TypeExpr get range from functional type |
tConsName
:: TypeExpr -> (String,String) get name from constructed type |
tConsArgs
:: TypeExpr -> [TypeExpr] get arguments from constructed type |
trTypeExpr
:: (Int -> a) -> ((String,String) -> [a] -> a) -> (a -> a -> a) -> ([(Int,Kind)] -> a -> a) -> TypeExpr -> a transform type expression |
isTVar
:: TypeExpr -> Bool is type expression a type variable? |
isTCons
:: TypeExpr -> Bool is type declaration a constructed type? |
isFuncType
:: TypeExpr -> Bool is type declaration a functional type? |
isForallType
:: TypeExpr -> Bool is type declaration a forall type? |
updTVars
:: (Int -> TypeExpr) -> TypeExpr -> TypeExpr update all type variables |
updTCons
:: ((String,String) -> [TypeExpr] -> TypeExpr) -> TypeExpr -> TypeExpr update all type constructors |
updFuncTypes
:: (TypeExpr -> TypeExpr -> TypeExpr) -> TypeExpr -> TypeExpr update all functional types |
updForallTypes
:: ([(Int,Kind)] -> TypeExpr -> TypeExpr) -> TypeExpr -> TypeExpr update all forall types |
argTypes
:: TypeExpr -> [TypeExpr] get argument types from functional type |
resultType
:: TypeExpr -> TypeExpr get result type from (nested) functional type |
rnmAllVarsInTypeExpr
:: (Int -> Int) -> TypeExpr -> TypeExpr rename variables in type expression |
updQNamesInTypeExpr
:: ((String,String) -> (String,String)) -> TypeExpr -> TypeExpr update all qualified names in type expression |
trOp
:: ((String,String) -> Fixity -> Int -> a) -> OpDecl -> a transform operator declaration |
opName
:: OpDecl -> (String,String) get name from operator declaration |
opFixity
:: OpDecl -> Fixity get fixity of operator declaration |
opPrecedence
:: OpDecl -> Int get precedence of operator declaration |
updOp
:: ((String,String) -> (String,String)) -> (Fixity -> Fixity) -> (Int -> Int) -> OpDecl -> OpDecl update operator declaration |
updOpName
:: ((String,String) -> (String,String)) -> OpDecl -> OpDecl update name of operator declaration |
updOpFixity
:: (Fixity -> Fixity) -> OpDecl -> OpDecl update fixity of operator declaration |
updOpPrecedence
:: (Int -> Int) -> OpDecl -> OpDecl update precedence of operator declaration |
trFunc
:: ((String,String) -> Int -> Visibility -> TypeExpr -> Rule -> a) -> FuncDecl -> a transform function |
funcName
:: FuncDecl -> (String,String) get name of function |
funcArity
:: FuncDecl -> Int get arity of function |
funcVisibility
:: FuncDecl -> Visibility get visibility of function |
funcType
:: FuncDecl -> TypeExpr get type of function |
funcRule
:: FuncDecl -> Rule get rule of function |
updFunc
:: ((String,String) -> (String,String)) -> (Int -> Int) -> (Visibility -> Visibility) -> (TypeExpr -> TypeExpr) -> (Rule -> Rule) -> FuncDecl -> FuncDecl update function |
updFuncName
:: ((String,String) -> (String,String)) -> FuncDecl -> FuncDecl update name of function |
updFuncArity
:: (Int -> Int) -> FuncDecl -> FuncDecl update arity of function |
updFuncVisibility
:: (Visibility -> Visibility) -> FuncDecl -> FuncDecl update visibility of function |
updFuncType
:: (TypeExpr -> TypeExpr) -> FuncDecl -> FuncDecl update type of function |
updFuncRule
:: (Rule -> Rule) -> FuncDecl -> FuncDecl update rule of function |
isExternal
:: FuncDecl -> Bool is function externally defined? |
allVarsInFunc
:: FuncDecl -> [Int] get variable names in a function declaration |
funcArgs
:: FuncDecl -> [Int] get arguments of function, if not externally defined |
funcBody
:: FuncDecl -> Expr get body of function, if not externally defined |
funcRHS
:: FuncDecl -> [Expr] |
rnmAllVarsInFunc
:: (Int -> Int) -> FuncDecl -> FuncDecl rename all variables in function |
updQNamesInFunc
:: ((String,String) -> (String,String)) -> FuncDecl -> FuncDecl update all qualified names in function |
updFuncArgs
:: ([Int] -> [Int]) -> FuncDecl -> FuncDecl update arguments of function, if not externally defined |
updFuncBody
:: (Expr -> Expr) -> FuncDecl -> FuncDecl update body of function, if not externally defined |
trRule
:: ([Int] -> Expr -> a) -> (String -> a) -> Rule -> a transform rule |
ruleArgs
:: Rule -> [Int] get rules arguments if it's not external |
ruleBody
:: Rule -> Expr get rules body if it's not external |
ruleExtDecl
:: Rule -> String get rules external declaration |
isRuleExternal
:: Rule -> Bool is rule external? |
updRule
:: ([Int] -> [Int]) -> (Expr -> Expr) -> (String -> String) -> Rule -> Rule update rule |
updRuleArgs
:: ([Int] -> [Int]) -> Rule -> Rule update rules arguments |
updRuleBody
:: (Expr -> Expr) -> Rule -> Rule update rules body |
updRuleExtDecl
:: (String -> String) -> Rule -> Rule update rules external declaration |
allVarsInRule
:: Rule -> [Int] get variable names in a functions rule |
rnmAllVarsInRule
:: (Int -> Int) -> Rule -> Rule rename all variables in rule |
updQNamesInRule
:: ((String,String) -> (String,String)) -> Rule -> Rule update all qualified names in rule |
trCombType
:: a -> (Int -> a) -> a -> (Int -> a) -> CombType -> a transform combination type |
isCombTypeFuncCall
:: CombType -> Bool is type of combination FuncCall? |
isCombTypeFuncPartCall
:: CombType -> Bool is type of combination FuncPartCall? |
isCombTypeConsCall
:: CombType -> Bool is type of combination ConsCall? |
isCombTypeConsPartCall
:: CombType -> Bool is type of combination ConsPartCall? |
missingArgs
:: CombType -> Int |
varNr
:: Expr -> Int get internal number of variable |
literal
:: Expr -> Literal get literal if expression is literal expression |
combType
:: Expr -> CombType get combination type of a combined expression |
combName
:: Expr -> (String,String) get name of a combined expression |
combArgs
:: Expr -> [Expr] get arguments of a combined expression |
missingCombArgs
:: Expr -> Int get number of missing arguments if expression is combined |
letBinds
:: Expr -> [(Int,Expr)] get indices of variables in let declaration |
letBody
:: Expr -> Expr get body of let declaration |
freeVars
:: Expr -> [Int] get variable indices from declaration of free variables |
freeExpr
:: Expr -> Expr get expression from declaration of free variables |
orExps
:: Expr -> [Expr] get expressions from or-expression |
caseType
:: Expr -> CaseType get case-type of case expression |
caseExpr
:: Expr -> Expr get scrutinee of case expression |
caseBranches
:: Expr -> [BranchExpr] get branch expressions from case expression |
isVar
:: Expr -> Bool is expression a variable? |
isLit
:: Expr -> Bool is expression a literal expression? |
isComb
:: Expr -> Bool is expression combined? |
isLet
:: Expr -> Bool is expression a let expression? |
isFree
:: Expr -> Bool is expression a declaration of free variables? |
isOr
:: Expr -> Bool is expression an or-expression? |
isCase
:: Expr -> Bool is expression a case expression? |
trExpr
:: (Int -> a) -> (Literal -> a) -> (CombType -> (String,String) -> [a] -> a) -> ([(Int,a)] -> a -> a) -> ([Int] -> a -> a) -> (a -> a -> a) -> (CaseType -> a -> [b] -> a) -> (Pattern -> a -> b) -> (a -> TypeExpr -> a) -> Expr -> a transform expression |
updVars
:: (Int -> Expr) -> Expr -> Expr update all variables in given expression |
updLiterals
:: (Literal -> Expr) -> Expr -> Expr update all literals in given expression |
updCombs
:: (CombType -> (String,String) -> [Expr] -> Expr) -> Expr -> Expr update all combined expressions in given expression |
updLets
:: ([(Int,Expr)] -> Expr -> Expr) -> Expr -> Expr update all let expressions in given expression |
updFrees
:: ([Int] -> Expr -> Expr) -> Expr -> Expr update all free declarations in given expression |
updOrs
:: (Expr -> Expr -> Expr) -> Expr -> Expr update all or expressions in given expression |
updCases
:: (CaseType -> Expr -> [BranchExpr] -> Expr) -> Expr -> Expr update all case expressions in given expression |
updBranches
:: (Pattern -> Expr -> BranchExpr) -> Expr -> Expr update all case branches in given expression |
updTypeds
:: (Expr -> TypeExpr -> Expr) -> Expr -> Expr update all typed expressions in given expression |
isFuncCall
:: Expr -> Bool is expression a call of a function where all arguments are provided? |
isFuncPartCall
:: Expr -> Bool is expression a partial function call? |
isConsCall
:: Expr -> Bool is expression a call of a constructor? |
isConsPartCall
:: Expr -> Bool is expression a partial constructor call? |
isGround
:: Expr -> Bool is expression fully evaluated? |
allVars
:: Expr -> [Int] get all variables (also pattern variables) in expression |
rnmAllVars
:: (Int -> Int) -> Expr -> Expr rename all variables (also in patterns) in expression |
updQNames
:: ((String,String) -> (String,String)) -> Expr -> Expr update all qualified names in expression |
trBranch
:: (Pattern -> Expr -> a) -> BranchExpr -> a transform branch expression |
branchPattern
:: BranchExpr -> Pattern get pattern from branch expression |
branchExpr
:: BranchExpr -> Expr get expression from branch expression |
updBranch
:: (Pattern -> Pattern) -> (Expr -> Expr) -> BranchExpr -> BranchExpr update branch expression |
updBranchPattern
:: (Pattern -> Pattern) -> BranchExpr -> BranchExpr update pattern of branch expression |
updBranchExpr
:: (Expr -> Expr) -> BranchExpr -> BranchExpr update expression of branch expression |
trPattern
:: ((String,String) -> [Int] -> a) -> (Literal -> a) -> Pattern -> a transform pattern |
patCons
:: Pattern -> (String,String) get name from constructor pattern |
patArgs
:: Pattern -> [Int] get arguments from constructor pattern |
patLiteral
:: Pattern -> Literal get literal from literal pattern |
isConsPattern
:: Pattern -> Bool is pattern a constructor pattern? |
updPattern
:: ((String,String) -> (String,String)) -> ([Int] -> [Int]) -> (Literal -> Literal) -> Pattern -> Pattern update pattern |
updPatCons
:: ((String,String) -> (String,String)) -> Pattern -> Pattern update constructors name of pattern |
updPatArgs
:: ([Int] -> [Int]) -> Pattern -> Pattern update arguments of constructor pattern |
updPatLiteral
:: (Literal -> Literal) -> Pattern -> Pattern update literal of pattern |
patExpr
:: Pattern -> Expr build expression from pattern |
Type synonym: Update a b = (b -> b) -> a -> a
transform program |
get imports from program |
update program |
update name of program |
update imports of program |
update type declarations of program |
update functions of program |
update infix operators of program |
get all program variables (also from patterns) |
lift transformation on expressions to program |
rename programs variables |
update all qualified names in program |
rename program (update name of and all qualified names in program) |
transform type declaration |
get visibility of type declaration |
get type parameters of type declaration |
get constructor declarations from type declaration |
is type declaration a basic data type? |
update type declaration |
update name of type declaration |
update visibility of type declaration |
update type parameters of type declaration |
update constructor declarations of type declaration |
update newtype constructor declaration of type declaration |
update synonym of type declaration |
update all qualified names in type declaration |
transform constructor declaration |
get visibility of constructor declaration |
update constructor declaration |
update name of constructor declaration |
update arity of constructor declaration |
update visibility of constructor declaration |
update arguments of constructor declaration |
update all qualified names in constructor declaration |
transform newtype constructor declaration |
|
|
update newtype constructor declaration |
update name of newtype constructor declaration |
update visibility of newtype constructor declaration |
update argument of newtype constructor declaration |
|
transform type expression |
is type declaration a functional type? |
is type declaration a forall type? |
update all type constructors |
update all functional types |
update all forall types |
get argument types from functional type
|
get result type from (nested) functional type
|
rename variables in type expression |
update all qualified names in type expression |
get precedence of operator declaration |
update operator declaration |
update name of operator declaration |
update fixity of operator declaration |
update precedence of operator declaration |
transform function |
get visibility of function |
update function |
update name of function |
update arity of function |
update visibility of function |
update type of function |
update rule of function |
is function externally defined? |
get variable names in a function declaration |
rename all variables in function |
update all qualified names in function |
update arguments of function, if not externally defined |
update body of function, if not externally defined |
get rules external declaration |
is rule external? |
update rules arguments |
update rules body |
update rules external declaration |
get variable names in a functions rule |
rename all variables in rule |
update all qualified names in rule |
transform combination type |
is type of combination FuncCall? |
is type of combination FuncPartCall? |
is type of combination ConsCall? |
is type of combination ConsPartCall? |
|
get number of missing arguments if expression is combined |
get branch expressions from case expression |
transform expression |
update all literals in given expression |
update all combined expressions in given expression |
update all let expressions in given expression |
update all free declarations in given expression |
update all case expressions in given expression |
update all case branches in given expression |
update all typed expressions in given expression |
is expression a call of a function where all arguments are provided? |
is expression a partial function call? |
is expression a call of a constructor? |
is expression a partial constructor call? |
rename all variables (also in patterns) in expression |
update all qualified names in expression |
transform branch expression |
get pattern from branch expression |
get expression from branch expression |
update branch expression |
update pattern of branch expression |
update expression of branch expression |
get literal from literal pattern |
is pattern a constructor pattern? |
update pattern |
update constructors name of pattern |
update arguments of constructor pattern |
update literal of pattern |