Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An example in Language manual, a strange type error occurred. #688

Closed
shinsahara opened this issue Sep 1, 2018 · 8 comments
Closed

An example in Language manual, a strange type error occurred. #688

shinsahara opened this issue Sep 1, 2018 · 8 comments
Labels
bug Incorrect behaviour of the tool Mergable A fix is available on a branch to merge for release
Milestone

Comments

@shinsahara
Copy link

Description

There is a strange type error in the merge sort example.
in the Language manual(Feb 2018, version 11, version of Overture .exe 2.6.0) p.57-58.

The example is following:

class MergeSort

operations
public mergesort : seq of nat ==> seq of nat 
mergesort(s) ==
	cases s:
		[] -> return [], 
		[x] -> return [x],
		s1 ^ s2 -> return lmerge(mergesort(s1), mergesort(s2))
	end;
	
functions
public lmerge : seq of nat * seq of nat -> seq of nat 
lmerge (s1,s2) ==
	if s1 = [] then s2 
	elseif s2 = [] then s1 
	elseif (hd s1) < (hd s2) then 
		[hd s1] ^ (lmerge (tl s1, s2)) 
	else 
		[hd s2] ^ (lmerge (s1, tl s2));

end MergeSort

MergeSort.vdmpp.zip

Steps to Reproduce

  1. read the MergeSort.vdmpp in a project.

Expected behavior:
There is no error.

Actual behavior:
Following type error occurred at line 4:
"Operation returns void value. Actual: (() | [] | seq of (nat)) Expected: seq of (nat) "

Reproduces how often:
every time.

Versions

Which version of Overture are you using?

Version: 2.6.2
Build date: 2018 May 18 11:17 CEST
Git commit description: Release/2.6.2

Also, please include the OS and what version of the OS you're running.

macOS High Sierra version 10.13.8

Additional Information

On VDMTools, there is no error, and can get the result as following:

>> version
The VDM++ Toolbox v9.0.7 - Sat 09-Jun-2018 09:00:27 +0900
VDM Toolbox is currently developed with the cooperation of Research Center
for Architecture-Oriented Formal Methods of Kyusyu University.
>> d new MergeSort().mergesort([3,1,5,2])
[ 1, 2, 3, 5 ]
@nickbattle
Copy link
Contributor

nickbattle commented Sep 1, 2018 via email

@shinsahara
Copy link
Author

I think that "others statement" is optional in VDM10_lang_man and VDMTools's langmanpp_a4E.
So, adding an others clause is not the answer of this problem.

cases statement = ‘cases’, expression, ‘:’,
                  cases statement alternatives, 
                  [ ‘,’, others statement ], ‘end’ ;

@peterwvj peterwvj added the bug Incorrect behaviour of the tool label Sep 3, 2018
@peterwvj peterwvj added this to the v2.6.4 milestone Sep 3, 2018
@nickbattle
Copy link
Contributor

nickbattle commented Sep 5, 2018 via email

@shinsahara
Copy link
Author

In following example,
if n was not in set {1,2} then VDMTools fall in Run-Time Error 119.
But, if "def type check" was "on" then VDMTools said "Type error [19]."

public casetest : nat ==> nat
casetest(n) == (
	cases n:
		(1)	-> return 1,
		(2) -> return 2
	end
);

In following example,
VDMTools said "there is no type error."

public mergesort : seq of nat ==> seq of nat 
mergesort(s) ==
	cases s:
		[] -> return [], 
		[x] -> return [x],
		s1 ^ s2 -> return lmerge(mergesort(s1), mergesort(s2)) 
	end;

@nickbattle
Copy link
Contributor

nickbattle commented Sep 6, 2018 via email

@nickbattle
Copy link
Contributor

I've now changed VDMJ to determine whether any of the patterns in a cases statement will definitely match a value of the type passed to the statement. If (at least) one of the cases always matches, it has the same effect as an "others" clause - that is, suppressing the drop-through behaviour that could return a void value. So for example:

	cases s:
		[] -> return [], 
		[x] -> return [x]
		-- s1 ^ s2 -> return s2 ^ s1
	end;

This raises an error because the statement can return no value (assuming the operation is ==> seq of nat). If you comment-in the extra case, which is guaranteed to match any sequence (even [] matches a^b, though that is caught earlier anyway), then there is no type error because there is no void path.

It will take me a little longer to move the changes over to Overture...

@nickbattle
Copy link
Contributor

Fix now available in ncb/development.

@nickbattle nickbattle added the Mergable A fix is available on a branch to merge for release label Sep 12, 2018
@nickbattle
Copy link
Contributor

Just pushed a correction - Overture/VDMJ wasn't matching [] and {} with the concatenation and union patterns. Now it does (as VDMTools does). This is important here, otherwise (say) "a ^ b" wouldn't match any value of a sequence type, only non-empty sequences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behaviour of the tool Mergable A fix is available on a branch to merge for release
Projects
None yet
Development

No branches or pull requests

3 participants