0

So for any given language, if we implement the same program(i.e same output for any given input) twice, using different syntax (i.e. using i++ instead of i+1) will the two programs have the same semantics? Why? Does the same apply in case where we use different constructs (i.e. Arrays vs Arraylists)?

Thanks

Rakim
  • 1,087
  • 9
  • 21
  • 40

2 Answers2

1

Yes. Depending on the programming language, there can be (combinations of) different syntax constructs with identical semantics.

For example, we can define a programming language with 3 constructs: A and B, both of which are semantically equivalent, and composition (e.g XY for any X and Y where any of these can either be A, B or any composition thereof). Hence program A is equivalent to program B. Also AA is equal to AB, BA and BB etc.

Further, if we extend the language with C which is semantically equivalent to AA, then, for example, BC is equivalent to AAA etc.

jotik
  • 17,044
  • 13
  • 58
  • 123
  • so it is not definite that it's gonna have the same semantics? – Rakim Mar 08 '16 at 12:43
  • @Rakim What do you mean? – jotik Mar 08 '16 at 12:45
  • You said depending on the programming language. So I guess the "different construct - same semantics" rule doesn't always apply. EDIT: I think I got what you mean. You are talking about the different ways we can implement the same thing based on the given language – Rakim Mar 08 '16 at 12:46
  • @Rakim Yes. That would include all programming languages which allow you to specify more than 2 semantically different programs. – jotik Mar 08 '16 at 12:48
  • @Rakim I mean all programming languages in which it is possible to write at two or more programs with different semantics. – jotik Mar 08 '16 at 12:54
1

So for any given language, if we implement the same program(i.e same output for any given input) twice, using different syntax (i.e. using i++ instead of i+1) will the two programs have the same semantics?

That question is a tautology. The answer is yes. Obviously.

If two different programs produce the same results for all possible input sets, then they do have the same semantics. By definition1.

Why?

Because that is what "same semantics" means!

Does the same apply in case where we use different constructs (i.e. Arrays vs Arraylists)?

Yes.

(One data structure might use more memory, and that might cause an OOME for one version and not the other ... for certain input datasets. But then I would argue that the programs DO NOT produce the same results for all possible inputs.)

Note that this applies to all practical programming languages. Any programming language where there are programs that can only be written one way ... is probably too restrictive to be usable.


1 - OK, so anyone who has studied programming semantics would probably have a fit when they read that. But I am trying to provide an intuitive explanation rather than one that has a decent mathematical foundation. Horses for courses ... as they say.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216