<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl'
	href='http://xml.resource.org/authoring/rfc2629.xslt' ?>

<?rfc strict='yes' ?>
<?rfc toc='yes' ?>
<?rfc symrefs='yes' ?>
<?rfc editing='no' ?>

<!DOCTYPE rfc SYSTEM "rfc2629.dtd">

<rfc ipr="full2026" docName="draft-ietf-opes-rules-p-00">

<front>
	<title>P: Message Processing Language</title>

	<?rfc include='author.beck.xml' ?>
	<?rfc include='author.rousskov.xml' ?>

	<date day="16" month="September" year="2003" />

	<area>Applications</area>
	<workgroup>Open Pluggable Edge Services</workgroup>
	<keyword>Internet-Draft</keyword>
	<keyword>OPES</keyword>
	<keyword>rules</keyword>

	<abstract>

		<t>P is a simple configuration language designed for specification of
		message processing instructions at application proxies. P can be used
		to instruct an intermediary how to manipulate the application message
		being proxied. Such instructions are needed in an Open Pluggable Edge
		Services (OPES) context.</t>

	</abstract>

</front>

<middle>

<section anchor="section_intro" title="Introduction">

	<t>The Open Pluggable Edge Services (OPES) architecture <xref
	target="I-D.ietf-opes-architecture" />, enables cooperative
	application services (OPES services) between a data provider, a data
	consumer, and zero or more OPES processors.  The application services
	under consideration analyze and possibly transform application-level
	messages exchanged between the data provider and the data consumer.
	OPES processors need to be told what services are to be applied to
	what application messages. P language can be used for this
	configuration task.</t>

	<t>In other words, P language primary objective is to express
	statements similar to:</t>

	<figure anchor="opes-chainr">
		<artwork><![CDATA[
		if message meets criteria C,
		then apply service S;
]]></artwork>
	</figure>

	<t>Thus, P programs mostly deal with formulating
	message-dependent conditions and executing services.</t>

	<t>P design attempts to satisfy several conflicting goals:

	<list style="hanging">

		<t hangText="flexibility:">Application intermediaries deal with a wide
		range of applications and protocols (SMTP, HTTP, RTSP, IM, etc.). The
		language must be able to accommodate virtually all known tasks in
		selecting a desired adaptation service for a message of a known
		application protocol (and conceivable future applications).</t>

		<t hangText="efficiency:">Language interpretation must be efficient
		enough to be comparable with other message processing overheads at a
		typical application proxy (e.g., interpreting HTTP headers to
		determine response cachability).</t>

		<t hangText="simplicity:">Typical configurations must be easy to write
		and understand for a typical OPES system administrator.</t>

		<t hangText="correctness:">Many message handling configurations are
		written without direct access to intermediaries that will use those
		configurations.  The extent of off-line (compile-time) correctness
		checks should catch all syntax errors and many common semantic errors
		such as undefined values and type conflicts.</t>

		<t hangText="compactness:">It is possible that some processing
		instructions will be piggybacked as headers/metadata to messages they
		refer to, placing stringent size requirements on language code.</t>

		<t hangText="security:">It should be difficult if not impossible to
		write malicious code that would result in security vulnerability
		of compliant language interpreter.</t>

	</list></t>

	<t>While P addresses OPES needs, its design is meant to be applicable for
	a variety of similar intermediary configuration tasks such as access
	control list (ACL) specification and message routing in proxy meshes or
	load-balancing environments.</t>

	<t>P design is based on a minimal useful subset of features from several
	programming languages such as R (S), Smalltalk, and C++. Technically
	speaking, P is a single-assignment, lazy evaluation, strongly typed
	functional programming language.</t>

</section>



<section anchor="section_syntax" title="Syntax">

	<t>P syntax is defined by the following Augmented Backus-Naur Form
	(ABNF) <xref target="RFC2234" />:</t>

	<figure anchor="figure_bnf">
		<artwork><?rfc include='art.p-bnf.xml' ?></artwork>
	</figure>

	<t>XXX: add /* comments */.</t>

</section>

<section anchor="section_elements" title="Language elements">

	<section anchor="section_objects" title="Objects">

		<t>P is centered around the concept of an "object" that is similar to
		objects from other object-oriented languages. An object is,
		essentially, a piece of data or information. The value of an object is
		indistinguishable from the object itself. Object type is defined by
		the semantics of applicable operations and manipulations.  Almost
		everything in P is an object, even a piece of code. Here are a few P
		objects, listed one per line:</t>

		<figure><artwork>
	0
	"http://www.ietf.org/"
	Core
	{ a := 1/0; }
		</artwork></figure>

		<t>Many objects contain other objects, often called members.  Members
		are accessible by their name, using the member access operator (".").
		Member access operator has a single parameter: the name of the
		member to access. All P objects support "." operator, but
		not all objects have members. Here are a few examples:</t>

		<figure><artwork>
	Http.message.headers
	Core.interpreter.stop
	"string".nosuchmember
		</artwork></figure>

		<t>Many objects support operators other than member access. For
		example, member objects that support function call "()" operator are
		often call methods.</t>

		<figure><artwork>
	Http.message.headers.have(header)
	Core.interpreter.stop()
	1 / 0
	"string" + "string"
		</artwork></figure>
		
		<t>P operators are described in <xref target="section_operators" />.
		below.</t>
		
		<t>P does not have built-in facilities for describing object types.
		When writing a P program, only objects known to interpreter (e.g.,
		Core) and objects generated by known objects (e.g.,
		Core.import("Http")) can be used. P supports loadable modules that can
		be used to add objects to support new application protocols.  In fact,
		P core supports no application protocols directly. Instead, modules
		like "Http" can be used to process messages depending on application
		protocol being proxied.</t>

		<t>No default (silent) object type conversion is supported. However,
		explicit conversion (casting) is rarely needed because many methods
		are polymorphic (can work with several object types).</t>
		
	</section>

	<section anchor="section_operators" title="Operators">

		<t>Several operators are used in P to denote common operations.  These
		symbols are deemed to improve readability of P code as compared to
		their spelled-out-in-English counterparts.</t>

		<texttable>

			<preamble>P Operators</preamble>

			<ttcol width='20%' align='center'>operator</ttcol>
			<ttcol align='left'>default semantics</ttcol>

			<c>A == B</c>
			<c>A is semantically equal to B; does not modify A or B.</c>

			<c>A != B</c>
			<c>semantical inequality, same as !(A == B).</c>

			<c>!A</c>
			<c>logical negation, same as (A == false)</c>

			<c>A and B</c>
			<c>logical concatenation, same as !(!A or !B)</c>

			<c>A or B</c>
			<c>logical disjunction (inclusive), same as !(!A or !B)</c>

			<c>A + B</c>
			<c>sum of A and B; does not modify A or B.</c>

			<c>A * B </c>
			<c>product of A and B; does not modify A or B.</c>

			<c>A - B</c>
			<c>difference between A and B; does not modify A or B.</c>

			<c>A / B</c>
			<c>ratio of A to B; does not modify A or B.</c>

			<c>A.n</c>
			<c>access to A's member named n; does not modify A;
			fails if A has no member named n.</c>

			<c>A(...)</c>
			<c>object A is to perform a function call with zero or more
			parameters; may modify A and/or parameters</c>

		</texttable>

		<t>Operator precedence defines natural evaluation order used in
		mathematics and many programming languages. In the following list,
		operators are ordered based on their precedence. Operators with
		smaller precedence index are evaluated first. Operators with the same
		precedence index are evaluated in the left-to-right order of occurrence
		in an expression.

		<list style="numbers">

			<t>.</t>

			<t>()</t>

			<t>!</t>

			<t>* /</t>
			<t>+ -</t>

			<t>== !=</t>

			<t>and</t>
			<t>or</t>

		</list></t>

		<t>Except for the member access operator ("."). operators do not have
		to be supported by an object. Moreover, operator semantics may differ
		from one object to another (or even from one invocation to another for
		the same object though the latter is unlikely to be common in
		practice). Object writers SHOULD follow common operator semantics and
		MUST document actual operator semantics when adding support for these
		operators to their objects. The interpreter MUST NOT allow object
		writers to change operator precedence.</t>

		<t>Operators are not global special symbols but are passed to the
		object for interpretation, along with their parameters. Applying an
		operator is semantically equivalent to calling an object method. For
		example, the following three expressions are equivalent:</t>

		<figure anchor="fig_operator_interp_example">
			<artwork><![CDATA[
	a + b + c
	(a.+(b)) + c
	(a.+(b)).+(c)]]></artwork>
		</figure>

		<t>The "a + b + c" form is preferred for purely visual reasons.  Core
		P module provides basic objects and operators for them (e.g., boolean
		and integer). Application-specific modules usually provide
		applications-specific objects; those objects usually have
		application-specific methods and may not have methods to support
		operations common for basic types. For example, an Http module
		supplies an HTTP header object that does not have a "*" method.</t>

	</section>

	<section anchor="section_expressions" title="Expressions">

		<t>P expressions are used in if-statements to specify the condition
		for the if-statement body to be interpreted.</t>

		<figure anchor="fig_expression_example">
			<artwork><![CDATA[
	if (Http.request.method == "GET" and time.current() > time.noon) {
		...
	}]]></artwork>
		</figure>

		<t>Evaluation of an expression stops when the value of an expression
		is known and cannot be changed by further evaluation. This
		short-circuiting optimization technique is common to many programming
		languages. In the following example, the value of A will never be
		interpreted when C is interpreted, regardless of the context where C
		is used:</t>

		<figure anchor="fig_expression_short">
			<artwork><![CDATA[
	C := false and A;
	if (C) { ... };
	if (!C) { ... };
	...]]></artwork>
		</figure>

	</section>


	<section anchor="section_statements" title="Statements">

		<t>Objects are manipulated using if-statements and function-calls.</t>

		<figure anchor="fig_statement_example">
			<artwork><![CDATA[
	if (Http.request.method == "GET") {
		Services.applyOne(serviceFoo);
	}]]></artwork>
		</figure>

	</section>

	<section anchor="section_assignments" title="Assignments">

		<t>Most procedural programming languages use variables to store
		intermediate processing results. In such languages, a variable is
		essentially a named piece of memory that can be assigned a value and
		can be updated with new values as needed. P does not have such
		variables. Instead, P uses a "single assignment" approach: an
		expression can be tagged with a name and that name can be reused many
		times in the program. On the surface, this is equivalent to having all
		"traditional" variables declared as "constant". The following two
		if-statements are semantically equivalent in P:</t>

		<figure anchor="fig_assignment_example">
			<artwork><![CDATA[
	if (Http.request.headers.have(Http.makeHeader("Client-IP"))) {...}

	h := Http.makeHeader("Client-IP");
	hs := Http.request.headers();
	if (hs.have(h)) {...}]]></artwork>
		</figure>

		<t>If the expression changes, a new name must be used to tag the new
		expression. After an assignment statement, the value of the name is
		not the value of the expression, but the expression itself.  Thus, the
		following two code fragments are equivalent and make no sense in P
		(the first fragment would make sense in languages such as C++):</t>

		<figure anchor="fig_assignment_double_example">
			<artwork><![CDATA[
	h := Http.makeHeader("Client-IP");
	h := Http.makeHeader("Server-IP");

	h := Http.makeHeader("Client-IP");
	Http.makeHeader("Client-IP") := Http.makeHeader("Server-IP");]]></artwork>
		</figure>

		<t>The interpreter can but does not have to evaluate the expression
		named in the assignment statement until the name is actually used in
		an expression that requires evaluation (e.g., as a parameter of a
		function call statement). This allows for optional performance
		optimizations where only used expressions are evaluated.</t>

		<t>P does not have user-defined functions. However, some code reuse is
		possible because P code is a valid expression and, hence, can be
		named and reused:</t>

		<figure anchor="fig_assignment_code_example">
			<artwork><![CDATA[
	code := { ... complicated service action ... };
	if (condition1) { code; };
	...
	if (condition2) { code; };]]></artwork>
		</figure>

		<t>XXX: document whether expression has to be evaluated in the
		assignment context or use context. Document name scope.</t>

	</section>

</section>

<section anchor="section_modules" title="Modules">

	<t>Application-specific support is available in P via modules.  Basic P
	primitives such as integer types and boolean operations comprise the Core
	module.  Module is an object. The Core modules supplies the following
	methods to manipulate other modules:

	<list style="hanging">

		<t hangText='Core.import("M"):'>load a module called "M" and
		return it as the result.</t>

		<t hangText='Core.lookup(M):'>start looking up unresolved attributes
		and method identifiers in a previously loaded module M.</t>

	</list></t>

	<t>The Core module is assumed to be loaded (and being looked up) before
	the interpretation starts.</t>
		
	<t>XXX: document lookup conflict resolution.</t>

</section>

<section anchor="section_service" title="OPES Services">

	<t>Services module contains basic attributes and methods for searching and
	executing OPES services:

	<list style="hanging">

		<t hangText='Services.findOne(URI):'>returns a service object that
		corresponds to the specified URI. Fails if no corresponding object
		exists.</t>

		<t hangText='Services.applyOne(service, ...):'>applies the specified
		service to the current application message and optionally supplies
		service-specific application parameters. XXX: should parameters
		include the part of the message to be modified or just services
		metadata?</t>

	</list></t>

	<t>Here is a service application example for a German to French
	translation service:</t>

	<figure anchor="fig_services_example">
		<artwork><![CDATA[
	Http := import("Http");
	if (Http.response.language_is("german")) {
		service := Services.find("opes://services/tran/german/french");
		service.toDialect("southern");
		Services.applyOne(service, Http.request.headers);
	}]]></artwork>
	</figure>

	<t>XXX: explain how failures are propagated and can be handled</t>

	<t>XXX: add Core.interpreter.stop and Core.interpreter.restart
	methods.</t>

</section>

<section anchor="section" title="Failures">

	<t>Virtually any P statement may fail: expression denominator may be zero,
	named members may not exist, objects may not support applied operators,
	service execution may fail, interpreter may ran out of resources during an
	assignment, etc. A failure immediately stops interpretation of the first
	surrounding code block and assigns that block a boolean value of
	false.</t>

	<t>If the failed block is a part of a larger expression, the interpreter
	MUST continue evaluating the expression containing the failed block using
	usual expression evaluating rules, including short-circuiting boolean
	expressions. If the failed block is a stand-alone statement, that
	statement fails and the failure is propagated using the above rules.  If
	the implicit code block surrounding the program fails (XXX: document or
	require an implicit surrounding block like XML does), the entire P program
	interpretation terminates with a failure.</t>

	<t>Failure propagation rules allow to catch failures, similar to an
	exception mechanisms in languages like C++ or Java, except
	that P exceptions are not objects (they carry no information). For 
	example, here is a simple way to introduce a backup/failover
	service:</t>

	<figure anchor="fig_failures_failover">
		<artwork><![CDATA[
	{
		...
		Services.applyOne(unsafeService);
	} or {
		...
		Services.applyOne(failoverService);
	};]]></artwork>
	</figure>

	<t>The following example illustrates how a failure-prone service can be
	retried twice if needed:</t>

	<figure anchor="fig_failures_retry">
		<artwork><![CDATA[
		code := {
			/* code executing the service */
		};
		code or code or code;]]></artwork>
	</figure>

	<t>It is possible to force the interpreter to fail using the 
	"Core.interpreter.fail(reason)" call. This is handy when there
	is a logical failure that the interpreter cannot detect on
	its own:</t>
	
	<figure anchor="fig_failures_forced">
		<artwork><![CDATA[
		{
			/* large piece of code executing several services,
			   each manipulating the current HTTP message ... */

			/* checkpoint */
			if (!Http.message.headers.have("Content-Length")) {
				Core.interpreter.fail("services did not set CL");
			}

			/* OK, continue message manipulation ... */
		} or {
			/* recover from failure ... */
		}]]></artwork>
	</figure>

</section>

<section anchor="section_security" title="Security Considerations">

	<t>XXX: document non-obvious vulnerabilities: too many names, too deep
	nesting, invalid math, too much error logging; execution of unauthorized
	services, unauthorized exposure of sensitive information to authorized
	services.</t>

</section>

<section anchor="section_compliance" title="Compliance">

	<t>XXX: define what a compliant interpreter is.</t>

</section>

<appendix anchor="section_examples" title="Examples">

	<t>This appendix contains half-baked examples to illustrate P usage in
	common OPES environments. Example themes are taken from <xref
	target="I-D.beck-opes-irml" /> to ease the comparison with IRML.</t>

	<!-- <appendix anchor="section_example_provider" title="Data provider"> -->

	<t>Here is a data provider example:</t>

	<figure anchor="fig_example_provider">
		<artwork><![CDATA[
	interpreter.languageVersion("1.0"); // fails if incompatible

	Http := import("Http");
	lookup(Http);

	// Is the requested web document our home page?
	isHome := request.uri.looksLikeHome();

	// Does the user send us a specific cookie?
	cookie := makeHeader("Cookie", "sew=23");
	haveCookie := request.headers.have(cookie);

	if (isHome and haveCookie) {
		Services := import("Services");
		service := Services.findOne("opes://local.net/add-lcl-content");
		service.clientIp(request.clientIp);
		Services.applyOne(service);	
	}]]></artwork>
	</figure>

	<!-- <section anchor="section_example_consumer" title="Data consumer"> -->

	<t>Here is a data consumer example:</t>

	<figure anchor="fig_example_consumer">
		<artwork><![CDATA[
	Services := import("Services");
	service := Services.findOne("opes://privacy.net/priv-serv");
	service.action("remove-referer");
	Services.applyOne(service);]]></artwork>
	</figure>

</appendix>

<!--

<appendix anchor="section_todo" title="To-do">

	<t><list style="hanging">

		<t hangText=":"></t>

	</list></t>

</appendix>

-->

<appendix anchor="section_change_log" title="Change Log">

	<t>Internal WG revision control IDs: $RCSfile: rules-lang.xml,v $ $Revision: 1.5 $.</t>

</appendix>

</middle>

<back>
<references title="Normative References">

<!-- <?rfc include='reference.RFC.2119.xml' ?>  -->
<?rfc include='reference.RFC.2234.xml' ?>
<!-- <?rfc include='reference.RFC.3238.xml' ?> -->

<?rfc include='reference.I-D.ietf-opes-architecture.xml' ?>
<!-- <?rfc include='reference.I-D.ietf-opes-end-comm.xml' ?> -->
<!-- <?rfc include='reference.I-D.ietf-opes-scenarios.xml' ?> -->

</references>

<references title="Informative References">

<!--
	<?rfc include='reference.I-D.ietf-opes-protocol-reqs.xml' ?>
	<?rfc include='reference.I-D.ietf-fax-esmtp-conneg.xml' ?>
	<?rfc include='reference.RFC.3080.xml' ?>
-->

	<?rfc include='reference.RFC.2616.xml' ?>
	<?rfc include='reference.I-D.beck-opes-irml.xml' ?>

</references>

</back>

</rfc>
