Lambda Calculus Function Reduction steps - lambda-calculus

I am studying Lambda Calculus. Could someone help with this reduction?
(λa.((aλb.λc.c)λd.λe.d))(λf.λg.f)
The actual result is λb.λc.c. But i need the steps to solve it
I have done these steps but i got stuck:
(λa.((aλb.λc.c)λd.λe.d))(λf.λg.f)
(λa.(aλb.λd.λe.d))(λf.λg.f)
(λf.λg.f)(λb.λd.λe.d)
(λg.λb.λd.λe.d)
Am i doing the steps wrong? Is there any rule I don't know?

visual evaluation
It can be challenging to build a visual model of steps using text alone. As the other answer notes, your () are used in a strange way. I hope this answer helps -
fix parens:
(λa.((a λb.λc.c) λd.λe.d)) (λf.λg.f)
eval:
(λa.a (λb.λc.c) (λd.λe.d)) (λf.λg.f)
=========
__________________________/
/
(λa.a (λb.λc.c) (λd.λe.d))
= \ \
| \ \
| \ \
(λf.λg.f) (λb.λc.c) (λd.λe.d)
========= /
_________/ ______/
/ /
(λf.λg.f) (λd.λe.d)
= \__
| \
(λg.(λb.λc.c)) (λd.λe.d)
=========
_______________/
/
(λg.λb.λc.c)
=======
/
/
λb.λc.c
higher intuition
Incidentally λb.λc.c is equivalent to Church's FALSE, λa.λb.b. Going back to your original program, given Church's booleans -
true := λa.λb.a
false := λa.λb.b
Take the original expression -
(λa.a (λb.λc.c) (λd.λe.d)) (λf.λg.f)
Rewrite using true and false -
(λa.a false true) true
Evaluates to -
true false true
Evaluates to -
false
We've leaned that (λa.a false true) gives us the inverse of a boolean. We can name it not -
true := λa.λb.a
false := λa.λb.b
not := λp.p false true
It works like this -
not true //=> false
not false //=> true

Related

Mathematica Series and Solve function

This is my first mathmatica code,
I defined the functions:
\[Beta] := v/c
\[Gamma] := 1/Sqrt[1 - \[Beta]^2]
TotalE[\[Gamma][\[Beta]]] := \[Gamma]mc^2
KE := TotalE[\[Gamma][\[Beta]]] - mc^2
No i want to make a series expansion of KE at β → 0 up to order 2,
I tried:
Series[KE, {\[Beta], 1, 2}]
But i got the error massage:
General::ivar: v/c is not a valid variable.
I also wanted to define Ekin as function of β,
so i used Solve function to get the inverse function, β[Ekin]:
Solve[KE, \[Beta]]
The same errors arises again:
Solve::ivar: v/c is not a valid variable.
Try this
Clear[\[Gamma],\[Beta],mc,KE,s,v,c]
\[Gamma] = 1/Sqrt[1 - \[Beta]^2];
TotalE[\[Gamma]*\[Beta]] = \[Gamma]*mc^2;
KE = TotalE[\[Gamma]*\[Beta]] - mc^2;
s=Normal[Series[KE, {\[Beta], 1, 2}]]/.\[Beta]->v/c
Reduce[KE==0, \[Beta]]/.\[Beta]->v/c
which returns
O-mc^2 + mc^2/(Sqrt[2]*Sqrt[1 - v/c]) -
(mc^2*(-1 + v/c))/(4*Sqrt[2]*Sqrt[1 - v/c]) +
(3*mc^2*(-1 + v/c)^2)/(32*Sqrt[2]*Sqrt[1 - v/c])
and
(mc != 0 && v/c == 0)||(-1+v^2/c^2 !=0 && mc == 0)
What that is trying to do is do your calculations with the simple variable beta, before you turn that into v/c and after the calculations replace beta with v/c.
But there are still things about the way you have written that which worry me. You are kind of writing TotalE like it is a function, but that is not the way to define a Mathematica function and I am concerned this may be going to get you into trouble.
Please let me know if I have misunderstood some of what you are trying to do and explain what I've done wrong and I will try to find a way to fix that.

For-loop in lua with löve2D, deleting variable

i'm a bit beginner about coding, and my english isn't great, i hope i'll be able to make my question clear:
So I have a for-loop in my code, and 2 if in it, in each if, I see if something is true or false, if it's true, I delete a portion of the loop. like that:
for n=#Test,1, -1 do
if Test[n].delete == true then
table.remove(Test, n )
end
if Test[n].y > 0 then
table.remove(Test, n )
end
end
kind of like that, and my problem is, if the first if make Test[n] being deleted, then the game crash at the next if because Test[n] Doesn't exist anymore. I solved the problem by making 2 for-loop, one for each if.
But I saw someone who did it without 2 for-loop, and it's not crashing, I tried to figure what was wrong but I can't find it.
If someone could find what is wrong with what I wrote, I would be thankful!
So here is the moment that makes problem in my code, on line 9, if the condition are met, i table.remove(ListeTirs, n ), then on line 17, when code try to find it again for test it, it bug :
for n=#ListeTirs,1, -1 do
if ListeTirs[n].Type == "Gentils"
then local nAliens
for nAliens=#ListeAliens,1,-1 do
if
Collide(ListeTirs[n], ListeAliens[nAliens]) == true
then
ListeTirs[n].Supprime = true
table.remove(ListeTirs, n )
ListeAliens[nAliens].Supprime = true
table.remove(ListeAliens, nAliens)
end
end
end
if
ListeTirs[n].y < 0 - ListeTirs[n].HauteurMoitie or ListeTirs[n].y > Hauteur + ListeTirs[n].HauteurMoitie or ListeTirs[n].x > Largeur + ListeTirs[n].LargeurMoitie or ListeTirs[n].x < 0 - ListeTirs[n].LargeurMoitie
then
ListeTirs[n].Supprime = true
table.remove(ListeTirs, n)
end
end
I hope it's clear, I could post the whole code but I don't feel it's necessary, if it is, I will add it.
Thank you :)
for n=#Test,1, -1 do
local should_be_removed
-- just mark for deletion
if Test[n].delete = true then
should_be_removed = true
end
-- just mark for deletion
if Test[n].y > 0 then
should_be_removed = true
end
-- actually delete (at the very end of the loop body)
if should_be_removed then
table.remove(Test, n )
end
end

setTargetReturn in fPortfolio package

I have a three asset portfolio. I need to set the target return for my second asset
whenever i try i get this error
asset.ts <- as.timeSeries(asset.ret)
spec <- portfolioSpec()
setSolver(spec) <- "solveRshortExact"
constraints <- c("Short")
setTargetReturn(Spec) = mean(colMeans(asset.ts[,2]))
efficientPortfolio(asset.ts, spec, constraints)
Error: is.numeric(targetReturn) is not TRUE
Title:
MV Efficient Portfolio
Estimator: covEstimator
Solver: solveRquadprog
Optimize: minRisk
Constraints: Short
Portfolio Weights:
MSFT AAPL NORD
0 0 0
Covariance Risk Budgets:
MSFT AAPL NORD
Target Return and Risks:
mean mu Cov Sigma CVaR VaR
0 0 0 0 0 0
Description:
Sat Apr 19 15:03:24 2014 by user: Usuario
i have tried and i have searched the web but i have no idea how to set the target return
for a specific expected return of the data set. i could copy the mean of my second asset # but i think due to decimal it could affect the answer.
I ran into this error , when using 2 assets.
Appears to be a bug in the PortOpt methods.
When there's 2 assets, it runs : .mvSolveTwoAssets
Which looks for the TargetReturn in the portfolioSpecs.
But as you know, targetReturn isn't always needed.
But in your code , you have 2 separate variables for spec.
'spec' , and 'Spec'
i.e.: 'Spec' .. assuming this is a typo, then this line needs to be changed.
setTargetReturn(Spec) = mean(colMeans(asset.ts[,2]))

Is there something wrong with this xpath, or is Marklogic misbehaving?

I've got a weird problem with what should be a simple xpath not returning data when I'm sure the data is present in Marklogic. I can see the data in question through a more general xpath, but not get it specifically.
xpath that works:
/log:record[log:changes/log:change/#field = "moduleCodes"]
=> a long sequence of log:record elements with "moduleCodes" field changes
xpath that doesn't:
/log:record/log:changes/log:change[#field = "moduleCodes"]
=> empty sequence
(I've ommitted the log namespace definition brevity.)
Trying to figure out what's going on, I started with the first, working, xpath and built on it:
/log:record[log:changes/log:change/#field = "moduleCodes"]/log:changes/log:change
=> sequence of log:change elements including some with #field = "moduleCodes"
/log:record[log:changes/log:change/#field = "moduleCodes"]/log:changes/log:change[#field]
=> sequence of log:change elements including some with #field = "moduleCodes"
/log:record[log:changes/log:change/#field = "moduleCodes"]/log:changes/log:change[#field = "moduleCodes"]
=> empty sequence
Am I misunderstanding something fundamental? I can't see any reason why the xpaths putting the predicate on the log:change would return an empty sequence when everything else works as I expect. This feels like Marklogic getting confused somehow to me, but I want to make sure it's not just me missing a subtlety of xpath before I start talking like that.
I just tried the paths with a different field-name. It works as I expect with (at least some) other values.
I just restarted the ML cluster; no change.
Edit:
All of the xpaths above work fine in Oxygen, so it seems to be just ML that's behaving like this. I tried adding fn:doc() to the front of all the paths, in case that helped, but it made no difference.
Here's an (anonymised) record that I believe should match all the xpaths:
<log:record id="00000001" date="2013-04-14T01:42:02.922+01:00" type="change" xmlns:log="some/namespace/definition">
<log:head>
<some-header-info/>
</log:head>
<log:changes>
<log:change field="dateModified">
<log:old-value>2012-11-06T00:00:00.0000000</log:old-value>
<log:new-value>2013-03-20T00:00:00.0000000</log:new-value>
</log:change>
<log:change field="moduleCodes">
<log:old-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
</log:moduleCodes>
</log:old-value>
<log:new-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
<log:moduleCodes-value code="BBB"/>
</log:moduleCodes>
</log:new-value>
</log:change>
</log:changes>
</log:record>
As best I can recreate your test with 6.0-2.3, this works for me.
When debugging database queries, one useful technique is to move things in memory. If it still doesn't work, this throws suspicion on the database query. When I try that using 6.0-2.3, the results seem to be correct.
declare namespace log="some/namespace/definition" ;
document {
<log:record id="00000001" date="2013-04-14T01:42:02.922+01:00" type="change" xmlns:log="some/namespace/definition">
<log:head>
<some-header-info/>
</log:head>
<log:changes>
<log:change field="dateModified">
<log:old-value>2012-11-06T00:00:00.0000000</log:old-value>
<log:new-value>2013-03-20T00:00:00.0000000</log:new-value>
</log:change>
<log:change field="moduleCodes">
<log:old-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
</log:moduleCodes>
</log:old-value>
<log:new-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
<log:moduleCodes-value code="BBB"/>
</log:moduleCodes>
</log:new-value>
</log:change>
</log:changes>
</log:record> }
/log:record[log:changes/log:change/#field = "moduleCodes"]/xdmp:path(.)
=>
/log:record
declare namespace log="some/namespace/definition" ;
document {
<log:record id="00000001" date="2013-04-14T01:42:02.922+01:00" type="change" xmlns:log="some/namespace/definition">
<log:head>
<some-header-info/>
</log:head>
<log:changes>
<log:change field="dateModified">
<log:old-value>2012-11-06T00:00:00.0000000</log:old-value>
<log:new-value>2013-03-20T00:00:00.0000000</log:new-value>
</log:change>
<log:change field="moduleCodes">
<log:old-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
</log:moduleCodes>
</log:old-value>
<log:new-value>
<log:moduleCodes>
<log:moduleCodes-value code="AAA"/>
<log:moduleCodes-value code="BBB"/>
</log:moduleCodes>
</log:new-value>
</log:change>
</log:changes>
</log:record> }
/log:record/log:changes/log:change[#field = "moduleCodes"]/xdmp:path(.)
=>
/log:record/log:changes/log:change[2]
So the implication is that the problem is in the index or the way the index is queried. You can try to debug that using xdmp:query-trace(true()) at the start of your query. For example:
declare namespace log="some/namespace/definition" ;
xdmp:query-trace(true()),
/log:record[log:changes/log:change/#field = "moduleCodes"]/xdmp:describe(.),
/log:record/log:changes/log:change[#field = "moduleCodes"]/xdmp:describe(.)
With 6.0-2.3 these both return the expected results for me.
fn:doc("test")/log:record
fn:doc("test")/log:record/log:changes/log:change[2]
Here are the traces, from the ErrorLog.txt file:
Analyzing path: fn:collection()/log:record[log:changes/log:change/#field = "moduleCodes"]/xdmp:describe(.)
Step 1 is searchable: fn:collection()
Step 2 is searchable: log:record[log:changes/log:change/#field = "moduleCodes"]
Step 3 is unsearchable: xdmp:describe(.)
First 2 steps of path are searchable: fn:collection()/log:record[log:changes/log:change/#field = "moduleCodes"]
Gathering constraints.
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 2 predicate 1 contributed 3 constraints: log:changes/log:change/#field = "moduleCodes"
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 2 predicate 1 contributed 1 constraint: log:changes/log:change/#field = "moduleCodes"
Step 2 contributed 4 constraints: log:record[log:changes/log:change/#field = "moduleCodes"]
Executing search.
Selected 1 fragment to filter
xdmp:eval("declare namespace log="some/namespace/definition" ;&#1...", (), <options xmlns="xdmp:eval"><database>598453498912235799</database><root>/tmp</root><isolati...</options>)
Analyzing path: fn:collection()/log:record/log:changes/log:change[#field = "moduleCodes"]/xdmp:describe(.)
Step 1 is searchable: fn:collection()
Step 2 is searchable: log:record
Step 3 is searchable: log:changes
Step 4 is searchable: log:change[#field = "moduleCodes"]
Step 5 is unsearchable: xdmp:describe(.)
First 4 steps of path are searchable: fn:collection()/log:record/log:changes/log:change[#field = "moduleCodes"]
Gathering constraints.
Step 2 contributed 1 constraint: log:record
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 4 predicate 1 contributed 1 constraint: #field = "moduleCodes"
Step 4 contributed 1 constraint: log:change[#field = "moduleCodes"]
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 4 predicate 1 contributed 1 constraint: #field = "moduleCodes"
Comparison contributed hash value constraint: log:change/#field = "moduleCodes"
Step 4 predicate 1 contributed 1 constraint: #field = "moduleCodes"
Step 4 contributed 1 constraint: log:change[#field = "moduleCodes"]
Step 3 contributed 1 constraint: log:changes
Executing search.
Selected 1 fragment to filter

What are the Integer values of Boolean False and True in VB6?

I'm working with a bit of old VB6 code that goes thus...
Dim STATUS As Integer
STATUS = -1
If (Not STATUS) Then
' do something
Else
' do something else
End If
so I was, naturally, wondering which branch of this code is executed. So does anyone know what the numeric values of True and False are in VB6?
True is stored as -1 and false as 0. Any non-zero value is considered as true.
To see why it is so please check - http://www.vbforums.com/showthread.php?t=405047
In VB 6, True has a numeric value of -1. False has a numeric value of 0.
The reason for this is because the Boolean data type is stored as a 16-bit signed integer. Therefore,-1 evaluates to 16 1s in binary (1111111111111111). False is 16 0s (0000000000000000). This produces the relationship that has held throughout the evolution of BASIC: True = Not False.
Not really an answer, but just poking about, I typed this into the immediate window, with these results:
For x = -5 To 5 : ? x, CBool(x), ( x = True ), ( x = False ) : Next x
-5 True False False
-4 True False False
-3 True False False
-2 True False False
-1 True True False
0 False False True
1 True False False
2 True False False
3 True False False
4 True False False
5 True False False
(I tested more values, but only -1 and 0 had anything "interesting" going on. The rest were all True/False/False.) So, empirically, I'd say that the comparison is being done arithmetically unless you cast with CBool. Why? I can't really say...

Resources