How to hide multiply defined constants - isabelle

This questions extends the question How to hide defined constants.
I import theory A, B, and C, maybe in future also D, E, ...
All theories define a function f. I want to hide the definition of f in my current theory without changing the imported theories. When I write term f I get A.f. When I add hide_const (open) f to my current theory, A.f is hidden but now I get B.f as f. How can I completely hide f?
I need something like (hide_const (open) f)+.

The versions of function f from each theory have separate names (A.f, B.f, C.f), and these must all be hidden separately.
You are allowed to hide multiple names with a single hide_const command, though, and this is what I would recommend:
hide_const (open) A.f B.f C.f

Related

In Lean, how can i get the element from an exists_unique in a definition

I want to make the following
variables {Ω : Type} (P:set Ω → Ω → Prop)(a:Ω)
def G :set Ω → Ω:=
begin
intro V,
by_cases h: ∃!u : Ω, P V u,
--I want to use the u that is unique as return in this case, and in the other case use the a
sorry,
exact a,
end
I tried using the exists.elim and exists_unique.elim but i can't figure out how to use them properly, also I can't use h.some because I'm not using the axiom of choice.
I just want to know how to construct the function proving it's well defined, thanks.
You can use classical.some h to obtain a witness using the axiom of choice. Note that you are already using the axiom of choice via the by_cases tactic.
If you allow yourself to assume fintype Ω, then you can use fintype.choose h to use brute force instead of an axiom.

In functional programming, is there a name for a function that takes an x and gives back a tuple (x, x)?

I was wondering if there is a commonly used term for a function that turns a value into a tuple-2 in ML-family languages, or functional programming languages more generally?
let toTuple2 x = (x, x)
In stack-based programming languages such as Forth, dup is a core operator that does duplicate the top stack element (not exactly a tuple though).
In Haskell, various packages provide this function under names like dup, dupe or double. Notice that two-tuples are also a core element of arrows, and dup = id &&& id.
I have not found anything specific to ML.
I don't know about the name of that specific function.
However, that function can be seen as a special case of a more general one:
let applyCtorToXX c x = c x x
Indeed, you can verify that toTuple2 is equivalent to applyCtorToXX (,).
In combinatory logic, or at least in how it is presented in To Mock a Mockingbird, such a function is named a "Warbler", and the symbol W is used for it (i.e. Wxy = xyy is the definition used in the book).
Looking at it from this perspective, your toTuple2 is W (,), which is the application of a warbler to the 2-tuple constructor.

OWLAPI recursive axioms

I have in input N + 1 items, where N are role/OWLObjectProperty and the last one is a concept/OWLClass.
The problem is that I have to produce an OWLClassExpression like this one, considering the input (a,b,C):
OWLClassExpression axiom = factory.getOWLObjectSomeValuesFrom( factory.getOWLObjectProperty( "#a") , factory.getOWLObjectSomeValuesFrom(factory.getOWLObjectProperty("#b", C));
This is easy because I only have 2 roles, but I need a general solution for N roles, since I cannot predict the input of the user.
The axiom will be nested, but I do not know if there is a possibile to build a structure like that.
It is possible - you can replace C with a method call that recursively builds your expression, or you can reduce it to a list. E.g., a list of properties that you navigate in reverse order, starting at the axiomatic lever (C) and wrapping the previously created object in a new class expression.
However, the owl api has no utility class to do this, you'll have to code it from scratch.

Converting code to pure functional form

In a sense, any imperative code can be converted to pure functional form by making every operation receive and pass on a 'state of the world' parameter.
However, suppose you have some code that is almost in pure functional form except that, buried many layers of function calls deep, are a handful of imperative operations that modify global or at least widely accessed state e.g. calling a random number generator, updating a counter, printing some debug info, and you want to convert it to pure functional form, algorithmically, with the minimum of changes.
Is there a way to do this without essentially turning the entire program inside out?
This isn't technically all that hard.
If all you want to do is avoid modifying global state, e.g, make the code re-entrant, then for any side-effecting function F that reads X, writes global variable Y,
and returns Z, transform it into F' that reads X, modifies a struct containing Y passed to it, and returns Z. If A calls B calls .. transitively calls F,
and F modifies global Y, then A' must build a reference to a struct containing Y and pass it to B. Now all your functions only modify values passed to them; they have no side effect on global state. (Well, we can argue about what A' does).
[Somebody may complain that (File/Screen/Device) Output done by F can't be handled. Well, either you want the immediate side effect on the world or you don't. If you don't, add the "state" of the Output as Y, and modify that; A' can return the desired Output as a result.]
If you insist on making the the program functional then any side effect of F on global Y has be changed to pass Y in, copying Y while changing it to produce Y', and then F' must return a pair . Callers of F must pass in Y, and use the resulting Y' in all code reachable from the call site.
The bit about copying gets you into real trouble: where is the logic to do a deep copy of Y to be found? If you find it, you may discover that deep copy produces an enormous structure and your storage demand gets impossible quickly. Now you need to find a way to make Y' share the parts of Y that haven't changed.
Now, if you want to do either of these tasks on a big code, you probably don't want to do it by hand; people are bad at handling this kind of detail and they want to cheat/rewrite/... If you really want to do this, you want a source-to-source program transformation system, which can mechanically apply the necessary steps.
I'll note that standard compilation techniques convert programs to so called "static single assignment (SSA)" form, which converts as much of the program, represented in the IR, into a functional program because it is easier to transform/optimize. They still worry about global storage.

Recursive function parameters

I made a simple recursive function, and expected it to work (but it doesn't):
open System
open System.Threading
let f =
let r = Random()
let rec d =
printfn "%d" (r.Next())
Thread.Sleep(1000)
d
d
f
With the help of Intellisense I ended up with the following working function (but without understanding why previous function didn't work):
open System
open System.Threading
let f : unit =
let r = Random()
let rec d() =
printfn "%d" (r.Next())
Thread.Sleep(1000)
d()
d()
f
So why do I need to explicitly state unit and ()?
In the first version, you declared a recursive object (let rec d), or a value. You're saying that the d object is recursive, but how an object could be recursive? How does it call itself? Of course, this doesn't make sense.
It's not possible to use recursive objects in F# and this is the reason why your first version doesn't work.
In the second version, you declared a recursive function (let rec d()). Adding (), you're explicitly stating that d is a function.
Furthermore you explicitly stated, with unit, that the function f (called just once) will not return anything, or, at least, you're saying that f will return a value of a not specific type. In F#, even the simplest functions must always return a value.
In your case, F# will try to infer the type that f will return. Because there's no specific type annotation and your f is not doing something (like a calculation) that will return a specific value using a specific type, the F# compiler will assign a generic return type to f, but your code is still ambiguous and you have to specify the unit type (the simplest type that a F# function could return) to be more specific.
The value restriction error is related indeed to F#'s powerful type inference. Please have a look at this interesting article about this error.
In your first attempt, you define not a function, but a value. The value d is defined in terms of itself - that is, in order to know what d is, you need to first know what d is. No wonder it doesn't work!
To make this a bit more clear, I will point out that your definition is of the same kind as this:
let x = x
Would you expect this to work?
In your second attempt, you gave d a parameter. It is the parameter that made it a function and not a value. Compare:
let rec x() = x()
This will still cause a stack overflow when executed, but at least it will compile: it's a function that unconditionally calls itself.
You didn't have to give it specifically a unit parameter, any parameter would do. You could have made it a number, a string, or even a generic type. It's just that unit is the simplest option when you don't care what it is.
And you didn't actually need to annotate f with a type. That was an extraneous step.
In conclusion, I'd like to point out that even in your second code block, f is still a value, not a function. In practical terms it means that the code inside f will be executed just once, when f is defined, and not every time you mention f as part of some other expression, which is apparently what you intuitively expect.

Resources