[mercury-users] Manipulating Types and Tuples

Ondrej Bojar oboj7042 at ss1000.ms.mff.cuni.cz
Wed May 7 21:27:47 AEST 2003


Hi.

On Wed, 7 May 2003, Goncalo Jorge Coelho e Silva wrote:
> %:- myType ---> int ; string.

There are two errors in your type declaration. First, you misspelled the
declaration. It should be:

:- type myType ---> ...
   ^^^^

Second, it is not permitted to mix together different types without
'discriminating' them by distinct functors. In your case, the type
declaration should be:

:- type myType ---> i(int); s(string).

Read the section Discriminated unions of the Language Reference to learn
more.

> main(IO0,IO) :-
>                 %construct(myType,1,[29|"string"]) = MyTerm,
>                 %List = [1|2|[]],
>                 %construct_tuple(List) = Univ,
>
>                 print("Tuple/Functor test", IO0, IO1),
>                 nl(IO1, IO).

I would suggest you to forget the 'construct' predicates at all. In
general, you construct term just by typing them. Also, forget the
'construct_tuple' predicate and construct the tuples using the {,}
syntax. (See the section Syntax in the Language Reference). Your example
should be:


main(IO0, IO) :-
  MyTerm = [i(29), s("string)],
  Univ = {1,2}, % personally, I'd call the variable Tuple, not Univ
  ...

The variable MyTerm will be of type list(myType). Mercury can infer the
types automatically, so you do not need to declare the type of the
variable anywhere. You also do not learn the inferrect type of variables,
unless you make a type error. A type error is when you use a variable in
two contexts and mercury infers two conflicting types for the variable.
For instance:

  ...
  MyTerm = [i(29), s("string"],  % list(myType)
  (
  if MyTerm2 = "just_a_string"   % string
  then ...
  ...
  ),
  MyTerm2 = MyTerm ++ [30]  % list(myType) and list(int) cannot
                            % be appended, because the resulting
                            % variable would be of type
                            % list(SOMETHING_NOT_PROPERLY_TYPED)

Andrew.


>  ... that, didn't work. :\
>  Since all the *strange* instructions dealing with
> tuples and types are commented, I shouldn't
> have a reason to get this, should I?:
>
> mmc -E inter
> mercury_compile: can't open file `construct.int'.
> make: *** [run] Error 1
>
>  Does this means that the compiler installations
> misses something? Bad install?
>
> ...and, when I uncomment the line of the
> myType declaration, since I don't believe
> I have to import module 'type_desc' or
> anything to handle type declarations, I also
> shouldn't be getting this, would I?
>
> inter.m:010: Error: unrecognized declaration: myType ---> int ; string.
>
> Can you guys tell me what am I
> doing wrong here?
>
>
> ...still, about the tuple construction, regarding these
> 2 functions, on the constructor module:
>
> :- func construct(type_desc__type_desc, int, list(univ)) = univ.
> :- mode construct(in, in, in) = out is semidet.
>
> 	% construct_tuple(Args) = Term
> 	%
> 	% Returns a tuple whose arguments are given by Args.
> :- func construct_tuple(list(univ)) = univ.
>
>   What's the middle argument in the 1st one? The functor arity?
> The 'construc.m' description was not explicit to me.
>
>
> Thank you so much.
>
> Cheers,
> Goncalo
>
>
> --------------------------------------------------------------------------
> 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