当前位置:网站首页>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
边栏推荐
- C语音常用的位运算技巧
- Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!
- Discrete mathematics: reasoning rules
- Hedhat firewall
- Pytorch common code snippet collection
- 线上故障突突突?如何紧急诊断、排查与恢复
- Application and development trend of image recognition technology
- "2022" is a must know web security interview question for job hopping
- Using openpyxl module to write the binary list into excel file
- Global and Chinese markets for industrial X-ray testing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

【LeetCode】88. Merge two ordered arrays

整理混乱的头文件,我用include what you use

One plus six brushes into Kali nethunter

Remote control service
![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](/img/e8/de158982788fc5bc42f842b07ff9a8.jpg)
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

微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益

Express routing, express middleware, using express write interface

Game 280 of leetcode week

Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #

Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
随机推荐
Global and Chinese markets for industrial X-ray testing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
If the consumer Internet is compared to a "Lake", the industrial Internet is a vast "ocean"
To sort out messy header files, I use include what you use
Change the background color of a pop-up dialog
Database postragesq BSD authentication
I use these six code comparison tools
Database postragesq PAM authentication
[development of large e-commerce projects] performance pressure test - Optimization - impact of middleware on performance -40
Express routing, express middleware, using express write interface
Complex, complicated and numerous: illustration of seven types of code coupling
Application and Optimization Practice of redis in vivo push platform
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
Async/await you can use it, but do you know how to deal with errors?
Database postragesq role membership
Global and Chinese market of veterinary thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
One plus six brushes into Kali nethunter
小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
[swagger]-swagger learning
Win:使用组策略启用和禁用 USB 驱动器
Win: enable and disable USB drives using group policy