modelica / ModelicaSpecification

Specification of the Modelica Language
https://specification.modelica.org
Creative Commons Attribution Share Alike 4.0 International
104 stars 40 forks source link

Omission from the Modelica 3.2 grammar #402

Closed modelica-trac-importer closed 6 years ago

modelica-trac-importer commented 6 years ago

Reported by stefanv on 7 Sep 2010 18:08 UTC Section 12.4.2.1 of the Modelica 3.2 specification describes the use of "function partial application", however the grammar was not updated to reflect this. The simplest change to the grammar to allow this would be to add the following line to the 'primary' production:

| **function** component_reference "(" [ named_arguments ] ")"

We should make this change immediately so the grammar is at least correct, but we should discuss at the upcoming design meeting whether there should be a separate 'function_partial_application' production instead.


Migrated-From: https://trac.modelica.org/Modelica/ticket/402

modelica-trac-importer commented 6 years ago

Modified by stefanv on 7 Sep 2010 18:50 UTC

modelica-trac-importer commented 6 years ago

Modified by dietmarw on 7 Sep 2010 19:05 UTC

modelica-trac-importer commented 6 years ago

Comment by HansOlsson on 8 Sep 2010 07:08 UTC I would propose to do it properly and introduce:

function_arguments:
   (function name function_call_args | expression ) [ "," function_arguments ]
  | named_arguments

named_argument: IDENT "=" (function name function_call_args | expression )

Or even:

function_arguments:
   function_argument [ "," function_arguments ]
  | named_arguments

named_argument: IDENT "=" function_argument

function_argument: function name function_call_args | expression 
modelica-trac-importer commented 6 years ago

Comment by stefanv on 8 Sep 2010 12:38 UTC I would agree, but I would then further factor out the new type of expression.

modelica-trac-importer commented 6 years ago

Comment by stefanv on 8 Sep 2010 12:51 UTC Further to my last comment, I propose:

function_arguments:
   function_argument [ "," function_arguments | 'for' for_indices ]
   | named_arguments 

named_arguments:
   named_argument [ "," named_arguments ] 

named_argument:
   IDENT "=" function_argument

function_argument:
   'function' component_reference "(" [ named_arguments ] ")"
   | expression

Besides factoring out 'function_argument' into its own production for clarity, there are two other differences from what Hans wrote above:

  1. I used 'component_reference' instead of 'name'. My reading of 12.4.2.1 suggests to me that that is what is intended.

  2. Instead of 'function_call_arguments', I used '"(" [ named_arguments ] ")"'. Again, 12.4.2.1 suggests that only named arguments are permitted when creating a partial function application.

modelica-trac-importer commented 6 years ago

Comment by HansOlsson on 8 Sep 2010 14:18 UTC My main point was to not have it in the general primary and it is good that we agree on that.

I also agree that it should only be named_arguments.

However, I don't see what lead to the conclusion that it should be a general component-reference as 12.4.2.1 refers to a func_name and other function calls in expressions now use name (it was previously (=Modelica 2.2) component_reference in the grammar with a semantic rule, since we had to left-factoring the grammar to make acceptable to LL-parser; looking at the grammar there seems to be some minor items left of the old style).

modelica-trac-importer commented 6 years ago

Comment by stefanv on 8 Sep 2010 14:46 UTC 12.4.2 specifically says:

A  functional  argument  can  be  provided  in  one  of  the  following  forms  to  be  passed  to  a  formal  parameter  of function type in a function call: 

a)  as a function name [Parabola example below], 
b)  as a function partial application (Section 12.4.2.1 below),  
c)  as a function that is a component, 
d)  as a function partial application of a function that is a component (example in Section 12.4.2.1 below).

Parts (c) and (d) suggest to me that a component_reference is allowed. The following (contrived yet seemingly valid) example illustrate this (ignore the fact that this model isn't balanced):

partial function Integrand
    input Real x;
    output Real y;
end Integrand;

function Integrate
    input Real x1, x2;
    input Integrand integrand;
    output Real integral;
algorithm
    integral := (x2-x1) * (integrand(x1) + integrand(x2)) / 2;
end Integrate;

function IntegrateSelectedFunction
    input Real x1, x2;
    input Integrand integrands[:];
    input Integer funcSel;
    output Real integral;
algorithm
    integral := Integrate(x1,x2,function integrands[funcSel]())
end IntegrateSelectedFunction;

function IntegrateTheFirstOfThese
    input Real x1, x2;
    input Integrand integrand1, integrand2;
    output Real integral;
algorithm
    integral := IntegrateSelectedFunction(1,x1,x2,{integrand1,integrand2})
end IntegrateTheFirstOfThese;

function Power
    input Real x, p;
    output Real y;
algorithm
    y := x^p;
end Power;

model M
    Real from, to, y;
equation
    y = IntegrateTheFirstOfThese(from,to,
                     function Power(p=2),
                 function Power(p=3));
end M;
modelica-trac-importer commented 6 years ago

Comment by dietmarw on 10 Sep 2010 07:27 UTC Replying to [comment:5 stefanv]:

I added the updated version of the MoParser and the Grammar.html provided by Stefan Vorkoetter in r4152 to the tools/MoParser branch.

modelica-trac-importer commented 6 years ago

Comment by HansOlsson on 13 Sep 2010 16:25 UTC The example from Stefan relies on making array of function pointers.

As far as I see we have not added rules for expressions involving functions pointers (like making arrays, if-expressions); and the corresponding typing rules. Some such rules are probably good for the future, but for the first step it does not seem needed and thus name should suffice.

modelica-trac-importer commented 6 years ago

Comment by HansOlsson on 15 Sep 2010 13:39 UTC From design meeting:

function_arguments:
   function_argument [ "," function_arguments | 'for' for_indices ]
   | named_arguments 

named_arguments:
   named_argument [ "," named_arguments ] 

named_argument:
   IDENT "=" function_argument

function_argument:.
   'function' name "(" [ named_arguments ] ")"
   | expression

The current intention is to only allow an argument of function type to be called and used as arguments to other functions, i.e. not inside if-expressions or in arrays.

Therefore add 'scalar' in some places in 12.4.2 to make this clearer.

modelica-trac-importer commented 6 years ago

Modified by HansOlsson on 15 Sep 2010 13:53 UTC

modelica-trac-importer commented 6 years ago

Comment by dietmarw on 15 Sep 2010 23:48 UTC Note: A new version of Maplesoft's MoParser (based on the outcome of this ticket) has been added to the repo in r4184 and the grammar definitions from the provided Grammar.html have been updated in r4186 for ModelicaReference.ModelicaGrammar