MTProto 移动协议 TL组合子的正式描述

主条目:TL 的正式描述。另见TL 语言

TL 中的组合子声明如下:

combinator-decl ::= full-combinator-id { opt-args } { args } = result-type  ;
full-combinator-id ::= lc-ident-full | _
combinator-id ::= lc-ident-ns | _
opt-args ::= { var-ident { var-ident } : [ excl-mark ] type-expr  }
args ::= var-ident-opt : [ conditional-arg-def ] [ !] type-term 
args ::= [ var-ident-opt : ] [ multiplicity * ] [{ args } ]
args ::= ( var-ident-opt { var-ident-opt } :[ !] type-term  )
args ::= [ !] type-term 
multiplicity ::= nat-term 
var-ident-opt ::= var-ident | _
conditional-arg-def ::= var-ident [ . nat-const ] ?
result-type ::= boxed-type-ident { subexpr } 
result-type ::= boxed-type-ident  < subexpr { , subexpr }>

我们将阐明这一切意味着什么。

在下文中,构造函数的字段、变量和参数都指同一事物。

可选字段声明

必填字段声明

重复

例子:

matrix {m n : #} a : m* [ n* [ double ] ] = Matrix m n;

在功能上等同于

aux_type {n : #} (_ : %Tuple double n) = AuxType n; matrix {m : #} {n : #} (a : %Tuple %(AuxType n) m) = Matrix m n; 

此外,内置类型Tuple可以Vector定义为:

tnil {X : Type} = Tuple X 0; tcons {X : Type} {n : #} hd:X tl:%(Tuple X n) = Tuple X (S n); vector {X : Type} (n : #) (v : %(Tuple X n)) = Vector X; 

实际上,以下等效条目被认为是定义Vector(即,正是此条目用于计算vector构造函数及其偏函数的名称):

vector {t : Type} # [ t ] = Vector t;

如果我们用展开它Tuple,就能得到前面的定义。

条件字段

施工

	args ::= var-ident-opt : [ conditional-arg-def ] [ !] type-term 
conditional-arg-def ::= var-ident [ . nat-const ]?

允许分配仅当前面类型为 `T` 的必填或可选字段的值不为空(或者,如果应用了#特殊的二进制位选择运算符,则其所选位不为零)时才存在的字段。示例:.

	用户 {fields:#} id:int first_name:(fields.0?string) last_name:(fields.1?string) friends:(fields.2?%(Vector int)) = User fields; 
get_users req_fields:# ids:%(Vector int) = Vector %(User req_fields)