简体   繁体   中英

Understanding type synonyms and parameterized type synonyms in Haskell

I have the follow type:

type Name = String

type Assignation a = Name -> a

and the following function declaration:

actAsig :: Assignation a -> Name -> a -> Assignation a

This function should return a new Assignation that returns the newly assigned value for the supplied Name , and the previously-assigned values for other Name s.

The specification of actAsig , if I understand right, is that given

assignation1 :: Assignation a
newName :: Name
newValue :: a

assignation2 = actAsig assignation1 newName newValue

The requirement on assignation2 is that

assignation2 name = if name == newName then newValue else assignation1 name

So, you can write it exactly like that:

actAsig :: Assignation a -> Name -> a -> Assignation a
actAsig assignation1 newName newValue name = 
    if name == newName then newValue else assignation1 name

Cactus has already brilliantly addressed any questions you might have had about the implementation of the actAsig function, however, I feel that the questions in the title of the post have been left unaddressed.


First of all, Haskell has both a value level language ( Object Language ) and a type level language ( Metalanguage ). They are both powerful but not normally equally powerful, at least not for the same things. However, some things work on both sides, such as constants, variables and functions.

A type level constant is:

type Name = String

— this is normally called a "type alias": it means Name is made in (almost) all respects equivalent to String . This doesn't introduce a new type, only a new name for an existing type. This is useful for making the intent in your code more readable if you don't want to introduce an actual new type with data or newtype , in which case you'd be introducing a new set of values that need to be mapped from and to existing sets of values (ie types). Therefore, type is just a thing of convenience, if you will, not a measure of safety, which is instead what data and newtype are for.

However, the Haskell type level language also supports functions, ie mappings from 1 or more types to some existing type. You could consider this to be parametric aliasing, so to speak.

type Assignation a = Name -> a

This creates a type function called Assignation with a parameter a . Given a type a , it returns some type, in this case the type Name -> a . So Assignation Int returns Name -> Int ; Assignation Person returns Name -> Person and so on. Note that sometimes type functions don't make use of one or more of their parameters, just like value level functions:

type Empty a = ()

If we combine this new knowledge, we can start thinking in terms of type level evaluation/reduction, ie evaluation that only takes places within the borders of type signatures, type functions, type class constraint computations etc.

Let's apply this to the type signature of actAsig , step by step:

Assignation a -> Name   -> a -> Assignation a
Assignation a -> String -> a -> Assignation a
(Name -> a)   -> String -> a -> (Name -> a)
(String -> a) -> String -> a -> (String -> a)

So the higher order function type you are seeing on the last line above, is the actual type of actAsig , all abstractions eliminated/reduced away.

In human language terms, the signature descirbes a function that

  1. takes a function f that maps strings to values of some type a .
  2. takes a string
  3. takes a value of that type a
  4. returns a new function of the same type as f .

So in a way, actAsig processes functions: it takes functions and returns new, possibly modified functions.


Furthermore, Haskell also has (somewhat basic) means of fuzzying the line between typelevel and value-level, by making computations in the type realm involve references (dependencies) into the value level, but that's outside of the scope of this post, and would lead us into the world of Dependent Types and Dependent Type Theory .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM