A Formal Description Method of Matrix Code Generation Technology Based on Coq
By formally describing data types and primitives in Coq and extending DPIA technology, the problem of variable naming conflicts and insufficient complex matrix code generation capabilities in matrix code generation is solved, and the generation and correctness of efficient parallel imperative matrix codes are achieved. verify.
Patent Information
- Application Number
- CN202111367911.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-18
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2041-11-18
AI Technical Summary
The existing Data Parallel Idealised Algol (DPIA) technology lacks formal verification, variable naming conflicts, and insufficient matrix code generation capabilities in matrix code conversion, and cannot effectively generate complex matrix codes such as matrix transposition and block matrix operations.
In the Coq theorem prover, formal verification of code conversion rules is achieved by formally describing data types, functional primitives and command primitives, extending primitives and rules, generating matrix codes, and solving variable naming through automated rewriting strategies Conflict, supports matrix assignment, inversion, transposition and other operations.
It realizes the efficient generation of executable parallel imperative matrix codes in Coq, ensures the correctness of code conversion and the ability to generate complex matrix codes, solves the problem of variable naming conflicts, and improves the scalability of matrix code generation.
Smart Images

Figure CN114154114B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of formal description technology, and in particular to a formal description method of Coq-based matrix code generation technology. Background Art
[0002] The demand for intelligent computing has increased the complexity of matrix calculations. Parallel hardware has greatly improved the performance of matrix calculations. High-performance matrix codes are all imperative codes, but functional matrix descriptions are suitable for proving the mathematical properties of matrices and optimizing matrix expressions, but the matrix codes of functional languages are not efficient. Atkey et al. proposed a method for converting functional matrix descriptions to imperative codes, Data Parallel Idealised Algol (DPIA) (Atkey R, Steuwer M, Lindley S, et al. Strategy Preserving Compilation for Parallel Functional Code [J]. 2017). This technology is an extension of Syntactic Control of Inference Revisited (O'Hearn PW, Power AJ, Takeyama M, et al. Syntactic Control of Interference Revisited [J]. 1997.), which describes a conversion from high-level functional code with parallel strategies to parallel imperative code. However, this method has a high degree of abstraction, poor intuitiveness, and is not easy to convince with its reliability. There are still various problems to be solved in the implementation. The detailed analysis is as follows:
[0003] First, DPIA technology lacks formal verification of code conversion rules, making it difficult to ensure the correctness of the code conversion process. Contemporary theorem proving tools (such as Coq) can prove and verify mathematical theorems and support program correctness. However, in order to formally describe the mathematical rule system in Coq, a series of problems need to be solved.
[0004] Second, in order to formally verify the DPIA transformation rules in the Coq theorem prover, these rules need to be implemented in Coq. To this end, it is necessary to describe the types of function constants introduced by DPIA and ensure that both function definitions and transformation rule definitions can pass type checking.
[0005] Third, there is no specific solution for automatically generating new names for new variables in DPIA technology. In the process of converting functional code to C code, new variables often need to be introduced. Typical scenarios include: local variables in parallel operations, loop variables in loop bodies, and these variables cannot conflict with each other, otherwise the code will not run correctly.
[0006] Fourth, since the programs processed by DPIA technology are mainly matrix programs, the matrices need to be formalized in Coq first. Although there are some formal methods for matrices in the field of theorem proving, some of these formalization techniques are cumbersome and some are not suitable for expressing functional programs about matrices. The primitives and rules provided by DPIA itself are not sufficient to support the translation of matrix functional programs, so it is impossible to generate more complex matrix codes, such as matrix transposition, block matrix operations, etc.
[0007] In order to solve the above problems, the present invention proposes a formal description method of matrix code generation technology based on Coq. Summary of the invention
[0008] In view of the above, the object of the present invention is to provide a formal description method of matrix code generation technology based on Coq. The method formally describes the matrix code generation technology in a Coq theorem prover based on the DPIA (Data Parallel Idealised Algol) technology principle for generating efficient matrix imperative code (C). The method solves the problem of repeated naming of new variable declarations in the technology and expands the primitives and rules in the technology to realize the conversion of more functional matrix programs.
[0009] In order to achieve the above object, the present invention adopts the following technical solution:
[0010] A formal description method of matrix code generation technology based on Coq, the method comprising the following steps:
[0011] Step S1, formally describe data types, functional primitives, and command primitives;
[0012] Step S2, formally describing the code conversion rules, wherein the code conversion rules include: conversion from high-level functional primitives to high-level imperative macros, conversion from high-level imperative macros to low-level for loops, and explicit conversion between commands and receivers and expressions;
[0013] Step S3, using the rewrite function in Coq to write an automated rewriting strategy to achieve matrix code generation;
[0014] Step S4, construct a variable name automatic generation function, establish and prove the number of variable declarations in different commands, and then introduce the lemma into the command display translation rule to achieve no repeated declaration of new variable names when translating commands;
[0015] Step S5: Expand the original sentences and rules to realize the generation of more matrix codes, wherein the original sentences include: matrix assignment, inversion, transposition, scalar function primitives and index function primitives.
[0016] Furthermore, in step S1, the data types include: numeric, index, array, dual and corresponding receivers and expressions; the functional primitives include: scalar functional primitives, array functional primitives and dual functional primitives; the command primitives include: empty command, sequential execution, assignment, new variable declaration, sequential for loop and parallel for loop.
[0017] Furthermore, the data types are formally described, including:
[0018] The commonly used major category Type in Coq is used to implement data types and phrase types belonging to type kind and phrase type phrase; data types include: numeric type num, index type idx that accepts a natural number as a parameter, array type ary that accepts a natural number and a data type as parameters, and dual type tensor that accepts two data types as parameters; receiver types and expression types accept a data type as an input parameter to return a phrase type.
[0019] Furthermore, the scalar function primitive is formally described, specifically including:
[0020] The negation function neg accepts a numerical expression parameter and outputs a numerical expression; the addition function add, the multiplication function mul, the division function div and the subtraction function sub accept two numerical expression parameters and output a numerical expression; the array function primitive is formally described, specifically including: the mapping function map accepts a natural number n, data types s and t, a mapping function, and an array of expressions of length n and element type s as parameters, and the output type is an array expression of length n and data type t, wherein the mapping function map accepts a mapping function, which accepts data in the form of data type The expression of data type s is used as a parameter and the expression of data type t is used as an output; the sequential mapping function mapSeq and the parallel mapping function mapPar are of the same type as the mapping function map; the reduction function reduce accepts a natural number, data types s and t, a calculation function, an initial value, an array of expressions of length n and element type s as parameters, and outputs an expression type of t, where the calculation function accepts the expression type of s and the expression type of t, and uses the expression type of t as the output type, and the initial value is the expression type of t; the reduction function reduceS executed sequentially The type of eq is the same as that of the reduction function reduce; the combination function zip accepts a natural number, data types s and t, an array of expressions of length n and element type s, and an array of expressions of length n and data type t as parameters, and the output type is an array expression of length n and element type s and t; the query function fun_idx accepts a natural number n, data type t, an array expression of length n and data type t, and an index expression less than n as parameters, and the output type is an expression of type t; the receiver combination left component function zipAcc1 accepts a self The receiver combination right component function zipAcc2 accepts a natural number n, data types s and t, and a dual type receiver of length n and data types s and t as input, and outputs a receiver of length n and data type s; the receiver combination right component function zipAcc2 accepts a natural number n, data types s and t, and a dual type receiver of length n and data types s and t as input, and outputs a receiver of length n and data type t; the receiver query function idxAcc accepts a natural number n, data type t, an array receiver of length n and data type t, and an index expression less than n as parameters, and outputs a receiver of data type t.
[0021] Furthermore, the dual functional primitives are formally described, including:
[0022] The dual function Pair accepts data types s and t, an expression of data type s, and an expression of data type t as parameters, and outputs an expression of the dual of data types s and t; the left component function Fst accepts data types s and t, and an expression of the dual of data types s and t as parameters, and outputs an expression of data type s; the right component function Snd accepts data types s and t, and an expression of the dual of data types s and t as parameters, and outputs an expression of data type t; the receiver dual left component function pairAcc1 accepts receivers of data types s and t, and the dual of data types s and t as parameters, and outputs a receiver of data type s; the receiver dual right component function pairAcc2 accepts receivers of data types s and t, and the dual of data types s and t as parameters, and outputs a receiver of data type t;
[0023] Among them, the command primitive is formally described, including:
[0024] The sequential statement seq accepts a pair of command types as parameters, and its output type is command type; the empty statement skip is a command type; the assignment statement assign accepts a data type d and a pair of a receiver of d and an expression as parameters, and its output type is command type; the new variable declaration statement accepts a data type t and a function as parameters, and its output type is command type, wherein the function accepted by the new variable declaration statement takes as parameters a receiver with parameter t and a pair of expressions, and its output type is command type; the sequential loop seqfor accepts a natural number n and a function as parameters, and its output type is command, wherein the function accepted by the sequential loop seqfor takes as input an index expression less than n, and its output type is command type; the parallel loop parfor accepts a natural number n, a data type t, and a function as parameters, and its output type is command type, wherein the function accepted by the parallel loop parfor takes as input an array receiver with a length of n and a data type t, an index expression less than n, and a receiver with a data type t, and its output type is command.
[0025] Furthermore, in step S2, the conversion from high-level functional primitives to high-level imperative macros is implemented by an Acceptor-passing translator and a Continuation-passing translator;
[0026] The commands and receivers are converted into explicit expressions by a command translator, a receiver translator and an expression translator;
[0027] The formal description of the Acceptor-passing translator includes:
[0028] Translator A accepts data type d, expressions of data type d, and receivers as parameters, and the output type is command type; the formal description of the Acceptor-passing rules includes: the conversion rules of numeric type expressions e and receiver a through translator A; the conversion rules of array type expressions e and receiver a through translator A; the conversion rules of dual type expressions and receivers through translator A; the conversion rules of negating numeric type expressions through translator A; the conversion rules of adding numeric type expressions e1 and e2 through translator A; the conversion rules of multiplying numeric type expressions e1 and e2 The array expression of sequential mapping mapSeq passes through the conversion rules of translator A; the array expression of parallel mapping mapPar passes through the conversion rules of translator A; the array expression of sequential reduction reduceSeql passes through the conversion rules of translator A; the array expression of combination function zip passes through the conversion rules of translator A; the expression of dual function Pair passes through the conversion rules of translator A; the expression of left component function Fst passes through the conversion rules of translator A; the expression of right component function Snd passes through the conversion rules of translator A;
[0029] Formal description of the Continuation-passing translator, including:
[0030] The translator C accepts a data type d, an expression of data type d and a function as parameters, and the output type is a command type, wherein the translator C accepts a function whose input parameter is an expression of data type d and the output type is a command type;
[0031] Formal description of Continuation-passing rules, including:
[0032] The expression of data type d and the subsequent call operation c pass through the conversion rules of translator C; the negation of numeric type expressions passes through the conversion rules of translator C; the addition of numeric type expressions e1 and e2 passes through the conversion rules of translator C; the multiplication of numeric type expressions e1 and e2 passes through the conversion rules of translator C; the array expression acted on by parallel mapping mapPar passes through the conversion rules of translator C; the array expression acted on by sequential mapping mapSeq passes through the conversion rules of translator C; the array expression acted on by sequential reduction reduceSeql passes through the conversion rules of translator C; the array expression acted on by the combination function zip passes through the conversion rules of translator C; the expression acted on by the dual function Pair passes through the conversion rules of translator C;
[0033] The formal description of the conversion from high-level imperative macros to low-level for loops specifically includes:
[0034] The parallel mapping mapParl accepts a natural number n, data types s and t, a function, an array expression xs of length n and data type s, and a receiver out of length n and data type t as parameters, and uses a parallel loop parfor to apply the mapping function f to each element of xs and store the result in out, wherein the parallel mapping mapParl accepts a function whose input parameters are an expression of data type s and a receiver of data type y;
[0035] The sequence map mapSeql accepts a natural number n, data types s and t, a function, an array expression xs of length n and data type s, and a receiver out of length n and data type t as parameters, and uses a sequence loop seqfor to apply the mapping function f to each corresponding element of xs and out, wherein the sequence map mapSeql accepts a function whose input parameters are an expression of data type s and a receiver of data type t;
[0036] The sequential reduction reduceSeql accepts a natural number n, data types d1 and d2, a function, an initial value, an array expression xs of length n and data type d1, and a subsequent processing function c, and is implemented by creating a new temporary variable, assigning the initial value to the variable, and then applying f to each element in the array and the temporary variable through a sequential loop seqfor, and finally applying the subsequent processing function to the temporary variable, wherein the sequential reduction reduceSeql accepts a function whose input parameters are expressions of data types d1 and d2 and a receiver of data type d2, the initial value accepted by the sequential reduction reduceSeql is an expression of data type d2, the subsequent processing function c accepted by the sequential reduction reduceSeql takes the data type d2 as a parameter, and its output is a command type.
[0037] Furthermore, the explicit conversion between commands and receivers and expressions includes the formalization of the translation rules of commands in Coq, the translation rules of receivers in Coq, and the translation rules of expressions in Coq, where:
[0038] The translation rules of the commands are formalized in Coq, specifically including:
[0039] The explicit translation rules of the empty statement skip; the explicit translation rules of the sequential execution statement seq; the explicit translation rules of the assignment statement assign; the explicit translation rules of the new variable generation statement new; the explicit translation rules of the sequential loop statement seqfor; the explicit translation rules of the parallel loop statement parfor; the formalization of the translation rules of the receiver in Coq, specifically including: the explicit translation rules of the receiver with data type d and the environment and path; the explicit translation rules of the array receiver acted by the receiver query function idxAcc; the explicit translation rules of the receiver acted by the receiver dual left component function pairAcc1; the explicit translation rules of the receiver acted by the receiver dual right component function pairAcc2; the explicit translation rules of the receiver acted by the receiver combination left component function zipAcc1; the explicit translation rules of the receiver acted by the receiver combination right component function zipAcc2;
[0040] The translation rules of the above expressions are formalized in Coq, specifically including:
[0041] Display translation rules for expressions of data type d, environments and paths; display translation rules for negation expressions; display translation rules for addition expressions; display translation rules for multiplication expressions; display translation rules for expressions acted upon by the combination function zip; display translation rules for expressions acted upon by the dual function Pair; display translation rules for expressions acted upon by the dual left component function Fst; display translation rules for expressions acted upon by the dual right component Snd; display translation rules for expressions acted upon by the query function fun_idx.
[0042] Furthermore, the step S3 specifically includes:
[0043] Define the rewriting rules of high-level functional primitives and translator A in the automatic rewriting strategy Asimpl1;
[0044] Define the remaining rewriting rules for translator A in the automatic rewriting strategy Asimpl2;
[0045] Define the rewriting rules of high-level functional primitives and translator C in the automatic rewriting strategy Csimpl1;
[0046] Define the remaining rewriting rules for translator C in the automatic rewriting strategy Csimpl2;
[0047] Define command, lvalue, and rvalue display translation rules in the automatic rewriting strategy CGsimpl;
[0048] Encapsulate Asimpl1, Asimpl2, Csimpl1, Csimpl2, and CGsimpl into the automatic rewriting strategy Allsimpl;
[0049] Define the rewriting rule strnat_simpl for strings to natural numbers.
[0050] Furthermore, the step S4 specifically includes:
[0051] Define the variable name generation functions vstr and istr, which automatically generate the corresponding variable name string according to the input natural number, where vstr is a temporary variable and istr is a loop variable;
[0052] Define the number of temporary variables generated in different commands. The empty statement skip command generates 0, the number generated by the sequential execution statement seq is the sum of the number of variables generated in the sequentially executed statements p1 and p2, and the assignment statement assign generates 0;
[0053] The number of new variable generation statements new is the number of new variable generation statements p plus 1.
[0054] The number of loops generated by the sequential loop statement seqfor is the number of loops generated by the loop body p plus 1;
[0055] The number of parallel loop statements parfor generates is the number of loops p plus 1;
[0056] The variable naming rules are combined with the command display translation, in which a natural number is added to the command display translator as an identification parameter, and then the command display translation rules with automatic generation of variable names for skip, seq, assign, new, seqfor, and parfor are defined.
[0057] Further, the step S5 includes extending the primitive extension and extending the rule;
[0058] The extension of the primitive extension specifically includes:
[0059] Define the root function sqrt of numeric type expression; define the absolute value function abs of numeric expression; define the index expression minus 1 function idxpred; define the sum function addidx, subtraction function subidx, multiplication function mulidx, and division function dividex of index expression; define the reverse function of array expression; define the order reversal function reverseSeq of array expression; define the copy function copy of array expression; define the order copy function copySeq of array expression; define the parallel transposition function transPar of two-dimensional array expression; define the order reversal function reverseSeql implemented by the sequential for loop; define the order copy function copySeql implemented by the sequential loop; define the parallel transposition function transParl implemented by the parallel loop;
[0060] The expansion of the rules specifically includes:
[0061] Define the conversion rules between the root function sqrt and translator A; define the conversion rules between the absolute value function abs and translator A; define the conversion rules between the sequence reversal function reverseSeq and translator A; define the conversion rules between the sequence copy function copySeq and translator A; define the conversion rules between the parallel transposition function transPar and translator A; define the conversion rules between the root function sqrt and translator C; define the conversion rules between the absolute value function abs and translator C; define the conversion rules between the sequence reversal function reverseSeq and translator C; define the conversion rules between the sequence copy function copySeq and translator C; define the parallel The conversion rules of the transposition function transPar and the translator C; define the display translation rules of the numerical expression acted upon by the root function sqrt; define the display translation rules of the numerical expression acted upon by the absolute value function abs; define the display translation rules of the index expression acted upon by the index expression minus 1 function idxpred; define the display translation rules of the index expression acted upon by the index expression sum function addidx; define the display translation rules of the index expression acted upon by the index expression subtraction function subidx; define the display translation rules of the index expression acted upon by the index expression multiplication function mulidx; define the display translation rules of the array assignment statements.
[0062] The beneficial effects of the present invention are:
[0063] On the one hand, the present invention applies formal technology to matrix code generation technology, and implements the matrix code generation technology of converting functional code with execution strategy into imperative code that can be executed efficiently and in parallel in Coq theorem prover. The formal description of this technology in Coq has strict definition and can pass type checking, laying a foundation for future rule correctness verification. On the other hand, a method for automatic variable naming is proposed, which solves the problem of temporary variable naming duplication in matrix code generation technology, and expands this technology to realize the generation of more matrix codes. BRIEF DESCRIPTION OF THE DRAWINGS
[0064] Figure 1 A schematic diagram of a framework of a formal description method of a Coq-based matrix code generation technology provided in Example 1;
[0065] Figure 2 A schematic diagram of the DPIA major type kind provided in Example 1;
[0066] Figure 3 A schematic diagram of the DPIA data type data provided in Example 1;
[0067] Figure 4 A schematic diagram of the DPIA phrase type phrase provided in Example 1;
[0068] Figure 5 A schematic diagram of the DPIA code conversion process provided in Example 1;
[0069] Figure 6 A table of the functional matrix program provided in Example 1;
[0070] Figure 7 This is a test result diagram based on 4 cores provided in Example 1;
[0071] Figure 8 This is a test result diagram based on 8 cores provided in Example 1. DETAILED DESCRIPTION
[0072] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0073] Example 1
[0074] See also Figure 1-Figure 8In this embodiment, a formal description method of matrix code generation technology based on Coq is provided. The logic block diagram of the method is as follows: Figure 1 As shown, the method specifically includes:
[0075] 1) Formal description of data types, functional primitives, and command primitives. The specific implementation is as follows:
[0076] ① Establish the Coq inductive definition of data types: The data types in the DPIA principle include numeric types, index types, array types, dual types, and corresponding receiver and expression types, such as Figure 2 , 3 , as shown in 4. This example constructs the Coq formal definitions of these types:
[0077] Definition kinds:Type:=Type.
[0078] Inductive data:kinds:=
[0079] |num:data(*numeric type*)
[0080] |idx:nat->data(*index type*)
[0081] |ary:nat->data->data(*array type*)
[0082] |tensor:data->data->data.(*dual type*)
[0083] Definition phrase:kinds:=Type.
[0084] This example constructs a higher-order type declaration for the receiver and expression types:
[0085] Receiver type acc:data->phrase.
[0086] Expression type exp:data->phrase.
[0087] The receiver and expression types are the writable domain (left value) and readable domain (right value) of the variable respectively. The receiver type can be assigned by an expression, and the expression type directly participates in the calculation.
[0088] ② Establish Coq type declaration of functional primitives: DPIA contains a set of basic function primitives (containing only name, type and function description), and the present invention establishes Coq type description of this set of functions. These basic function primitives are divided into four categories. The first category is scalar operation primitives, which are mainly used for scalar calculations; the second category is high-level functional primitives without execution strategies, which are used to process array data; the third category is high-level functional primitives with execution strategies, which are used to process array data; the fourth category is functional primitives for data layout, zip converts two vectors of equal length into arrays with dual elements, Pair constructs duality, Fst and Snd deconstruct a pair, and fun_idx searches for a value in an array.
[0089] Scalar primitives
[0090] neg:(exp num)->(exp num).
[0091] add mul div sub:(exp num)->(exp num)->(exp num).
[0092] zero one:exp num.
[0093] Array Function Primitives
[0094] map:forall[n:nat][st:data],((exp s)->(exp t))->(exp(ary ns))->(exp(ary nt)).
[0095] mapSeq:forall[n:nat][st:data],((exp s)->(exp t))->(exp(ary ns))->(exp(ary nt)).
[0096] mapPar:forall[n:nat][st:data],((exp s)->(exp t))->(exp(ary ns))->(exp(ary nt)).
[0097] reduce:forall[n:nat][st:data],((exp s)->(exp t)->(exp t))->(exp t)->(exp(ary ns))->(exp t).
[0098] reduceSeq: for all [n: nat][s t: data], ((exp s) -> (exp t) -> (exp t)) -> (exp t) -> (exp (ary n s)) -> (exp t).
[0099] zip: for all [n: nat][s t: data], (exp (ary n s)) -> (exp (ary n t)) -> (exp (ary n (tensor s t))).
[0100] fun_idx: for all [n: nat][t: data], (exp (ary n t)) -> (exp (idx n)) -> (exp t).
[0101] zipAcc1: for all [n: nat][s t: data], (acc (ary n (tensor s t))) -> acc (ary n s).
[0102] zipAcc2: for all [n: nat][s t: data], (acc (ary n (tensor s t))) -> acc (ary n t).
[0103] pairAcc1: for all [s t: data], (acc (tensor s t)) -> (acc s).
[0104] pairAcc2: for all [s t: data], (acc (tensor s t)) -> (acc t).
[0105] idxAcc: for all [n: nat][t: data], (acc (ary n t)) -> (exp (idx n)) -> (acc t).
[0106] Dual primitives
[0107] Pair: for all [s t: data], (exp s) -> (exp t) -> (exp (tensor s t)).
[0108] Fst: for all [s t: data], (exp (tensor s t)) -> (exp s).
[0109] Snd:forall[st:data],(exp(tensor st))->(exp t).
[0110] ③ Establish the command primitive and the Coq definition of the translator: DPIA command primitives are divided into two categories. The first category is commands, where seq (denoted as ';) is a sequential execution command; skip does not do any operation; new is used for variable declaration; assign (denoted as |:=) is an assignment statement. For loops are constructed by for and parfor combinators. The parallel for loop parfor is different from the usual for loop in that it needs to declare an array variable, which is used for the output of each iteration. This ensures that the implementation of parfor is safe. This embodiment constructs the Coq definitions of these command primitives:
[0111] Inductive comm:Type:=
[0112] |seq:comm*comm->com
[0113] |skip:comm
[0114] |assign:forall{d:data},(acc d)*(exp d)->comm
[0115] |new:forall{t:data},((acc t)*(exp t)->comm)->comm
[0116] |seqfor:forall(n:nat),((exp(idx n))->comm)->comm
[0117] |parfor:forall(n:nat)(t:data),(acc(ary nt))->((exp(idx n))->(acc t)->comm)->comm.
[0118] 2) Formal description of translators and code conversion rules. DPIA translation rules are mainly divided into three categories. The first category is the conversion rules defined by Acceptor-passing and Continuation-passing. The rules in this part need to be implemented by Acceptor-passing translators (A) and Continuation-passing translators (C). These rules can convert functional expressions into imperative codes. The second category uses low-level for loops to implement high-level imperative macros. The third category is the translation rules generated by imperative programs. The rules in this part need to be implemented by the command translator (CodeGen comm ), Receiver Translator (CodeGen acc ), Expression Translator (CodeGen exp ) and data type translators (CodeGen data ), mainly used for translation in the third step, such as Figure 5 shown.
[0119] The following is a formal description of the first type of translation rules in Coq. The acceptor-passing translator A(e)δ(a) is equivalent to an assignment statement, where a and e are the receiver and expression of the assignment respectively. The following is the formal definition of this part of the translator and the translation rules in Coq:
[0120] Formal description of the Acceptor-passing translator: A:forall[d:data],(exp d)->(acc d)->comm. Denoted as A(?)|(?).
[0121] The A translator takes an acceptor and an expression as parameters, which is equivalent to an assignment command.
[0122] Formal description of Acceptor-passing rules:
[0123] Rule: A(e) num (a)=a:= num e
[0124] Coq implementation: forall(a:acc num)(x:exp num),A(x)|(a)=a|:=x.
[0125] Rule: A(e) n.δ (a) = mapParl nδδ (λx aa:= δ x)ea
[0126] Coq implementation: forall (n:nat) (d:data) (a:acc (ary n d)) (x:exp (ary n d)), A(x) | (a) = mapParl (fun x a => a := x) x a.
[0127] Rule: A(e) δ1×δ2 (a) = pairAcc1 a := δ1 fst e; pairAcc2 a := δ2 snd e
[0128] Coq implementation: forall (n:nat) (d1 d2:data) (a:acc (tensor d1 d2)) (x:exp (tensord1 d2)), A(x) | (a) = (a.x1 := Fst x)'; a.x2 := Snd x.
[0129] Rule: A(neg e) num (a) = C(e) num (λx.a := num (neg x))
[0130] Coq implementation: forall (a:acc num) (e:exp num), A(neg e) | (a) = C(e) | (fun(x:expnum) => a := neg x)
[0131] Rule: A(add e1 e2) num (a) = C(e1) num (λx.C(e2) num (λy.a := num (add x y))
[0132] Coq implementation: forall (a:acc num) (e1 e2:exp num), A(add e1 e2) | (a) = C(e1) | (funx => C(e2) | (fun y => a := add e1 e2)).
[0133] Rule: A(mul e1 e2) num (a) = C(e1) num (λx.C(e2) num (λy.a := num (mul x y))
[0134] Coq implementation: forall (a: acc num) (e1 e2: exp num), A (mul e1 e2) | (a) = C (e1) | (fun x => C (e2) | (fun y => a := mul e1 e2)).
[0135] Rule: A (mapSeq n,δ1,δ2 fe) n.δ2 (a) = C (e) n.δ1 (λx. mapSeql n,δ1,δ2 (λy. (λo. A (fy) δ2 )) x a)
[0136] Coq implementation: forall (n: nat) (d1 d2: data) (f: exp d1 -> exp d2) (e: exp (ary n d1)) (a: acc (ary n d2)), A (mapSeq f e) | (a) = C (e) | (fun (x: exp (ary n d1)) => mapSeql (fun y => (fun o => A (fy) | (o))) x a).
[0137] Rule: A (mapPar n,δ1,δ2 fe) n.δ2 (a) = C (e) n.δ1 (λx. mapSeql n,δ1,δ2 (λy. (λo. A (fy) δ2 )) x a)
[0138] Coq implementation: forall (n: nat) (d1 d2: data) (f: exp d1 -> exp d2) (e: exp (ary n d1)) (a: acc (ary n d2)), A (mapPar fe) | (a) = C (e) | (fun (x: exp (ary n d1)) => mapParl (fun y => (fun o => A (fy) | (o))) x a).
[0139] Rule: A (reduceSeq n,δ1,δ2 f i e) n.δ2 (a)
[0140] = C (e) n.δ1 (λx. C (i) (λy. reduceSeql n,δ1,δ2 (λx’. (λy’. (λo. A (fx’y’ )δ2 ))) y x (λr. A (r) δ2 (a)))
[0141] Coq implementation: forall (n:nat) (d1 d2:data) (f:exp d1 -> exp d2 -> exp d2) (i:exp d2) (e:exp(ary n d1)) (a:acc d2), A(reduceSeq f i e)|(a) = C(e)|(fun (x:exp(ary n d1)) => C(i)|(fun y => reduceSeql(fun x' => (fun y' => (fun o => A(f x' y')|(o)))) y x (fun r => A(r)|(a)))).
[0142] Rule: A(zip e1 e2) n,δ1,δ2 (a) = A(e1) n.δ1 (zipAcc1 a); A(e2) n.δ1 (zipAcc2 a)
[0143] Coq implementation: forall (n:nat) (d1 d2:data) (e1:exp(ary n d1)) (e2:exp(ary n d2)) (a:acc(ary n(tensor d1 d2))), A(zip e1 e2)|(a) = A(e1)|(zipAcc1 a)'; A(e2)|(zipAcc2 a).
[0144] Rule: A(Pair δ1,δ2 e1 e2)δ1×δ2(a) = A(e1) δ1 (pairAcc1 δ1,δ2 a); A(e2) δ2 (pairAcc2 δ1,δ2 a)
[0145] Coq implementation: forall (d1 d2:data) (e1:exp d1) (e2:exp d2) (a:acc(tensor d1 d2)), A(Pair e1 e2)|(a) = A(e1)|(pairAcc1 a)'; A(e2)|(pairAcc2 a).
[0146] Rule: A(Fst δ1,δ2 e)δ1(a) = C(e) δ1×δ2 (λx.a := δ1 Fst δ1,δ2 x)
[0147] Coq implementation: forall(d1 d2:data)(e:exp(tensor d1 d2))(a:acc d1),A(Fst e)|(a)=C(e)|(fun x=>a|:=Fst x).
[0148] Rule: A(Snd δ1,δ2 e) δ2 (a) = C(e) δ1×δ2 (λx.a:= δ2 S δ1,δ2 x)
[0149] Coq implementation: forall(d1 d2:data)(e:exp(tensor d1 d2))(a:acc d2),A(Snd e)|(a)=C(e)|(fun x=>a|:=Snd x).
[0150] Formal description of the continuation-passing translator: C:forall[d:data],(exp d)->((exp d)->comm)->comm. Denoted as C(?)|(?).
[0151] The C translator takes an expression and a function that takes that expression as an argument, and does the equivalent of applying the latter to the former.
[0152] Formal description of the Continuation-passing rule:
[0153] Rule: C(e) δ (c) = ce
[0154] Coq implementation: forall(d:data)(e:exp d)(c:exp d->comm), C(e)|(c)=c e.
[0155] Rule: C(neg e) num c=C(e) num (λx.c(neg x))
[0156] Coq implementation: forall(e:exp num)(c:exp num->comm),C(neg)|(c)=C(e)|(fun x=>c(neg x)).
[0157] Rule: C(add e1e2) num c=C(e1) num (λx.C(e2) num(λy.c(add xy))))
[0158] Coq implementation: forall(e1 e2:exp num)(c:exp num->comm),C(add e1 e2)|(c)=C(e1)|(fun x=>C(e2)|(fun y=>c(add xy))).
[0159] Rule: C(mul e1 e2) num c=C(e1) num (λx.C(e2) num (λy.c(mul xy))))
[0160] Coq implementation: forall(e1 e2:exp num)(c:exp num->comm),C(mul e1 e2)|(c)=C(e1)|(fun x=>C(e2)|(fun y=>c(mul xy))).
[0161] Rule: C(mapPar n,δ1,δ2 fe)n.δ2(c)=new n.δ2 (λtmp.A(mapPar n,δ1,δ2 fe) n.δ2 (fsttmp);c(snd tmp)))
[0162] Coq implementation: forall(n:nat)(d1 d2:data)(f:exp d1->exp d2)(e:exp(ary n d1))(c:exp(ary n d2)->comm),C(mapPar fe)|(c)=new(fun tmp=>A(mapPar fe)|(fsttmp)';c(sndtmp)).
[0163] Rule: C(mapSeq n,δ1,δ2 fe)n.δ2(c)=new n.δ2 (λtmp.A(mapSeq n,δ1,δ2 fe) n.δ2 (fsttmp);c(snd tmp)))
[0164] Coq implementation: forall(n:nat)(d1 d2:data)(f:exp d1->exp d2)(e:exp(ary n d1))(c:exp(ary n d2)->comm),C(mapSeq fe)|(c)=new(fun tmp=>A(mapSeq fe)|(fsttmp)';c(sndtmp)).
[0165] Rule: C(reduceSeq n,δ1,δ2 fie) δ2 (c)
[0166] =C(e1) n.δ1 (λx.C(i) δ2 (λy.reduceSeql n,δ1,δ2 (λx'y'o.A(fx'y') δ2 (o))yxc))
[0167] Coq implementation: forall(n:nat)(d1 d2:data)(f:exp d1->exp d2->exp d2)(i:exp d2)(e:exp(ary n d1))(c:exp d2->comm),C(reduceSeq fie)|(c)=C(e)|(fun x=>C(i)|(fun y=>reduceSeql(fun x'y'o=>A(fx'y')|(o))yxc)).
[0168] Rules: C(zip n,δ1,δ2 e1 e2) n.δ1×δ2 (c) = C(e1) n.δ1 (λx.C(e2) n.δ2 (λy.c(zip n,δ1,δ2 xy)))
[0169] Coq implementation: forall(n:nat)(d1 d2:data)(e1:exp(ary n d1))(e2:exp(ary n d2))(c:exp(ary n(tensor d1 d2))->comm),C(zip e1 e2)|(c)=C(e1)|(fun x=>C(e2)|(fun y=>c(zip xy))).
[0170] Rule: C(Pair n,δ1,δ2 e1 e2) δ1×δ2 (c) = C(e1) n.δ1(λx.C(e2) n.δ1 (λy.c(Pair n,δ1,δ2 xy)))
[0171] Coq implementation: forall(d1 d2:data)(e1:exp d1)(e2:exp d2)(c:exp(tensor d1 d2)->comm),C(@Pair d1 d2 e1 e2)|(c)=C(e1)|(fun x=>C(e2)|(fun y=>c(@Pair d1 d2x y))).
[0172] Rule: C(Fst δ1×δ2 e) δ2 (c) = C(e) δ1×δ2 (λx.c(Fst δ1×δ2 x))
[0173] Coq implementation: forall(d1 d2:data)(e:exp(tensor d1 d2))(c:exp d1->comm),C(@Fst d1 d2 e)|(c)=C(e)|(fun x=>c(@Fst d1 d2 x)).
[0174] Rule: C(Snd δ1×δ2 e) δ2 (c) = C(e) δ1×δ2 (λx.c(Snd δ1×δ2 x))
[0175] Coq implementation: forall(d1 d2:data)(e:exp(tensor d1 d2))(c:exp d2->comm),C(@Snd d1 d2 e)|(c)=C(e)|(fun x=>c(@Snd d1 d2 x)).
[0176] The second category is the formalization of the low-level for loop implementation of high-level imperative macros in Coq. There are three main ones, namely mapParl, mapSeql and reduceSeql, as follows:
[0177] Definition mapParl(n:nat)(st:data)(f:(exp s)->(acc t)->comm)
[0178] (xs:(exp(ary ns)))(out:(acc(ary nt))):=
[0179] parfor nt out(fun i=>fun o=>f(fun_idx ns xs i)o).
[0180] Definition mapSeql(n:nat)(st:data)(f:(exp s)->(acc t)->comm)
[0181] (xs:(exp(ary ns)))(out:(acc(ary nt))):=
[0182] seqfor n(fun i=>f(fun_idx ns xs i)(idxAcc nt out i)).
[0183] Definition reduceSeql(n:nat)(d1 d2:data)(f:(exp d1)->(exp d2)->(accd2)->comm)
[0184] (init:exp d2)(xs:exp(ary n d1))(c:exp d2->comm):=
[0185] new d2(fun ac=>
[0186] let(vw,vr):=ac in
[0187] seq(assign d2(vw,init),
[0188] seq(seqfor n(fun i=>f(fun_idx n d1 xs i)vr vw),
[0189] c vr))).
[0190] The third type of conversion rules are mainly used to convert commands, left values, and right values into corresponding imperative codes.
[0191] In code display translation, the DPIA identifier needs to be converted into the output program identifier through environment mapping. The present invention formally defines the environment type storing the mapping and the corresponding translation function in Coq as follows:
[0192] Definition env:=list(string*string).
[0193] Fixpoint getvar(s:string)(eta:env):string:=
[0194] match eta with
[0195] |(x,y)::tl=>ifstring_dec sx then y else getvar s tl
[0196] |nil=>""
[0197] end.
[0198] For example, getvar "a" ["a", "A"] = "A", which means that the identifier "a" in DPIA is translated into "A".
[0199] The display translation of the receiver and expression variables in the code display translation needs to be translated through their corresponding access paths and variable names. The present invention constructs the display translation function of the access path type and the expression variable for this requirement. The formal definition of the access path in Coq is as follows:
[0200] Inductive path:=Aidx(i:nat)|Sacc(p:string).
[0201] Definition pslist:=listpath.
[0202] pslist is the access path of the expression. The display translation of the expression variable needs to be realized through the expression variable name and the corresponding access path. The present invention constructs the function in Coq as follows:
[0203] Fixpoint applyps(v:string)(ps:pslist):string:=
[0204] matchps with
[0205] |nil=>v
[0206] |(Aidx i)::tl=>applyps(v)tl++"["++(writeNat i)++"]"
[0207] |(Sacc p)::tl=>applyps(v++p)tl
[0208] end.
[0209] For example, apply "x" [Aidx 3] = "x [3]" means accessing the third element in the x array.
[0210] Here is the formal definition of commands, receivers, and expression translators in Coq:
[0211] CodeGen comm :env->comm->string.
[0212] CodeGen acc :forall(p:phrase),env->pslist->p->string.
[0213] CodeGen exp :forall(p:phrase),env->pslist->p->string.
[0214] CodeGen data :data->string->string.
[0215] CodeGen comm The translator takes a command as a parameter and outputs its displayed translation as a string. env is a parameter that contains the correspondence between the DPIA identifier and the translated generated code identifier. acc The translator takes a receiver as a parameter and outputs its displayed translation as a string. exp The translator takes an expression as a parameter and outputs its explicit translation as a string. data The translator accepts a data type and a variable name and returns the declaration of the variable, which is mainly used for the declaration of temporary variables. This example builds CodeGen in Coq data The formal definition of the specific implementation is as follows:
[0216] Fixpoint CodeGen_data(d:data)(s:string):string:=
[0217] match dwith
[0218] |num=>"float"++s
[0219] |idx n=>"int"
[0220] |ary nd=>(CodeGen_data d(s++"["++(writeNat n)++"]"))
[0221] |tensor d1 d2=>"
[0222] struct{
[0223] "++(CodeGen_data d1 s)++"x1;
[0224] "++(CodeGen_data d2 s)++"x2;
[0225] }"
[0226] end.
[0227] Here is the command translation rule formalized in Coq:
[0228] Rules: CodeGen comm (skip,η)= / *skip* /
[0229] Coq implementation: CodeGen_comm eta skip = " / *skip* / ".
[0230] Rules: CodeGen comm (p1; p2, η) = CodeGen comm (p1,η)CodeGen comm (p2,)
[0231] Coq implementation: forall(p1 p2:comm),CodeGen_comm eta(p1';p2) cnt = (CodeGen_commetap1)++(CodeGen_comm etap2)
[0232] Rules: CodeGen comm (a:= num e)=CodeGen acc (a,η,[])“=”CodeGen exp (e,η,[]);
[0233] Coq implementation: forall(a:acc num)(e:exp num),CodeGen_comm eta(a|:=e)=(CodeGen_acc(acc num)eta[]a)++"="++(CodeGen_exp(exp num)eta[]e)++";".
[0234] Rules: CodeGen comm (new δ (λv.p),η)=CodeGen data [δ](v); CodeGen comm (p(v,v),v::η)
[0235] Coq implementation: forall(d:data)(p:acc d*exp d->comm),CodeGen_comm eta(new p)=(CodeGen_data d)"v"++";"++(CodeGen_comm(("v","v")::eta)(p(vwt"v",vrd"v"))).
[0236] Rules: CodeGen comm (for n(λi.p),η)=for(int i=0;i <n;i+=1){CodeGen comm (p,i::η)}
[0237] Coq implementation: forall(n:nat)(p:exp(idx n)->comm),CodeGen_comm eta(seqfor np)="for(int i=0; i <n;i+=1){"++(CodeGen_comm(("i","i")::eta)(p(vrd"i")))++"}".
[0238] Rules: CodeGen comm (parforn,δa(λi op),η)=
[0239] #pragma omp parallel for
[0240] for(int i=0; i <n;i+=1){CodeGen comm (p[idxAcc aio],i::η,)}
[0241] Coq implementation: forall(n:nat)(d:data)(a:acc(ary nd))(p:exp(idx n)->acc d->comm),CodeGen_comm eta(parfor nd ap)=
[0242] "#pragma omp parallel for
[0243] for(int i=0; i <n;i+=1){"++(CodeGen_comm(("i","i")::eta)(p(vrd"i")(a'[(vrd"i"])))++"}".
[0244] Here is the translation rule for lvalues formalized in Coq:
[0245] Rule: CodeGen acc[δ] (x, η, p) = η(x)(rev p)
[0246] Coq implementation: forall (d:data) (x:string) (ps:pslist), CodeGen_acc (acc d) eta ps (vwt x) = applyps (getvar x eta) (rev ps).
[0247] CodeGen acc[δ] (idxAcc n,δ1 (a i, η, p) = CodeGen acc [n.δ](a, η, CodeGen exp [idx[n]](i, η, [])::ps))
[0248] Coq implementation: forall (d:data) (n:nat) (a:acc(ary n d)) (i:exp(idx n)) (ps:pslist), CodeGen_acc (acc d) eta ps (a'[i]) = CodeGen_acc (acc(ary n d)) eta (Aidx (CodeGen_exp (exp(idx n)) eta [] i)::ps) a.
[0249] Rule: CodeGen acc[δ1] (pairAcc1 δ1,δ2 (a, η, p) = CodeGen acc[δ1×δ2] (a, η,.x1::ps)
[0250] Coq implementation: forall (d1 d2:data) (a:acc(tensor d1 d2)) (ps:pslist), CodeGen_acc (acc d1) eta ps (a.x1) = CodeGen_acc (acc(tensor d1 d2)) eta (Sacc ".x1"::ps) a.
[0251] Rule: CodeGen acc[δ2] (pairAcc2 δ1,δ2 (a, η, p) = CodeGen acc[δ1×δ2] (a, η,.x2::ps)
[0252] Coq implementation: forall(d1 d2:data)(a:acc(tensor d1 d2))(ps:pslist),CodeGen_acc(acc d2)eta ps(a.x2)=CodeGen_acc(acc(tensor d1 d2))eta(Sacc".x2"::ps)a.
[0253] Rules: CodeGen acc[δ1] (zipAcc1 δ1,δ2 a,η,i::p)=CodeGen acc[n.(δ1×δ2)] (a,η,i::.x1::ps)
[0254] Coq implementation: forall(n:nat)(d1 d2:data)(a:acc(ary n(tensor d1 d2)))(ps:pslist)(i:nat),CodeGen_acc(acc(ary n d1))eta(Aidx i::ps)(zipAcc1 a)=CodeGen_acc(acc(ary n(tensor d1) d2)))eta(Aidx i::Sacc".x1"::ps)a.
[0255] Rules: CodeGen acc[δ2] (zipAcc2 δ1,δ2 a,η,i::p)=CodeGen acc[n.(δ1×δ2)] (a,η,i::.x2::ps)
[0256] Coq implementation: forall(n:nat)(d1 d2:data)(a:acc(ary n(tensor d1 d2)))(ps:pslist)(i:nat),CodeGen_acc(acc(ary n d2))eta(Aidx i::ps)(zipAcc2 a)=CodeGen_acc(acc(ary n(tensor d1) d2)))eta(Aidx i::Sacc".x2"::ps)a.
[0257] Here is the translation rule for rvalues formalized in Coq:
[0258] Rules: CodeGen exp[δ] (x,η,p)=η(x)(rev p)
[0259] Coq implementation: forall(d:data)(x:string)(ps:pslist),CodeGen_exp(exp d)eta ps(vrd x)=applyps(getvar x eta)(rev ps).
[0260] Rules: CodeGen exp[num] (neg e,η,[])=(-CodeGen exp[num] (e,η,[]))
[0261] Coq implementation: forall(e:exp num),CodeGen_exp(exp num)eta[](negate e)="(-"++(CodeGen_exp(exp num)eta[]e)++")".
[0262] Rules: CodeGen exp[num] (e1+e2,η,[])=(CodeGen exp[num] (e1,η,[])+CodeGen exp[num] (e2,η,[]))
[0263] Coq implementation: forall(e1 e2:exp num),CodeGen_exp(exp num)eta[](add e1 e2)="("++(CodeGen_exp(exp num)eta[]e1)++"+"++(CodeGen_exp(exp num)eta[]e2)++")".
[0264] Rules: CodeGen exp[n.(δ1×δ2)] (zip n,δ1,δ2 e1 e2,η,i::.x1::p)=CodeGen exp[n.δ1] (zip n,δ1,δ2 e1 e2,η,i::p)
[0265] Coq implementation: forall(e1 e2:exp num),CodeGen_exp(exp num)eta[](mul e1 e2)="("++(CodeGen_exp(exp num)eta[]e1)++"*"++(CodeGen_exp(exp num)eta[]e2)++")".
[0266] Rules: CodeGen exp[n.(δ1×δ2)] (zip n,δ1,δ2e1 e2, η, i ::.x1 :: p) = CodeGen exp[n.δ1] (zip n,δ1,δ2 e1 e2, η, i :: p)
[0267] Coq implementation: forall (n:nat) (d1 d2:data) (e1:exp(ary n d1)) (e2:exp(ary n d2)) (i:nat) (ps:pslist), CodeGen_exp(exp(ary n(tensor d1 d2))) eta (Aidx i :: Sacc ".x1" :: ps) (zip e1 e2) = CodeGen_exp(exp(ary n d1)) eta (Aidx i :: ps) e1.
[0268] Rule: CodeGen exp[n.(δ1×δ2)] (zip n,δ1,δ2 e1 e2, η, i ::.x2 :: p) = CodeGen exp[n.δ2] (zip n,δ1,δ2 e1 e2, η, i :: p)
[0269] Coq implementation: forall (n:nat) (d1 d2:data) (e1:exp(ary n d1)) (e2:exp(ary n d2)) (i:nat) (ps:pslist), CodeGen_exp(exp(ary n(tensor d1 d2))) eta (Aidx i :: Sacc ".x2" :: ps) (zip e1 e2) = CodeGen_exp(exp(ary n d2)) eta (Aidx i :: ps) e2.
[0270] Rule: CodeGen exp[δ1×δ2] (pair δ1,δ2 e1 e2, η,.x1 :: p) = CodeGen exp[δ1] (e1, η, p)
[0271] Coq implementation: forall (n m:nat) (d1 d2:data) (e1:exp d1) (e2:exp d2) (ps:pslist), CodeGen_exp(exp(tensor d1 d2)) eta (Sacc ".x1" :: ps) (Pair e1 e2) = CodeGen_exp(exp d1) eta ps e1.
[0272] Rule: CodeGenexp[δ1×δ2] (Pair δ1,δ2 e1 e2, η, x2::p) = CodeGen exp[δ2] (e2, η, p)
[0273] Coq implementation: forall (n m:nat) (d1 d2:data) (e1:exp d1) (e2:exp d2) (ps:pslist), CodeGen_exp (exp (tensor d1 d2)) eta (Sacc ".x2" :: ps) (Pair e1 e2) = CodeGen_exp (exp d2) eta ps e2.
[0274] Rule: CodeGen exp[δ1] (Fst δ1,δ2 e, η, p) = CodeGen exp[δ1×δ2] (e, η,.x1::p)
[0275] Coq implementation: forall (d1 d2:data) (e:exp (tensor d1 d2)) (ps:pslist), CodeGen_exp (exp d1) eta ps (Fst e) = CodeGen_exp (exp (tensor d1 d2)) eta (Sacc ".x1" :: ps) e.
[0276] Rule: CodeGen exp[δ2] (Snd δ1,δ2 e, η, p) = CodeGen exp[δ1×δ2] (e, η,.x2::p)
[0277] Coq implementation: forall (d1 d2:data) (e:exp (tensor d1 d2)) (ps:pslist), CodeGen_exp (exp d2) eta ps (Snd e) = CodeGen_exp (exp (tensor d1 d2)) eta (Sacc ".x2" :: ps) e.
[0278] Rule: CodeGen exp[δ] (fun_idx n,δ e i, η, p) = CodeGen exp[n.δ] (e, η, CodeGen exp[idx[n]] (i, η, [])::p)
[0279] Coq implementation: forall(d:data)(n:nat)(e:exp(ary nd))(i:exp(idx n))(ps:pslist),CodeGen_exp(expd)eta ps(e|[i])=CodeGen_exp(exp(ary nd))eta(Aidx(CodeGen_exp(exp(idx n))eta[]i)::ps)e.
[0280] 3) Code conversion is realized by using rewriting technology. In Coq, matrix code generation is realized by using rewriting technology through custom rewriting strategies.
[0281] LtacAsimpl1:=
[0282] rewrite? AmapSeq,? AmapPar,? AreduceSeq,? AreverseSeq,? AcopySeq,? AtransPar.
[0283] LtacAsimpl2:=
[0284] rewrite? Azip,? Afst,? Asnd;
[0285] rewrite? Aadd,? Amul,? Asqrt,? Aabs,? Avar_ary,? Avar_tensor,? Avar_num.
[0286] Ltac Csimpl1:=
[0287] repeat(rewrite?CmapPar,?CmapSeq,?CreduceSeq,?CreverseSeq,?CcopySeq,?AtransPar);
[0288] try unfold mapParl,mapSeql,reduceSeql,reverseSeql,copySeql,transParl.
[0289] Ltac Csimpl2:=rewrite? Czip,? Cfst,? Csnd;rewrite? Cmul,? Cadd,? Csqrt,? Cabs,? Cvar.
[0290] Ltac CGsimpl:=
[0291] rewrite? CGskip,? CGseq,? CGassign,? CGassign_ary,? CGnew,? CGfor,? CGparfor;
[0292] repeat cnt_simpl;rewrite? CGzip1,? CGzip2,? CGfst,? CGsnd;
[0293] rewrite? CGidxAcc,? CGidx,? CGmul,? CGadd,? CGsqrt,? CGabs,
[0294] ? CGaddidx,? CGmulidx,? CGsubidx,? CGidxpred,? CGaccvar,? CGexpvar.
[0295] LtacAllsimpl:=Asimpl1;Asimpl2;Csimpl1;Csimpl2;CGsimpl.
[0296] Ltac strnat_simpl:=simpl;rewrite? wtStrNat;simpl.
[0297] Phase 2: Improvement and expansion of matrix code generation technology:
[0298] 1) Solve the problem of repeated variable naming:
[0299] The variable name generation function is defined in Coq as follows:
[0300] Definition vstr(cnt:nat):string:="v"++(writeNat cnt).
[0301] Definition istr(cnt:nat):string:="i"++(writeNat cnt).
[0302] Among them, vstr is the variable name of the temporary variable, istr is the variable name of the loop variable in the loop, and both are generated by a natural number. The natural number depends on the number of variable declarations in the program. For example, when the functional matrix code is initially input, the natural number is 0. As the main sentence of the command is translated, the natural number gradually increases to the number of temporary variable declarations in the translated command. Since the number of temporary variable declarations in different commands is different, a function for calculating the number of variable declarations is defined. The function takes the command as input and the number of temporary variable declarations as output. The skip command does not perform any operation, so there is no temporary variable declaration. The number of temporary variables in the seq command is the sum of the number of temporary variable declarations in the two commands executed sequentially. Assign is an assignment statement without temporary variable declaration. The new command declares a new variable. Both seqfor and parfor contain loop variable declarations.
[0303] Fixpoint cnt_var(c:comm):=
[0304] match c with
[0305] |skip=>0
[0306] |seq(c1,c2)=>cnt_var c1+cnt_var c2
[0307] |assign ac=>0
[0308] |@new tp=>1+cnt_var(p(vwt"",vrd""))
[0309] |seqfor np=>1+cnt_var(p(vrd""))
[0310] |parfor nt ap=>1+cnt_var(p(vrd"")(vwt""))
[0311] end.
[0312] The above definition is used to combine the variable naming rules with the command display translation, as follows:
[0313] CodeGen comm :env->comm->nat->string.
[0314] CodeGen comm (skip,η,cnt)= / *skip* /
[0315] CodeGen comm (p1; p2, η, cnt) = CodeGencomm (p1,η,cnt)CodeGen comm (p2,η,cnt_varp1+cnt)
[0316] CodeGen comm (a:= num e,η,cnt)=CodeGen acc (a,η,[])“=”CodeGen exp (e,η,[]);
[0317] CodeGen comm (new δ (λv.p),η,cnt)=let v:=vstr cnt in
[0318] CodeGen data[δ] (v) CodeGen comm (p(vwtδv,vrdδv),v::η),(cnt+1))
[0319] CodeGen comm (for n (λi.p),η,cnt)=let i:=istr cnt in
[0320] for(int i=0; i <n;i+=1){CodeGen comm (p,i::η,cnt+1)}
[0321] CodeGen comm (parfor n,δ a(λi op),eta,cnt)=let i:=istr cnt in
[0322] #pragma omp parallel for
[0323] for(int i=0; i <n;i+=1){CodeGen comm (p[idxAcc aio],i::η,cnt+1)}
[0324] 2) Expand primitives and rules:
[0325] We can generate the corresponding code for matrix operations through this compilation technology. At the same time, the rule has good extensibility. By extending the rule, we can rewrite different vector and matrix expressions. The following is the corresponding extension we made based on the vector operation functions (vector norm, vector absolute value sum, vector assignment, vector reversal, vector assignment and matrix transposition).
[0326] Primitive extension:
[0327] idxpred: for all (n: nat), exp(idx n).
[0328] addidx subidx mulidx dividx: for all (n: nat), (exp(idx n)) -> (exp(idx n)) -> (exp(idx n)).
[0329] reverse: for all [n: nat][s: data], (exp(ary n s)) -> (exp(ary n s)).
[0330] reverseSeq: for all [n: nat][s: data], (exp(ary n s)) -> (exp(ary n s)).
[0331] copy: for all [n: nat][s: data], (exp(ary n s)) -> (exp(ary n s)).
[0332] copySeq: for all [n: nat][s: data], (exp(ary n s)) -> (exp(ary n s)).
[0333] transPar: for all [m n: nat][s: data], (exp(ary m(ary n s))) -> (exp(ary n(ary m s))).
[0334] reverseSeql[n: nat][d1: data](xs: exp(ary n d1))(out: acc(ary n d1)) :=
[0335] seqfor n(fun i => assign d1(idxAcc out i, fun_idx xs(subidx n(idxpred n) i))).
[0336] copySeql[n: nat][d1: data](xs: exp(ary n d1))(out: acc(ary n d1)) :=
[0337] seqfor n (fun i => assign d1(idxAcc out i, fun_idx xs i)).
[0338] transParl[m n:nat][d1:data](xs:exp(ary m(ary n d1)))(out:acc(ary n(ary m d1))) :=
[0339] parfor n(ary m d1) out (fun i => fun o => seqfor m(fun j =>
[0340] assign d1(idxAcc(idxAcc out i) j, fun_idx(fun_idx xs j) i)).
[0341] Rule extension
[0342] A(sqrt e) num (a) = C(e) num (λx.a := num (sqrt x))(square root)
[0343] A(abs e) num (a) = C(e) num (λx.a := num (abs x))(absolute value)
[0344] A(reverseSeq n,δ e) n.δ (a) = C(e) n.δ (λx.reverseSeql n,δ x a)
[0345] A(copySeq n,δ e) n.δ (a) = C(e) n.δ (λx.copySeql n,δ x a)
[0346] A(transPar m,n,δ e) n.δ (a) = C(e) n,m.δ (λx.transParl n,m,δ x a)
[0347] C(sqrt e) num c = C(e) num (λx.c(sqrt x))
[0348] C(abs e) num c=C(e) num (λx.c(abs x))
[0349] C(reverseSeq n,δ in) n.δ (c)=new n.δ2 (λtmp.A(reverseSeq n,δ faith) n.δ2 (fst tmp);c(sndtmp)))
[0350] C(copySeq n,δ in) n.δ2 (c)=new n.δ (λtmp.A(copySeq n,δ in) n.δ (fst tmp);c(snd tmp)))
[0351] C(transPar m,n,δ in) m.n.δ2 (c)=new mnδ(λtmp.A(transPar m,n,δ in) m.n.δ (fst tmp);c(sndtmp)))
[0352] CodeGen exp[num] (sqrt e,η,[])=(sqrt CodeGen exp[num] (e,η,[]))
[0353] CodeGen exp[num] (abs e,η,[])=(abs CodeGen exp[num] (e,η,[]))
[0354] CodeGen exp[idx[n]] (idxpred n,η,[])=(n-1)
[0355] CodeGen exp[idx[n]] (addidx e1 e2,η,[])=(CodeGen exp[idx[n]] (e1,η,[])+CodeGen exp[idx[n]] (e2,η,[]))
[0356] CodeGen exp[idx[n]]] (subidx e1 e2,η,[])=(CodeGen exp[idx[n]] (e1,η,[])-CodeGenexp[idx[n]] (e2,η,[]))
[0357] CodeGen exp[idx[n]] (mulidx e1 e2,η,[])=(CodeGen exp[idx[n]] (e1,η,[])*CodeGen exp[idx[n]] (e2,η,[]))
[0358] The original DPIA cannot realize code generation for block matrices because the initial value needs to be assigned in reduceSeql, which is of type num in vector operations. However, block matrices are assigned initial values to small matrices when using reduceSeql, and in the translation rules of the assignment command assign, there are only rules for the num type. For this reason, we have added new rules for array types.
[0359] CodeGen comm (a: = n.δe,η,cnt) = CodeGen comm (for n(λi.idxAcc ai:=δfun_idxe i),η,cnt)
[0360] This rule is an explicit translation of the array type assignment command, converting the array type assignment into a sequentially executed for loop, whose loop body is represented by "a[i] = e[i];", and whose semantics is to assign values to the elements of the array in sequence.
[0361] After adding this rule, we can complete the block matrix multiplication code generation, where the sizes of the two matrices are m1*n1 and p1*n1, and the internal blocks are m*n and p*n.
[0362] #pragma omp parallel for
[0363] for(int i0=0;i0 <m1;i0+=1)
[0364] {#pragma omp parallel for
[0365] for(int i1=0; i1 <p1;i1+=1)
[0366] {float v2[n1][m2][p2];
[0367] for(int i3=0; i3 <n1;i3+=1)
[0368] {for(int i4=0; i4 <m2;i4+=1)
[0369] {for(int i5=0;i5<p2;i5+=1)
[0370] {float v6[n2];
[0371] for(int i7=0;i7<n2;i7+=1)
[0372] {v6[i7]=(A1[i0][i3][i4][i7]*B1[i1][i3][i5][i7]);}
[0373] float v8;
[0374] v8=0;
[0375] for(int i9=0;i9<n2;i9+=1)
[0376] {v8=(v6[i9]+v8);}
[0377] v2[i3][i4][i5]=v8;}}}
[0378] float v10[m2][p2];
[0379] for(int i11=0;i11<m2;i11+=1)
[0380] {for(int i12=0;i12<p2;i12+=1)
[0381] {v10[i11][i12]=mzero[i11][i12];}}
[0382] for(int i11=0;i11<n1;i11+=1)
[0383] {#pragma omp parallel for
[0384] for(int i12=0;i12<m2;i12+=1)
[0385] {for(int i13=0;i13<p2;i13+=1)
[0386] {v10[i12][i13]=(v2[i11][i12][i13]+v10[i12][i13]);}}}
[0387] #pragma omp parallel for
[0388] for(int i14=0; i14 <m2;i14+=1)
[0389] {for(int i15=0; i15 <p2;i15+=1)
[0390] {out[i0][i1][i14][i15]=v10[i14][i15];}}}}
[0391] The correctness of such a complex imperative program is difficult to judge directly, so it is necessary to formally verify the generation process of such code to ensure the reliability of the generated matrix code. Figure 6 It represents some basic functional matrix programs that can generate imperative code. The generated matrix C program was subjected to corresponding speed tests. The processor used in this test was Intel i7-8565U (4 cores, 8 cores). This test is mainly divided into the generated sequential execution C code and the C code with OpenMp parallelization. The main test results are as follows Figure 7 and Figure 8 shown.
[0392] The parts not described in detail in the present invention are all known technologies to those skilled in the art. The preferred specific embodiments of the present invention are described in detail above. It should be understood that a person skilled in the art can make many modifications and changes based on the concept of the present invention without creative work. Therefore, any technical solution that can be obtained by a person skilled in the art through logical analysis, reasoning or limited experiments based on the concept of the present invention on the basis of the prior art should be within the scope of protection determined by the claims.
Claims
1. A formal description method of matrix code generation technology based on Coq, characterized in that: The method comprises the following steps: Step S1, formally describe data types, functional primitives, and command primitives; Step S2, formally describing the code conversion rules, wherein the code conversion rules include: conversion from high-level functional primitives to high-level imperative macros, conversion from high-level imperative macros to low-level for loops, and explicit conversion between commands and receivers and expressions; Step S3, using the rewrite function in Coq to write an automated rewriting strategy to achieve matrix code generation; Step S4, construct a variable name automatic generation function, establish and prove the number of variable declarations in different commands, and then introduce the lemma into the command display translation rule to achieve no repeated declaration of new variable names when translating commands; Step S5, expanding the original sentence and the rules to realize the generation of more matrix codes, wherein the original sentence includes: matrix assignment, inversion, transposition, scalar function primitives and index function primitives; In step S1, the data types include: value, index, array, duality and corresponding acceptors and expressions; the functional primitives include: scalar functional primitive, array functional primitive and dual functional primitive; the command primitives include: empty command, sequential execution, assignment, new variable declaration, sequential for loop and parallel for loop; Formal description of data types, including: Use the commonly used major categories in Coq, Type, to implement data types and phrase types, kind, and phrase types; Data types include: numeric type num, index type idx that accepts a natural number as a parameter, array type ary that accepts a natural number and a data type as a parameter, dual type tensor that accepts two data types as parameters; receiver type and expression type that accept a data type as an input parameter and return a phrase type; The scalar functional primitive is formally described, specifically including: The negation function neg accepts a numerical expression parameter and outputs a numerical expression; The addition function add, the multiplication function mul, the division function div, and the subtraction function sub accept two numerical expression parameters and output a numerical expression; The array functional primitive is formally described, including: The mapping function map accepts a natural number n, data types s and t, a mapping function, and an array of expressions of length n and element type s as parameters, and the output type is an array expression of length n and data type t, where the mapping function map accepts a mapping function that accepts an expression of data type s as a parameter and outputs an expression of data type t; It is stipulated that the sequential mapping function mapSeq and the parallel mapping function mapPar have the same type as the mapping function map; The reduction function reduce accepts a natural number, data types s and t, a calculation function, an initial value, an array of expressions of length n and element type s as parameters, and outputs an expression type of t, wherein the calculation function accepts the expression type of s and the expression type of t, and uses the expression type of t as the output type, and the initial value is the expression type of t; The type of the sequentially executed reduction function reduceSeq is the same as the type of the reduction function reduce; The combination function zip accepts a natural number, data types s and t, an array of expressions of length n and element type s, and an array of expressions of length n and data type t as parameters, and the output type is an array expression of length n and element type the dual of s and t; The query function fun_idx accepts a natural number n, data type t, an array expression of length n and data type t, and an index expression less than n as a parameter, and outputs an expression of type t; The receiver combination left component function zipAcc1 accepts a natural number n, data types s and t, and a receiver of the dual type of length n and data types s and t as input, and outputs a receiver of length n and data type s; The receiver combination right component function zipAcc2 accepts a natural number n, data types s and t, and a receiver of the dual type of length n and data types s and t as input, and outputs a receiver of length n and data type t; The receiver query function idxAcc accepts a natural number n, a data type t, an array receiver of length n and data type t, and an index expression less than n as parameters, and the output is a receiver of data type t; The dual functional primitives are formally described, including: The dual function Pair accepts data types s and t, an expression of data type s, and an expression of data type t as parameters, and outputs an expression of the dual of data types s and t; The left component function Fst accepts data types s and t, and expressions of the dual of data types s and t as parameters, and outputs an expression of data type s; The right component function Snd accepts data types s and t, and expressions of data types that are the dual of s and t as parameters, and outputs an expression of data type t; The receiver dual left component function pairAcc1 accepts data types s and t, receivers of data types s and t's dual as parameters, and outputs a receiver of data type s; The receiver dual right component function pairAcc2 accepts data types s and t, receivers of data types s and t's dual as parameters, and outputs a receiver of data type t; Among them, the command primitive is formally described, including: The sequence statement seq accepts a pair of command types as parameters, and the output type is the command type; The empty statement skip is a command type; The assignment statement assign accepts a data type d and the pair of d's receiver and expression as parameters, and the output type is the command type; The new variable declaration statement accepts a data type t and a function as parameters, and the output type is a command, wherein the function accepted by the new variable declaration statement takes as parameters the receiver whose parameter is t and the dual of the expression, and the output type is a command type; The sequential loop seqfor accepts a natural number n and a function as parameters, and the output type is a command, wherein the function accepted by the sequential loop seqfor accepts an index expression less than n as input, and its output type is a command type; The parallel loop parfor accepts a natural number n, a data type t, and a function as parameters, and the output type is a command type, wherein the function accepted by the parallel loop parfor accepts an array receiver with a length of n and a data type of t, an index expression less than n, and a receiver with a data type of t as input, and the output type is a command; In the step S2, the conversion from high-level functional primitives to high-level imperative macros is implemented by an Acceptor-passing translator and a Continuation-passing translator; The commands and receivers are converted into explicit expressions by a command translator, a receiver translator and an expression translator; The formal description of the Acceptor-passing translator includes: Translator A accepts data type d, an expression of data type d and a receiver as parameters, and its output type is command type; Formal description of the Acceptor-passing rules, including: The numeric expression e and the receiver a are converted by the translator A. The array type expression e and the receiver a are converted by the translator A. The dual type expression and receiver go through the transformation rules of translator A; The negation of a numeric expression is subject to the conversion rules of translator A; The addition of numeric expressions e1 and e2 is processed by the conversion rules of translator A. The multiplication of numeric expressions e1 and e2 is processed by the conversion rules of translator A. The array expression after sequential mapping mapSeq passes through the conversion rules of translator A; The array expression acted upon by the parallel mapping mapPar is subjected to the conversion rules of the translator A; The array expression acted upon by the sequential reduction reduceSeql passes through the conversion rules of translator A; The array expression on which the combined function zip acts is transformed by the conversion rules of translator A; The expression acted upon by the dual function Pair passes through the transformation rules of the translator A; The expression on which the left component function Fst acts passes through the transformation rules of the translator A; The expression on which the right component function Snd acts is subjected to the conversion rules of the translator A; Formal description of the Continuation-passing translator, including: The translator C accepts a data type d, an expression of data type d and a function as parameters, and the output type is a command type, wherein the translator C accepts a function whose input parameter is an expression of data type d and the output type is a command type; Formal description of Continuation-passing rules, including: The expression of data type d and the subsequent call operation c are converted by the translator C. The negation of numeric type expressions is subject to the conversion rules of translator C; The addition of numeric expressions e1 and e2 is processed by the conversion rules of translator C. The multiplication of numeric expressions e1 and e2 is subject to the conversion rules of translator C; The array expression acted upon by the parallel mapping mapPar is subjected to the conversion rules of the translator C; The array expression after sequential mapping mapSeq passes through the conversion rules of translator C; The array expression acted upon by the sequential reduction reduceSeql passes through the conversion rules of the translator C; The array expression acted upon by the combined function zip is converted by the conversion rules of the translator C; The expression acted upon by the dual function Pair passes through the conversion rules of the translator C; The formal description of the conversion from high-level imperative macros to low-level for loops specifically includes: The parallel mapping mapParl accepts a natural number n, data types s and t, a function, an array expression xs of length n and data type s, and a receiver out of length n and data type t as parameters, and uses a parallel loop parfor to apply the mapping function f to each element of xs and store the result in out, wherein the parallel mapping mapParl accepts a function whose input parameters are an expression of data type s and a receiver of data type y; The sequence map mapSeql accepts a natural number n, data types s and t, a function, an array expression xs of length n and data type s, and a receiver out of length n and data type t as parameters, and uses a sequence loop seqfor to apply the mapping function f to each corresponding element of xs and out, wherein the sequence map mapSeql accepts a function whose input parameters are an expression of data type s and a receiver of data type t; The sequential reduction reduceSeql accepts a natural number n, data types d1 and d2, a function, an initial value, an array expression xs of length n and data type d1, and a subsequent processing function c, and is implemented by creating a new temporary variable, assigning the initial value to the variable, and then applying f to each element in the array and the temporary variable through a sequential loop seqfor, and finally applying the subsequent processing function to the temporary variable, wherein the sequential reduction reduceSeql accepts a function whose input parameters are expressions of data types d1 and d2 and a receiver of data type d2, the initial value accepted by the sequential reduction reduceSeql is an expression of data type d2, the subsequent processing function c accepted by the sequential reduction reduceSeql accepts a parameter of data type d2, and its output is a command type.
2. The method for formally describing matrix code generation technology based on Coq according to claim 1, characterized in that: The explicit conversion between commands and receivers and expressions, the formal description of which includes: the formalization of the translation rules of commands in Coq, the formalization of the translation rules of receivers in Coq, and the formalization of the translation rules of expressions in Coq, where: The translation rules of the commands are formalized in Coq, specifically including: The explicit translation rules for the empty statement skip; Display translation rules for sequential execution statements seq; The explicit translation rules of the assignment statement assign; The explicit translation rules for the new variable generation statement new; The explicit translation rules of the sequential loop statement seqfor; Explicit translation rules for the parallel loop statement parfor; The receiver translation rules are formalized in Coq, specifically including: Explicit translation rules for receivers of data type d with environments and paths; The display translation rules of the array receiver acted on by the receiver query function idxAcc; The explicit translation rule of the receiver on which the receiver dual left component function pairAcc1 acts; The explicit translation rule of the receiver acting on the receiver dual right component function pairAcc2; The display translation rule of the receiver to which the receiver combination left component function zipAcc1 acts; The display translation rule of the receiver to which the receiver combination right component function zipAcc2 acts; The translation rules of the above expressions are formalized in Coq, specifically including: Rules for explicit translation of expressions of data type d with environments and paths; Take the explicit translation rules for negative expressions; Explicit translation rules for additive expressions; Explicit translation rules for multiplication expressions; The explicit translation rules for expressions that the zip function acts on; Explicit translation rules for expressions acted upon by the dual function Pair; Explicit translation rules for expressions on which the dual left component function Fst acts; Explicit translation rules for expressions acting on the dual right component Snd; Query the explicit translation rules of the expression acted upon by the function fun_idx.
3. The method for formally describing matrix code generation technology based on Coq according to claim 2, characterized in that: The step S3 specifically includes: Define the rewriting rules of high-level functional primitives and translator A in the automatic rewriting strategy Asimpl1; Define the remaining rewriting rules for translator A in the automatic rewriting strategy Asimpl2; Define the rewriting rules of high-level functional primitives and translator C in the automatic rewriting strategy Csimpl1; Define the remaining rewriting rules for translator C in the automatic rewriting strategy Csimpl2; Define command, lvalue, and rvalue display translation rules in the automatic rewriting strategy CGsimpl; Encapsulate Asimpl1, Asimpl2, Csimpl1, Csimpl2, and CGsimpl into the automatic rewriting strategy Allsimpl; Define the rewriting rule strnat_simpl for strings to natural numbers.
4. The method for formally describing matrix code generation technology based on Coq according to claim 3, characterized in that: The step S4 specifically includes: Define the variable name generation functions vstr and istr, which automatically generate the corresponding variable name string according to the input natural number, where vstr is a temporary variable and istr is a loop variable; Define the number of temporary variables generated in different commands. The empty statement skip command generates 0, the number generated by the sequential execution statement seq is the sum of the number of variables generated in the sequentially executed statements p1 and p2, and the assignment statement assign generates 0; The number of new variable generation statements new is the number of new variable generation statements p plus 1. The number of loops generated by the sequential loop statement seqfor is the number of loops generated by the loop body p plus 1; The number of parallel loop statements parfor generates is the number of loops p plus 1; The variable naming rules are combined with the command display translation, in which a natural number is added to the command display translator as an identification parameter, and then the command display translation rules with automatic generation of variable names for skip, seq, assign, new, seqfor, and parfor are defined.
5. The method for formally describing matrix code generation technology based on Coq according to claim 4, characterized in that: The step S5 includes extending the primitive extension and extending the rule, wherein extending the primitive extension specifically includes: Define the root function sqrt of numeric type expressions; Define the absolute value function abs of a numerical expression; Define the index expression minus 1 function idxpred; Define the summation function addidx, subtraction function subidx, multiplication function mulidx, and division function dividx of the index expression; Define the reverse function of array expression; Define the reverse order function reverseSeq of array expressions; Define the copy function of array expression; Define the sequential copy function copySeq of array expressions; Define the parallel transposition function transPar of two-dimensional array expression; Define the sequence reversal function reverseSeql implemented by the sequential for loop; Define the sequential copy function copySeql implemented by the sequential loop; Define the parallel transposition function transParl implemented by the parallel loop; The expansion of the rules specifically includes: Define the conversion rules of the root-finding function sqrt and the translator A; Define the conversion rules between the absolute value function abs and the translator A; Define the sequence reversal function reverseSeq and the conversion rules of the translator A; Define the sequential copy function copySeq and the conversion rules of translator A; Define the parallel transposition function transPar and the conversion rules of the translator A; Define the conversion rules of the root-finding function sqrt and the translator C; Define the conversion rules between the absolute value function abs and the translator C; Define the sequence reversal function reverseSeq and the conversion rules of the translator C; Define the conversion rules of the sequential copy function copySeq and the translator C; Define the parallel transposition function transPar and the conversion rules of the translator C; Define the numerical expression display translation rules for the root-finding function sqrt; Define the numerical expression display translation rules for the absolute value function abs; Define the display translation rules for index expressions that are acted upon by the index expression subtraction function idxpred; define the display translation rules for index expressions that are acted upon by the index expression sum function addidx; define the display translation rules for index expressions that are acted upon by the index expression subtraction function subidx; define the display translation rules for index expressions that are acted upon by the index expression multiplication function mulidx; define the display translation rules for array assignment statements.
