Using tuples in Miranda 
Author Message
 Using tuples in Miranda

Hi all,

Does anyone know how to construct a tuple in Miranda from a list?
(i.e. from [1,2,3] to (1,2,3))  Preferrably using a built-in function like ":"
in lists.

Thanks in advance.

Paul.



Sat, 11 Nov 1995 20:20:04 GMT  
 Using tuples in Miranda
There is no *uniform* way of converting a list, like [1,2,3]
into a tuple (1,2,3).

A list of length 2 is sent into the type (num,num), a list of
length 3 into the *different* type (num,num,num).

You can do the conversion for a fixed length of list, returning an error
or undefined value at lists of any other length, but that was not how I
interpreted your question.

=======================================================================


Computing Laboratory                    phone:  +44-227-764000
University of Kent                                extn. 3820
Canterbury, Kent,                       fax:    +44-227-762811
CT2 7NF, UK

=======================================================================



Sat, 11 Nov 1995 21:52:33 GMT  
 Using tuples in Miranda

Hi,

Thanks to all those who replied.  The intended meaning of my question was :

" Is it possible to convert a list (variable length) of the same type (obviously) into a tuple? "

i.e. something like : convert :: [*] -> (*,*)
                                          ^^ depends on the length of the list

Paul.



Sat, 11 Nov 1995 21:58:11 GMT  
 Using tuples in Miranda
: Does anyone know how to construct a tuple in Miranda from a list?
: (i.e. from [1,2,3] to (1,2,3))  Preferrably using a built-in function like ":"
: in lists.

You would have to write a separate function for each length of list.
For your example of lists of length 3, you would write:

list_to_triple [x,y,z] = (x,y,z)

It is not possible to write a more general function to take a
list of length n and return an n-tuple - the type system does not
allow it.

Jonathan.
--

Artificial Intelligence Systems Research Group    PHONE: +44 91 374 2549
School of Engineering and Computer Science        
University of Durham
Durham DH1 3LE, UK



Sat, 11 Nov 1995 22:55:12 GMT  
 Using tuples in Miranda
: Thanks to all those who replied.  The intended meaning of my question was :

: " Is it possible to convert a list (variable length) of the same
: type (obviously) into a tuple? "

: i.e. something like : convert :: [*] -> (*,*)
:                                         ^^ depends on the length of the list

The answer is still no, not in Miranda!  There is no type equivalence
between tuples of different lengths.  What this problem would require
is being able to unify the types (*,*) and (*,(*,*)), which is not
possible using the type system of Miranda.  I suppose it would be
possible in a language with type equivalence between tuples of type
(*,**,***) and (*,(**,***)), though.

Jonathan.

--

Artificial Intelligence Systems Research Group    PHONE: +44 91 374 2549
School of Engineering and Computer Science        
University of Durham
Durham DH1 3LE, UK



Sun, 12 Nov 1995 17:57:16 GMT  
 Using tuples in Miranda

Quote:
(Jonathan E Hazan) writes:

..

Quote:
> possible using the type system of Miranda.  I suppose it would be
> possible in a language with type equivalence between tuples of type
> (*,**,***) and (*,(**,***)), though.

Are there languages with this feature?

Carsten

--

"One day I may find it quite amusing
 How touching tongues make life so confusing"     Anne Clark



Tue, 14 Nov 1995 21:08:50 GMT  
 Using tuples in Miranda

Quote:


> (Jonathan E Hazan) writes:
> ..
> > possible using the type system of Miranda.  I suppose it would be
> > possible in a language with type equivalence between tuples of type
> > (*,**,***) and (*,(**,***)), though.

> Are there languages with this feature?

Yes - "Sticks&Stones" by Luca Cardelli has this feature. S&S is a
functional language with extensions for doing VLSI layout and is based on
ML. S&S uses run-time type checking so no type inference is involved. I
asked the net about many moons ago, and got the feeling that such type
equivalences make type inference very difficult - I never followed up
adequately to see if this was the case though - any comments anyone ?

This might imply that ML has (or had) this feature, but I'm not sure.


Dept. of Computer Science, Trinity College, Dublin 2, IRELAND



Wed, 15 Nov 1995 00:50:33 GMT  
 Using tuples in Miranda


 > (Jonathan E Hazan) writes:
 > ..
 > > possible using the type system of Miranda.  I suppose it would be
 > > possible in a language with type equivalence between tuples of type
 > > (*,**,***) and (*,(**,***)), though.
 >
 > Are there languages with this feature?

Caml (v3.1, not light), has this equivalence, since it knows only
pairs, and not tuples. That is, a type like ('a * 'b * 'c) is in fact
('a * ('b * 'c)). I see no theoretical problem in it, but it supposes
an implementation where tuples are represented using conses.

However it doesn't mean you can correctly type a function from lists
to tuples, or reverse:

#let pair_of_list [a;b] = a,b ;;
Value pair_of_list is <fun> : 'a list -> 'a * 'a

#let triple_of_list [a;b;c] = a,(b,c) ;;
Value triple_of_list is <fun> : 'a list -> 'a * 'a * 'a

As you can see types are different, so there is no hope you could do
something like that. For that you would need runtime type-checking,
accepting a function like:

#let rec tuple_of_list = fun [] -> ()
#       | (a::l) -> a,tuple_of_list l ;;
line 2: ill-typed phrase, a,tuple_of_list l has an instance of type:
'a * 'b which should match type: unit in function
   [] -> () | a::l -> a,tuple_of_list .

Jacques
--
---------------------------------------------------------------------------




Wed, 15 Nov 1995 12:42:29 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. PROP: tuple.index(), tuple.count()

2. PROP: tuple.index(), tuple.count() (fwd)

3. tuple to list to tuple conversion

4. exporting boost::tuples::tuple to python

5. Top 10 uses for Miranda

6. using GOFER/MIRANDA in a first-year CS course (2)

7. using GOFER/MIRANDA in a first-year CS course

8. using xview/x11 from miranda

9. using cPickle to store large number of tuples

10. Idiom: Using arrays to simulate tuples

11. ISE 4.5: Compiler bug concerning TUPLE[Generic] or maybe FUNCTION [ANY, TUPLE[Generic] ...

12. sorting list of tuples by second (third...) tuple item

 

 
Powered by phpBB® Forum Software