[mercury-users] Re: string__suffix ... problem help required

Ralph Becket rafe at cs.mu.OZ.AU
Fri Nov 15 08:05:14 AEDT 2002


The Mercury bugs list is for reporting bugs in the distribution,
not your own code.  Please use the Mercury users' list instead.

Noel  Pinto, Thursday, 14 November 2002:
> Hi,
> 
> I am now working with the string module. I have written a code for 
> accepting a string from the user and calculate the suffix from the 
> string.
> 
> What I understand is that string__suffix will find the suffix from 
> the string and it takes two parameters.
> 
> The code given below is not perfect... I am getting an error.
> 
> :- module presuffix.
> 
> :- interface.
> 
> :- import_module io.
> 
> :- pred main(io__state::di, io__state::uo) is det.
> 
> :- implementation.
> 
> :- import_module string, exception.
> 
> main -->
>         print(" Enter a string : "), flush_output,
>         io__read_line_as_string(Str),
> 
>         {       string__prefix(Str, ResStr)

The io module says:

:- pred io__read_line_as_string(io__result(string), io__state, io__state).

The string module says:

:- pred string__prefix(string, string).

so your first problem is that Str cannot simultaneously be a string and
an io__result(string).

Which, lo and behold, the compiler reports with what we hoped was great
clarity:
> I get the following error...
> presuffix.m:017: In clause for predicate `presuffix:main/2':
> presuffix.m:017:   in argument 1 of call to predicate `string:prefix/2':
> presuffix.m:017:   type error: variable `Str' has type `(io:result(string))',
> presuffix.m:017:   expected type was `string'.
> For more information, try recompiling with `-E'.
Can you tell us how this error message is unclear?

>                 ->
>                 ValString = ResStr
>                 ;
>                 throw(" Error ")
>         },
>         print(ValString).
> 
> I may not have understood how string_suffix works exactly but what 
> I understand is that it will accept a string and show the 
> suffix.

The string module says:

        % string__suffix(String, Suffix) is true iff Suffix is a
	% suffix of String.  Same as string__append(_, Suffix, String).

www.m-w.com says:

Main Entry: 1suf·fix
Pronunciation: 's&-fiks
Function: noun
Etymology: New Latin suffixum, from Latin, neuter of suffixus, past
participle of suffigere to fasten underneath, from sub- + figere to
fasten -- more at FIX
Date: 1778
an affix occurring at the end of a word, base, or phrase -- compare PREFIX
- suf·fix·al /'s&-fik-s&l, (")s&-'fik-s&l/ adjective

If you pick an arbitrary point in a string then *a* suffix is the
substring following that point.  For example, the suffixes of "baz" are
"baz", "az", "z", and "".

Which will lead to another problem for you since the string module
says:
:- mode string__suffix(in, out) is multi.
But main/2 is only allowed to be det or cc_multi.  So you'll have
to decide how you want to solve this problem, too.

> If I add `with_type` string
> to Str then I get the following error

You generally only need `with_type` to resolve ambiguity.  The compiler
is not complaining about ambiguity, but rather an unresolvable
inconsistency in your program.  The error message concerning ambiguity
mentions the word "ambiguous".

- Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list