[mercury-users] read_term(T) and Mercury types

Ondrej Bojar oboj7042 at ss1000.ms.mff.cuni.cz
Sun Apr 13 03:24:39 AEST 2003


Hi.

Here is the sample code:

:- module testio.

:- interface.

:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.

main -->
  io__read_line_as_string(MaybeString),
  (
  if { MaybeString = ok(String) }
  then
    io__write_string(String)
  else
    io__write_string("An error occurred -- either eof or I/O error\n")
  ).


I suggest you reading more the Mercury Language Reference, because several
important topics are already "hidden" in this code. First of all the Types
section and then DCGs and also the 'if-then-else' and 'switches'. In my
example I didn't use a switch to handle different possible outputs from
io__read_list_as_string, I used an 'if-then-else' construction. With
switches, the code would be:

  io__read_line_as_string(MaybeString),
  (
  { MaybeString = ok(String) },
    io__write_string(String)
  ;
  { MaybeString = eof },
    io__write_string("End-of-file.\n")
  ;
  { MaybeString = error(ErrorMessage, LineNo) },
    io__write_string(ErrorMessage)
  ).

Note that with switches, you have to cover all the cases of the type
io__read_result(T) (see the Library Documentation, module io). There is no
"default" arm of a switch.

O.

On Fri, 11 Apr 2003, Goncalo Jorge Coelho e Silva wrote:

>
> Hi,
>
> I'm new to Mercury and it's been a while
> I've been trying to get to read something
> from the STD_IN (a word/term) and get
> it printed.
>
> I've tried a couple of approaches, none of which
> sucessful.
>
> 1) Using read_term(T) and write_string
>
> 2) Using io__read_word(Char_List) and io__write_many(Char_List)
>    and io__write_list(Char_List,"",InputString)
>
>  Help would be welcome.
>  The problem seems to be on dealing with the
> output term type:
> 'term_io:read_term(T)'
> and getting it to be 'casted' to a 'string'. Would
> this be possible in Mercury? and/or wise?
>  On theory, the 'T' regarding the input of the
> predicatre 'read_term' should accept a string,
> right?
>
> ---
> Description (1)
>
> I'd get:
>  in argument 1 of call to predicate `io:write_string/3':
>  inter.m:036:   type error: variable `Result' has type
>  `(term_io:read_term(T))',
>  inter.m:036:   expected type was `string'.
>
> Tried:
>  Tried using a sort of 'cast' with
>  "with_type(Term, Type)" or "Term with_type Type"
> but it's no good.
>
>  Plus, I didn't get to find out which module
> pred 'with_type' is on.
>
> Code:
> -----------------------
> :- module inter.
>
> :- interface.
>
> :- import_module io.
>
> :- pred main(io__state, io__state).
> :- mode main(di, uo) is det.
>
>
> :- implementation.
>
> :- import_module array.
> :- import_module string.
> :- import_module list.
> :- import_module term_io.
> %:- import_module type_desc.
> %:- import_module std_util.
>
> %:- type ResultS == string.
>
> main -->
>         %(1)
>         %:- type ResultS == string.
> term_io__read_term(Result).
>         %{
>         %Result with_type string.
>         %}
>         %{
>         %with_type(Result, string)
>         %}.
> %io__write_string(Result).
>
>         %(2)
>         %io__read_word(Char_List),
>         %io__write_many(Char_List),
>         %io__write_many(Plain_List),
>         % io__write_list(Char_List,"",InputString).
>         %{string__format("'-'", Char_List, InputString)},
>         %{string__format("%s %i %c %f\n", Char_List, InputString)}.
>         %{string__format("%s %i %c %f\n", Char_List, InputString)}.
> ------------------
>
>  How can I get through this?
>
>  Plus, if I had to declare ":- type ResultS == string."
> where's the right place to do it? Between "implementation"
> and "main" I get:
>   inter.m:020: Error: variable on LHS of type definition.
>
>  Before "implementation" and afterwards
> "main" makes no sense to me.
>
> Thanks,
> Goncalo
> (still reading the documentation... :) )
> --------------------------------------------------------------------------
> 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
> --------------------------------------------------------------------------
>

--------------------------------------------------------------------------
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