当前位置:网站首页>Learn tla+ (XII) -- functions through examples
Learn tla+ (XII) -- functions through examples
2022-07-05 01:41:00 【Liu Xiaojing】
Functions and examples
function
TLA+ In addition to Numbers , character string , Boolean value , Model value 4 There are two basic types , There are also two complex types , Set and function . Functions form the basis of all practical complex types .
The form of the function looks the same as the operators introduced before . There are two ways to define functions :
Function == [s \in S |-> foo] \* here foo It can be any equation . Notice I'm using theta |->, No :
Function[s \in S] == foo
Infinite sets can be used in function definitions , Such as
Doubles == [n \in Nat |-> 2*n ]
Doubles[12] = 24 \* The call mode is [], instead of ()
\* multi-parameter
Sum == [x, y \in 1..10 |-> x + y] \* Limit the x,y The value of the 1..10 in
Sum[2,3] = 5
Sum[2,30] \* error,30 be not in 1..10 in
Addx(x) == [y \in Nat |-> x+y+1]
Addx(2)[3] = 6 \* This call x=2,y=3
Set of functions
The syntax of the function set is :SetOfFunctions == [A -> B], Notice I'm using theta -> , instead of |->. -> The result of needs to be used |-> To describe . for example :
S1 == {1, 2}
S2 == {3, 4}
[s \in S1 |-> S2] = [1 |-> {3, 4}, 2 |-> {3, 4}] = <<{3,4},{3,4}>> \* This is a tuple
[S -> S] = {[1 |-> 3, 2 |-> 3], [1 |-> 3, 2 |-> 4], [1 |-> 4, 2 |-> 3], [1 |-> 4, 2 |-> 4]} = {<<3, 3>>, <<3, 4>>, <<4, 3>>, <<4, 4>>} \* This is a collection ,[1 |-> 3, 2 |-> 3] Medium 1 Represents the first element ,2 For the second element , If you will 1,2 Replace with 2,3, It cannot be expressed as <<3,3>> 了 , It can only be expressed as [2|->3,3|->3]
example
1、 Given a string , Returns the number of each character . Such as : Given <<“a”,“b”,“a”>> return [a |-> 2, b |-> 1]
\* 1 Convert tuples into sets , At this time, the set contains elements of non repeating tuples 2 Traversing set elements , Count the number of each element in the tuple
Range(S) == {S[t]:t \in 1..Len(S)}
Counter(str) == [c \in Range(str) |-> Cardinality({n \in 1..Len(str) : str[n] = c})]
Counter(<<"a","b","a">>) = [a |-> 2, b|-> 1]
2、 Given a set S And number N, return N individual S Cartesian product of , Such as Op(S, 3) == S \X S \X S
Op(S,N) == [1..N -> S]
边栏推荐
- I use these six code comparison tools
- PowerShell:在代理服务器后面使用 PowerShell
- Basic operation of database and table ----- the concept of index
- 无心剑英译席慕容《无怨的青春》
- Remote control service
- Express routing, express middleware, using express write interface
- [development of large e-commerce projects] performance pressure test - Performance Monitoring - heap memory and garbage collection -39
- runc hang 导致 Kubernetes 节点 NotReady
- Delaying wages to force people to leave, and the layoffs of small Internet companies are a little too much!
- Async/await you can use it, but do you know how to deal with errors?
猜你喜欢
MATLB|多微电网及分布式能源交易
R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
Basic operation of database and table ----- phased test II
Introduction to redis (1)
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
One plus six brushes into Kali nethunter
Wechat applet: wechat applet source code download new community system optimized version support agent member system function super high income
Yyds dry inventory swagger positioning problem ⽅ formula
Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
线上故障突突突?如何紧急诊断、排查与恢复
随机推荐
One plus six brushes into Kali nethunter
Win: enable and disable USB drives using group policy
[CTF] AWDP summary (WEB)
MySQL backup and recovery + experiment
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
Codeforces Round #770 (Div. 2) ABC
无心剑英译席慕容《无怨的青春》
整理混乱的头文件,我用include what you use
Redis(1)之Redis简介
es使用collapseBuilder去重和只返回某个字段
Incremental backup? db full
Es uses collapsebuilder to de duplicate and return only a certain field
Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization
Discrete mathematics: propositional symbolization of predicate logic
[Chongqing Guangdong education] National Open University spring 2019 1042 international economic law reference questions
Global and Chinese market of portable CNC cutting machines 2022-2028: Research Report on technology, participants, trends, market size and share
微信小程序;胡言乱语生成器
Database postragesq PAM authentication