Is it a pure function? - scala

I have following function:
def timestamp(key: String)
: String
= Monoid.combine(key, Instant.now().getEpochSecond.toString)
and wanted to know, if it is pure or not? A pure function for me is, given the same input returns always the same output. But the function above, given always the same string will returns another string with another time, that it is in my opinion not pure.

No, it's not pure by any definition I know of. A good discussion of pure functions is here: https://alvinalexander.com/scala/fp-book/definition-of-pure-function. In Alvin's definition of purity he says:
A pure function has no “back doors,” which means:
...
It cannot depend on any external I/O. It can’t rely on input from files, databases, web services, UIs, etc; it can’t produce output, such as writing to a file, database, or web service, writing to a screen, etc.
Reading the time of the current system uses I/O so it is not pure.

You are right, it is not a pure function as it returns different result for the same arguments. Mathematically speaking it is not a function at all.
Definition of Pure function from Wikipedia
The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices (usually—see below).
Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices (usually—see below).

Related

AppleScript pass expression into function to be re-evaluated repeatedly? (or: AppleScript handler with callback?)

I think the correct description for what I'm trying to do is be able to pass an expression or function/handler into another handler as a parameter/argument. Some code to be evaluated inside the receiving handler. Similar to Javascript callbacks, I think.
For example, something like this:
on waitFor(theConditionExpression)
timeout_start(5) -- start a 5 second timer
repeat until (theConditionExpression or timeout_isExpired())
delay 0.1
end repeat
return theConditionExpression
end waitFor
theConditionExpression should be some expression or function that evaluates to a boolean result.
not really relevant to the question, but just FYI, timeout_start(…) and timeout_isExpired() are two simple handlers I've written that do exactly what they say. (…start() doesn't return anything, …isExpired() returns a boolean).
Of course, typically if I pass in some boolean expression, it will evaluate that expression once, at the time I pass it in. But I want it to evaluate it every time it's referenced in the code inside the handler.
Some languages (not sure about AS) have some kind of eval() function that you can pass it some code as a string and it will execute that string as code. Theoretically that could solve this, but: (a) I don't know if AS has anything like that, but even if it does, (b) it's not desired for various reasons (performance, injection risks, etc.)
So I'm thinking something more like eg. JavaScript's ability to pass in a function (named or anonymous) as function parameter/argument that can be re-evaluated every iteration in a loop, etc. (eg. like the compareFn argument in JS's Array.sort(compareFn)).
Can AS do anything like this, and if so how?
Thanks!
I'm going to suggest (pro forma) that an AppleScript application with an on idle handler is generally a better solution for wait conditions than a repeat/delay loop. It's more efficient for the system, and doesn't freeze up the script. But that would involve reconceptualizing your script, and I'm not certain it would work in this case, given the way you formed the problem.
There's an old but good site called AppleScript Power Handlers that shows a bunch of nifty-neato tricks for sophisticated use of AppleScript handlers: passing handlers as values or parameters; creating Script Objects within handlers; making closures and constructors. I'm pretty sure the answer to your request is in there. aLikely you'll want to set up a bunch of handlers that serve as condition expressions, then pass them as parameters to the evaluating handler. Or maybe you'll want to set up a script object containing the condition handlers and call it as needed?
At any rate, see what you can do with it, and ask more specific questions if you run into problems.

Simulink code generation: function stubs from Function Caller blocks and their return values/arguments

In my Simulink model I have several Function Caller blocks like this:
Simple Function Caller block
The function prototype would simply be y = someFunction(). The output argument uses a custom enum type and is given as someEnum(1).
The output signal is defined as one-dimensional throughout.
When generating code from the model, these Function Callers have always yielded a function stub in the expected form of
extern someEnum someFunction(void);.
However, after a lot of changes recently, I've just noticed that code generation now suddenly yields function stubs in the form of
extern void someFunction(someEnum *rty_y);
for some (not all!) Function Caller blocks.
I have compared every parameter about the Function caller blocks and the related output signals that I could find but I can't find any difference between the affected ones and those working as expected in the current version or the same blocks in previous versions. All functions and signals have been renamed, but that's also true for those Function Caller blocks that are not affected.
The Code Generation options are also identical.
I have tried to understand from the help files what might cause the coder to use pointer arguments instead of direct return values for the function stubs but couldn't find anything.
Any hint at what might cause the code generator to use pointers would be greatly appreciated.
Found the problem. Some of the affected blocks had their C/C++ return argument set to "void" in their "Configure C/C++ Function Interface" dialog.
Some of the affected blocks (unfortunately, both of those I had checked before as well) were still set to "y" here and I had to change the setting to "void" and back to "y" before it yielded the desired result.

How does a fuzzer deal with invalid inputs?

Suppose that I have a program that takes a pointer as its input. Without prior knowledge about the structure of the pointee, how does a fuzzer create valid inputs that can actually hits the internal of the program? To make this more concrete, imagine an artificial C program
int myprogram (unknow_pointer* input){
printf("%s", input->name);
}
In some situations, the tested program first checks the input format. If the input format is not good, it raises an exception. In such situations, how can a fuzzer reach program points beyond that exception-raising statement?
Most fuzzers don't know anything about the internal structure of the program. Different fuzzers dealt with this in a various ways:
Not deal with it at all. Just throw random inputs and hope to produce an input that will pass some/all checks. (for example - radamasa)
Mutate a valid input - take a known valid input, and mutate it (flip bits, remove parts, add parts, etc.) in many cases it will be valid enough to pass some or all of the checks. For example - if you want to fuzz VLC, you will take a valid movie file as the input for the fuzzer, which will provide mutations of it to VLC. Those are often called mutation based fuzzers. (for example - zzuf)
If you have prior knowledge of the input's structure, build a model of the input, and then mutate specific fields within it. A big advantage of such method is the ability to deal with very specific types of fields - checksums, hashes, sizes, etc. Those are often called generation based fuzzers. (for example - spike, sulley and their successors, peach)
However, in recent years a new kind of fuzzers was evolved - feedback based fuzzers - these fuzzers perform mutations on a valid (or not) input, and based on feedback they receive from the fuzzed program they decide how and what to mutate next. The feedback is received by instrumenting the program execution, either by injection tracing in compile time, injecting the tracing code by patching the program in runtime, or using hardware tracing mechanisms. First among them is AFL (you can read more about it here).
A fuzzer throws every sort of random combination of inputs at the attack surface. The intention is to look for any opportunity for a "golden BB" to get past the input checks and get a response that can be further explored.

What does the "world" mean in functional programming world?

I've been diving into functional programming for more than 3 years and I've been reading and understanding many articles and aspects of functional programming.
But I often stumbled into many articles about the "world" in side effect computations and also carrying and copying the "world" in IO monad samples. What does the "world" means in this context? Is this the same "world" in all side effect computation context or is it only applied in IO monads?
Also the documentation and other articles about Haskell mention the "world" many times.
Some reference about this "world":
http://channel9.msdn.com/Shows/Going+Deep/Erik-Meijer-Functional-Programming
and this:
http://www.infoq.com/presentations/Taming-Effect-Simon-Peyton-Jones
I expect a sample, not just explanation of the world concept. I welcome sample code in Haskell, F#, Scala, Scheme.
The "world" is just an abstract concept that captures "the state of the world", i.e. the state of everything outside the current computation.
Take this I/O function, for example:
write : Filename -> String -> ()
This is non-functional, as it changes the file (whose content is part of the state of the world) by side effect. If, however, we modelled the world as an explicit object, we could provide this function:
write : World -> Filename -> String -> World
This takes the current world and functionally produces a "new" one, with the file modified, which you can then pass to consecutive calls. The World itself is just an abstract type, there is no way to peek at it directly, except through corresponding functions like read.
Now, there is one problem with the above interface: without further restrictions, it would allow a program to "duplicate" the world. For example:
w1 = write w "file" "yes"
w2 = write w "file" "no"
You've used the same world w twice, producing two different future worlds. Obviously, this makes no sense as a model for physical I/O. To prevent examples like that, a more fancy type system is needed that makes sure that the world is handled linearly, i.e., never used twice. The language Clean is based on a variation of this idea.
Alternatively, you can encapsulate the world such that it never becomes explicit and thereby cannot be duplicated by construction. That is what the I/O monad achieves -- it can be thought of as a state monad whose state is the world, which it threads through the monadic actions implicitly.
The "world" is a concept involved in one kind of embedding of imperative programming into a purely functional language.
As you most certainly know, purely functional programming requires the result of a function to depend exclusively on the values of the arguments. So suppose we want to express a typical getLine operation as a pure function. There are two evident problems:
getLine can produce a different result each time it's called with the same arguments (no arguments, in this case).
getLine has the side effect of consuming some portion of a stream. If your program uses getLine, then (a) each invocation of it must consume a different part of the input, (b) each part of the program's input must be consumed by some invocation. (You can't have two calls to getLine reading the same input line twice unless that line occurs twice in the input; you can't have the program randomly skip a line of input either.)
So getLine just can't be a function, right? Well, not so fast, there's some tricks we could do:
Multiple calls to getLine can return different results. To make that compatible with purely functional behavior, this means that a purely functional getLine could take an argument: getLine :: W -> String. Then we can reconcile the idea of different results on each call by stipulating that each call must be made with a different value for the W argument. You could imagine that W represents the state of the input stream.
Multiple calls to getLine must be executed in some definite order, and each must consume the input that was left over from the previous call. Change: give getLine the type W -> (String, W), and forbid programs from using a W value more than once (something that we can check at compilation). Now to use getLine more than once in your program you must take care to feed the earlier call's W result to the succeeding call.
As long as you can guarantee that Ws are not reused, you can use this sort of technique to translate any (single-threaded) imperative program into a purely functional one. You don't even need to have any actual in-memory objects for the W type—you just type-check your program and analyze it to prove that each W is only used once, then emit code that doesn't refer to anything of the sort.
So the "world" is just this idea, but generalized to cover all imperative operations, not just getLine.
Now having explained all that, you may be wondering if you're better off knowing this. My opinion is no, you aren't. See, IMO, the whole "passing the world around" idea is one of those things like monad tutorials, where too many Haskell programmers have chosen to be "helpful" in ways that actually aren't.
"Passing the world around" is routinely offered as an "explanation" to help newbies understand Haskell IO. But the problem is that (a) it's a really exotic concept for many people to wrap their heads around ("what do you mean I'm going to pass the state of the whole world around?"), (b) very abstract (a lot of people can't wrap their head around the idea that nearly every function your program will have an unused dummy parameter that neither appears in the source code nor the object code), and (c) not the easiest, most practical explanation anyway.
The easiest, most practical explanation of Haskell I/O, IMHO, goes like this:
Haskell is purely functional, so things like getLine can't be functions.
But Haskell has things like getLine. This means those things are something else that's not a function. We call them actions.
Haskell allows you to treat actions as values. You can have functions that produce actions (e.g., putStrLn :: String -> IO ()), functions that accept actions as arguments (e.g., (>>) :: IO a -> IO b -> IO b), etc.
Haskell however has no function that executes an action. There can't be an execute :: IO a -> a because it would not be a true function.
Haskell has built-in functions to compose actions: make compound actions out of simple actions. Using basic actions and action combinators, you can describe any imperative program as an action.
Haskell compilers know how to translate actions into executable native code. So you write an executable Haskell program by writing a main :: IO () action in terms of subactions.
Passing around values that represent "the world" is one way to make a pure model for doing IO (and other side effects) in pure declarative programming.
The "problem" with pure declarative (not just functional) programming is obvious. Pure declarative programming provides a model of computation. These models can express any possible computation, but in the real world we use programs to have computers do things that aren't computation in a theoretical sense: taking input, rendering to displays, reading and writing storage, using networks, controlling robots, etc, etc. You can directly model almost all of such programs as computation (e.g. what output should be written to a file given this input is a computation), but the actual interactions with things outside the program just isn't part of the pure model.
That's actually true of imperative programming too. The "model" of computation that is the C programming language provides no way to write to files, read from keyboards, or anything. But the solution in imperative programming is trivial. Performing a computation in the imperative model is executing a sequences of instructions, and what each instruction actually does depends on the whole environment of the program at the time it is executed. So you can just provide "magic" instructions that carry out your IO actions when they are executed. And since imperative programmers are used to thinking about their programs operationally1, this fits very naturally with what they're already doing.
But in all pure models of computation, what a given unit of computation (function, predicate, etc) will do should only depend on its inputs, not on some arbitrary environment that can be different every time. So not only performing IO actions but also implementing computations which depend on the universe outside the program is impossible.
The idea for the solution is fairly simple though. You build a model for how IO actions work within the whole pure model of computation. Then all the principles and theories that apply to the pure model in general will also apply to the part of it that models IO. Then, within the language or library implementation (because it's not expressible in the language itself), you hook up manipulations of the IO model to actual IO actions.
This brings us to passing around a value that represents the world. For example, a "hello world" program in Mercury looks like this:
:- pred main(io::di, io::uo) is det.
main(InitialWorld, FinalWorld) :-
print("Hello world!", InitialWorld, TmpWorld),
nl(TmpWorld, FinalWorld).
The program is given InitialWorld, a value in the type io which represents the entire universe outside the program. It passes this world to print, which gives it back TmpWorld, the world that is like InitialWorld but in which "Hello world!" has been printed to the terminal, and whatever else has happened in the meantime since InitialWorld was passed to main is also incorporated. It then passes TmpWorld to nl, which gives back FinalWorld (a world that is very like TmpWorld but it incorporates the printing of the newline, plus any other effects that happened in the meantime). FinalWorld is the final state of the world passed out of main back to the operating system.
Of course, we're not really passing around the entire universe as a value in the program. In the underlying implementation there usually isn't a value of type io at all, because there's no information that's useful to actually pass around; it all exists outside the program. But using the model where we pass around io values allows us to program as if the entire universe was an input and output of every operation that is affected by it (and consequently see that any operation that doesn't take an input and output io argument can't be affected by the external world).
And in fact, usually you wouldn't actually even think of programs that do IO as if they're passing around the universe. In real Mercury code you'd use the "state variable" syntactic sugar, and write the above program like this:
:- pred main(io::di, io::uo) is det.
main(!IO) :-
print("Hello world!", !IO),
nl(!IO).
The exclamation point syntax signifies that !IO really stands for two arguments, IO_X and IO_Y, where the X and Y parts are automatically filled in by the compiler such that the state variable is "threaded" through the goals in the order in which they are written. This is not just useful in the context of IO btw, state variables are really handy syntactic sugar to have in Mercury.
So the programmer actually tends to think of this as a sequence of steps (depending on and affecting external state) that are executed in the order in which they are written. !IO almost becomes a magic tag that just marks the calls to which this applies.
In Haskell, the pure model for IO is a monad, and a "hello world" program looks like this:
main :: IO ()
main = putStrLn "Hello world!"
One way to interpret the IO monad is similarly to the State monad; it's automatically threading a state value through, and every value in the monad can depend on or affect this state. Only in the case of IO the state being threaded is the entire universe, as in the Mercury program. With Mercury's state variables and Haskell's do notation, the two approaches end up looking quite similar, with the "world" automatically threaded through in a way that respects the order in which the calls were written in the source code, =but still having IO actions explicitly marked.
As explained quite well in sacundim's answer, another way to interpret Haskell's IO monad as a model for IO-y computations is to imagine that putStrLn "Hello world!" isn't in fact a computation through which "the universe" needs to be threaded, but rather that putStrLn "Hello World!" is itself a data structure describing an IO action that could be taken. On this understanding what programs in the IO monad are doing is using pure Haskell programs to generate at runtime an imperative program. In pure Haskell there's no way to actually execute that program, but since main is of type IO () main itself evaluates to such a program, and we just know operationally that the Haskell runtime will execute the main program.
Since we're hooking up these pure models of IO to actual interactions with the outside world, we need to be a little careful. We're programming as if the entire universe was a value we can pass around the same as other values. But other values can be passed into multiple different calls, stored in polymorphic containers, and many other things that don't make any sense in terms of the actual universe. So we need some restrictions that prevent us from doing anything with "the world" in the model that doesn't correspond to anything that can actually be done to the real world.
The approach taken in Mercury is to use unique modes to enforce that the io value remains unique. That's why the input and output world were declared as io::di and io::uo respectively; it's a shorthand for declaring that the type of the first paramter is io and it's mode is di (short for "destructive input"), while the type of the second parameter is io and its mode is uo (short for "unique output"). Since io is an abstract type, there's no way to construct new ones, so the only way to meet the uniqueness requirement is to always pass the io value to at most one call, which must also give you back a unique io value, and then to output the final io value from the last thing you call.
The approach taken in Haskell is to use the monad interface to allow values in the IO monad to be constructed from pure data and from other IO values, but not expose any functions on IO values that would allow you to "extract" pure data from the IO monad. This means that only the IO values incorporated into main will ever do anything, and those actions must be correctly sequenced.
I mentioned before that programmers doing IO in a pure language still tend to think operationally about most of their IO. So why go to all this trouble to come up with a pure model for IO if we're only going to think about it the same way imperative programmers do? The big advantage is that now all the theories/code/whatever that apply to all of the language apply to IO code as well.
For example, in Mercury the equivalent of fold processes a list element-by-element to build up an accumulator value, which means fold takes an input/output pair of variables of some arbitrary type as the accumulator (this is a very common pattern in the Mercury standard library, and is why I said state variable syntax often turns out to be very handy in other contexts than IO). Since "the world" appears in Mercury programs explicitly as a value in the type io, it's possible to use io values as the accumulator! Printing a list of strings in Mercury is as simple as foldl(print, MyStrings, !IO). Similarly in Haskell, generic monad/functor code works just fine on IO values. We get a whole lot of "higher-order" IO operations that would have to be implemented anew specialised to IO in a language that handles IO by some completely special mechanism.
Also, since we avoid breaking the pure model by IO, theories that are true of the computational model remain true even in the presence of IO. This makes reasoning by the programmer and by program-analysis tools not have to consider whether IO might be involved. In languages like Scala for example, even though much "normal" code is in fact pure, optimizations and implementation techniques that work on pure code are generally inapplicable, because the compiler has to presume that every single call might contain IO or other effects.
1 Thinking about programs operationally means understanding them in terms of the operations the computer will carry out when executing them.
I think the first thing we should read about this subject is Tackling the Awkward Squad. (I didn't do so and I regret it.)
The author actually describes the GHC's internal representation of IO as world -> (a,world) as "a bit of a hack".
I think this "hack" is meant to be a kind of innocent lie. I think there are two kinds of lie here:
GHC pretends the 'world' is representable by some variable.
The type world -> (a,world) basically says that if we could in some way instantiate the world, then the "next state" of our world is functionally determined by some small program running on a computer. Since this is clearly not realizable, the primitives are (of course) implemented as functions with side effects, ignoring the meaningless "world" parameter, just like in most other languages.
The author defends this "hack" on the two bases:
By treating the IO as a thin wrapper of the type world -> (a,world), GHC can reuse many optimizations for the IO code, so this design is very practical and economical.
The operational semantics of the IO computation implemented as above can be proved sound provided the compiler satisfies certain properties. This paper is cited for the proof of this.
The problem (that I wanted to ask here, but you asked it first so forgive me to write it here) is that in the presense of the standard 'lazy IO' functions, I'm no longer sure that the GHC's operational semantics remains sound.
The standard 'lazy IO' functions such as hGetContents internally calls unsafeInterleaveIO which in turn is equivalent to
unsafeDupableInterleaveIO for single thread programs.
unsafeDupableInterleaveIO :: IO a -> IO a
unsafeDupableInterleaveIO (IO m)
= IO ( \ s -> let r = case m s of (# _, res #) -> res
in (# s, r #))
Pretending that the equational reasoning still works for this kind of programs (note that m is an impure function) and ignoring the constructor , we have
unsafeDupableInterleaveIO m >>= f ==> \world -> f (snd (m world)) world , which semantically would have the same effect that Andreas Rossberg described above: it "duplicates" the world. Since our world cannot be duplicated this way, and the precise evaluation order of a Haskell program is virtually unpredictable --- what we get is an almost unconstrained and unsynchronized concurrency racing for some precious system resources such as file handles. This kind of operation is of course never considered in Ariola&Sabry. So I disagree with Andreas in this respect -- the IO monad doesn't really thread the world properly even if we restrict ourselves within the limit of the standard library (and this is why some people say lazy IO is bad).
The world means just that - the physical, real world. (There is only one, mind you.)
By neglecting physical processes that are confined to the CPU and memory, one can classify every function:
Those that do not have effects in the physical world (except for ephemeral, mostly unobservable effects in the CPU and RAM)
Those that do have observable effects. for example: print something on the printer, send electrons through network cables, launch rockets or move disk heads.
The distinction is a bit artificial, insofar as running even the purest Haskell program in reality does have observable effects, like: your CPU getting hotter, which causes the fan to turn on.
Basically every program you write can be divided into 2 parts (in FP word, in imperative/OO world there is no such distinction).
Core/Pure part: This is your actual logic/algorithm of the application that is used to solve the problem for which you have build the application. (95% of applications today lack this part as they are just a mess of API calls with if/else sprinkled, and people start calling themselves programmers) For ex: In an image manipulation tool the algorithm to apply various effects to the image belongs to this core part. So in FP, you build this core part using FP concepts like purity etc. You build your function that takes input and return result and there is no mutation whatsoever in this part of your application.
The outer layer part: Now lets says you have completed the core part of the image manipulation tool and have tested the algorithms by calling function with various input and checking the output but this isnt something that you can ship, how the user is supposed to use this core part, there is no face of it, it is just a bunch of functions. Now to make this core usable from end user point of view, you need to build some sort of UI, way to read files from disk, may be use some embedded database to store user preferences and the list goes on. This interaction with various other stuff, which is not the core concept of your application but still is required to make it usable is called the world in FP.
Exercise: Think about any application you have build earlier and try to divide it into above mentioned 2 parts and hopefully that will make things more clear.
The world refers to interacting with the real world / has side effects - for example
fprintf file "hello world"
which has a side effect - the file has had "hello world" added to it.
This is opposed to purely functional code like
let add a b = a + b
which has no side effects

Can pure functions read global state?

Please note: by a "pure" function, I don't mean "pure virtual"
I'm referring to this
If a function "reads" some global state, does that automatically render it impure? or does it depend on other factors?
If it automatically renders it impure, please explain why.
If it depends on other factors, please explain what are they.
A "pure" function is a function whose result depends only on its input arguments. If it reads anything else, it is not a pure function.
In certain specialized instances, yes. For example, if you had a global cache of already-computed values that was only read and written by your function, it would still be mathematically pure, in the sense that the output only depended on the inputs, but it wouldn't be pure in the strictest sense. For example:
static int cache[256] = {0};
int compute_something(uint8_t input)
{
if(cache[input] == 0)
cache[input] = (perform expensive computation on input that won't return 0);
return cache[input];
}
In this case, so long as no other function touches the global cache, it's still a mathematically pure function, even though it technically depends on external global state. However, this state is just a performance optimization -- it would still perform the same computation without it, just more slowly.
Pure functions are required to construct pure expressions. Constant expressions are pure by definition.
So, if your global 'state' doesn't change you are fine.
Also see referential transparency:
A more subtle example is that of a function that uses a global variable (or a dynamically scoped variable, or a lexical closure) to help it compute its results. Since this variable is not passed as a parameter but can be altered, the results of subsequent calls to the function can differ even if the parameters are identical. (In pure functional programming, destructive assignment is not allowed; thus a function that uses global (or dynamically scoped) variables is still referentially transparent, since these variables cannot change.)
In Haskell for instance, you can create an endless list of random numbers on the impure side, and pass that list to your pure function. The implementation will generate the next number your pure function is using only when it needs it, but the function is still pure.