-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs arrears #152
Docs arrears #152
Changes from 15 commits
ee55161
eeaf349
dd5280d
34cac7f
b324988
2a8960f
bc0835f
01023df
6b258bb
35761a9
5eb80ff
a4e25b5
63580d7
949c006
475b931
ae8ff30
df8e54a
ec44d46
826256d
97f9568
7471bd1
0a59e24
530ca60
52fc4d6
bce402b
30e8e2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,37 @@ A journeyman of one's craft -- a woodworker, electrician, or a plumber, say -- a | |
|
||
@subsection{Be Intentional About Effects} | ||
|
||
Qi encourages a style that avoids "accidental" effects. A flow should either be pure (that is, it should be free of "side effects" such as printing to the screen or writing to a file), or its entire purpose should be to fulfill a side effect. It is considered inadvisable to have a function with sane inputs and outputs (resembling a pure function) that also performs a side effect. It would be better to decouple the effect from the rest of your function (@seclink["Use_Small_Building_Blocks"]{splitting it into smaller functions}, as necessary) and perform the effect explicitly via the @racket[effect] form, or otherwise escape from Qi using something like @racket[esc] (note that @seclink["Identifiers"]{function identifiers} used in a flow context are implicitly @racket[esc]aped) in order to perform the effect. This will ensure that there are no surprises with regard to @seclink["Order_of_Effects"]{order of effects}. | ||
Qi encourages a style that avoids "accidental" effects. | ||
|
||
In functional programming, "effects" refer to anything that the function does that is not captured in its inputs and outputs. This could include things like printing to the screen, writing to a file, or mutating a global variable. | ||
|
||
A @tech{flow} should either be pure (that is, free of such side effects), or its entire purpose should be to fulfill a side effect. It is considered inadvisable to have a function with sane inputs and outputs (resembling a pure function) that also performs a side effect. It would be better to decouple the effect from the rest of your function (@seclink["Use_Small_Building_Blocks"]{splitting it into smaller functions}, as necessary) and perform the effect explicitly via the @racket[effect] form, or otherwise escape from Qi using something like @racket[esc] (note that @seclink["Identifiers"]{function identifiers} used in a flow context are implicitly @racket[esc]aped) in order to perform the effect. This will ensure that there are no surprises with regard to @seclink["Order_of_Effects"]{order of effects}. | ||
|
||
For example, say that we wish to perform a simple numeric transformation on an input number, but also print the intermediate values to the screen. We might do it this way: | ||
|
||
@examples[ | ||
#:eval eval-for-docs | ||
#:label #f | ||
(define (my-square v) | ||
(displayln v) | ||
(sqr v)) | ||
|
||
(define (my-add1 v) | ||
(displayln v) | ||
(add1 v)) | ||
|
||
(~> (3) my-square my-add1) | ||
] | ||
|
||
This is considered poor style since we've mixed pure functions with implicit effects. Instead, following the above guideline, we would write it this way: | ||
|
||
@examples[ | ||
#:eval eval-for-docs | ||
#:label #f | ||
(~> (3) (ε displayln sqr) (ε displayln add1)) | ||
] | ||
|
||
This uses the pure functions @racket[sqr] and @racket[add1], extracting the effectful @racket[displayln] as an explicit @racket[effect]. | ||
|
||
@section{Debugging} | ||
|
||
|
@@ -172,9 +202,16 @@ Methodical use of @racket[gen] together with the @seclink["Using_a_Probe"]{probe | |
; in: lambda | ||
} | ||
|
||
@bold{Meaning}: The Racket interpreter received syntax, in this case simply "lambda", that it considers to be invalid. Note that if it received something it didn't know anything about, it would say "undefined" rather than "bad syntax." Bad syntax indicates known syntax used in an incorrect way. | ||
@bold{Meaning}: The expander (@seclink["It_s_Languages_All_the_Way_Down"]{either the Racket or Qi expander}) received syntax, in this case simply "lambda", that it considers to be invalid. Note that if it received something it didn't know anything about, it would say "undefined" rather than "bad syntax." Bad syntax indicates known syntax used in an incorrect way. | ||
|
||
@bold{Common example}: A Racket expression has not been properly escaped within a Qi context. For instance, @racket[(flow (lambda (x) x))] is invalid because the wrapped expression is Racket rather than Qi. To fix this, use @racket[esc], as in @racket[(flow (esc (lambda (x) x)))]. | ||
@bold{Common example}: A Racket expression has not been properly escaped within a Qi context. For instance, @racket[(☯ (lambda (x) x))] is invalid because the wrapped expression is Racket rather than Qi. To fix this, use @racket[esc], as in @racket[(☯ (esc (lambda (x) x)))]. | ||
|
||
@codeblock{ | ||
; not: bad syntax | ||
; in: not | ||
} | ||
|
||
@bold{Common example}: Similar to the previous one, a Racket expression has not been properly escaped within a Qi context, but in a special case where the Racket expression has the same name as a Qi form. In this instance, you may have used @racket[(☯ not)] expecting to invoke Racket's @racket[not] function, since @seclink["Identifiers"]{function identifiers may be used as flows directly} without needing to be escaped. But as Qi has a @racket[not] form as well, Qi's expander first attempts to match this against legitimate use of Qi's @racket[not], which fails, since this expects a flow as an argument and cannot be used in identifier form. To fix this in general, use an explicit @racket[esc], as in @racket[(☯ (esc not))]. In this specific case, you could also use Qi's @racket[(☯ NOT)] instead. | ||
|
||
@bold{Common example}: Trying to use a Racket macro (rather than a function), or a macro from another DSL, as a @tech{flow} without first registering it via @racket[define-qi-foreign-syntaxes]. In general, Qi expects flows to be functions unless otherwise explicitly signaled. | ||
|
||
|
@@ -356,16 +393,18 @@ So in general, use mutable values with caution. Such values can be useful as sid | |
|
||
@subsubsection{Order of Effects} | ||
|
||
Qi flows may exhibit a different order of effects (in the functional programming sense) than equivalent Racket functions. | ||
Qi @tech{flows} may exhibit a different order of effects (in the @seclink["Be_Intentional_About_Effects"]{functional programming sense}) than equivalent Racket functions. | ||
|
||
Consider the Racket expression: @racket[(map sqr (filter odd? (list 1 2 3 4 5)))]. As this invokes @racket[odd?] on all of the elements of the input list, followed by @racket[sqr] on all of the elements of the intermediate list, if we imagine that @racket[odd?] and @racket[sqr] print their inputs as a side effect before producing their results, then executing this program would print the numbers in the sequence @racket[1,2,3,4,5,1,3,5]. | ||
|
||
The equivalent Qi flow is @racket[(~> ((list 1 2 3 4 5)) (filter odd?) (map sqr))]. As this sequence is @seclink["Don_t_Stop_Me_Now"]{"deforested" by Qi's compiler} to avoid multiple passes over the data and the memory overhead of intermediate representations, it invokes the functions in sequence @emph{on each element} rather than @emph{on all of the elements of each list in turn}. The printed sequence with Qi would be @racket[1,1,2,3,3,4,5,5]. | ||
The equivalent Qi flow is @racket[(~> ((list 1 2 3 4 5)) (filter odd?) (map sqr))]. As this sequence is @seclink["Don_t_Stop_Me_Now"]{deforested by Qi's compiler} to avoid multiple passes over the data and the memory overhead of intermediate representations, it invokes the functions in sequence @emph{on each element} rather than @emph{on all of the elements of each list in turn}. The printed sequence with Qi would be @racket[1,1,2,3,3,4,5,5]. | ||
|
||
Yet, either implementation produces the same output: @racket[(list 1 9 25)]. | ||
|
||
So, to reiterate, while the output of Qi flows will be the same as the output of equivalent Racket expressions, they may nevertheless exhibit a different order of effects. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This statement is wrong. The output of a function can depend on effects, so there's no guarantee the output of a flow using effectful functions will be the same as if you hadn't reordered things with deforestation. e.g. mapping with a function add-count that uses a global variable:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've fixed this, thanks! I'm still thinking about how best to phrase the other section re: your other comment and what the precise guarantees about effects are that we provide. |
||
|
||
If you'd like to ensure a particular order of effects, use @racket[effect] at the appropriate points in your flow. If you'd like to use Racket's order of effects, define your flow using @racket[esc] (although this would lose any Qi compiler optimizations). | ||
|
||
@section{Effectively Using Feedback Loops} | ||
|
||
@racket[feedback] is Qi's most powerful looping form, useful for arbitrary recursion. As it encourages quite a different way of thinking than Racket's usual looping forms do, here are some tips on "grokking" it. | ||
|
@@ -463,9 +502,9 @@ Using this approach, you would need to register each such foreign macro using @r | |
|
||
@subsection{Bindings are an Alternative to Nonlinearity} | ||
|
||
In some cases, we'd prefer to think of a nonlinear @tech{flow} as a linear sequence on a subset of arguments that happens to need the remainder of the arguments somewhere down the line. In such cases, it is advisable to employ bindings so that the flow can be defined on this subset of them and employ the remainder by name. | ||
In some cases, we'd prefer to think of a nonlinear @tech{flow} as a linear sequence on a subset of arguments that happens to need the remainder of the arguments somewhere down the line. In such cases, it is advisable to employ @seclink["Binding"]{bindings} so that the flow can be defined on this subset of them and employ the remainder by name. | ||
|
||
For example, these are equivalent: | ||
For example, for a function called @racket[make-document] accepting two arguments that are the name of the document and a file object, these implementations are equivalent: | ||
|
||
@codeblock{ | ||
(define-flow make-document | ||
|
@@ -477,8 +516,8 @@ For example, these are equivalent: | |
} | ||
|
||
@codeblock{ | ||
(define (make-document name file) | ||
(~>> (file) | ||
(define-flow make-document | ||
(~>> (== (as name) _) | ||
file-contents | ||
(parse-result document/p) | ||
△ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like "considered inadvisable" here. I think it would be better to make the reasons explicit up front. Something like, "Qi may reorder operations not marked as effects, so it's better to separate effects from other computations to ensure they work as expected".
I don't think I understand how
esc
andeffect
relate to suppressing effect re-ordering. Are you promising that neither will ever be reordered, or are you promising thateffect
will never be reordered but also suggesting that pairs of effects inside the sameesc
in Racket code will of course not be re-ordered relative to each other, but not making any promises re: pairs of effects in separateesc
forms?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, something like the latter. I think we are promising:
esc
, anything inside that will be untouched (but it may still be reordered wholesale at a higher level)effect
, the effect will happen along with the annotated flow, whenever that happens (but the whole side-effecting flow(effect f g)
may itself be moved around)To take an example, with this normalization rule:
It would rewrite:
Here, the effect stays with the flow it annotated, but it happens at a different time.
So what we are promising may be some kind of "effect locality" rather than any particular order of effects. Tbh I'm not sure if this is a good way to think about it and what exactly our guarantees imply. I will think about it some more.
On a side note, the former of these bullets (re:
esc
) makes me feel that matching literal uses ofracket/list
'smap
,filter
, etc. is probably not the right long term thing, and that we'd probably want aqi/list
language that actually includesmap
andfilter
forms. This would ensure that we do not cross theesc
boundary to do optimizations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the only way to really guarantee order of effects is to wrap each flow in
effect
? Maybe? [I don't think this will matter most of the time to most people, and it suggests that highly effectful programs benefit from a more "direct" imperative style.]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if wrapping every flow with
effect
would guarantee an order, except that doing so could mean that no compiler pattern would match (but in this case, if there are just a lot of emptyeffect
forms, we would likely normalize that away in any case, like(effect ground flo)
should, I think, be rewritten toflo
).Here's a first attempt at formalizing our guarantees about effects:
For two flows
f
andg
, we could define a relation "g is downstream of f" as the outputs off
are used, either directly or transitively, as inputs tog
.Then, our guarantees about effects are:
effect
s onf
will occur before any effects ong
.effect
s on any flowφ
will happen whenφ
is called.That is, we guarantee that an
effect
will never be separated from the flow it annotates (what we could call "locality"), though when this is invoked is not guaranteed, aside from point (1) above.Do we feel this is a useful characterization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might seem silly, but do we optimize either of the flows within
(effect f g)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we would optimize both.