How are multiple return arguments from a choice accessed - daml

I have a choice defined as:
MyChoice: ( ContractId X, ContractId X )
....
return ( a, b )
How do I use a or b in a scenario as an argument for something else? Dot notation doesn't appear to work.

You can use ._1 and ._2 as accessors; or you can use fst and snd as functions to extract the elements.
You can also bind the components of a pair using pattern matching when the choice is exercised, like so:
somecode = do
(x,y) <- exercise myCid MyChoice with ...
doStuffWith x
doStuffWIth y

Related

How is one implementation of the constant function equal to a version using a lambda?

I'm currently revising the chapter for the lambda (\) expressions in Haskell. Was just wondering if someone could please help explain how did this:
const :: a → b → a
const x _ = x (this part)
Get defined into this:
const :: a → (b → a)
const x = λ_ → x (how did it become like this?)
The signatures a -> b -> a and a -> (b -> a) are parsed exactly the same, much like the arithmetic expressions 1 - 2 - 3 and (1 - 2) - 3 are the same: the -> operator is right-associative, whereas the - operator is left associative, i.e. the parser effectively puts the parentheses in the right place if not explicitly specified. In other words, A -> B -> C is defined to be A -> (B -> C).
If we explicitly write a -> (b -> a), then we do this to put focus on the fact that we're dealing with curried functions, i.e. that we can accept the arguments one-by-one instead of all at once, but all multi-parameter functions in Haskell are curried anyway.
As for why const x _ = x and const x = \_ -> x are equivalent: well first, to be pedantic they're not equivalent, see bottom of this answer. But let's ignore that for now.
Both lambdas and (single) function clauses are just ways to define functions. Like,
sqPl1 :: Int -> Int
sqPl1 x = x^2 + 1
does the same as
sqPl1 = \x -> x^2 + 1
It's just a different syntax. Some would say the f x = ... notation is just syntactic sugar for binding x in a lambda, i.e. f = \x -> ..., because Haskell is based on lambda calculus and in lambda calculus lambdas are the only way to write functions. (That's a bit of an oversimplification though.)
I said they're not quite equivalent. I'm referring to two things here:
You can have local definitions whose scope outlasts a parameter binding. For example if I write
foo x y = ec * y
where ec = {- expensive computation depending on `x` -}
then ec will always be computed from scratch whenever foo is applied. However, if I write it as
foo x = \y -> ec * y
where ec = {- expensive computation depending on `x` -}
then I can partially apply foo to one argument, and the resulting single-argument function can be evaluated with many different y values without needing to compute ec again. For example map (foo 3) [0..90] would be faster with the lambda definition. (On the flip side, if the stored value takes up a lot of memory it may be preferrable to not keep it around; it depends.)
Haskell has a notion of constant applicative forms. It's a subtle topic that I won't go into here, but that can be affected by whether you write a function as a lambda or with clauses or expand arguments.

is there a way to return a fun that can have more than one arity?

Erlang functions can share the same name but have different arity. For example:
name(X) -> X.
name(X,Y) -> X * Y.
I would like to be able to return a fun from a function that behaves in the same way.
In this example I want a way to return fun/1 and fun/2 as one return, so the returned fun can be called in either way, exactly as the function example above.
function() ->
fun(X) -> X end,
fun(X,Y) -> X * Y end.
I could return a tuple {F1, F2} but that's ugly to use.
I could return a fun that takes a list as it's argument, but that's also rather ugly with calls like F([X]) or F([X, Y]).
Here you have 2 different functions with the same name:
name(X) -> X.
name(X,Y) -> X * Y.
These two functions, as anonymous functions, are: fun name/1 and fun name/2 respectively.
If you put any of them in a variable, say… F1 = fun name/1 or F2 = fun name/2, you will not be able to use those vars later interchangeably, since F1(1) will work, but F1(1,2) will fail (and viceversa with F2).
If you don't know the # of arguments you'll receive in runtime (let's say you receive a list of arguments of variable length), then you'll need to use erlang:apply/2 or erlang:apply/3 to dynamically evaluate the function. In that case, I can offer you 2 ways (depending on the version of erlang:apply that you prefer):
With apply/2:
use_name(Args) ->
Fun = function(length(Args)),
erlang:apply(Fun, Args).
function(1) -> fun(X) -> X end;
function(2) -> fun(X, Y) -> X * Y end.
With apply/3:
use_name(Args) ->
erlang:apply(?MODULE, name, Args).
name(X) -> X.
name(X,Y) -> X * Y.
Hope this helps.

difficult to understand function definition

cube (x,y,z) =
filter (pcubes x) cubes
cubes = [(a,b,c) | a <- [1..30],b <- [1..30],c <- [1..30]]
pcubes x (b,n,m) = (floor(sqrt(b*n)) == x)
so this code works, cubes makes a list of tuples,pcubes is used with filter to filter all the cubes in which floor(sqrt(b*n)) == x is satisfied,but the person who has modified my code wrote pcubes x in filter (pcubes x) cubes,how does this work.pcubes x makes a function that will initial the cubes x (b,n,m) that will take in a tuple and output a bool.the bool will be used in the filter function. How does this sort of manipulation happen? how does pcubes x access the (b,n,m) part of the function?
In Haskell, we don't usually use tuples (ie: (a,b,c)) to pass arguments to functions. We use currying.
Here's an example:
add a b = a + b
Here add is a function that takes a number, the returns another function that takes a number, then returns a number. We represent it's type as so:
add :: Int -> (Int -> Int)
Because of the way -> behaves, we can remove the parentheses in this case:
add :: Int -> Int -> Int
It is called like this:
(add 1) 2
but because of the way application works, we can just write:
add 1 2
Doesn't that look like our definition above, of the form add a b...?
Your function pcubes is similar. Here's how I'd write it:
pcubes x (b,n,m) = floor (sqrt (b*n)) == x
And as someone else said, it's type could be represented as:
pcubes :: Float -> (Float, Float, Float) -> Bool
When we write pcubes 1 the type becomes:
pcubes 1 :: (Float, Float, Float) -> Bool
Which, through currying, is legal, and can quite happily be used elsewhere.
I know, this is crazy black functional magic, as it was for me, but before long I guarantee you'll never want to go back: curried functions are useful.
A note on tuples: Expressions like (a,b,c) are data . They are not purely function-argument expressions. The fact that we can pull it into a function is called pattern matching, though it's not my turn to go into that.

Haskell sugar for "applyable" class adts containing functions ex. Isomorphisms

Specifically, inspired by J's conjucation operator (g&.f = (f inverse)(g)(f))
I need a way to augment functions with additional information. The obvious way is to use ADT. Something like:
data Isomorphism a b = ISO {FW (a -> b) , BW (b -> a)}
(FW f) `isoApp` x = f x
(BW g) `isoApp` x = g x
But the need for an application function really hurts code readability when most of the time you just want the forward function.
What would be very useful is a class:
class Applyable a b c | a b -> c where
apply :: a -> b -> c
(I think the b could be made implicit with existential quantifiers but I'm not comfortable enough to be sure I wouldn't get the signature wrong)
Now the apply would be made implicit so you could just write
f x
as you would any other function. Ex:
instance Applyable (a -> b) a b where
apply f x = f x
instance Applyable (Isomorphism a b) a b where
apply f x = (FW f) x
inverse (Iso f g) = Iso g f
then you could write something like:
conjugate :: (Applyable g b b) => g -> Iso a b -> b -> a
f `conjugate` g = (inverse f) . g . f
Ideally the type signature could be inferred
However, these semantics seem complicated, as you would also need to modify (.) to support Applyable rather than functions. Is there any way to simply trick the type system into treating Applyable datatypes as functions for all normal purposes?
Is there a fundamental reason that this is impossible / a bad idea?
As far as I know, function application is possibly the one thing in the entire Haskell language that you cannot override.
You can, however, devise some sort of operator for this. Admittedly f # x isn't quite as nice as f x, but it's better than f `isoApp` x.

Multiline function calls in Coffeescript

Hi everyone: Suppose I have a function "foo" that should receive two functions as parameters. If I have two lambda functions, I can call "foo" as follows:
foo (-> 1),(-> 2)
In this case, "foo" receives two functions, one that just returns 1 and another that returns 2.
However, usually lambda functions are more complicated, so putting both functions on a single line is impractical. Instead, I would like to write two multiline lambda functions. However, I can't figure out for the life of me how to accomplish this in coffeescript- Ideally, I would want to write it as follows, but it throws an error:
foo
->
1
,
->
2
The best I can come up with that works is super ugly:
foo.apply [
->
1
,
->
2
]
Can any Coffeescript guru show me how I can do this, without getting an error? Thanks!
I believe this is one situation where anonymous functions seem to not be the answer. They are very practical and idiomatic in a lot of situations but even they have limitations and can be less readable if used in extreme situations.
I would define the two functions in variables and then use them as parameters:
func1 = ->
x = 2
y = 3
z = x+y
return z+2*y
func2 = ->
a = "ok"
return a + " if you want this way"
foo func1, func2
But if you decide lambdas would be preferable, just use the parenthesis around the parameters of foo:
foo ((->
x = 2
y = 3
z = x+y
return z+2*y
),(->
a = "ok"
return a + " if you want this way"
)
)
It is not because you are using CoffeScript that you should avoid parenthesis at any cost :)
This should suffice (you could indent the second lamda if you want):
f (->
x = 1
1 + 2 * x),
->
y = 2
2 * y
given the function f:
f = (a,b) -> a() + b()
the result should give 3 + 4 = 7
Functions are implicitly called if a variable or function follows them. That's why
foo
->
2
,
->
3
won't work; the coffeescript compiler only sees a variable followed by an unexpected indent on the next line. Explicitly calling it
foo(
->
2
, ->
3
)
will work.
You can implicitly call a function with multiple paramenters, you just need to line up the comma with the beginning of the function call
foo ->
2
, ->
3