About substitution of free occurances: can we have a substitution of an entire expression(function, application), or just of a variable:
Example:
Current expression \x.\y.(y, z)
Expression to be replaced: \y.(y z) with p so we will have \x.p.
Is it possible?
Yes, it is possible. One way is to consider the substitution of occurrences. An occurrence is a string over the set {1,2,3}, and for every lambda-term M, the sub-expression M/u at occurrence u is defined as:
M/[] = M
M/0u = N/u, if M=\x. N
M/1u = P/u, if M=PQ
M/2u = Q/u, if M=PQ
(I use the symbol [] to denote the empty string.)
Now define the substitution M[u := N] as the term obtained from replacing the occurrence u with N in M. I've seen this kind of substitution in some of P.-L. Curien work.
Related
I have seen this notation in bash:
a=${b[c--]}
or also
a[++b]=$c
or the other way around:
a[b++]=$c
but if I execute it on the command line nothing happens.
OSX#26:~ $ a[++b]=2
OSX#27:~ $ echo ${a[++b]}
OSX#28:~ $
What is the use of this notation ?
Edit:
I am asking about this notation. As I have seen it it is a variable, because of the $ in front. But ++ does remind me of increment, so I am confused. Is this some sort of variable increment ?
c-- is an arithmetic expression, it means "return the value of c and then decrement it by 1". ${b[n]} denotes the n-th element of the array b (where the first element has index 0). Expressions inside the square brackets are interpreted as arithmetic (with some exceptions like *, #, '1' etc.) Let's try it:
b=(x y z)
c=2
a=${b[c--]}
echo $a $c # Outputs: z 1
so c-- returns 2, but sets c to 1. b[2], i.e. z, is then assigned to a.
++c is similar to c--, but it adds 1 to c before returning its value.
So I have this exercise that I'm stuck on:
A formula is:
tru
fls
variable(V) iff V is an atom.
or(Flist) iff every element in the list is a formula
there are implies, and, neg too. the form looks similar.
We can represent a truth assignment (an assignment of values to variables) by a Prolog list of the form [Var1/Value1, Var2/Value2,...VarN/ValueN]. Write a predicate sub(?F,?Asst,?G) which succeeds iff G is a formula which is a result of substituting the variables of F with corresponding values from the assignment Asst. (You can assume that the truth assignment A is at least partially instantiated).
E.g.
sub(variable(x), [x/tru], tru).
true
sub(or([variable(a),variable(b)]), [a/tru,b/fls], G).
G = or(tru,fls)
true
I've tried
sub(variable(x),[x/value],G):-
G = variable(value).
But it just returns false.
Edit: Sorry I didn't make the question clear, Can someone explain to me if there's a way to assign values associated with variables in a list to another variable? I think it has something to do with unification.
Variables are placeholders.
Beware of case sensitivity: Prolog variable names start with an uppercase character or underscore, atoms with a lowercase character.
Your code snippet of sub/3 assumes that the list of
key-value pairs has exactly a length of one ([x/value]).
By using member/2 the lists can have arbitrary length.
When handling n-ary logical connectives like and / or, you probably want a short-circuit implementation that returns as soon as possible. Like so:
sub(tru,_,tru).
sub(fls,_,fls).
sub(variable(X),Assoc,Value) :-
member(X/Value,Assoc).
sub(or([]),_,fls).
sub(or([X|Xs]),Assoc,V) :-
sub(X,Assoc,T),
( T = tru, V = tru % short-circuit logical-or
; T = fls, sub(or(Xs),Assoc,V)
).
I am having trouble parsing sequences that begin with capital letters into variables using Prolog's DCG notation. For instance, if I have the string
f a X y Z X
and a DCG that parses this string, is there any way to parse each capitalized letter into a unique Prolog variable. E.g., parse Y to a variable and each X to a variable? The intended application would be to build the functor
T = f(a,X,y,Z,X)
via a DCG rule ending with the statement
{T =.. [Head|Args]}
Maybe you are looking for term_to_atom/3:
?- term_to_atom(Term, 'f(a,X,y,Z,X)').
Term = f(a, _G304, y, _G306, _G304).
If you are using capital letters as an atom, you have to surround them between single quotes. E.g. 'X' and 'Z'.
Otherwise, they will be considered as prolog variables.
How can I pass values to a given expression with several variables? The values for these variables are placed in a list that needs to be passed into the expression.
Your revised question is straightforward, simply
f ## {a,b,c,...} == f[a,b,c,...]
where ## is shorthand for Apply. Internally, {a,b,c} is List[a,b,c] (which you can see by using FullForm on any expression), and Apply just replaces the Head, List, with a new Head, f, changing the function. The operation of Apply is not limited to lists, in general
f ## g[a,b] == f[a,b]
Also, look at Sequence which does
f[Sequence[a,b]] == f[a,b]
So, we could do this instead
f[ Sequence ## {a,b}] == f[a,b]
which while pedantic seeming can be very useful.
Edit: Apply has an optional 2nd argument that specifies a level, i.e.
Apply[f, {{a,b},{c,d}}, {1}] == {f[a,b], f[c,d]}
Note: the shorthand for Apply[fcn, expr,{1}] is ###, as discussed here, but to specify any other level description you need to use the full function form.
A couple other ways...
Use rule replacement
f /. Thread[{a,b} -> l]
(where Thread[{a,b} -> l] will evaluate into {a->1, b->2})
Use a pure function
Function[{a,b}, Evaluate[f]] ## l
(where ## is a form of Apply[] and Evaluate[f] is used to turn the function into Function[{a,b}, a^2+b^2])
For example, for two elements
f[l_List]:=l[[1]]^2+l[[2]]^2
for any number of elements
g[l_List] := l.l
or
h[l_List]:= Norm[l]^2
So:
Print[{f[{a, b}], g[{a, b}], h[{a, b}]}]
{a^2 + b^2, a^2 + b^2, Abs[a]^2 + Abs[b]^2}
Two more, just for fun:
i[l_List] := Total#Table[j^2, {j, l}]
j[l_List] := SquaredEuclideanDistance[l, ConstantArray[0, Length[l]]
Edit
Regarding your definition
f[{__}] = a ^ 2 + b ^ 2;
It has a few problems:
1) You are defining a constant, because the a,b are not parameters.
2) You are defining a function with Set, Instead of SetDelayed, so the evaluation is done immediately. Just try for example
s[l_List] = Total[l]
vs. the right way:
s[l_List] := Total[l]
which remains unevaluated until you use it.
3) You are using a pattern without a name {__} so you can't use it in the right side of the expression. The right way could be:
f[{a_,b_}]:= a^2+b^2;
I'm studying the mtl library and trying to do some MonadTransformers of my own. I was checking the Control.Monad.State.StateT declaration, and across all the code, I see this syntax:
execStateT :: (Monad m) => StateT s m a -> s -> m s
execStateT m s = do
~(_, s') <- runStateT m s
return s'
What does this ~ operand mean?
This is the notation for a lazy pattern in Haskell. I can't say that I'm familiar with it but from here:
It is called a lazy pattern, and has
the form ~pat. Lazy patterns are
irrefutable: matching a value v
against ~pat always succeeds,
regardless of pat. Operationally
speaking, if an identifier in pat is
later "used" on the right-hand-side,
it will be bound to that portion of
the value that would result if v were
to successfully match pat, and ⊥
otherwise.
Also, this section may be useful.
For a normal pattern match, the value that should be matched needs to be evaluated, so that it can be compared against the pattern.
~ denotes a lazy pattern match: It is just assumed that the value will match the pattern. The match is then only done later, if the value of a matched variable is actually used.
It's equivalent to
execStateT m s = do
r <- runStateT m s
return (snd r)
or
execStateT m s =
runStateT m s >>= return . snd