Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a function on a subset of an existing type?

Tags:

haskell

I'm new to Haskell and would like to know whether it's possible to define a function that is only defined on a subset of an already existing type, without actually having to define a new type.

Example: I want to create a function that only accepts even integers (or even natural numbers, etc.) and returns, e.g. that number squared, like:

squared :: 2*Integer -> Integer
squared n = n*n

The above two lines do not work, of course.

I know I could write it like this:

squared' :: Integer -> Integer
squared' n 
  | (even n) = n*n
  | otherwise = error "n is not even!"

or something similar, but I want to know whether something like the non-working example is possible, as well.

I hope this question is not completely stupid (or was already answered) but I really don't know a lot of Haskell yet (so searching for an answer was kind of difficult as well)...

like image 466
Lustique Avatar asked Nov 01 '13 15:11

Lustique


People also ask

What are the different types of subsets in math?

Subsets are classified as. Proper Subset. Improper Subsets. A proper subset is one that contains few elements of the original set whereas an improper subset, contains every element of the original set along with the null set.

Can a subset contain all the elements in a set?

That is, a subset can contain all the elements that are present in the set. The subsets of any set consists of all possible sets including its elements and the null set. Let us understand with the help of an example.

What is the difference between a subset and superset?

If a set A is a collection of even number and set B consists of {2,4,6}, then B is said to be a subset of A, denoted by B⊆A and A is the superset of B. Learn Sets Subset And Superset to understand the difference. The elements of sets could be anything such as a group of real numbers, variables, constants, whole numbers, etc.

How do you find the number of subsets of a function?

Number of subsets: {2}, {4}, {6}, {2,4}, {4,6}, {2,6}, {2,4,6} and Φ or {}. There is no particular formula to find the subsets, instead, we have to list them all, to differentiate between proper and improper one.


2 Answers

In general no. Such a thing is called a subset type, it's a hallmark of dependent types which Haskell doesn't have. Usually it's implemented by boxing a value with a proof that the value satisfies some property, but since we have no notion of proofs in Haskell, we're stuck.

Usually the way to fake it is with "smart constructors".

newtype Even = Even {unEven :: Integer} deriving (Eq, Show, Ord)

toEven :: Integer -> Maybe Even
toEven a | even a = Just $ Even a
         | otherwise = Nothing

And then hide the Even constructor.

If you really really want it, you can switch to a language that can interop with Haskell that has dependent types (Coq and Agda spring to mind).

like image 129
Daniel Gratzer Avatar answered Oct 22 '22 01:10

Daniel Gratzer


No. The type system would need to support refinement types (or full dependent types, as suggested by @jozefg).

Here is a Haskell extension with refinement types.

http://goto.ucsd.edu/~rjhala/liquid/haskell/blog/blog/2013/01/01/refinement-types-101.lhs/

like image 36
seanmcl Avatar answered Oct 22 '22 01:10

seanmcl