Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does left arrow <- mean outside a do block?

I came across with the following code recently and it bothers me a lot

lowerSafeForeignCall dflags block
| (entry, middle, CmmForeignCall { .. }) <- blockSplit block
= do
 -- do block stuffs
 -- Block doesn't end in a safe foreign call:
| otherwise = return block

This piece of code is from https://phabricator.haskell.org/rGHCb0534f78a73f972e279eed4447a5687bd6a8308e

in file compiler/cmm/CmmLayoutStack.hs

line 983

I really would like to konw what is this <- in the second line. I believe lowerSafeForeignCall is a function and the | and 'otherwise' indicate this function uses guards. So

(entry, middle, CmmForeignCall { .. }) <- blockSplit block

must be of type Bool. But the <- is outside any do block. I did some search online but still not a single clue about this usage.

like image 932
Theodora Avatar asked May 31 '19 10:05

Theodora


People also ask

What does this left arrow mean here?

what does this left arrow means here? It means "set", or assignment. So in most syntax, those lines are i = 0 and i = i + 1. This was probably avoided because in math, = means ==, and assignment isn't a useful concept. Thanks for contributing an answer to Mathematics Stack Exchange!

What is the arrow used for in math?

An arrow is a graphical symbol, such as ← or →, or a pictogram, used to point or indicate direction. In its simplest form, an arrow is a triangle, chevron, or concave kite, usually affixed to a line segment or rectangle, and in more complex forms a representation of an actual arrow (e.g. U+27B5).

What is the shape of an arrow?

In its simplest form, an arrow is a triangle, chevron, or concave kite, usually affixed to a line segment or rectangle, and in more complex forms a representation of an actual arrow (e.g. ➵ U+27B5). The direction indicated by an arrow is the one along the length of the line or rectangle towards the single pointed end.

What are the different types of arrows in Unicode?

Arrow appereance can be absolutly different: arrows can be wavy ↝, zigzag ↯, heavy , different directed ⥄, circle ⭮, double-headed , feathered or ribbon-like ⮵. Mostly there are horizontal arrows encoded in Unicode: to the left and to the right .


2 Answers

That's a pattern guard:

guard       →   pat <- infixexp      (pattern guard)

[...]

A guard has one of the following forms:

  • pattern guards are of the form p <- e, where p is a pattern (see Section 3.17) of type t and e is an expression type t. They succeed if the expression e matches the pattern p, and introduce the bindings of the pattern to the environment.

Where normal guards are limited to a boolean check, pattern guards can match against an arbitrary pattern and define local variables. (In your case entry, middle, and the contents of CmmForeignCall will be directly available in the function body.)

You can think of boolean guards as equivalent to pattern guards with a pattern of True:

| expr

works like

| True <- expr
like image 158
melpomene Avatar answered Oct 18 '22 02:10

melpomene


This is a pattern guard [Haskell-wiki]. Since Haskell'10 a guard is a list of qualifiers. A qualifier can be a condition (like in the old guards), and pattern guards.

Haskell will thus (lazily) evaluate the expression on the right side of the arrow <- and aim to match it with the pattern on the left of the arrow. If that succeeds, than the guard (well that part of the guard) is successful. If all the parts of the guard are successful, then the rule "fires".

In this specific case the only part of the pattern that might fail is the fact that the third item of the 3-tuple is not a CmmForeignCall data constructor. Furthermore by using this pattern guard, we can of course use entry, middle in the body of the expression.

like image 41
Willem Van Onsem Avatar answered Oct 18 '22 04:10

Willem Van Onsem