General Information
Tutorials
Reference Manuals
Libraries
Translation Tasks
Tools
Administration
|
Tutorial on Type AnalysisFunction Types
We finally extend our language towards the orthogonal use of
functions, i.e. wherever a typed entity is allowed it can
have a function type. In particular, evaluation of an expression
may yield a function, which may be called, assigned to a variable,
passed as an argument, or returned as a function result.
For that purpose it is sufficient to add another
Here is an example program that defines a function type and a higher order function: FctTypeExamp[127]== begin fun mul (int x, real y) real begin return x * y; end; fun add (int x, real y) real begin return x + y; end; type (int, real -> real) fct; fun apply2 (real z, fct ff) real begin return ff (2, z); end; var real r; r = apply2 (3.1, add); r = apply2 (3.1, mul); end This macro is attached to a product file. The following productions are added to the grammer: Abstract function type syntax[128]== RULE: TypeDenoter ::= FunctionType END; RULE: FunctionType ::= '(' ParamTypes '->' TypeDenoter ')' END; RULE: ParamTypes LISTOF ParamType END; RULE: ParamType ::= TypeDenoter END; This macro is invoked in definition 133.
The specifications for Function type denotation[129]== RULE: TypeDenoter ::= FunctionType COMPUTE TypeDenoter.Type = FunctionType.Type; END; SYMBOL FunctionType INHERITS TypeDenotation, OperatorDefs END; RULE: FunctionType ::= '(' ParamTypes '->' TypeDenoter ')' COMPUTE FunctionType.GotOper += ListOperator ( FunctionType.Type, FunctionType.Type, ParamTypes.OpndTypeList, TypeDenoter.Type); .moreTypeProperies = ORDER (ResetTypeName (FunctionType.Type, "function..."), ResetTypeLine (FunctionType.Type, LINE)); END; This macro is invoked in definition 133.
The introduction of function types to our language allows programs
to use values which represent functions. They have a function type
which must fit to the type required in the context.
For example, the
In this case we have to specify structural equivalence of function
types, in oder to let the type rules allow such uses of functions.
If we would specify name equivalence instead, then for the above
example, the signature of the function declaration and the
type
Structural type equivalence is specified for denotations of
function types that either occur in a type denotation or
as the signature of a declared function.
We state that two types Function type equivalence[130]== RULE: FunctionHead ::= '(' Parameters ')' TypeDenoter COMPUTE FunctionHead.GotType += ORDER ( AddTypeToBlock ( FunctionHead.Type, FunctionClass, VResetComponentTypes (FunctionHead.Type, ConsDefTableKeyList (TypeDenoter.Type, Parameters.OpndTypeList))), ResetTypeName (FunctionHead.Type, "function..."), ResetTypeLine (FunctionHead.Type, LINE)); END; RULE: FunctionType ::= '(' ParamTypes '->' TypeDenoter ')' COMPUTE FunctionType.GotType += AddTypeToBlock (FunctionType.Type, FunctionClass, VResetComponentTypes (FunctionType.Type, ConsDefTableKeyList (TypeDenoter.Type, ParamTypes.OpndTypeList))) <- .moreTypeProperies; END; SYMBOL ParamTypes INHERITS OpndTypeListRoot END; SYMBOL ParamType INHERITS OpndTypeListElem END; RULE: ParamType ::= TypeDenoter COMPUTE ParamType.Type = TypeDenoter.Type; END; This macro is invoked in definition 133. Function class[131]== FunctionClass; This macro is invoked in definition 132.
We require for our language, that a function type FunctionType.pdl[132]== Function class[131] This macro is attached to a product file. FunctionType.lido[133]== Abstract function type syntax[128] Function type denotation[129] Function type equivalence[130] This macro is attached to a product file. FunctionType.con[134]== Function type syntax[142] This macro is attached to a product file.
|