You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unfinished, random math exercises collected from Specifying Systems, LearnTLA, and elsewhere.
---- MODULEF ----
EXTENDSNaturals,FiniteSets,Sequences(* 1. Set of all permutations of {"T","L","A"} including repetitions. *)PermsWithReps(S)==[1..Cardinality(S)->S]ASSUMEPermsWithReps({"T","L","A"})={<<"T","T","T">>,<<"T","T","L">>,<<"T","T","A">>,<<"T","L","T">>,<<"T","L","L">>,<<"T","L","A">>,<<"T","A","T">>,<<"T","A","L">>,<<"T","A","A">>,<<"L","T","T">>,<<"L","T","L">>,<<"L","T","A">>,<<"L","L","T">>,<<"L","L","L">>,<<"L","L","A">>,<<"L","A","T">>,<<"L","A","L">>,<<"L","A","A">>,<<"A","T","T">>,<<"A","T","L">>,<<"A","T","A">>,<<"A","L","T">>,<<"A","L","L">>,<<"A","L","A">>,<<"A","A","T">>,<<"A","A","L">>,<<"A","A","A">>}(* 2. All combinations of a two-digit lock. *)TwoDigitLock==[1..2->0..9]ASSUME/\(0..9)\X(0..9)=TwoDigitLock/\{<<n,m>>:n,m\in10..19}\notinSUBSETTwoDigitLock(* 3. All combinations of a three-digit lock. *)ThreeDigitLock==[1..3->0..9]ASSUME/\(0..9)\X(0..9)\X(0..9)=ThreeDigitLock/\{<<n,m,o>>:n,m,o\in10..19}\notinSUBSETThreeDigitLock(* 4. All pairs (including repetitions) of the natural numbers. *)PairsOfNaturals==[1..2->Nat]ASSUME{<<n,m>>:n,m\in0..100}\subseteqPairsOfNaturals(* 5. All triples... *)TriplesOfNaturals==[1..3->Nat]ASSUME{<<n,m,o>>:n,m,o\in0..25}\subseteqTriplesOfNaturals(* 6. Set of all pairs and triples... *)PairsAndTriplesOfNaturals==[1..2->Nat]\cup[1..3->Nat]ASSUME/\{<<n,m>>:n,m\in0..100}\subseteqPairsAndTriplesOfNaturals/\{<<n,m,o>>:n,m,o\in0..25}\subseteqPairsAndTriplesOfNaturals(* 7. What is the Cardinality of 3. ? *)Cardinality3==Cardinality(ThreeDigitLock)ASSUMECardinality3=1000(* 8. What is the Cardinality of 6. (PairsAndTriplesOfNaturals) ? *)--------------------------------------------------------------(* 9. The range/image/co-domain of a function. *)Range(f)=={f[x]:x\inDOMAINf}ASSUMERange([a|->1,b|->2,c|->3])=1..3(* 10. The permutations of a set _without_ repetition. *)Perms(S)=={f\in[S->S]:Range(f)=S}ASSUMEPerms({1,2,3})={<<1,2,3>>,<<1,3,2>>,<<2,1,3>>,<<2,3,1>>,<<3,1,2>>,<<3,2,1>>}Perms2(S)==\* If for all w in S there exists a v in S for which f[v]=w,\* there can be no repetitions as a consequence. The predicate\* demands for all elements of S to be in the range of f.{f\in[S->S]:\Aw\inS:\Ev\inS:f[v]=w}ASSUMEPerms2({1,2,3})={<<1,2,3>>,<<1,3,2>>,<<2,1,3>>,<<2,3,1>>,<<3,1,2>>,<<3,2,1>>}Perms3(S)=={f\in[S->S]:\Ai,j\inDOMAINf:i#j=>f[i]#f[j]}ASSUMEPerms3({1,2,3})={<<1,2,3>>,<<1,3,2>>,<<2,1,3>>,<<2,3,1>>,<<3,1,2>>,<<3,2,1>>}(* 11. Reverse a sequence (a function with domain 1..N). *)Reverse(seq)==[i\in1..Len(seq)|->seq[Len(seq)+1-i]]ASSUMEReverse(<<1,2,3>>)=<<3,2,1>>ASSUMEReverse(<<>>)=<<>>(* 12. An (infix) operator to quickly define a function mapping an x to a y. *)x:>y==[e\in{x}|->y]ASSUME"x":>42=[x|->42](* 13. Merge two functions f and g *)f++g==[x\in(DOMAINf)\cup(DOMAINg)|->IFx\inDOMAINfTHENf[x]ELSEg[x]]ASSUME<<1,2,3>>++[i\in1..6|->i]=<<1,2,3,4,5,6>>(* 14. Advanced!!! Inverse of a function f (swap the domain and range). *)Inverse(f)==CHOOSEg\in[Range(f)->DOMAINf]:\As\inDOMAINf:g[f[s]]=sASSUMEInverse(("a":>0)++("b":>1)++("c":>2))=((0:>"a")++(1:>"b")++(2:>"c"))
====
-------------------------- MODULESyntaxExcercises --------------------------
(* The idea is that learners get the spec with the operator _definitions_ replaced with TRUE. A learner can then check their definitions with TLC. *)EXTENDSIntegers,Sequences,TLC(***************************************************************************)(* The set of the fibonacci numbers: 1,1,2,3,5,8,13,... *)(***************************************************************************)MyNat==1..21\* perhaps, introduce model definitions right away?fib[n\inMyNat]==IFn<=2THEN1ELSEfib[n-1]+fib[n-2]ASSUMEfib[12]=144RECURSIVEfibRecurse(_)fibRecurse(n)==IFn<=2THEN1ELSEfibRecurse(n-1)+fibRecurse(n-2)ASSUMEfibRecurse(12)=144(***************************************************************************)(* The set of fibonacci numbers in the range [10,20). *)(***************************************************************************)FibSet=={fib[n]:n\in10..19}ASSUMEFibSet={55,89,144,233,377,610,987,1597,2584,4181}FibSetRecurse=={fibRecurse(n):n\in10..19}ASSUMEFibSetRecurse={55,89,144,233,377,610,987,1597,2584,4181}(***************************************************************************)(* The sequence of fibonacci numbers in the range [10,20). *)(***************************************************************************)FibSeq==[e\in1..10|->fib[e+9]]ASSUMEFibSeq=<<55,89,144,233,377,610,987,1597,2584,4181>>(***************************************************************************)(* The range of a function func. *)(***************************************************************************)Range(func)=={func[e]:e\inDOMAINfunc}(* Change the 5., 10, 15. element of a function f to "abc", "def" and "ghi" *)(* TCommit video Minute 12:30 *)Replace(func,pos,value)==[funcEXCEPT![pos] = value]ASSUME[i\in1..3|->i^2]=Replace(Replace(Replace([i\in1..3|->0],1,1),2,4),3,9)(***************************************************************************)(* Excercise page 235/70 *)(* The sequence obtained by removing the i-th element of seq. *)(***************************************************************************)Remove(seq,i)==[j\in(1..Len(seq)-1)|->(IFj<iTHENseq[j]ELSEseq[j+1])](***************************************************************************)(* Excercise page 341/91 *)(* Recursive definition of the Len(seq) _operator_ with Tail. *)(***************************************************************************)RECURSIVERecLen(_)RecLen(seq)==IFseq=<<>>THEN0ELSE1+RecLen(Tail(seq))(***************************************************************************)(* An alternative variant which treats seq as the function it is and *)(* returns the maximum of its DOMAIN which corresponds to the number of *)(* elements in seq. *)(***************************************************************************)DomLen(seq)==CHOOSEm\in(DOMAINseq):(DOMAINseq)=1..m(***************************************************************************)(* Write an operator that returns the cardinality of a given set. *)(***************************************************************************)RECURSIVECard(_)Card(S)==IFS={}THEN0ELSE1+Card(S\{CHOOSEe\inS:TRUE})ASSUMECard({})=0/\Card({1,1,2,2,3,3})=3(***************************************************************************)(* Write an operator that takes a tuple/sequence and, if the tuple is *)(* length two, returns the reversed tuple, and otherwise raises an error. *)(***************************************************************************)ReverseOfTwo(seq)==IFLen(seq)=2THEN<<seq[2],seq[1]>>ELSEAssert(FALSE,"")ASSUME<<4,5>>=ReverseOfTwo(<<5,4>>)(***************************************************************************)(* Write an operator that takes a sequence seq and, if the sequence is of *)(* uneven length, returns the reversed sequence, otherwise return seq. *)(***************************************************************************)Uneven(n)==n%2=1Reverse(seq)==[i\in1..Len(seq)|->seq[Len(seq)-(i-1)]]ReverseUneven(seq)==IFUneven(Len(seq))THENReverse(seq)ELSEseqASSUME<<1,2,3,4,5>>=ReverseUneven(<<5,4,3,2,1>>)ASSUME<<1,2,3,4>>=ReverseUneven(<<1,2,3,4>>)(* Sum of elements in seq *)(***************************************************************************)(* Remove those elements from sequence seq for which Filter(_) holds. *)(***************************************************************************)RECURSIVESubseq(_,_)Subseq(Filter(_),seq)==IFseq=<<>>THEN<<>>ELSEIFFilter(Head(seq))THEN<<Head(seq)>>\oSubseq(Filter,Tail(seq))ELSESubseq(Filter,Tail(seq))Even(n)==~Uneven(n)ASSUMESubseq(Even,<<1,2,1,3,3,6,8,8,23,42>>)=<<2,6,8,8,42>>SelectSeq2(Test(_),s)==LETS[i\in0..Len(s)]==IFi=0THEN<<>>\* As a basis set the s[0] = <<>>ELSEIFTest(s[i])THENS[i-1]\o<<s[i]>>\* copy previous value and concat with s[i]ELSES[i-1]\* just copy the previous valueINS[Len(s)]\* The maximum (last function position) contains the collected sequenceASSUMESubseq(Even,<<1,2,1,3,3,6,8,8,23,42>>)=SelectSeq2(Even,<<1,2,1,3,3,6,8,8,23,42>>)(***************************************************************************)(* Given DOMAIN Tuple is the set of numbers Tuple is defined over, write *)(* an operator that gives a set of the values of the Tuple, ie the range. *)(***************************************************************************)(***************************************************************************)(* Write an operator that takes two sets S1 and S2 and determines if the *)(* double of every element in S1 is an element of S2. *)(***************************************************************************)(***************************************************************************)(* Given a sequence of sets, write an operator that determines if a given *)(* element is found in any of the sequence’s sets. *)(* IE Op("a", <<{"b", "c"}, {"a", "c"}>>) = TRUE. *)(***************************************************************************)(* Given the (built-in) set Nat (Naturals), create a subset of S whose largest element is 42. *)(* Hint: Override Nat with 1..1000 on the Advanced Options page of the Model editor. By default *)(* Nat is represented as a UserValue in TLC. A UserValue is _not_ enumerable, whereas a 1..1000 *)(* is represented as a (finite) IntervalValue which subsequently can be enumerated. *)Subset42=={i\inNat:i<43}\*ASSUME Subset42 = 1..42(* The inverse of a function *)Inverse(f)==CHOOSEg\in[Range(f)->DOMAINf]:\As\inDOMAINf:g[f[s]]=sASSUMEInverse("a":>0@@"b":>1@@"c":>2)=(0:>"a"@@1:>"b"@@2:>"c")(* Merge two functions f and g *)Merge(f,g)==[x\in(DOMAINf)\cup(DOMAINg)|->IFx\inDOMAINfTHENf[x]ELSEg[x]]ASSUMEMerge(<<1,2,3>>,[i\in1..6|->i])=<<1,2,3,4,5,6>>(* The permutations of a set *)Perms(S)=={f\in[S->S]:\Aw\inS:\Ev\inS:f[v]=w}ASSUMEPerms({1,2,3})={<<1,2,3>>,<<1,3,2>>,<<2,1,3>>,<<2,3,1>>,<<3,1,2>>,<<3,2,1>>}
=============================================================================
Graphs
With the definitions from the Graphs module, under what conditions
is <<p>> an element of Path(G)?
Find the following values, and use TLC to check your answers.
(See the file PrintValues.tla in the folder AsynchronousMemory.)
Define an operator AF such that, if r is a record, then:
if r has a "count" field, then AF(r) is r with the count
field incremented by 1, otherwise, AF(r) is obtained from
r by adding a "count" field with value 0.
Define an operator Reverse, so if s is any sequence, then
Reverse(s) is sequence s in reverse order. (Hint: you
don't have to use recursion.) Test it with
TLC. (Don't forget to check that it works on the empty
sequence, << >> .)
Define a function Sum whose domain is Seq(Nat) such
that Sum(s) is the sum of the elements of s. (Let Sum(<< >>) equal 0.)
Determine which of the following formulas are tautologies.
Which of the following formulas are valid for all sets S, T, and U?
(S\subseteq T) <=> (S \cup T = T)
(S \subseteq T) <=> \A x \in S : x \in T(S=T)<=>(S\subseteqT)/\(T\subseteqS)(S\subseteqT)<=>(S\T={})(S\T)\cup(T\S)=(S\cupT)\(S\capT)(S\(T\capU))=(S\T)\cup(S\U)
Determine which of the following formulas are temporal
tautologies. For each one that isn't, give a counterexample.
(a) Use TLC to print all Pythagorean triples <<i,j,k>> with i^2 + j^2 = k^2 for natural numbers i,j,k \leq N.
Start with N=20. (Don't be clever and use the formula for
generating Pythagorean triples; have TLC to it in a straightforward
fashion.)
(b) Next, get TLC to print only essentially Pythagorean triples--that
is, Pythagorean triples <<i, j, k>> such that i \leq j and i,j, and k
have no common factor. Try it with N = 50, 100, ... . Why does TLC
take longer to generate each example as N increases?
This issue is primarily about the content and less about the front-end (IDE). That said, why do you think that a Jupyter notebook is a good first-user front-end? My concern is that users would have to learn a new front-end once they move from constant- to state-level TLA+. If this is about a web-experience, the VSCode extension runs on Gitpod and VS/Github codespaces.
Not having to install anything is one point, but I also like the experience of being able to easily check the output of the expression and then seeing the solution. It's true it's yet another tool which a beginner does not necessarily need to know, but for me notebooks are the most convenient way to try out things, I also like that I can document the code with markdown in the same file. It's a matter of preference and familiarity I guess.
About the exercises itself I think they have a good progression from easy to more difficult.
Unfinished, random math exercises collected from Specifying Systems, LearnTLA, and elsewhere.
Graphs
With the definitions from the Graphs module, under what conditions
is
<<p>>
an element ofPath(G)
?Find the following values, and use TLC to check your answers.
(See the file PrintValues.tla in the folder AsynchronousMemory.)
Define an operator AF such that, if r is a record, then:
if r has a "count" field, then AF(r) is r with the count
field incremented by 1, otherwise, AF(r) is obtained from
r by adding a "count" field with value 0.
Define an operator Reverse, so if s is any sequence, then
Reverse(s) is sequence s in reverse order. (Hint: you
don't have to use recursion.) Test it with
TLC. (Don't forget to check that it works on the empty
sequence,
<< >>
.)Define a function
Sum
whose domain isSeq(Nat)
suchthat
Sum(s)
is the sum of the elements ofs
. (LetSum(<< >>)
equal0
.)Determine which of the following formulas are tautologies.
tautologies. For each one that isn't, give a counterexample.
(a) Use TLC to print all Pythagorean triples
<<i,j,k>>
withi^2 + j^2 = k^2
for natural numbers i,j,k \leq N.Start with N=20. (Don't be clever and use the formula for
generating Pythagorean triples; have TLC to it in a straightforward
fashion.)
(b) Next, get TLC to print only essentially Pythagorean triples--that
is, Pythagorean triples
<<i, j, k>>
such that i \leq j and i,j, and khave no common factor. Try it with N = 50, 100, ... . Why does TLC
take longer to generate each example as N increases?
https://github.com/tlaplus/Examples/blob/master/specifications/SpecifyingSystems/SimpleMath/README
https://github.com/tlaplus/Examples/blob/master/specifications/SpecifyingSystems/AdvancedExamples/README
https://github.com/tlaplus/Examples/blob/master/specifications/SpecifyingSystems/TLC/README
https://github.com/tlaplus/Examples/blob/master/specifications/SpecifyingSystems/HourClock/README
https://github.com/tlaplus/Examples/blob/master/specifications/SpecifyingSystems/Liveness/README
https://github.com/tlaplus/Examples/blob/master/specifications/SpecifyingSystems/FIFO/README
https://github.com/tlaplus/Examples/blob/master/specifications/SpecifyingSystems/CachingMemory/README
https://github.com/tlaplus/Examples/blob/master/specifications/SpecifyingSystems/RealTime/README
The text was updated successfully, but these errors were encountered: