Could it be possible to check the variable starting alphabet

51 views
Skip to first unread message

androi...@gmail.com

unread,
Sep 12, 2019, 4:25:59 PM9/12/19
to tlaplus
If I want to check that my variable must start with specific letter would it be possible.

Variable myletter = 'Alpha'

And condition is myletter must start with 'A' and ends on 'a'

Thanks in advance

Stephan Merz

unread,
Sep 12, 2019, 4:37:13 PM9/12/19
to tla...@googlegroups.com
Hello,

according to TLA+ semantics, a string is a sequence of characters, so you could write something like

myletter[1] = "A"[1] /\ myletter[Len(myletter)] = "a"[1]

However, TLC treats strings as primitive objects and doesn't convert them to sequences / functions when needed. This also means that you cannot really do any string manipulation in your specification if you want to be able to analyze it with TLC. I'd recommend using sequences instead of strings.

Regards,
Stephan

--
You received this message because you are subscribed to the Google Groups "tlaplus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tlaplus+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tlaplus/ba8f39d6-65f2-4e8e-a93f-a7e0123d8fc0%40googlegroups.com.

androi...@gmail.com

unread,
Sep 12, 2019, 11:20:37 PM9/12/19
to tlaplus
Thanks Merz for the quick response.

Just want to ask how this would be possible with sequence, if you can guide.

Stephan Merz

unread,
Sep 12, 2019, 11:30:57 PM9/12/19
to tla...@googlegroups.com
Just want to ask how this would be possible with sequence, if you can guide.

It depends on what you consider is important for your specification. Since you are interested in the first and last elements of a sequence of characters, you could let your variable take a sequence of elements, perhaps somewhat as below.

CONSTANT
  Letter,    \* set representing all letters, instantiated for example by a set {a,b,c} of model values
  a, b, c    \* representing individual letters
ASSUME {a,b,c} \subseteq Letter

VARIABLE myletter

Init == myletter = << a,b,a >>

UpdateLetter ==  \* non-deterministically change some letter
  \E i \in 1 .. Len(myletter) : \E l \in Letter : myletter' = [myletter EXCEPT ![i] = l]

GoodLetters ==   \* some state predicate corresponding to desired state
  /\ myletter[1] = a
  /\ myletter[Len(myletter)] = c

Hope this helps,
Stephan

Leslie Lamport

unread,
Sep 13, 2019, 3:04:39 AM9/13/19
to tlaplus
Stephan is incorrect when he says that TLC treats strings as primitive objects.  It treats them as sequences, but of elements that it can't handle.  Some sequence operations (I believe \o is one of them) work on strings.  The documentation on the latest versions of the tools should tell you which operators work, or you can just experiment.  TLC may be able to handle a spec that does what you want.


On Thursday, September 12, 2019 at 1:37:13 PM UTC-7, Stephan Merz wrote:
Hello,

according to TLA+ semantics, a string is a sequence of characters, so you could write something like

myletter[1] = "A"[1] /\ myletter[Len(myletter)] = "a"[1]

However, TLC treats strings as primitive objects and doesn't convert them to sequences / functions when needed. This also means that you cannot really do any string manipulation in your specification if you want to be able to analyze it with TLC. I'd recommend using sequences instead of strings.

Regards,
Stephan

On 12 Sep 2019, at 13:25,   wrote:

If I want to check that my variable must start with specific letter would it be possible.

Variable myletter = 'Alpha'

And condition is myletter must start with 'A' and ends on 'a'

Thanks in advance

--
You received this message because you are subscribed to the Google Groups "tlaplus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to  ...

jansh...@gmail.com

unread,
Sep 20, 2019, 5:36:21 AM9/20/19
to tlaplus
So it means and overall my digging in TLA+, I come to know that this is not possible in TLA+ to check the value of a string/variable by matching some other string, or check a specific format of a string.

Leslie Lamport

unread,
Sep 20, 2019, 6:38:05 AM9/20/19
to tlaplus
Your original request was to be able to check if a string starts with
`A' and ends with 'a'.  I believe this is done in TLA+ by the
following operator:

   StartsWith_A_andEndsWith_a(s) ==
     /\ Len(s) > 0
     /\ s[1] = "A"[1]
     /\ s[Len(s)] = "a"[1]

However, I'm not sure because TLC cannot evaluate this, and I suspect
TLAPS can't handle it either, though I haven't tried.  TLC can handle
the following definition, and a few tests confirm that it is the
operator you wanted:

   StartsWith_A_andEndsWith_a(s) ==
      LET sx == s \o "x"
      IN  /\ SubSeq(sx, 1, 1) = "A"
          /\ SubSeq(sx, Len(s), Len(s)) = "a"

There are lots of TLA+ expressions that TLC can't evaluate.  For
example, consider this definition:

   TheSetOfAllPrimes ==
      LET IsAPrime(n) == ...
      IN  {n \in Nat : IsAPrime(n)}

If IsAPrime is properly defined, TLC could evaluate the expression

   42 \in TheSetOfAllPrimes

However, it can't evaluate

    {2*n : n \in Nat} \intersect TheSetOfAllPrimes

It would be hard to extend TLC to be able to evaluate this expression.
It would not be hard to extend it to handle the first definition of
StartsWith_A_andEndsWith_a.  However, there are many other things that
seem to be more important for us to do.  

Whether TLA+ is the best language for you to use depends on what you
want to do.  If you want to check the correctness of a concurrent system
and you want to use a little bit of string manipulation in your spec,
it probably is.  If you want to do something that requires complicated
string processing, it probably isn't.

Leslie

MK Bug

unread,
Sep 23, 2019, 3:42:19 AM9/23/19
to tlaplus
Thanks for the help
Reply all
Reply to author
Forward
0 new messages