当前位置:网站首页>Tla+ through examples (XI) -- propositional logic and examples
Tla+ through examples (XI) -- propositional logic and examples
2022-07-05 01:41:00 【Liu Xiaojing】
Propositional logic and examples
Logic symbols have been used in previous examples , Mainly \A ,\E, => Equisign .
\E
E yes "exists" The first letter of , It means existence . When writing down \E x \in S : P(x) It means in S There is at least one x bring P It's true . use \E The return value of the first expression is Boolean .
\E x \in {1,2} : x > 0 by true
\E x \in {} : x > 0 by false
\E It means that there is an element that meets the condition ,~\E There is no element that meets the condition
\A
A Of "All" The first letter of , When writing down \A x \in S : P(x) Means for S All elements in ,P It's all true . When there is an element that makes P When it is false , Then the expression returns FALSE.
\A x \in {} : FALSE = TRUE
~\A And \A It means the opposite .
=> and <=>
P => Q intend If P It's true , that Q It's true . Be careful :P When it is false ,Q It can also be true . stay TLC in P => Q Equivalent to ~P / Q.
P <=> Q intend P and Q Both are true or false . amount to (~P /\ ~Q) / (P /\ Q)
Be careful :=> And <=> As a logical connector , And /\ and \/ Have the same priority , The indentation rule is the same .
example
1、 Given a set of numbers S And a number N, Define an expression , Return whether it can be in S Find N Elements make this N The sum of these elements is 0.
\* The topic can be broken down into :1、 from S Found a subset in ,2 The length of the subset is N,3, Find the sum of the elements of the subset 4、 There is a subset , The sum of elements is equal to 0
\* Find a subset with length N A set of sets of
Op2(S,n) == { e \in SUBSET S : Cardinality(e) = n }
\* Find the sum of set elements
RECURSIVE Sum(_)
Sum(S) == IF S = {} THEN 0
ELSE LET x == CHOOSE x \in S : TRUE
IN x + Sum(S \ {x})
\* Define expressions
Sum2Zero(S,N) == \E e \in Op2(S,N) : Sum(e)=0
2、 Given an expression Op(,_) And a collection S, Define an expression , Return expression for set S Whether all elements in are exchangeable . for example IsCommutative(Add(_,_),{1,2}) = TRUE , because Add(1,2) and Add(2,1) equal .
IsCommutative(Op(_,_), S) == \A x \in S, y \in S : Op(x,y) = Op(y,x)
3、 Given a set , Returns the maximum number in the set
Max(S) == CHOOSE x \in S : \A y \in S : y <= x
\* Note that the judgment here must be <= , It can't be <. Because there is no number bigger than itself .
4、 Given a set S, If the elements in the set p Prime number ,p-2 It's a prime number or p+2 Prime number , be p Twins are prime numbers . seek S The largest twin prime number in .
\* 1、 Find out whether a number is a prime number
IsPrime(x) == x > 1 /\ ~\E d \in 2..(x-1) : x % d = 0
\* 2、 Find out whether a number is a twin prime number
IsTwinPrime(a) == /\ IsPrime(a) /\ (IsPrime(a-2) \/ IsPrime(a+2))
\* 3、 Find the set of twin primes
GetTwinPrime(S) == { a \in S : IsTwinPrime(a)}
\* 4、 Find the maximum twin prime
LargestTwinPrime(S) == CHOOSE x \in GetTwinPrime(S): \A y \in GetTwinPrime(S) : x >=y
5、 Returns the largest pair of twin primes in the above set , If a twin prime is missing , Then the pair is invalid . for example :{3,5,13} The largest twin prime pair of is <<3,5>>, because 13 The twin prime of 11 Not in the assembly .
\* 1、 Find the set of twin prime pairs from the set of twin prime numbers , If a,a+2 All in the set , The retention a.
GetPairTwinPrime(S) == { a \in GetTwinPrime(S): a+2 \in GetTwinPrime(S)}
\* 2、 Find the maximum value from the set of twin prime pairs x, that <<x,x+2>> That's what I'm looking for
LargestPariTwinPrime(S) == LET x == CHOOSE x \in GetPairTwinPrime(S) :
\A y \in GetPairTwinPrime(S) : x >=y
IN <<x,x+2>>
6、 Given a positive integer tuple stockprices Used to express the change of stock value during the day , Write an operator to determine the maximum profit you can make , Suppose you can only buy and sell first and buy and sell at most once .
\* Can only buy and sell 1 Time , Just find the largest difference between the two numbers ,
\* Only buy first and then , You need to subtract the previous number from the latter number
\* 1、 Convert tuples into tuple pairs , And the first element is in front of the second element
\* for example <<1,2,3>> Turn into <<1,2>>,<<1,3>>,<<2,3>> Equivalent to {1} \X {2,3} \union {2} \X {3}
RECURSIVE newSxS(_)
newSxS(S) == IF Len(S) > 1
THEN LET newset== <<{Head(S)},{S[t]:t \in 2..Len(S)}>>
IN newset[1] \X newset[2] \union newSxS(Tail(S))
ELSE {}
\* Choose from the binary set <<x,y>> The one with the largest difference is ok
MaxProfit(stockprices) == LET sxs == newSxS(stockprices)
IN LET res == CHOOSE <<x,y>> \in sxs:
\A <<a,b>> \in sxs : y-x >= b-a
IN IF res[2]-res[1] > 0 THEN res[2]-res[1]
ELSE 0 \* If there is no profit, do not operate . If you have to buy and sell once , Then return to res[2]-res[1] that will do
边栏推荐
- Using openpyxl module to write the binary list into excel file
- Remote control service
- 微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
- 无心剑英译席慕容《无怨的青春》
- Main window in QT application
- Win: use PowerShell to check the strength of wireless signal
- After reading the average code written by Microsoft God, I realized that I was still too young
- MySQL regexp: Regular Expression Query
- 整理混乱的头文件,我用include what you use
- MATLB|多微电网及分布式能源交易
猜你喜欢
Remote control service
DOM basic syntax
增量备份 ?db full
Express routing, express middleware, using express write interface
ROS command line tool
【大型电商项目开发】性能压测-性能监控-堆内存与垃圾回收-39
Basic operation of database and table ----- the concept of index
[development of large e-commerce projects] performance pressure test - Performance Monitoring - heap memory and garbage collection -39
runc hang 导致 Kubernetes 节点 NotReady
Yyds dry goods inventory kubernetes management business configuration methods? (08)
随机推荐
Yyds dry goods inventory [Gan Di's one week summary: the most complete and detailed in the whole network]; detailed explanation of MySQL index data structure and index optimization; remember collectio
19. Delete the penultimate node of the linked list
Database postragesql client connection default
Database postragesq PAM authentication
Nebula Importer 数据导入实践
Incremental backup? db full
Phpstrom setting function annotation description
Yyds dry goods inventory kubernetes management business configuration methods? (08)
DOM basic syntax
Are you still writing the TS type code
Global and Chinese market of portable CNC cutting machines 2022-2028: Research Report on technology, participants, trends, market size and share
Application and development trend of image recognition technology
Application and Optimization Practice of redis in vivo push platform
Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
What sparks can applet container technology collide with IOT
Yyds dry inventory swagger positioning problem ⽅ formula
[Chongqing Guangdong education] National Open University spring 2019 1042 international economic law reference questions
力扣剑指offer——二叉树篇
If the consumer Internet is compared to a "Lake", the industrial Internet is a vast "ocean"
Great God developed the new H5 version of arXiv, saying goodbye to formula typography errors in one step, and mobile phones can also easily read literature