Jena recursive reasoning issue - jena

In the below example, the infM doesn't infer that {r myProperty v}, any ideas why?
However, it does infer that {r owl:sameAs r1} from the mySameAs asserted statement. But if I write:
this.infM = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MINI_RULE_INF, this.baseM);
this.infM = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MINI_RULE_INF, this.infM);
then it also outputs that {r myProperty v}
Example code:
this.baseM = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
this.infM = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MINI_RULE_INF, this.baseM);
OntResource r = baseM.createOntResource(null);
OntResource r1 = baseM.createOntResource(null);
OntProperty myProperty = baseM.createOntProperty("http://example.com#my_property");
OntResource v = baseM.createOntResource("http://example.com#a_value");
OntProperty mySameAs = baseM.createOntProperty("http://example.com#mySameAs");
baseM.add(mySameAs, RDFS.subPropertyOf, OWL.sameAs);
r1.addProperty(myProperty, v);
r.addProperty(mySameAs, r1);
infM.rebind();
assertTrue(infM.contains(r, myProperty, v));

This looks like a bug to me. I've logged it as jena-61 on the Apache Jena Jira.

Related

Record transformation

Suppose I have the following type:
type Temp<'b,'c> =
{
A : string
B : 'b
C : 'c
D : string
}
And I want to create a function that receives Temp<string,string> and outputs Temp<int,int>. I tried two approaches. The most cumbersome (f1) works and the most logical (in my view) does not (f2).
let f1 (r : Temp<string,string>) = //works
{
A = r.A
B = r.B |> int
C = r.C |> int
D = r.D
}
//doesn't work
let f2 (r : Temp<string,string>) = {r with B = r.B |> int; C = r.C |> int}
Is there another way to construct such a function without having to repeat all the fields in the body?
As mentioned before, you can not (ATM) use the f2 approach but you can simply create a generic version of your f1 approach and use it.
type Temp<'b,'c> = {
A: string
B: 'b
C: 'c
D: string
}
module Temp =
let bind fB fC temp =
{
A = temp.A
B = fB temp.B
C = fC temp.C
D = temp.D
}
let bind1 f = bind f f
let sTemp: Temp<string, string> = {
A = "a"
B = "b"
C = "c"
D = "d"
}
let iTemp: Temp<int, int> = sTemp |> Temp.bind int int // with two separate functions for each generic field
let iTemp: Temp<int, int> = sTemp |> Temp.bind1 int // with one function for both fields at once
I don't think so. A copy and update record expression can only be used to create a new record of the same type, but Temp<string, string> and Temp<int, int> are not the same type. I think there are a number of suggestions to broaden this along the lines you suggest in the F# language issue tracker (e.g. this one), but AFAIK, none have made it into the language yet.

How to Access a Value from a Builder using Custom Operation in a Computation Expression

I have a Computational Expression Builder which receives
a value during construction
type SomeBuilder<'e> (e: 'e) =
member this.Bind(x, fn) = ...
member this.Return x = ...
member this.ReturnFrom x = ...
let buildSome v = SomeBuilder(v)
buildSome 2 {
return 1
}
Now I'd like to access the value e from within the
Computational Expression via a custom operation so that
buildSome 2 {
return 1 + e()
}
So I really want to access properties/values in the underlying builder object and work with them
I imagine I would need something like
type SomeBuilder<'e> (e: 'e) =
member this.Bind(x, fn) = ...
member this.Return x = ...
member this.ReturnFrom x = ...
[<CustomOperation("e")>]
member this.E () = e
but that doesn't work.
So my question is
a) is something like this possible using CustomOperations and Computational Expressions
b) and if it is possible, how?
Disclaimer:
As usual in programming there is a million ways to achieve similar effects
in completely different ways. I am explicitly asking for this particular way
and I am OK if the answer is simply "No". But please refrain from answers that are non answers in the narrowest sense laid out here.
I'm not sure you'll like my answer and whether it's within your boundaries, but you could capture the builder instance using a trick like this:
type SomeBuilder<'e> (e: 'e) =
member this.Value = e
[<CustomOperation("extract", MaintainsVariableSpaceUsingBind = true, AllowIntoPattern = true)>]
member this.Extract (state) = this
member this.Bind(x, fn) = fn x
member this.Return x = x
member this.ReturnFrom x = x
let builder e = new SomeBuilder<_>(e)
let x = builder 1 {
extract into builder // now we've brought builder in the scope
printfn "here we can read the value = %d" builder.Value
return 0
}
To show that primary constructor arguments are in scope for the builder's instance methods:
type SomeBuilder<'e> (e: 'e) =
member __.Bind(x, fn) = fn x
member __.Return x = x
[<CustomOperation("e", MaintainsVariableSpaceUsingBind = true, AllowIntoPattern = true)>]
member __.E _ = e
SomeBuilder 2 {
e into i
return 1 + i }
// val it : int = 3
SomeBuilder "bar" {
e into s
return "foo" + s }
// val it : string = "foobar"
Consider the position of the custom operation inside the builder; it will ignore expressions that precede it.

How to print from F# like REPL does

I am trying to use F# as a REPL and scripting which uses C# library. When I evaluate an object in REPL, it prints its internal structure:
> <expression>;;
val it: <type> =
<subtype> {<prop> = <value>;
...
<prop> = <value>;}
Then I am writing a script with the same expression and want it to print same output. But I cannot find any print function which would do it. The closest I could find is printfn "%O" which uses ToString() method, which is not defined in my case and just prints the object type.
This seems to be a simple question but I cannot find it answered here or anywhere in Google.
How to generate the F# type signature similar to FSI in my own code? seems to be focused on type, and I basically need rather pretty-printed value.
PS: seems like it is code which is internal to fsi.exe. See fsi.fs and sformat.fs sources. I tried to invoke them through reflection, but simple Internal.Utilities.StructuredFormat.any_to_string(value) printed just a type. Would still be nice if anybody knows how to invoke it correctly, but for time being I decided not to spend more efforts on it.
I just had the same issue, but in my case, printfn "%A" gave exactly the same result as what I see in F# Interactive (bar the indentation):
For a list:
> let l = [(2,"a")];;
val l : (int * string) list = [(2, "a")]
> printfn "%A" l;;
[(2, "a")]
For a record:
> type R = { A: string; B: int };;
type R =
{A: string;
B: int;}
> let r = { A = "Foo"; B = 1 };;
val r : R = {A = "Foo";
B = 1;}
> printfn "%A" r;;
{A = "Foo";
B = 1;}
For a non-F# datatype:
> let u = UriBuilder("http", "bar", 80);;
val u : UriBuilder = http://bar:80/
> printfn "%A" u;;
http://bar:80/

Make WebSharper generate simple field access

I need a type that will be translated into a plain JS object so that F# field access will be translated into simple JS field access (this is necessary, since the object will be sent through postMessage, so it'll lose all its methods).
Again, I need
let x = a.b
to be translated into
var x = a.b;
without any method calls.
Here is a slightly modified example from the F# Reference:
namespace T
open IntelliFactory.WebSharper
[<JavaScript>]
module A =
type MyClass =
val a : int
val b : int
new(a0, b0) = { a = a0; b = b0; }
let myClassObj = new MyClass(35, 22)
let x = myClassObj.b
This won't translate with
x: error : Failed to translate property access: b.'
Ok, let's make those vals mutable:
namespace T
open IntelliFactory.WebSharper
[<JavaScript>]
module A =
type MyClass =
val mutable a : int
val mutable b : int
new(a0, b0) = { a = a0; b = b0; }
let myClassObj = new MyClass(35, 22)
let x = myClassObj.b
This will be successfully translated, but… MyClass.New returns an empty object. The question now starts looking much like a bugreport, right? So, back to the question.
Are there any other ways to achieve what I want?
There are additional issues with record-style constructors "{x = y}". I will have to look into this again on F# 3.0, the older F# did not produce sensible quotations for those and we did some partial workarounds in WebSharper. Right now your example breaks. So here is the working code with a static method instead of a constructor:
type MyClass private () =
[<DefaultValue>]
val mutable a : int
[<DefaultValue>]
val mutable b : int
static member Create(a0, b0) =
let c = MyClass()
c.a <- a0
c.b <- b0
c
let test () =
let myClassObj = MyClass.Create(35, 22)
let x = myClassObj.a
let y = myClassObj.b
JavaScript.Log(x, y)
Trivially, a record would also work.
In some cases where you want to go really low-level you can annotate members with the Inline attribute. When this is too much overhead you can use untyped API:
let x = obj ()
x?a <- 1
let y = x?a
JavaScript.Log(x, y)
try this:
type MyClass (a, b) =
member val A = a with get
member val B = b with get
let myClassObj = new MyClass(35, 22)
let x = myClassObj.B

Problem with computational workflow

trying to follow example in the expert f# book, and having an issue with the workflows...the code is as follows:
type Attempt<'a> = option<'a>
let succeed x = Some (x)
let fail = None
let bind p rest =
match p with
| None -> fail
| Some r -> rest r
let delay f = f()
type AttemptBuilder() =
member b.Return (x) = succeed x
member b.Bind (p, rest) = bind p rest
member b.Delay (f) = delay f
member b.Let (p, rest):Attempt<'a> = rest p //'
member b.ReturnFrom x = x
// using it:
let attempt = new AttemptBuilder()
let test foo =
attempt {
if not foo then return! fail else return foo
}
let check () =
attempt {
let! n1 = test true
let! n2 = test false
let! n3 = test true
let foo = n1,n2,n3
return foo
}
let foo = check ()
problem is , when all values are true, i get as expected, a Some(true, true, true), but if one of the values passed in is false, foo is null (!). Anyone ftw?
thanks!
This is just because None is actually represented as null at runtime (see the remarks on the Option<'T> page on MSDN). Also, note that you can add
member x.Zero() = fail
to your builder, and then you can write test as
let test x = attempt { if x then return foo }
which is a little cleaner to my eyes.

Resources