2

I think tuples in Haskell are like

  tuple :: (a,b)

which means a and b can be the same type or can be diffrent types

so if i define a function without giving the type for it then i will get probably (t,t1) or some diffrent types when i write :t function in ghci. So is it possible to get only the same types without defining it in function. I heard its not allowed in haskell

so i cant write some function like

  function [(x,x)]=[(x,x,x)]

to get the

  :t function
     function :: [(a,a)]->[(a,a,a)]

This is an exercise that i am trying to do and this exercise want me to write a function without defining a type.For example to get

  Bool->(Char,Bool) 

when i give

  :t function

in ghci. i should ve write--

  function True=('A',True)

i am not allowed to define the type part of a function So i cant write

  function::(Eq a)=>[(a,a)]->[(a,a,a)]

or something like that

6 Answers 6

6

You can use the function asTypeOf from the Prelude to restrict the type of the second component of your tuple to be the same as the type of the first component. For example, in GHCi:

> let f (x, y) = (x, y `asTypeOf` x, x)

> :t f
f :: (t, t) -> (t, t, t)
2
  • is there a any possible way to rename the 't' with 'a' ? May 16, 2013 at 11:45
  • 6
    No, as types are equal up to alpha-conversion, (t, t) -> (t, t, t) is really the same (a, a) -> (a, a, a); how GHC renders that type is really a detail of the implementation of the compiler and beyond your control. May 16, 2013 at 12:05
4

You can happily restrict the types to be equivalent .. by writing out the required type.

type Pair a = (a,a)
type Triple a = (a,a,a)

and then:

fn :: [Pair a] -> [Triple a]

will enforce the constraint you want.

1
  • Thanks for the answer but i think you got me wrong.This is an exercise that i am trying to do and this exercise want me to write a function without defining a type.For example to get Bool->(Char,Bool) when i give :t function in ghci. i should ve write-- function True=('A',True).i am not allowed to define the type part of a function. May 16, 2013 at 11:28
2

You can use type, as Don says. Or, if you don't want to bother with that (perhaps you only need it for one function), you can specify the type signature of the function like this:

function :: [(a,a)] -> [(a,a,a)]
function xs = map (\(a, b) -> (a, b, a)) xs -- just an example
2

I guess what you're looking for is the asTypeOf function. Using it you can restrict a type of some value to be the same as the one of another value in the function definition. E.g.:

Prelude> :t \(a, b) -> (a, b `asTypeOf` a)
\(a, b) -> (a, b `asTypeOf` a) :: (t, t) -> (t, t)
2

The following should work without asTypeOf:

trans (a,b) = case [a,b] of _ -> (a,b,a)
function xs = map trans xs
1

OK, if I've understood this correctly, the problem you're having really has nothing to do with types. Your definition,

function [(x,x)]=[(x,x,x)]

won't work because you're saying, in effect, "if the argument to function is a list with one element, and that element is a tuple, then call then bind x to the first part of the tuple and also bind x to the second part of the tuple". You can't bind a symbol to two expressions at once.

If what you really want is to ensure that both parts of the tuple are the same, then you can do something like this:

function [(x,y)] = if x == y then [(x,x,x)] else error "mismatch"

or this:

function2 [(x,y)] | x == y = [(x,x,x)]

...but that will fail when the parts of the tuple don't match.

Now I suspect what you really want is to handle lists with more than one element. So you might want to do something like:

function3 xs = map f xs
  where f (x, y) = if x == y then [(x,x,x)] else error "mismatch"

Any of these functions will have the type you want, Eq t => [(t, t)] -> [(t, t, t)] without you having to specify it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.