Unable to access all the related predicates in Prolog from C++ - c++

I am trying to access all the related predicates of a prolog file from C++.
I have the prolog file as,
"new_gryffindor.pl"
sits_right_of(parvati,lavender).
sits_right_of(lavender,neville).
sits_right_of(neville,alicia).
sits_right_of(alicia,fred).
sits_right_of(fred,george).
sits_right_of(george,lee).
sits_right_of(lee,dennis).
sits_right_of(dennis,dean).
sits_right_of(dean,ginny).
sits_right_of(ginny,angelina).
sits_right_of(angelina,seamus).
sits_right_of(seamus,colin).
sits_right_of(colin,harry).
sits_right_of(harry,hermoine).
sits_right_of(hermoine,ron).
sits_right_of(ron,natalie).
sits_right_of(natalie,katie).
sits_right_of(katie,parvati).
sits_left_of(X,Y) :- sits_right_of(Y,X).
are_neighbours_of(X,Y,Z) :- sits_left_of(X,Z),
sits_right_of(Y,Z).
next_to_each_other(X,Y) :- sits_left_of(X,Y);
sits_right_of(X,Y).
And I have integrated C++ and Prolog file with the C++ code,
term_t a = PL_new_term_ref();
term_t b = PL_new_term_ref();
term_t ans = PL_new_term_ref();
PL_put_variable(ans);
predicate_t p_consult = PL_predicate("consult", 1, "database");
term_t t = PL_new_term_ref();
PL_put_string_chars(t, "new_gryffindor.pl");
PL_call_predicate(NULL, 0, p_consult, t);
fun = PL_new_functor(PL_new_atom("sits_right_of"),2);
PL_cons_functor(ans, fun, a, b);
char *fact1;
char *fact2;
if(PL_call(ans, NULL)) {
PL_get_atom_chars(a, &fact1);
PL_get_atom_chars(b, &fact2);
cout << fact1 << " sits right of " << fact2;
}
This C++ Code is giving me the result for the very first predicate of "sits_right_of" ,i.e, as "parvati sits right of lavender". But I want to print all the related predicates of "sits_right_of" in C++ like the prolog which gives next similar predicate's values by using semicolon ";".
sits_right_of(X,Y).
X = parvati,
Y = lavender ;
X = lavender,
Y = neville ;
X = neville,
Y = alicia ;
X = alicia,
Y = fred ;
X = fred,
Y = george ;
X = george,
Y = lee ;
X = lee,
Y = dennis ;
X = dennis,
Y = dean ;
X = dean,
Y = ginny ;
X = ginny,
Y = angelina ;
X = angelina,
Y = seamus ;
X = seamus,
Y = colin ;
X = colin,
Y = harry ;
X = harry,
Y = hermoine ;
X = hermoine,
Y = ron ;
X = ron,
Y = natalie ;
X = natalie,
Y = katie ;
X = katie,
Y = parvati.
I tried looping statements in C++ but it is printing the same output for multiple times..
So please help me out to print the values of other similar predicates of prolog file in C++.

Related

Looping in Mata with OLS

I need help with looping in Mata. I have to write a code for Beta coefficients for OLS in Mata using a loop. I am not sure how to call for the variables and create the code. Here is what I have so far.
foreach j of local X {
if { //for X'X
matrix XX = [mata:XX = cross(X,1 , X,1)]
XX
}
else {
mata:Xy = cross(X,1 , y,0)
Xy
}
I am getting an error message "invalid syntax".
I'm not sure what you need the loop for. Perhaps you can provide more information about that. However the following example may help you implement OLS in mata.
Load example data from bcuse:
ssc install bcuse
clear
bcuse bwght
mata
x = st_data(., ("male", "parity","lfaminc","packs"))
cons = J(rows(x), 1, 1)
X = (x, cons)
y = st_data(., ("lbwght"))
beta_hat = (invsym(X'*X))*(X'*y)
e_hat = y - X * beta_hat
s2 = (1 / (rows(X) - cols(X))) * (e_hat' * e_hat)
B = J(cols(X), cols(X), 0)
n = rows(X)
for (i=1; i<=n; i++) {
B =B+(e_hat[i,1]*X[i,.])'*(e_hat[i,1]*X[i,.])
}
V_robust = (n/(n-cols(X)))*invsym(X'*X)*B*invsym(X'*X)
se_robust = sqrt(diagonal(V_robust))
V_ols = s2 * invsym(X'*X)
se_ols = sqrt(diagonal(V_ols))
beta_hat
se_robust
end
This is far from the only way to implement OLS using mata. See the Stata Blog for another example using quadcross, I like my example because it preserves a little more of the matrix algebra in the code.

Piecewise linear regression with SAS PHREG

How to implement a piecewise linear regression model in PHREG procedure of SAS?
For example with one knot at X=T:
Y = β_10 + β_11 . X if X ≤ T
Y = β_20 + β_21 . X if X >T
Given the model with the constraint of continuity:
Y = β_10 + β_11 . X if X ≤ T
Y = β_10 + (β_11 - β_21) T + β_21 . X if X >T
i.e :
Y= β_0 + β_1 . X + S_1
where
S_1 = ( β_11 - β_21 ) T if X >T and 0 otherwise.
Finally i would like to include it in a Cox model:
Proc PHREG
Model time * cas (censure) = X S_1 ;
Run ;
But the problem is S_1 has unknown beta coefficients in it.
Thanks for your help!

Unclassifiable statement and other errors in an IF in Fortran

I have the code:
if i < n then
x = topsep(1)
y = topsep(2)
realvor(n,1) = x + dx
realvor(n,2) = x + dy
imvor(n,1) = (realvor(n,1)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2)
imvor(n,2) = (realvor(n,2)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2)
tf = .TRUE.
else
x = botsep(1)
y = botsep(2)
realvor(n,1) = x + dx
realvor(n,2) = y - dy
imvor(n,1) = (realvor(n,1)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2)
imvor(n,2) = (realvor(n,2)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2)
tf = .FALSE.
endif
Both i and n are defined as integers and I am inside a do loop for n = 1,100. This throws up the following errors:
Error: Unclassifiable statement at (1) at the 'if i< n then'
Error: Unexpected ELSE statement at (1) at the 'else'
Error: Expecting END DO statement at (1) at the 'endif'
I can't see where these errors are coming from, no matter how I write the if statement (.NE. etc.) it seems to throw up the same things.
You forgot the parenthesis! According to the Fortran standard (2008, ch. 8.1.7.4), the if statement should read
if ( i < n ) then

Inserting a copy of a tuple into a list

I'm still new to Haskell, my problem is I have issues with adding a modified copy of data type instance into the list of those instances (don't know if it's not synonym).
Data type name is Task, short Tas.
If a task is fulfilled I need to mark it as fulfilled and if it's repeatable, I need to set next date for it (while keeping it in fulfilled tasks).
My approach is to create a copy of that task, basing on it, but with different ID and date and insert it into the list.
remakeDate serves for returning increased date of Task.
fulfilledTaskInList :: [Task] -> Int -> Int -> [Task]
fulfilledTaskInList [ ] check_ID pk_task = []
fulfilledTaskInList (x:xs) check_ID pk_task = if getId x == check_ID then
if getRepeatT x /= 0 then let help = remakeTask x pk_task in
(setFulfill x):help:xs
else (setFulfill x):xs
else x: fulfilledTaskInList xs check_ID pk_task
remakeTask x pk_task = do
(in_year, in_month, in_day) <- remakeDate (getYear x) (getMonth x) (getDay x) (getRepeatT x)
(Tas {id_Task = pk_task , name = showName x , descr = showDescr x, day = in_day, month = in_month, year = in_year, hour = getHour x, minute = getMinute x, fulfill = 0, repeatT = getRepeatT x})
Error thrown by ghci:
Task.hs:94:18:
Couldn't match expected type `IO b0' with actual type `Task'
In a stmt of a 'do' block:
(Tas
{id_Task = pk_task, name = showName x, descr = showDescr x,
day = in_day, month = in_month, year = in_year, hour = getHour x,
minute = getMinute x, fulfill = 0, repeatT = getRepeatT x})
In the expression:
do { (in_year, in_month, in_day) <- remakeDate
(getYear x) (getMonth x) (getDay x)
(getRepeatT x);
(Tas
{id_Task = pk_task, name = showName x, descr = showDescr x,
day = in_day, month = in_month, year = in_year, hour = getHour x,
minute = getMinute x, fulfill = 0, repeatT = getRepeatT x}) }
In an equation for `remakeTask':
remakeTask x pk_task
= do { (in_year, in_month, in_day) <- remakeDate
(getYear x)
(getMonth x)
(getDay x)
(getRepeatT x);
(Tas
{id_Task = pk_task, name = showName x, descr = showDescr x,
day = in_day, month = in_month, year = in_year, hour = getH
our x,
minute = getMinute x, fulfill = 0, repeatT = getRepeatT x})
}
Failed, modules loaded: Functions.
The error is saying that the do block in remakeTask is producing a Task as its last statement, but because it's a do block, it should produce a monadic value, in this case IO something.
You can add a return similar to the one in plusYear to fix this specific problem. However in general you seem to be using IO when you don't really need to, so you might instead investigate removing the do blocks along with the use of return and IO.

can I replace multiline like this in vim?

I want keep my js in this style. I want write a map in vim to do it faster.
from:
var a = x;
var b = y;
var c = z;
to:
var a = x
, b = y
, c = z
;
Use the following command.
%s/;\nvar /\r , /gc
My solution in a case like this would be
position cursor on first var (e.g. {)
1f;C-vjr,wC-vjexkJJ
For info, the Align script has the inverse operation:
var a = x, b = y, c = z;
VLeadera,, result:
var a = x;
var b = y;
var c = z;