How to call a constructed quotation in Factor - factor-lang

I have a word which constructs a quotation which I want to be called. However, when I load the code I get Cannot apply “call” to a run-time computed value. If I use the walker and step thru the code it executes as expected. How are you supposed to call a constructed quotation?
: ba>struct ( array class -- struct array )
[ <struct> swap ] keep struct-slots
[
[ type>> to-type ] keep
name>> setter-word 1quotation curry
[ over ] dip curry call drop
] each
;
EDITED: This does work
: ba>struct ( array class -- struct array )
[ <struct> swap ] keep struct-slots
[
[ type>> to-type ] keep
name>> setter-word 1quotation curry
[ over ] dip curry call( -- x ) drop
] each
;
The problem stems from the runtime not knowing what the stack effect is of a constructed quote. In these cases, you must declare what the stack looks like to the quote for the call.

Related

Is it possible to write a rust macro that transposes a matrix?

I am currently working on optimizing the rust jpeg decoder crate using SIMD. In order to avoid long repetitions in the code, I would like to write a macro that generates the following matrix transposition code :
s = [
i32x8::new(s[0].extract(0),s[1].extract(0),s[2].extract(0),s[3].extract(0),s[4].extract(0),s[5].extract(0),s[6].extract(0),s[7].extract(0), ),
i32x8::new(s[0].extract(1),s[1].extract(1),s[2].extract(1),s[3].extract(1),s[4].extract(1),s[5].extract(1),s[6].extract(1),s[7].extract(1), ),
i32x8::new(s[0].extract(2),s[1].extract(2),s[2].extract(2),s[3].extract(2),s[4].extract(2),s[5].extract(2),s[6].extract(2),s[7].extract(2), ),
i32x8::new(s[0].extract(3),s[1].extract(3),s[2].extract(3),s[3].extract(3),s[4].extract(3),s[5].extract(3),s[6].extract(3),s[7].extract(3), ),
i32x8::new(s[0].extract(4),s[1].extract(4),s[2].extract(4),s[3].extract(4),s[4].extract(4),s[5].extract(4),s[6].extract(4),s[7].extract(4), ),
i32x8::new(s[0].extract(5),s[1].extract(5),s[2].extract(5),s[3].extract(5),s[4].extract(5),s[5].extract(5),s[6].extract(5),s[7].extract(5), ),
i32x8::new(s[0].extract(6),s[1].extract(6),s[2].extract(6),s[3].extract(6),s[4].extract(6),s[5].extract(6),s[6].extract(6),s[7].extract(6), ),
i32x8::new(s[0].extract(7),s[1].extract(7),s[2].extract(7),s[3].extract(7),s[4].extract(7),s[5].extract(7),s[6].extract(7),s[7].extract(7), ),
];
The macro should be able to generate the code for different matrix sizes (4 or 8).
I have tried several different approaches, but I never manage to get the macro to repeat n times an n-item pattern.
The most logical to me would be:
macro_rules! square {
(($($x:tt),*), ($($y:tt),*)) => {
[
$([
$( s[$x].extract($y) ),*
]),*
]
};
($($x:expr),*) => { square!( ($($x),*) , ($($x),*) ) };
}
but it fails with
error: attempted to repeat an expression containing no syntax variables matched as repeating at this depth
You can do it, but you will need to handle the outer repetition through recursion:
macro_rules! square {
(#row [$($acc:expr),*] [$($before:expr),*] $current:expr $(, $after:expr)*) => {
square!(#row
[ $($acc,)*
stringify!($(s[$current].extract ($before),)*
s[$current].extract ($current)
$(, s[$current].extract ($after))*) ]
[ $($before,)* $current ]
$($after),*)
};
(#row [$($acc:tt)*] [$($before:expr),*]) => { vec![ $($acc)* ] };
($($r:expr),*) => {
square!(#row [] [] $($r),*)
};
}
Playground
I've called stringify! so that the code would compile on the playground without the dependencies. You will need to replace them to fit your needs (probably just remove the stringify! invocation and replace s by an identifier you pass to the macro).
The idea of accumulating the values in $acc and outputting them all at once at the end of the recursion is called the push-down accumulation pattern. If you are not familiar with this concept, it is described in detail in the little book of Rust macros.

How do you allow not foreseen properties in RDF when performing Shex validation?

We are creating our Shex definition files checking that some IRIs are of a given type. There is no problem with our generated code but sometimes we get files generated using Protege and most of the individuals are of type X plus owl:NamedIndividual, making our validation fail because now a given resource has 2 assertions of type rdf:type.
Adding owl:NamedIndividual to all shape checks seems like polluting the Shape definition, so how would you allow extra properties that do not conflict with your shape definition?
In Shex, by default the triple constraints are closed which means that a shape like:
:Shape {
rdf:type [ :X ]
}
means that a node that conforms to :Shape must have exactly one rdf:type declaration whose value is :X.
If you want to allow extra values for the rdf:type declaration, you can express it with the keyword EXTRA as:
:Shape EXTRA rdf:type {
rdf:type [ :X ]
}
The meaning now is that a conforming node must have rdf:type :X and can have zero or mode values for rdf:type.
Notice that the previous example could be defined as:
:Shape EXTRA a {
a [ :X ]
}
In the particular case that you only want to allow an extra rdf:type with value owl:NamedIndividual you could also define it as:
:Shape {
a [:X ] ;
a [ owl:NamedIndividual] ;
}
or as:
:Shape {
a [:X owl:NamedIndividual]{2} ;
}

How to distribute agents' attributes randomly according to specific probabilities in Netlogo?

I am relatively new to Netlogo, having completed only a handful of models. Currently working on one for my dissertation where I need to distribute agents' attributes randomly according to specific probabilities, some at the onset of the simulation, other attributes to be distributed throughout. This is connected with an extension of the trust game, for those familiar with it. I completed the conceptual model with a colleague who doesn't use Netlogo, so I am a bit stuck at the moment.
I think the rnd extension might be useful, but I can't quite figure out how to use it. My apologies if this seems redundant to any of you, but I really hope to get some help here.
extensions [ rnd]
;; divides agents into two types
breed [ sexworkers sexworker ]
breed [ officers officer ]
;; determines attributes of agents
sexworkers-own
[ assault? ;; is assaulted
trust? ;; probability to trust police to report assault
protection? ;; probability of good experience with police during report
prob-trust ] ;; probability to trust overall
officers-own
[ behavior ] ;; probability of treating sex workers well/badly during report
This is the start of the model, and then I want to distribute the attributes according to specific probabilities. I honestly haven't found a way to do this that works as I intend it to.
What I want is to start off, for every sex worker alike, a probability of 0.01 to be assaulted (prob-assault; assault?=true). Afterwards, with each tick, there is again the chance of 0.01 for sex workers to be assaulted.
Afterwards, in the subset of assault?=true, there is then a probability to report the assault (prob-report, 0.5. This is expressed by trust?=true/false. Within the subset of those who report, there is then a final probability of having a good/bad experience with police (prob-protection), here protection?=true/false.
These three attributes should be randomly distributed according to the probabilities, and also then result in a combined probability to trust police in the future, prob-trust. (prob-trust = prob-assault + prob-report + prob-protection).
What I have done (without rnd extension so far is this:
;; determines sex workers' behavior
ask sexworkers [ move ]
ask sexworkers [ victimize ]
ask sexworkers [ file ]
to victimize
ask sexworkers [
ifelse random-float 1 <= 0.0001
[ set assault? 1 ]
[ set assault? 0 ]
]
end
to file
ask sexworkers with [ assault? = 1 ] [
ifelse random-float 1 <= 0.5
[ cooperate ]
[ avoid ]
]
end
to cooperate
ask sexworkers [ set trust? 1 ]
end
to avoid
ask sexworkers [ set trust? 0 ]
end
What happens at the moment though is that there is no variation in attributes, all sex workers seem to have no assault and trust/not trust varying all simultaneously. I am not sure what is going on.
(1) You don't need the rnd extension for anything you are trying to do here. If you just want to take some action with some probability then your approach of if random-float 1 < <probablility value> is the correct approach. The rnd extension is when you want to get into weighted probability, for example choosing agents based on their income.
(2) NetLogo recognises true and false (capitalisation does not matter) as specific truth values. You should not use 1 and 0 as proxies for true and false. There are several advantages to using the truth values directly. The most obvious is readability, you can have statements like set trust? true and if trust? = true [do something]. More compactly, you can simply say if trust? [do something]. Other advantages include access to logical operators such as not and and for your conditions.
With regard to your actual problem of every agent having the same behaviour, you have nested your ask turtles type statements. For example, you have:
to file
ask sexworkers with [ assault? = 1 ] [
ifelse random-float 1 <= 0.5
[ cooperate ]
[ avoid ]
]
end
If you substitute the cooperate and avoid procedures into this code, you would get:
to file
ask sexworkers with [ assault? = 1 ] [
ifelse random-float 1 <= 0.5
[ ask sexworkers [ set trust? 1 ] ]
[ ask sexworkers [ set trust? 0 ] ]
]
end
So, if your random number is, say, 0.4 then ALL your sexworkers will have trust set to 1, not just the particular sexworker who 'rolled the die'.
You either need:
to file
ask sexworkers with [ assault? = 1 ] [
ifelse random-float 1 <= 0.5
[ set trust? true ]
[ set trust? false ]
]
end
Or you need:
to cooperate
set trust? true
end
to avoid
set trust? false
end
Use the first option if there's not really anything else that is being done. Use the second option if setting the trust? value is just one of many actions that the turtle should take when it is cooperating or avoiding.

Ruby - Why is this reverse sort not working correctly?

I have code that's sorting an array of arrays of objects based on a common method/value all objects share, but it's not quite working correctly. The code is supposed to give me the X highest totals based on the added value, but if I do .first(10) it misses the top values. If I do first(50), I see more, but there's still some higher values missing. What's going on here, shouldn't my sort and reverse ensure that first(x) will return the array with the highest sum? It seems first is grabbing without the sort being completed.
some_array = old_array.sort_by{ |ar| ar.sum(&:ppd) }.reverse.first(10)
Here's an example of what old_array looks like in case it adds clarity
old_array = [
[ obj1, obj2 ], #obj1.ppd + obj2.ppd = 50
[ obj3, obj4 ], #sum ppd = 55
[ obj5, obj6 ], #sum ppd = 60, does not show in first(10)
... #and so on with dozens more
]

Prolog Lists with Lists inside

I'm a total newbie in Prolog I have this code (I'm sorry if its not very friendly to read)
AdjOfAll(State,[FreeSpace],[Result]):-
Adj(State,FreeSpace,Result).
AdjOfAll(State,[Space|NextSpace],[X|Xs]):-
AdjOfAll(State,NextSpace,Xs),
Adj(State,Space,X).
my problem is that Adj returns [ (element_1), ... , (element_n) ]
My objective with AdjOfAll was to get,
[ ( element_1), ..., (element_n), (element_1_from 2nd adj), ... ]
but i get
[ [ ( element_1), ... ,(element_n)] , [ (element_1_from 2nd adj) ], ... ]
I hope its understandable what I'm trying to say, sorry for the bad English
see if flatten/2 can help you to get the right output. Place it after the top level call to AdjOfAll/3.
...
AdjOfAll(State, NextSpace, ResultNested),
flatten(ResultNested, Result),
...
BTW are you sure your predicates are named correctly? Usually they are lowercase.

Resources