coreasm-1.0.5-usermanual
TRANSCRIPT
-
7/22/2019 CoreASM-1.0.5-UserManual
1/29
CoreASM Language User Manual
engine version 1.0.5
www.coreasm.org
Roozbeh Farahbod
Copyright c 2006-2008Your criticism is welcome.
1
-
7/22/2019 CoreASM-1.0.5-UserManual
2/29
CONTENTS CoreASM Language User Manual
Contents
1 CoreASM Specification 4
1.1 Running CoreASM Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Kernel 5
2.1 Rule Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 Basic ASM Plug-ins 7
3.1 Block Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Choose Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Conditional Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 Forall Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.5 Let Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.6 Predicate Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.7 Number Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 Standard Plug-ins 12
4.1 Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.2 Extend Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.3 TurboASM Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.4 String Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.5 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.6 Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.7 Set Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.8 List Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.9 Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.10 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.11 M ap Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.12 S ignature Plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5 Additional Plug-ins 25
5.1 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.2 Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2
-
7/22/2019 CoreASM-1.0.5-UserManual
3/29
CONTENTS CoreASM Language User Manual
5.3 Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.3.1 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.3.2 Basic Derived Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.3.3 Special Derived Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.3.4 An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3
-
7/22/2019 CoreASM-1.0.5-UserManual
4/29
1 COREASM SPECIFICATION CoreASM Language User Manual
SpecificationName
SamplePlugin
. . .
Header Block
InitRuleName
Rule Declaration Block
Figure 1: Structure of a CoreASM Specification
1 CoreASM Specification
Figure 1 shows the structure of a CoreASM specification. Every specification starts with thekeyword followed by the name of the specification. Plug-ins that are required in thespecification are then listed one by one with the keyword
followed by the name of the plug-in.
TheHeaderblock is where various definitions take place. What goes into this section dependson the plug-ins that are used. The CoreASM Kernel does not define anything for the headersection.
Theinit ruleof the specification (the rule that creates the initial state) is defined by keyword followed by a rule name. This would be the rule that initializes the state of the machinethat is defined by the specification. The body of the init rule must be declared in the RuleDeclarationblock.
A sample CoreASM specification is presented in Spec 1.1.
1.1 Running CoreASM Specifications
To run a CoreASM specification you need to have a CoreASM engine driver. Currently, there aretwo engine drivers available:
CoreASM Eclipse Plug-in is a plug-in for the Eclipse (see www.eclipse.org) developmentenvironment that provides syntax highlighting and a nice GUI to control specification runs.
Carmais a command-line CoreASMengine driver. To run a specification using CarmasimplyrunCarmaon the command line and pass it the name of the specification file as an argument.
4
-
7/22/2019 CoreASM-1.0.5-UserManual
5/29
2 KERNEL CoreASM Language User Manual
Make sure to specify a termination condition (e.g., --steps 20or --empty-updates) for
the run. RunCarma
with --help for a complete list of options that controls its behavior.The following command runs MySpec using Carma and stops after 30 steps, or after a stepthat generates empty updates; it also dumps the final state before termination.
carma --steps 30 --empty-updates --dump-final-state MySpec.coreasm
Alternatively, to run the specification of Spec 1.1, one can use the following options whichwould make Carma to mark the end of each step and stop after 30 steps or when there isno agent with a defined program:
carma --marksteps --steps 30 --no-agent ThisIsCoreASM.coreasm
In this example, Carma will stop after three steps.
ThisIsCoreASM
Standard
InitRule
InitRule =
MMM
MMMMMMterminate :=
MMMMMMprogram(self) := @MainProgram
MMM
MainProgram =
MMM
terminate
MMMMMM
MMMMMMMMM "This is CoreASM."
MMMMMMMMMterminate :=
MMMMMM
MMM
MMMMMMprogram(self) :=
CoreASM Spec 1.1: CoreASM-Says-Hello example
2 Kernel
Kernel of the CoreASM engine provides the minimum set of vocabulary and rules to have aCoreASMspecification.
Basic values such as , , and are defined in the kernel along with thebackground of Boolean values (BOOLEAN) and the universe ofAgents. A function calledprogram
5
-
7/22/2019 CoreASM-1.0.5-UserManual
6/29
2 KERNEL CoreASM Language User Manual
is also defined in the kernel which maps agents to their programs (CoreASMrules). At any time
during the evaluation of a rule,
refers the the agent that is running the enclosing rule.CoreASMkernel also defines a couple of important operators:
value1 = value2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kernel
This is the equality operator.
id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kernel
This operator returns the rule element of a rule with the given name ( id). 1 It is useful inassigning rules to programs of agents. In the following example, Main is the name of a rule:
program(self) := Main
The above rule, assigns the rule namedMainas the value of the program of the agent runningthis rule.
@ id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kernel
Returns the rule element (rule body) or function element of a rule or function with the givenname (id). If the given name is the name of a rule, it works exactly the same as .Thus, ifMainis rule, we can have:
program(self) := @Main
2.1 Rule Forms
The following rule forms are defined in the kernel:
loc:= value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kernel
Assigns the value ofvalueto the location loc.
id rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kernel
Imports a new element, assigns it as the value of the environment variable id, and evaluatesrule.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kernel
Does nothing. This is like a NoOp.
6
-
7/22/2019 CoreASM-1.0.5-UserManual
7/29
3 BASIC ASM PLUG-INS CoreASM Language User Manual
3 Basic ASM Plug-ins
In this section we list the plug-ins that provide the basic ASM rule forms. All the plug-ins inthis section can be loaded individually (as instructed in each section) or all together with thefollowing
phrase,
BasicASMPlugins
which automatically loads the following plug-ins: BlockRule, ConditionalRule, ChooseRule,ForallRule, LetRule, and Number.
Note that the words Plugin and Plugins in the name of the plug-ins are optional. Forexample, Basic ASM plug-ins can also be loaded using the following line:
BasicASM
3.1 Block Rule
The Block Rule plug-in can be loaded by the following
phrase:
BlockRulePlugin
This plug-in provides the following rule form:
rule1 rule2 . . . rulenoptional
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Block Rule Plugin
Instructs the engine to evaluate all the given rules in parallel. The update generated by thisrule is the union of all the updates generated by rule1 to rulen.
3.2 Choose Rule
The Choose Rule plug-in can be loaded by the following
phrase:
ChooseRulePlugin
This plug-in provides the following rule form:
id value guardoptional
rule1 rule2optional
optional
. . . . . . . . Choose Rule Plugin
7
-
7/22/2019 CoreASM-1.0.5-UserManual
8/29
3 BASIC ASM PLUG-INS CoreASM Language User Manual
Chooses an element from the enumerable1 valuewhich satisfies guard, assigns it as the value
ofidand evaluates rule1
. If the
clause is provided,rule2
will be evaluated if no elementcan be found. The last keyword is optional.
The following example chooses the minimum price p from the set of prices and prints (seeSection 4.5) p on the screen:
p
prices
(
pi
prices
p
-
7/22/2019 CoreASM-1.0.5-UserManual
9/29
3 BASIC ASM PLUG-INS CoreASM Language User Manual
ForallRulePlugin
This plug-in provides the following rule form:
id value guardoptional
rule optional
. . . . . . . . . . . . . . . . . . . . . . . . Forall Rule Plugin
For all the elements in the enumerable valuethat satisfy guard, assigns the element to id, andevaluates rule. The following examples assigns the DefaultProgram rule as the program of allthe agents program of which is :
a
Agents
program(a) =
MMMprogram(a) := DefaultProgram
3.5 Let Rule
The Let Rule plug-in can be loaded by the following
phrase:
LetRulePlugin
This plug-in provides the following rule form:
id1 = value1 , id2 = value2 , . . . , idn = valuenoptional
rule . . . . . . . . . . . . . . . . . . . . . Let Rule Plugin
For all the given pairs ofidand value, assigns valuei as the value of the environment variableidi, and evaluates rule.
3.6 Predicate Logic
The Predicate Logic plug-in can be loaded by the following
phrase:
PredicateLogicPlugin
This plug-in provides the following functions and expression forms:
id value guard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Predicate Logic Plugin
This Boolean expression holds ifguardholds for all the elements ofvalue(which must be an
9
-
7/22/2019 CoreASM-1.0.5-UserManual
10/29
3 BASIC ASM PLUG-INS CoreASM Language User Manual
enumerable value).
id
value guard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Predicate Logic Plugin
This Boolean expression holds if there exists at least one element in value(which must be anenumerable value) that satisfies guard.
value1 != value2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Predicate Logic Plugin
This is the not-equal operator which is defined on all elements. The semantics of this operatoris equivalent to
(value1 = value2).
value1 bin-op value2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Predicate Logic Plugin
Performs a binary operation on the given values. The following operators are defined onBoolean values:
or, xor, and, implies
The following two operators are also defined which require value2 to be an enumerable:
memberofand notmemberof
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Predicate Logic Plugin
This is the negation operator which is defined on Boolean values.
3.7 Number Background
The Number plug-in can be loaded by the following
phrase:
NumberPlugin
This plug-in provides the number background (NUMBER) and a valuable set of functions andexpression forms.
value1 bin-op value2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Number Plugin
Performs binary operations on number values. Currently supported operators are
+ - * / d i v %
> > = < < =
10
-
7/22/2019 CoreASM-1.0.5-UserManual
11/29
3 BASIC ASM PLUG-INS CoreASM Language User Manual
which result in Number or Boolean values.
| value| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number PluginIfvalueis enumerable (such as a set), this operator will evaluate to the size ofvalue.
infinity: Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number PluginIs the positive infinity.
toNumber: Element Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number PluginThis is a conversion function that maps any value to a Number value (which can also be
). The following example uses this function to read a number from the environment:
MMMamount := input("Input Amount")
MMM
value = toNumber(amount)
MMMMMM
value =
MMMMMMMMM "Error."
MMMMMM
MMMMMMMMMDepositAmount(value)
isNaturalNumber:
Number
Boolean
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number Plugin
Returnstrueif the argument is a Natural number (i.e., positive non-zero integer).
isIntegerNumber: Number Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number PluginReturnstrueif the argument is an Integer number.
isRealNumber: Number Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number PluginReturnstrueif the argument is a valid non-infinite Real number.
isEvenNumber: Number
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number Plugin
Returnstrueif the argument is an Integer number divisible by two.
isOddNumber: Number Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number PluginReturnstrueif the argument is an Integer number which is not divisible by two.
11
-
7/22/2019 CoreASM-1.0.5-UserManual
12/29
4 STANDARD PLUG-INS CoreASM Language User Manual
The Number plug-in also provides a background for number ranges (NUMBER RANGE). Number
range elements are enumerable and can be defined using the following syntax.
[ valuestart .. valueend valuestepoptional
] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Number Plugin
Creates a range of numbers from valuestart to valueend with the optional step. It is alsopossible to use : instead of . In the following example, RandomGuess returns a randomnumber between 1 and 100:
RandomGuess =
MMM
rand
MMMMMM
x
[ 1 .. 100 ]
MMMMMMMMMrand := x
4 Standard Plug-ins
Most of the CoreASMplug-ins, including all the Basic ASM plug-ins, are included in the Standardplug-ins package. In this section we list the plug-ins that are provided by the Standard plug-inspackage in addition to the ones listed in the previous section. All these plug-ins can be loadedindividually (as instructed in each section) or all together with the following
phrase,
StandardPlugins
which automatically loads all the plug-ins listed in Section 3 in addition to the ones listed in thissection.
4.1 Abstraction
The Abstraction plug-in can be loaded by the following
phrase:
AbstractionPlugin
This plug-in provides the following rule form, which is useful when the specifier wants to leavethe detail of a rule abstract.
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Abstraction Plugin
In the following example, the rule SendMessage is left abstract:
12
-
7/22/2019 CoreASM-1.0.5-UserManual
13/29
4 STANDARD PLUG-INS CoreASM Language User Manual
SendMessage =
MMM
"Sending the message."
4.2 Extend Rule
The Extend Rule plug-in can be loaded by the following phrase:
ExtendRulePlugin
This plug-in provides the following rule form:
value id rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extend Rule Plugin
This rule has two semantics depending on value:
1. Ifvalue is a universe, it imports a new element, assigns it to id, and evaluates rule. Theresulting update set is the union of the updates generated by rule and a single update toadd the imported element to the universe value.
2. Ifvalueis a background, it gets the default element from the background, assigns it to idand evaluates rule. The resulting update set is the updates generated by rule.
In the following example, the universe Agentsis extended with a new agent and the program
of that agent is set to MainProgram:
Agents a
MMMprogram(a) := @MainProgram
However, the same result can be achieved by:
a
MMM
MMMMMMAgents(a) :=
MMMMMMprogram(a) := @MainProgram
MMM
4.3 TurboASM Rules
The TurboASM plug-in can be loaded by the following
phrase:
13
-
7/22/2019 CoreASM-1.0.5-UserManual
14/29
4 STANDARD PLUG-INS CoreASM Language User Manual
TurboASMPlugin
This plug-in provides the following rule forms:
rule1 optional
rule2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . TurboASM Plugin
Evaluates rule1, applies the generated updates in a virtual state, and evaluates rule2 in thatstate. The resulting update set is a sequential composition of the updates generated by rule1 andrule2. The optional keyword is meant to improve readability specially where the sequencerule is combined with other rule forms.
rule1 rule2 . . . rulenoptional
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . TurboASM Plugin
Similar to the rule (above), this rule form executes the listed rules in sequence. Itevaluates rule1, applies the generated updates in a virtual state, evaluates rule2 in that stateand applies the generated updates in a virtual state, and so on. The resulting update set is asequential composition of the updates generated by rule1 . . . rulen.
rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .TurboASM Plugin
Repeatedly evaluates rule, until the update set produced is either empty or inconsistent; atthat point, the accumulated updates are computed (the resulting update set can be inconsistentif the computation of the last step had produced an inconsistent set of updates).
(value) rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . TurboASM Plugin
This rule is equivalent to:
MMM
value
rule
loc
-
7/22/2019 CoreASM-1.0.5-UserManual
15/29
4 STANDARD PLUG-INS CoreASM Language User Manual
Divide(a, b) =
MMM
b > 0
MMMMMM
:= a / b
MMM
MMMMMM
MMMMMMMMM
:=
MMMMMMMMMerror :=
// also updates the global state
MMMMMM
MainProgram =
MMMdivision 0
MMMMMMMMM := a / b
MMMMMM
MMMMMMMMM
MMMMMMMMMMMM
:=
MMMMMMMMMMMMerror :=
// has no effect
MMMMMMMMM
MainProgram =
MMMdivision := Divide(5, 2)
Note that this time, Divide cannot change the global state as all the updates are discardedafter valueis evaluated.
id1 , id2, . . . , idnoptional
rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .TurboASM Plugin
Evaluates rulebut discards all the updates to locations addressed by id-s (as location names).In the following example, newValuewill get the local value offoo(5, 7)(i.e.,25) but the updateto foo(5, 7)will be discarded afterwards.
15
-
7/22/2019 CoreASM-1.0.5-UserManual
16/29
4 STANDARD PLUG-INS CoreASM Language User Manual
LocalRule =
MMM
foo
MMMMMM
MMMMMMMMMfoo(5, 7) := 25
MMMMMMMMMnewValue := foo(5, 7)
4.4 String Background
The String plug-in can be loaded by the following phrase:
StringPlugin
This plug-in provides the string background (STRING) and a small set of functions and expressionforms.
value1 + value2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . String Plugin
If both values are string, this operator concatenates the given string values in to one. If oneof the values is not a string value, it tries to convert it into a string value, and then concatenatesthe values. This operator is not defined on two non-string values.
With this operator, one can simply put values together to create a customized message:
"The amount of $" + amount + " is deposited to your account."
toString: Element String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . String PluginA conversion function that maps any value to a String value (which can also be ).
strlen: String Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . String PluginReturns the length of the given String value.
4.5 Input and Output
The IO plug-in can be loaded by the following
phrase:
IOPlugin
16
-
7/22/2019 CoreASM-1.0.5-UserManual
17/29
4 STANDARD PLUG-INS CoreASM Language User Manual
This plug-in provides the following rule form and function:
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . IO Plugin
Prints outvalueto the environment. Depending on the environment (engine driver) this valuecan be printed on the standard output.
input: String String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . IO PluginReads a string value from the environment. 1 will result in the same value. Please refer to
Section 4.4 for an introduction to the String Plugin.
The machine specified in Spec 4.1 is an extension of our This-Is-CoreASM example (seeSpec 1.1) that reads a name from the environment and prints out a greeting to that name:
CoreASMSaysHello
StandardPlugins
InitRule
InitRule =
MMM
MMMMMMterminate :=
MMMMMMprogram(self) := @MainProgram
MMMMMMname := input("What is your name?")
MMM
MainProgram =
MMM
terminate
MMMMMM
MMMMMMMMM "This is CoreASM."
MMMMMMMMMterminate :=
MMMMMMMMM "Hello " + name + "!"
MMMMMM
CoreASM Spec 4.1: CoreASM-Says-Hello example
4.6 Collection
The Collection plug-in can be loaded by the following
phrase:
CollectionPlugin
This plug-in provides the foundation for collections (i.e., sets, lists, maps, etc.) in CoreASM
17
-
7/22/2019 CoreASM-1.0.5-UserManual
18/29
4 STANDARD PLUG-INS CoreASM Language User Manual
and provides some general functions on collections. However, each specific collection background
(e.g., list or set) is provided by its corresponding plug-in.
foldl: Collection Function Element Element . . . . . . . . . . . . . . . . . . . . . .Collection Pluginfoldl(c, @func, init)processes the collection c (a set or a list) using the binary function
funcand the initial value init and returns the final result.
foldl([x1, . . . , xn], f , i) f(xn, f(xn1, . . . f (x1,init))) . . .)
foldr: Collection Function Element Element . . . . . . . . . . . . . . . . . . . . . .Collection Pluginfoldr(c, @func, init)processes the collection c (a set or a list) using the binary function
funcand the initial value init and returns the final result.
foldr([x1, . . . , xn], f , i) f(x1, f(x2, . . . f (xn,init))) . . .)
fold: Collection Function Element Element . . . . . . . . . . . . . . . . . . . . . . . Collection PluginThis is the same as foldr; see above.
map: Collection Function Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Collection Pluginmap(c, @func)applies the unary function functo all the elements ofc (any collection, such
as list and set) and returns a new collection (with the same structure as that ofc).
map([x1, . . . , xn], f) [f(x1), f(x2), . . . f (xn)]
filter: Collection Function Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collection Pluginfilter(c, @func) applies the boolean unary function func to all the elements of c and
returns a new collection with only those elements ofc for which funcreturns true.
size: Collection Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collection PluginReturns the size of the given collection.
4.7 Set Background
The Set plug-in can be loaded by the following
phrase:
SetPlugin
18
-
7/22/2019 CoreASM-1.0.5-UserManual
19/29
4 STANDARD PLUG-INS CoreASM Language User Manual
This plug-in provides the set background (SET) and a a number of functions and expression forms.
{ value1, . . . , valuenoptional
} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Set Plugin
Creates a set element that includes the listed values. The values should be basic terms (i.e.,no operators) or they should be surrounded in parentheses.
{ id| id value guardoptional
} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Set Plugin
This is the basic form set comprehension. It creates a set of all the elements in valuewhichsatisfy guard. Of course, valuemust be enumerable.
{ id exp| id1 value1 , . . . , idn valuenoptional
guard} . . . . . . . . . . . . . . . . . . . . . . . . Set Plugin
Creates a set element that contains all the elements of the form expwhich satisfy the guard.In this form, exp is a function of id1 . . . idn and every idi is bound to an enumerable valuei.
In the following example, SetAddtakes two setsset1and set2as input and produces a newset by adding every element ofset1 to all the elements ofset2:
SetAdd(set1, set2) =
MMM a
MMMMMMa := { x
(x1 + x2) | x1
set1, x2
set2 }
The result of evaluating SetAdd({1, 2, 3},{10, 20}) would be:
{22.0, 23.0, 12.0, 21.0, 13.0, 11.0}
value1 bin-op value2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Set Plugin
Performs a set binary operation where both value1 and value2 are sets. Currently, subset,union, intersect, and diff are supported.
Set background also provides two important rule forms which allow for parallel incrementalupdates of set data structures.
value loc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Set Plugin
If loc is a location in the state (e.g., a function) and its value is a set, this rule produces anupdate instruction (partial update) that adds valueto loc.
value loc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Set Plugin
19
-
7/22/2019 CoreASM-1.0.5-UserManual
20/29
4 STANDARD PLUG-INS CoreASM Language User Manual
If loc is a location in the state (e.g., a function) and its value is a set, this rule produces an
update instruction (partial update) that removesvalueto loc.
4.8 List Background
The List plug-in can be loaded by the following
phrase:
ListPlugin
This plug-in provides a list background (LIST) and a rich set of functions and operators on lists.
[ value1, value2, . . . , valuenoptional
] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .List Plugin
Creates a list element that includes value1 to valuen in the given order.2 List elements are
enumerable. The index of the first element is 1.
value1 + value2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List Plugin
If both values are list, this operator concatenates the given lists in to one list.
head: List Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List PluginReturns the first element of the list.
last: List Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List PluginReturns the last element of the list.
tail: List List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List PluginReturns all but the first element of the list.
cons: Element List List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List PluginCreates a new list with the given element as its head and given list as its tail.
nth: List Number Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .List Plugin2The old form of still works but it is deprecated and may not be supported in future releases
of the CoreASM engine.
20
-
7/22/2019 CoreASM-1.0.5-UserManual
21/29
4 STANDARD PLUG-INS CoreASM Language User Manual
Returns the nth element of the list. The index of the first element is 1.
take: List Number List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List Plugintake(list, i)returns the first i elements of list list.
drop: List Number List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List Plugindrop(list, i)returns what is left after dropping the first i elements of the list list.
reverse: List List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List PluginReturns a list consisting of the given lists elements in reverse order.
indexes: List Element List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .List PluginReturns a potentially empty list of the indexes of the given element in given list.
indices: List Element List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .List PluginThe same as indexes; see above.
setnth: List Number Element List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List Pluginsetnth(list, i, e), ifi is a valid index for list, returns a new list in which the element
at index i is e.
List background also provides the following rule forms to manipulate lists:
value loc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .List Plugin
If loc is a location in the state and its value is a list, this rule produces an update that addsvalueto loc. In lists order matters, so the update produced by this rule is NOT incremental (notlike the one for sets). As a result, there cannot be two parallel add rules operating on the samelist.
value loc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List Plugin
Iflocis a location in the state and its value is a list, this rule produces an update that removesthe first occurrence ofvaluefrom loc. As for add, this rule is also NOT incremental (not like theone for sets) and there cannot be two parallel remove rules operating on the same list.
value loc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List Plugin
If loc is a location in the state and value is a list, it removes the first element of the list and
21
-
7/22/2019 CoreASM-1.0.5-UserManual
22/29
4 STANDARD PLUG-INS CoreASM Language User Manual
puts it in the given location (shifiting the list to left).
value loc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . List Plugin
If loc is a location in the state and value is a list, it removes the last element of the list andputs it in the given location (shifiting the list to right).
In the following example,SortSet sorts elements of a given set into a list:
SortSet(set) =
MMM
MMMMMM
MMMMMMMMM := []
MMMMMMMMMtempSet := set
MMMMMM
MMM
MMMMMM (| tempSet | > 0)MMMMMMMMM
x
tempSet
(
y
tempSet
x
-
7/22/2019 CoreASM-1.0.5-UserManual
23/29
4 STANDARD PLUG-INS CoreASM Language User Manual
4.10 Stack
The Stack plug-in can be loaded by the following phrase:
StackPlugin
This plug-in provides the following stack operations and functions on lists:
value loc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Stack Plugin
Iflocis a location in the state and its value is a stack (i.e., a list), it pushes valueto the frontof the stack.
locv locs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Stack Plugin
Iflocs is a location in the state and its value is a stack (i.e., a list), it removes the first elementof the stack (top of the stack) and assigns it as the value oflocv.
peek: List Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Stack PluginReturns the top of the stack (first element of the list) without changing the stack.
4.11 Map Background
The Map plug-in can be loaded by the following
phrase:
MapPlugin
This plug-in provides a map background (MAP).
{ valuek1 -> valuev1, valuek2 -> valuev2, . . . , valuekn -> valuevnoptional
} . . . . . . . . . . . . . . . . . Map Plugin
Creates a map with the given key-value pairs. Map elements are enumerable; every map canbe viewed as a set of pairs which are represented by lists of size 2.
{ ->} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Map PluginCreates an empty map.
4.12 Signature Plugin
The Signature plug-in can be loaded by the following
phrase:
23
-
7/22/2019 CoreASM-1.0.5-UserManual
24/29
4 STANDARD PLUG-INS CoreASM Language User Manual
SignaturePlugin
The signature plug-in extends the header section ofCoreASM specifications (see Figure 1) toadd support for definition of functions, universes, and custom data types and also extends theengine to support for certain forms of type checking. This plug-in is still under development.The current version includes the following features:
Definition of universes through the following syntax (with optional initial elements): id ={ id1, ..., idn }
optional
Definition of enumeration backgrounds through the following syntax: ide ={ id1 , ..., idn
optional}
For example, the following line defines a new enumeration background of four elements:
PRODUCT = { Soda, Juice, Sandwich, Candy }
The elements are in fact defined as constant functions that hold values of the backgroundPRODUCT.
Definition of functions through the following syntax: idf : idu1 * ...* idun
optional
-> idr
As an example, the following signature defines a function named priceTable that mapspairs of string values to numbers:
priceTable : STRING * STRING -> NUMBER
Definition of derived functions through the following syntax: idf ( id1, ..., idn)
optional
= expression
As an example, the following declaration defines a derived function f(x, y) =x2 +y2:
f(x, y) = x^2 + y^2
Depending on the properties of the engine (see the Options Plugin, Section 5.1) the Signatureplug-in can use the signature information to perform the following checks:
24
-
7/22/2019 CoreASM-1.0.5-UserManual
25/29
5 ADDITIONAL PLUG-INS CoreASM Language User Manual
Type checking on assignments: if the Signature.TypeChecking property is set towarning, strict or on, before the updates are applied to the state, the SignaturePlugin checks the types of arguments and assigned values against the defined signaturesand issues a warning (in case of warning) or stops the execution of the engine with anerror (in case of strict or on).
Unknown identifiers: if the Signature.NoUndefinedId property is set to warning,strict or on, the Signature Plugin issues a warning (in case of warning) or stopsthe execution of the engine with an error (in case of strict or on) if a function nameis used and its signature is not defined in the header of the specification. This feature helpsin identifying typos in the specification.
5 Additional Plug-ins
The plug-ins listed in this section are currently not part of any plug-in packages.
5.1 Options
The Options plug-in can be loaded by the following phrase:
OptionsPlugin
The Options plug-in extends the header section of CoreASM specifications (see Figure 1) toprovide the following syntax i to set values of engine properties:
property value
Other plug-ins (such as the Signature Plugin, see Section 4.12) can use these options tocustomize their behavior.
5.2 Time
The Time plug-in can be loaded by the following
phrase:
TimePlugin
This plug-in provides the following monitored function:
now: Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Time PluginReturns a value representing the current time of the system. 1
25
-
7/22/2019 CoreASM-1.0.5-UserManual
26/29
5 ADDITIONAL PLUG-INS CoreASM Language User Manual
5.3 Math
The Math plug-in can be loaded by the following phrase:
MathPlugin
Math Plug-in extends theCoreASMengine to provide some basic mathematical functions. Most ofthese functions are equivalent of their Java counterparts in java.lang.Math. For such functions,the following descriptions are basically taken from the Java 2 Platform Standard Edition 5.0 APISpecification.
5.3.1 Constants
The value that is closer than any other to e, the base of the natural logarithms.
The value that is closer than any other to , the ratio of the circumference of a circle to itsdiameter.
5.3.2 Basic Derived Functions
Returns the absolute value ofv . Returns the arc cosine of an angle, in the range of 0 through . Returns the arc sine of an angle, in the range of/2 through /2. Returns the arc tangent of an angle, in the range of/2 through /2.
Converts rectangular coordinates (x, y) to polar (r, ) and returns .
Returns the cube root ofv . Returns the cube root ofv . Returns the smallest (closest to negative infinity) value that is greater than or
equal to the argument and is equal to a mathematical integer.
Returns the trigonometric cosine of an angle. Returns the hyperbolic cosine ofv . Returns Eulers numbere raised to the power ofv . Returnsev 1. Returns the largest (closest to positive infinity) value that is less than or equal
to the argument and is equal to a mathematical integer.
26
-
7/22/2019 CoreASM-1.0.5-UserManual
27/29
5 ADDITIONAL PLUG-INS CoreASM Language User Manual
Returns
x2 +y2 without intermediate overflow or underflow.
Computes the remainder operation on two arguments as pre-scribed by the IEEE 754 standard.
Returns the natural logarithm (basee) ofv. Returns the base 10 logarithm ofv . Returns the natural logarithm of the sum of the argument and 1; i.e.,ln(v +1).
Returns the greater of two values.
Returns the smaller of two values.
Returns the value of the first argument raised to the power of the second
argument.
Returns a random value with a positive sign, greater than or equal to 0.0 andless than 1.0.
Returns the closest mathematical integer to the argument. Returns zero if the argument is zero, 1.0 if the argument is greater than zero,1.0 if the argument is less than zero.
Returns the trigonometric sine of an angle.
Returns the hyperbolic sine ofv .
Returns the correctly rounded positive square root ofv ; i.e.,
v.
Returns the trigonometric tangent of an angle. Returns the hyperbolic tangent ofv . Converts an angle measured in radians to an approximately equivalent
angle measured in degrees.
Converts an angle measured in degrees to an approximately equivalentangle measured in radians.
5.3.3 Special Derived Functions
Computes the powerset of the given set.
{ } Returns the maximum value in a collection of numbers. If there is
one non-number in the collection, it returns undef.
{ } Returns the minimum value in a collection of numbers. If there is one
non-number in the collection, it returns undef.
27
-
7/22/2019 CoreASM-1.0.5-UserManual
28/29
5 ADDITIONAL PLUG-INS CoreASM Language User Manual
{ } This function returns the sum of a collection of numbers. If there isone non-number in the collection, it returns undef.
{ } This function returns the sum of a collection of numbers, after
applying function to the values in the collection. If there is one non-number in thecollection, it returns undef.
{
} This function returns the powerset of the given set of elements.
5.3.4 An Example
MathPluginExample
StandardPlugins
MathPlugin
Init
Init =
MMMprogram(self) := @Main
MMMa(1) := 5
MMMa(2) := 10
MMMa(100) := 500
Main =MMM
e = MathE
MMM
MMMMMM "e = " + e
MMMMMM print "log(e) = " + log(e)
MMMMMM "sin(30) = " + round( sin( toRadians(30) ) * 10 ) / 10
MMMMMM "asin(0.5) = " + round( toDegrees( asin(0.5) ) )
MMMMMM "min(51, 43) = " + min(51, 43)
MMMMMM "sum({1, 2, 100} ) = " + s u m ({1, 2, 100})MMMMMM "sum({1, 2, 100}, @ a ) = " + s u m ({1, 2, 100}, @a)MMMMMM x powerset({1, 2, 3, 4}) MMMMMMMMM
x
powerset({1, 2, 3})
MMMMMMMMMMMM x + " is a member of powerset(
{1, 2, 3
})"
MMMMMMMMM
MMMMMMMMMMMM x + " is not a member of powerset({1, 2, 3})"MMM
CoreASM Spec 5.1: Using Math Plug-in
As an example, the output of the CoreASM Spec 5.1 would be the following:
28
-
7/22/2019 CoreASM-1.0.5-UserManual
29/29
5 ADDITIONAL PLUG-INS CoreASM Language User Manual
sum( {1, 2, 100} ) = 103
min(51, 43) = 43asin(0.5) = 30
powerset({1, 2, 3}) = {{}, {3}, {2}, {3, 2}, {1}, {3, 1}, {2, 1}, {3, 2, 1}}
{2, 3} memberof powerset({1, 2, 3} = true
log(e) = 1
sum( {1, 2, 100}, @a ) = 515
e = 2.718281828459045
{2, 1, 4} is not a member of powerset({1, 2, 3})
sin(30) = 0.5
29