When I format this piece with ReSharper:
this.Tabs.Add(bb => bb
.Horizontal(h => h
.Vertical(v => v
.Box("0", upperView, "")
.Box("1", mainView, ""))));
I get:
this.Tabs.Add(bb => bb
.Horizontal(h => h
.Vertical(v => v
.Box("0", upperView, "")
.Box("1", mainView, ""))));
But I want it to stay the same. How would I do this?
Maybe deselecting
ReSharper →
Options →
Languages →
C# →
Formatting Style →
Other →
Align Multiline Constructs →
Expression
reduces the effect.
(Assumed you are using C#.)
Related
I have a recursive function which operates a binary tree of integers, implemented as a nested pair of pairs or ints. My function creates a new tree with a different structure, and calls itself recursively until some condition is met. The issue I'm finding is that the first time the code is run, it takes a really long time to JIT compile all the possible signatures of the function; afterwards it runs fine.
Here is minimal working example:
my_tree = ((((6 => 7) => (6 => 7)) => ((7 => 7) => (0 => 7))) => (((8 => 7) => (7 => 7)) => ((8 => 8) => (8 => 0)))) => ((((2 => 4) => 7) => (6 => (0 => 5))) => (((6 => 8) => (2 => 8)) => ((2 => 1) => (4 => 5))))
function tree_reduce(tree::Pair)
left, right = tree
left isa Pair && (left = tree_reduce(left))
right isa Pair && (right = tree_reduce(right))
return left + right
end
#show my_tree
#show tree_reduce(my_tree)
using MethodAnalysis
methods = methodinstances(tree_reduce)
#show length(methods)
Although this example is not perceptually slow, it still generates 9 method instances for:
tree_reduce(::Pair{Pair{Pair{Int64, Int64}, Pair{Int64, Int64}}, Pair{Pair{Int64, Int64}, Pair{Int64, Int64}}})
tree_reduce(::Pair{Pair{Int64, Int64}, Pair{Int64, Int64}})
tree_reduce(::Pair{Int64, Int64})
tree_reduce(::Pair{Pair{Pair{Pair{Int64, Int64}, Int64}, Pair{Int64, Pair{Int64, Int64}}}, Pair{Pair{Pair{Int64, Int64}, Pair{Int64, Int64}}, Pair{Pair{Int64, Int64}, Pair{Int64, Int64}}}})
etc ...
Is there a way of avoiding this / precompiling / speeding it up / writing a generic function / running particular (part of) a function in an interpreted mode? I would be prepared to make the overall performance of the code slightly works at the pice of having it run faster on the first top-level call to tree_reduce.
#nospecialize is an option, but I think in this case, a separate data structure which doesn't capture the whole data structure in its type is in order. Pair is really thought for strongly typed pairs of things, not for large nested structures.
julia> abstract type BiTree{T} end
julia> struct Branch{T} <: BiTree{T}
left::BiTree{T}
right::BiTree{T}
end
julia> struct Leaf{T} <: BiTree{T}
value::T
end
julia> Base.foldl(f, b::Branch) = f(foldl(f, b.left), foldl(f, b.right))
julia> Base.foldl(f, l::Leaf) = f(l.value)
julia> (→)(l::Branch, r::Branch) = Branch(l, r) # just for illustration
→ (generic function with 1 method)
julia> (→)(l, r::Branch) = Branch(Leaf(l), r)
→ (generic function with 2 methods)
julia> (→)(l::Branch, r) = Branch(l, Leaf(r))
→ (generic function with 3 methods)
julia> (→)(l, r) = Branch(Leaf(l), Leaf(r))
→ (generic function with 4 methods)
julia> my_tree = ((((6 → 7) → (6 → 7)) → ((7 → 7) → (0 → 7))) → (((8 → 7) → (7 → 7)) → ((8 → 8) → (8 → 0)))) → ((((2 → 4) → 7) → (6 → (0 → 5))) → (((6 → 8) → (2 → 8)) → ((2 → 1) → (4 → 5))));
julia> typeof(my_tree)
Branch{Int64}
julia> foldl(+, my_tree)
160
This also has the advantage that you can without danger of breaking anything overload other methods, such as for printing or indexing.
yes, with Base.#nospecialize:
function tree_reduce2(Base.#nospecialize(tree::Pair))
left, right = tree
left isa Pair && (left = tree_reduce(left))
right isa Pair && (right = tree_reduce(right))
return left + right
end
#show my_tree
#show tree_reduce2(my_tree)
using MethodAnalysis
methods = methodinstances(tree_reduce2)
#show length(methods) #1
Check the docs of the macro to know more of how to use it.
I am stuck in the implementation of a function and also I am not sure if it is the correct way to solve my problem.
Description of my problem
For the context, I want to be able to borrow (a unary operation) a field of a structure only if no references on this field or its parent already exist. Let me clarify my this with the following example. I hope things will become more clear with code.
struct p{ x, p2:{ x, p3: {x} }
let a = ref p
let b = ref p.p2
let c = ref p.p2.p3
Here I have a structure p with nested fields and 3 references: one on p and 2 on its fields.
I use a Map to store the mapping between referred and their ref:
<env>
1 |-> 0 // 1 is a and 0 is p
2 |-> 0.1 // 2 is b and 0.1 is p.p2
3 |-> 0.1.2 // 3 is c and 0.1.2 is p.p2.p3
</env>
So now, If I want to do the unary operator borrow on p.p2.p3.x:
borrow p.p2.p3.x;
This operation should fail because a,b and c exists in my env.
My code
So, I tried to implement this in this snippet:
module TEST-SYNTAX
import DOMAINS
syntax Ref ::= "ref" "{" Path "}"
syntax Path ::= List{Int,","}
syntax Stmts ::= List{Stmt, ";"}
syntax Stmt ::= Ref
| Int "borrow" "{" Path "}"
endmodule
module TEST
import TEST-SYNTAX
configuration <T>
<k>$PGM:Stmts</k>
<env> .Map </env>
</T>
rule S:Stmt ; Ss:Stmts => S ~> Ss
rule .Stmts => .
rule <k> ref { P:Path } => . ... </k>
<env> Rho:Map => Rho[!I:Int <- P] </env>
syntax Bool::= #checkborrow(Int, List, Path) [function]
syntax List ::= #pathToSubPaths(Path, List) [function]
rule <k> N:Int borrow { P:Path } => #checkborrow(N, #pathToSubPaths(P, .List), P) ... </k>
rule #pathToSubPaths(.Path, S:List) => S
endmodule
I am stuck on how I can implement the #checkborrow function. My idea is to first generate all the sub path of a given paths, for example:
#pathToSubPath(p.p2.p3.x) => { {p} , { p.p2 }, { p.p2.p3 }, { p.p2.p3.x } }
After, make a projection function on env to see if the element exist or not:
#refForSubPathsExist(SubPaths:Set) => {True, True, True, False}
Then reducing this returned Set with a folding OR
#checkborrow({True, True, True, False}) => True
For now, I am stuck in the implementation of #pathToSubPath.
Thank you if you had the courage to read the whole question :). I am unfamiliar with K, so I am looking for help.
NOTE:
We are using this version of K Framework: https://github.com/kframework/k/releases/tag/nightly-f5ea5c7
For the map method, why isn't it like the 2nd line instead of the 1st? It takes 2 parameters, a function, and something else, so why does it have 2 of the arrows?
Filterable f => (a → Boolean) → f a → f a
Filterable f => (a → Boolean) , f a → f a
This is currying. Briefly, you can replace a function of multiple arguments:
const f = (x, y) => x + y;
f(1, 2) // 3
With a series of functions of one argument:
const f = x => y => x + y;
f(1)(2) // 3
One advantage being that it’s easier to partially apply a curried function:
const add1 = f(1);
add1(2) // 3
This is used extensively in Haskell, which seems to be a source of inspiration for ramda.js, and in turn the lambda calculus on which Haskell is based.
Note that the function arrow type is right-associative, so these are equivalent:
Filterable f => (a → Boolean) → f a → f a
Filterable f => (a → Boolean) → (f a → f a)
Seen this way, a function of this type converts a predicate on as into a transformation on fs of as.
There’s also a relation to simple algebra: in type theory, a tuple (a, b) corresponds to a product (a × b), and a function arrow a → b corresponds to exponentiation (ba). You can convert between a → b → c and (a, b) → c for the same reason you can convert between (cb)a and cb × a.
I've just installed Idris v.1.0 and run the sample code from the Proof section of Rosetta Code, piece by piece. Everything works fine up to the following fragment, which produces the The 'class' keyword is deprecated. Use 'interface' instead. error.
-- 3.1, Prove that the addition of any two even numbers is even.
evensPlus1 : {a : MyNat} -> {b : MyNat} -> (EvNat a) -> (EvNat b) -> (EvNat (a :+ b))
evensPlus1 ea eb = ?proof31
There's not a single piece of 'class' in the source. What could be behind this issue?
Those are just warnings. The %elim-annotations are described in this deprecated chapter of the manual. You can safely delete them and finish the proof, e.g. like this:
evensPlus1 : (EvNat a) -> (EvNat b) -> (EvNat (a :+ b))
evensPlus1 EvO eb = eb
evensPlus1 (EvSS y) eb = EvSS (evensPlus1 y eb)
congS : {a : MyNat} -> {b : MyNat} -> (a = b) -> (S a = S b)
congS Refl = Refl
As I told before, I'm working in a library about algebra, matrices and category theory. I have decomposed the algebraic structures in a "tower" of record types, each one representing an algebraic structure. As example, to specify a monoid, we define first a semigroup and to define a commutative monoid we use monoid definition, following the same pattern as Agda standard library.
My trouble is that when I need a property of an algebraic structure that is deep within another one (e.g. property of a Monoid that is part of a CommutativeSemiring) we need to use a number of a projections equal to desired algebraic structure depth.
As an example of my problem, consider the following "lemma":
open import Algebra
open import Algebra.Structures
open import Data.Vec
open import Relation.Binary.PropositionalEquality
open import Algebra.FunctionProperties
open import Data.Product
module _ {Carrier : Set} {_+_ _*_ : Op₂ Carrier} {0# 1# : Carrier} (ICSR : IsCommutativeSemiring _≡_ _+_ _*_ 0# 1#) where
csr : CommutativeSemiring _ _
csr = record{ isCommutativeSemiring = ICSR }
zipWith-replicate-0# : ∀ {n}(xs : Vec Carrier n) → zipWith _+_ (replicate 0#) xs ≡ xs
zipWith-replicate-0# [] = refl
zipWith-replicate-0# (x ∷ xs) = cong₂ _∷_ (proj₁ (IsMonoid.identity (IsCommutativeMonoid.isMonoid
(IsCommutativeSemiring.+-isCommutativeMonoid
(CommutativeSemiring.isCommutativeSemiring csr)))) x)
(zipWith-replicate-0# xs)
Note that in order to access the left identity property of a monoid, I need to project it from the monoid that is within the commutative monoid that lies in the structure of an commutative semiring.
My concern is that, as I add more and more algebraic structures, such lemmas it will become unreadable.
My question is: Is there a pattern or trick that can avoid this "ladder" of record projections?
Any clue on this is highly welcome.
If you look at the Agda standard library, you'll see that for most specialized algebraic structures, the record defining them has the less specialized structure open public. E.g. in Algebra.AbelianGroup, we have:
record AbelianGroup c ℓ : Set (suc (c ⊔ ℓ)) where
-- ... snip ...
open IsAbelianGroup isAbelianGroup public
group : Group _ _
group = record { isGroup = isGroup }
open Group group public using (setoid; semigroup; monoid; rawMonoid)
-- ... snip ...
So an AbelianGroup record will have not just the AbelianGroup/IsAbelianGroup fields available, but also setoid, semigroup and monoid and rawMonoid from Group. In turn, setoid, monoid and rawMonoid in Group come from similarly open public-reexporting these fields from Monoid.
Similarly, for algebraic property witnesses, they re-export the less specialized version's fields, e.g. in IsAbelianGroup we have
record IsAbelianGroup
{a ℓ} {A : Set a} (≈ : Rel A ℓ)
(∙ : Op₂ A) (ε : A) (⁻¹ : Op₁ A) : Set (a ⊔ ℓ) where
-- ... snip ...
open IsGroup isGroup public
-- ... snip ...
and then IsGroup reexports IsMonoid, IsMonoid reexports IsSemigroup, and so on. And so, if you have IsAbelianGroup open, you can still use assoc (coming from IsSemigroup) without having to write out the whole path to it by hand.
The bottom line is you can write your function as follows:
open CommutativeSemiring CSR hiding (refl)
open import Function using (_⟨_⟩_)
zipWith-replicate-0# : ∀ {n}(xs : Vec Carrier n) → zipWith _+_ (replicate 0#) xs ≡ xs
zipWith-replicate-0# [] = refl
zipWith-replicate-0# (x ∷ xs) = proj₁ +-identity x ⟨ cong₂ _∷_ ⟩ zipWith-replicate-0# xs