Can I attach a unique, run time determined tag to a selection of objects? - haskell

Consider type B and its subtype A, determined by a predicate P. An instance would be natural numbers for B and prime numbers for A, with some primality test as P. It is straightforward to implement a smart constructor for such A, defined as a newtype or a Tagged.
Now suppose the subtyping predicate is not completely determined at compile time. For example, let P be membership in a collection determined by IO input: the operator enters the coefficients for an integral polynomial and we obtain a predicate that verifies that a given number is a value of that polynomial at some index.
Can I make sure that each polynomial, and the values validated for it, are tagged in a fashion that makes them compatible with each other and incompatible with any other polynomial? Among the operations I am going to need is conversion between values and their indices, and I want to type safeguard them to avoid confusion.
This is how I imagine it to be:
polynomial :: [Integer] -> Polynomial unique
toValue :: Integer -> Polynomial unique -> Value unique
fromValue :: Integer -> Polynomial unique -> Maybe (Index unique)
toValue' :: Index unique -> Value unique
fromValue' :: Value unique -> Index unique
The point is to obtain these latter two total functions.
But I have no idea how to go about defining this polynomial function. Where would it get the unique type from?

To do it in an ST-like way you would need a library of functions to operate on the Value s and Polynomial s types, and then have a function with a type like this:
withPolynomial :: (forall s . Polynomial s -> Integer) -> [Integer] -> Integer
This will convert the [Integer] into a Polynomial s and pass it to the argument function. This function will then do whatever it wants, using your library of functions where appropriate. Any Value s values are guaranteed not to leak from the enclosing withPolynomial application, so they can't get mixed up. However this will also prevent you from storing polynomials and their values for use in future computations.

Related

Type variables in function signature

If I do the following
functionS (x,y) = y
:t functionS
functionS :: (a, b) -> b
Now with this function:
functionC x y = if (x > y) then True else False
:t function
I would expect to get:
functionC :: (Ord a, Ord b) => a -> b -> Bool
But I get:
functionC :: Ord a => a -> a -> Bool
GHCI seems to be ok with the 2 previous results, but why does it give me the second? Why the type variable a AND b aren't defined?
I think you might be misreading type signatures. Through no fault of your own––the examples you using to inform your thinking are kind of confusing. In particular, in your tuple example
functionS :: (a,b) -> b
functionS (x,y) = y
The notation (_,_) means two different things. In the first line, (a,b) refers to a type, the type of pairs whose first element has type a and second has type b. In the second line, (x,y) refers to a specfiic pair, where x has type a and y has type b. While this "pun" provides a useful mnemonic, it can be confusing as you are first getting the hang of it. I would rather that the type of pairs be a regular type constructor:
functionS :: Pair a b -> b
functionS (x,y) = y
So, moving on to your question. In the signature you are given
functionC :: Ord a => a -> a -> Bool
a is a type. Ord a says that elements of the type a are orderable with respect to each other. The function takes two arguments of the same type. Some types that are orderable are Integer (numerically), String (lexicographically), and a bunch of others. That means that you can tell which of two Integers is the smaller, or which of two Strings are the smaller. However we don't necessarily know how to tell whether an Integer is smaller than a String (and this is good! Have you seen what kinds of shenanigans javascript has to do to support untyped equality? Haskell doesn't have to solve this problem at all!). So that's what this signature is saying –– there is only one single orderable type, a, and the function takes two elements of this same type.
You might still be wondering why functionS's signature has two different type variables. It's because there is no constraint confining them to be the same, such as having to order them against each other. functionS works equally well with a pair where both components are integers as when one is an integer and the other is a string. It doesn't matter. And Haskell always picks the most general type that works. So if they are not forced to be the same, they will be different.
There are more technical ways to explain all this, but I felt an intuitive explanation was in order. I hope it's helpful!

How does these two notations differ in Haskell?

I am new to Haskell and still kind of confused with some notations.
In the function header, i know that
func :: [Int] -> Int
indicates that the input is a list of integers and the output is an integer.
How does this differ from
func :: (Ord a) => [a] -> a
I am asking because they seem to be same, and I wonder why we use different notations for something identical.
The first one is very simple and as you have said, it takes a list of Int and returns a single Int.
The second one, however, can accept many different types for its input (including types you define yourself).
The key is (Ord a). What this is saying is that it has to be a list of orderable types, and if it satisfies that requirement then it is a valid type that can be passed into this particular function.
The Ord typeclass includes the following members:
<
<=
>
>=
So
func :: (Ord a) => [a] -> a
could potentially be a function that takes a list of orderable types and returns the maximum member of that list, as an example. This could be [Int], [Integer], [Float], and many other things.

Infinite (or lazy) sampling of a probability distribution

I would like to have a function that returns a lazy infinite list of integers following a poisson distribution, with a similar type signature to this one:
samplePoisson :: MonadRandom m -> Double -> m [Int]
where the Double argument is the lambda parameter of the poisson distribution.
I have looked into some libraries (statistics, random-fu) but none of them provide what i want.
Edit:
Yes, both libraries provide functions for generating single values that follow a distribution, which can be easily extended to generate a finite list by using replicateM.
Why do I want an infinite list? Because I don't know a priori the number of elements that must be generated.
Let me show an example.
Imagine I have a function that returns the position of the first 1 in a list.
findFirst1 :: [Int] -> Int
If I had the samplePoisson function for generating a lazy list, then I could write:
findFirst1 <$> samplePoisson 3.0
However, if I am not able to generate a lazy list, then I would have to write a new function with a type signature similar to this one (using the random-fu library):
findFirst1M :: RVar Int -> RVar Int
Hence, I lose the function findFirst1 that I originally had.
I hope I made myself clear this time.

forall as an intersection over those sets

I have been reading the existential section on Wikibooks and this is what is stated there:
Firstly, forall really does mean 'for all'. One way of thinking about
types is as sets of values with that type, for example, Bool is the
set {True, False, ⊥} (remember that bottom, ⊥, is a member of every
type!), Integer is the set of integers (and bottom), String is the set
of all possible strings (and bottom), and so on. forall serves as an
intersection over those sets. For example, forall a. a is the intersection over all types, which must be {⊥}, that is, the type (i.e. set) whose only value (i.e. element) is bottom.
How does forall serve as an intersection over those sets ?
forall in formal logic means that it can be any value from the universe of discourse. How does in Haskell it gets translated to intersection ?
Haskell's forall-s can be viewed as restricted dependent function types, which I think is the conceptually most enlightening approach and also most amenable to set-theoretic or logical interpretations.
In a dependent language one can bind the values of arguments in function types, and mention those values in the return types.
-- Idris
id : (a : Type) -> (a -> a)
id _ x = x
-- Can also leave arguments implicit (to be inferred)
id : a -> a
id x = x
-- Generally, an Idris function type has the form "(x : A) -> F x"
-- where A is a type (or kind/sort, or any level really) and F is
-- a function of type "A -> Type"
-- Haskell
id :: forall (a : *). (a -> a)
id x = x
The crucial difference is that Haskell can only bind types, lifted kinds, and type constructors, using forall, while dependent languages can bind anything.
In the literature dependent functions are called dependent products. Why call them that, when they are, well, functions? It turns out that we can implement Haskell's algebraic product types using only dependent functions.
Generally, any function a -> b can be viewed as a lookup function for some product, where the keys have type a and the elements have type b. Bool -> Int can be interpreted as a pair of Int-s. This interpretation is not very interesting for non-dependent functions, since all the product fields must be of the same type. With dependent functions, our pair can be properly polymorphic:
Pair : Type -> Type -> Type
Pair a b = (index : Bool) -> (if index then a else b)
fst : Pair a b -> a
fst pair = pair True
snd : Pair a b -> b
snd pair = pair False
setFst : c -> Pair a b -> Pair c b
setFst c pair = \index -> if index then c else pair False
setSnd : c -> Pair a b -> Pair a c
setSnd c pair = \index -> if index then pair True else c
We have recovered all the essential functionality of pairs here. Also, using Pair we can build up products of arbitrary arity.
So, how does is tie in to the interpretation of forall-s? Well, we can interpret ordinary products and build up some intuition for them, and then try to transfer that to forall-s.
So, let's look a bit first at the algebra of ordinary products. Algebraic types are called algebraic because we can determine the number of their values by algebra. Link to detailed explanation. If A has |A| number of values and B has |B| number of values, then Pair A B has |A| * |B| number of possible values. With sum types we sum the number of inhabitants. Since A -> B can be viewed as a product with |A| fields, all having type B, the number of the inhabitants of A -> B is |A| number of |B|-s multiplied together, which equals |B|^|A|. Hence the name "exponential type" that is sometimes used to denote functions. When the function is dependent, we fall back to the "product over some number of different types" interpretation, since the exponential formula no longer fits.
Armed with this understanding, we can interpret forall (a :: *). t as a product type with indices of type * and fields having type t, where a might be mentioned inside t, and thus the field types may vary depending on the choice of a. We can look up the fields by making Haskell infer some particular type for the forall, effectively applying the function to the type argument.
Note that this product has as many fields as many values of indices there are, which is pretty much infinite here, considering the potential number of Haskell types.
You have to view types in either negative or positive context—i.e. either in the process of construction or the process of use (have/receive and this is all probably best understood in Game Semantics, but I am not familiar with them).
If I "give you" a type forall a . a then you know I must have constructed it somehow. The only way for a particular constructed value to have the type forall a . a is that it could be a stand-in "for all" types in the universe of discourse—which is, of course, the intersection of their functionality. In sane languages no such value exists (Void), but in Haskell we have bottom.
bottom :: forall a . a
bottom = let a = a in a
On the other hand, if I somehow magically have a value of forall a . a and I attempt to use it then we get the opposite effect—I can treat it as anything in the union of all types in the universe of discourse (which is what you were looking for) and thus I have
absurd :: (forall a . a) -> b
absurd a = a
How does forall serve as an intersection over those sets ?
Here you may benefit from starting to read a bit about the Curry-Howard correspondence. To make a long story short, you can think of a type as a logical proposition, language expressions as proofs of their types, and values as normal form proofs (proofs that cannot be simplified any further). So for example, "Hello world!" :: String would be read as ""Hello world!" is a proof of the proposition String."
So now think of forall a. a as a proposition. Intuitively, think of this as a second-order quantified statement over a propositional variable: "For all statements a, a." It's basically asserting all propositions. This means that if x is a proof of forall a. a, then for any proposition P, x is also a proof of P. So, since the proofs of forall a. a are the proofs that prove any propositions, then it must follow that the proofs of forall a. a must be the same as what you'd get if you mapped each proposition to the set of its proofs and took their intersection. And the only normal-form proof (i.e. "value") that is common to all those sets is bottom.
Another informal way to look at it is that universal quantification is like an infinite conjunction (∀x.P(x) is like P(c0) ∧ P(c1) ∧ ...). Conjunction, seen from a model-theoretical view, is set intersection; the set of evaluation environments where A ∧ B is true is the intersection of the environments where A is true and the ones where B is true.

What is this abstract data type called?

I'm writing Haskell, but this could be applied to any OO or functional language with a concept of ADT. I'll give the template in Haskell, ignoring the fact that the arithmetic operators are already taken:
class Thing a where
(+) :: a -> a -> a
(-) :: a -> a -> a
x - y = x + negate y
(*) :: (RealFrac b) => a -> b -> a
negate :: a -> a
negate x = x * (-1)
Basically these are things that can be added and subtracted and also multiplied by real fractional values. One example might be a simple list of numbers: addition and subtraction are pairwise (in Haskell, "(+) = zipWith (+)"), and multiplication by a real multiplies every item in the list by the same amount. I've come across enough other examples to want to define it as a class, but I don't know exactly what to call it.
In Haskell its usually a monoid provided there is some kind of zero value.
Is this some known kind of object in the zoo of algebraic types? I've looked through rings, semirings, nearsemirings, groups etc without finding it.
This is a vector space: http://en.wikipedia.org/wiki/Vector_space. You have addition and scalar multiplication.

Resources