Why is it impossible to define: = in the GHC?

Why can't I define: = operator in GHC? Can this limitation be eliminated in future releases?

It displays here:

[1 of 1] Compiling Images           ( Images.hs, interpreted )

Images.hs:19:1:
    Invalid type signature: (:=) :: HasSetter s => s a -> a -> IO ()
    Should be of form <variable> :: <type>
+4
source share
1 answer

Constructors versus functions

Constructors create new data types from old data. Let's expand our own list:

data List a = Empty | Cons a (List a)

myList = Const 1 (Cons 2 Empty) -- 1:2:[] =[1,2]

uncons x' (Cons x xs) = if x == x' then xs else Cons x xs

Here Cons :: a -> List ais a special function that takes an element and a list and makes a longer list.

It is important that there is a difference between design functions and regular functions, so the compiler knows which one is valid in the template:

headM (Cons x xs) = Just x
headM Empty = Nothing

This makes sense, but it is not:

previousHead (uncons x xs) = Just x
previousHead xs = Nothing

because how can a computer know which item you deleted or did you delete it?

Infix Constructors and Functions

, , ,

data [a] = [] | a:[a]

, 1:2:[].

Infix , x:xs , infix ( infix, :, , .

- , , , . : , - , .

: =

:= ,

data Assignment a = Variable := Expression a

:=, , : , , - , (, ) :

(.:=) :: Maybe a -> a -> Maybe a
Nothing .:= x   = Just x
Just y  .:= x   = Just x
+9

Source: https://habr.com/ru/post/1525893/


All Articles