I did this on my own i just want confirmation that it is right. I am supposed to Modify ADT Specifications for Stacks to account for operations suck as count (returning the number of elements in a stack) , change_top(replacing the top of a stack by a given element) and wipe_out(remove all elements). Also to include new axioms and preconditions as needed.
Here is what i have:
TYPES
• STACK[G]
FUNCTIONS
• put: STACK[G] x G -> STACK[G]
• remove: STACK[G]-/> STACK[G]
• item: STACK[G] -/> G
• empty: STACK[G] -> BOOLEAN
• new: STACK[G]
• count: STACK[G] -> INTEGER
• change_top: STACK[G] x G -> STACK[G]
• wipe_out: STACK[G]
AXIOMS
For any x:G, s:STACK[G]
• A1 - item(put(s,x)) = x
• A2 - remove(put(s,x)) = s
• A3 - empty(new)
• A4 - not empty(put(s,x))
• A5 - count(new)
PRECONDITIONS
• remove(s:STACK[G]) require not empty(s)
• item (s:STACK[G]) require not empty(s)
• change_top (s:STACK[G]) require not empty(s)
Isn't there a TA you could talk to? Anyway, change_top should be marked -/> since you gave it a precondition. Should wipe_out take a stack argument?
Axiom A5 isn't well formed, since count returns an integer, not a boolean. You need another axiom for count, and axioms for change_top and wipe_out.
Related
I'm writing an operation schema in Z. This operation AssignValue maps a property to one or more values.
One property may be linked to one or more values, and one value may be linked to one or more properties, forming a many-to-many relation, R ⊆ Property × Value.
I'm not sure how to write this operation to indicate that one property could be mapped to one or more values. I have two versions here. Version A seems to map one property to only one value.
Version A:
--AssignValue---
| p? : Property
| v? : Value
-------
|R′ = R ∪ { p? ↦ v? }
-------
In Version B, I have added a powerset in the declaration of v? to indicate that v? is a set of values (more than one value).
Version B:
--AssignValue---
| p? : Property
| v? : P Value
-------
|R′ = R ∪ { p? ↦ v? }
-------
Which version is correct? or there is a better way to represent this? I'm new to z-notation, any help would be greatly appreciated. Thanks!
You have not shown the whole schema. I assume that you have a state schema S with a relation R : Property<->Value (equivalent to R ⊆ Property × Value) and AssignValue includes ∆S.
Both styles could work, although your version B is probably not what you intended.
A relation is allowed to have many pairs with the same domain element, so starting with
R = {p0 ↦ v0, p0 ↦ v1, p3 ↦ v16}
You could call AssignValueA with p?=p0, v?=v16 to get a state with
R = {p0 ↦ v0, p0 ↦ v1, p0 ↦ v16, p3 ↦ v16}
that is, p0 is now mapped to three separate values.
In your version B you have exactly the same thing, but your values are now sets of values. What you probably intended was that R would be a total function of type Property → Value. Now, assuming only properties p0 through p3, you would have the initial R as
R = {p0 ↦ {v0, v1}, p1 ↦ ∅, p2 ↦ ∅, p3 ↦ {v16}}
You need to define
--AssignValueB----------------
| ∆S
| p? : Property, v? Value
------------
| R' = R ⊕ {p? ↦ R p? ∪ {v?}}
------------------------------
This has the same interface as AssignValueA, allowing addition of a single value to a single property per call.
In both models a property may have no, one or many associated values, but the operation only allows one property to be assigned one extra value per call.
Exercise: try defining an operation that allows multiple properties to be assigned multiple extra values per call.
For an example of a big Z specification, I suggest this recently uploaded project: https://github.com/vinahradau/finma
For a one-to-many, I would use a relation (as an opposite to a function).
Example form the project above:
userAccessRigths: USER ↔ ROLE
userAccessRigths′ = userAccessRigths ∪ {(user?, role?)}
I've tried this
S -> e(Epsilon)
S -> SASBS
S -> SBSAS
A -> a
B -> b
Can someone verify if this is correct.
Your grammar is correct. Here is the proof.
First, we show that your grammar generates only strings with an equal number of a and b. Note that all productions with S on the LHS introduce an equal number of A as they do B. Therefore, any string of terminals derived from S will have an equal number of a and b.
Next, we show that all strings of a and b can be derived using this grammar. We proceed using mathematical induction.
Base case: S -> e and both S -> SASBS -> ASBS -> aSBS -> aBS -> abS -> ab and S -> SBSAS -> BSAS -> bSAS -> bAS -> baS -> ba, so the three shortest string in the language are generated by the grammar. There are no other strings in the language of length less than 4.
Induction hypothesis: all strings of length up to 2k in the language are generated by the grammar.
Inductive step: we must show all strings of length 2(k + 1) in the language are also generated by the grammar. If w = axb or w = bya for some strings x and y, then x and y are strings of length 2k in the language and are therefore generated by the grammar. In this case, we can use the same derivation with an extra application of either S -> SASBS -> ASBS -> aSBS -> aSbS -> aSb or S -> SBSAS -> BSAS -> bSAS -> bSaS -> bSa and then use the derivation for x or y to complete the derivation, yielding w. If, instead, w = axa or w = byb, then x or y is a string with exactly two more b than a or a than b. In this case, there must be a prefix p of w with |p| < |w| such that p is also a string in the language (see lemma below). If the prefix p is a word in the language, and w = pr, then r must also be a word in the language, so w must be the concatenation of two words in L. These words both have length less than |w| so less than 2(k + 1) and are generated by the grammar. If they are generated by the grammar then they are of the form SaSbS or SbSaS and their concatenation can be derived using the grammar by using the productions in the proper sequence. That is, S -> SASBS -> SASBSBSAS -> aSbSbSa = aSbS bSa <- aSbS SbSa (we are of course free to choose S -> e in that last reverse step justification).
So I have an assignment in haskell that I am stuck on with my logic and this is the problem I was given:
graphColor finds all possible colorings for a graph
a coloring is an assignment of nodes to colors
such that two nodes must have different colors if they are connected by an
edge
graphColor takes the following arguments
A list of colors
A list of nodes
A list of edges (i.e., pairs of nodes)
A partial coloring (i.e., a list of pairs of node and color)
Example list of edges
e1 :: [Edge]
e1 = [(1,3),(2,1),(2,5),(3,1),(3,2),(3,3),(3,4),(3,5),(5,6),(2,6)]
e2 :: [Edge]
e2 = [(1,2),(2,3),(3,4),(1,3),(2,4),(1,4)]
type Code = [(Char,Char)]
-- domain of our code
domain1 :: [Char]
domain1 = ['a'..'z']
-- associated range
range1 :: [Char]
range1 = ['q'..'z'] ++ ['a'..'p']
-- create a code out of our domain and range
code1 :: Code
code1 = zip domain1 range1
graphColor returns all complete colorings of the graph, if there is more than one solution, your list does not need to contain all of them, as long as it contains at least one.
Ex1:
> take 1 (graphColor "abc" [1..6] e1 [])
[[(6,'c'),(5,'a'),(4,'a'),(3,'c'),(2,'b'),(1,'a')]]
Ex2:
> take 1 (graphColor "abc" [1..4] e2 [])
[]
This is my code thus far which just returns the empty list no matter what.
graphColor :: [Color] -> [Node] -> [Edge] -> Coloring -> [Coloring]
-- Fill in your code here
graphColor colors nodes edges solution
| length nodes == length solution = [solution]
| otherwise = concat [graphColor colors nodes edges (newSol : solution) |
x <- nodes, y <- colors,
newSol <- [(x,y)],
notRepeat solution edges x y,
notElem newSol solution]
notRepeat :: Coloring -> [Edge] -> Node -> Color -> Bool
notRepeat solution edges x y
| x `elem` [z | (z,_) <- solution] = False
|otherwise = helpHelper edges [z | (z,y) <- solution] x
helpHelper edges matchedNodes x
= x `elem` [z | y <- matchedNodes, (z,y) <- edges] ++
[z | y <- matchedNodes, (y,z) <- edges]
Using the notion of subsets as predicates,
ℙ : ∀ {b a} → Set a → Set (a ⊔ suc b)
ℙ {b} {a} X = X → Set b
I'd like to consider structures endowed with a predicate on subsets,
record SetWithAPredicate {a c} : Set {!!} where
field
S : Set a
P : ∀ {b} → ℙ {b} S → Set c
This is an ill-formed construction due to the level quantification used in ℙ. Everything works fine when I use S, P as parameters to a module, but I'd like them to be records so that I can form constructions on them and give instances of them.
I've tried a few other things, such as moving the level b of ℙ inside the definition via an existential but then that led to metavaraible trouble. I also tried changing the type of P,
P : ℙ {a} S → Set c
but then I can no longer ask for, say, the empty set to have the property:
P-⊥ : P(λ _ → ⊥)
This is not well typed since Set != Set a ---I must admit, I tried to use Level.lift here, but failed to do so.
More generally, this will also not allow me to express closure properties, such as P is closed under arbitrary unions ---this is what I'm really interested in.
I understand that I can just avoid level polymorphism,
ℙ' : ∀ {a} → Set a → Set (suc zero ⊔ a)
ℙ' {a} X = X → Set
but then simple items such as the largest subset,
ℙ'-⊤ : ∀ {i} {A : Set i} → ℙ' A
ℙ'-⊤ {i} {A} = λ e → Σ a ∶ A • a ≡ e
-- Σ_∶_•_ is just syntax for Σ A (λ a → ...)
will not even typecheck!
Perhaps I did not realise the notion of subset as predicate appropriately ---any advice would be appreciated. Thank-you!
You need to lift b out from P like this
record SetWithAPredicate {a c} b : Set {!!} where
field
S : Set a
P : ℙ {b} S → Set c
Yes, this is ugly and annoying, but that's how it's done in Agda (an example from standard library: _>>=_ is not properly universe polymorphic). Lift can help sometimes, but it quickly gets out of hand.
Perhaps I did not realise the notion of subset as predicate
appropriately ---any advice would be appreciated.
Your definition is correct, but there is another one, see 4.8.3 in Conor McBride's lecture notes.
I have probability P(A|B=T,C=F,D=F,G=T) is this same as computing
P(A|B=T)*P(A|C=F)*P(A|D=F) *P(A|G=T) ?
P(A|B=T,C=F,D=F,G=T)=P(A|B=T)*P(A|C=F)*P(A|D=F) *P(A|G=T) ? where A is
the child of B, C, D, G thanks!
In general it is not true that the following holds.
P(A|B,C) = P(A|B) • P(A|C)
The following is true, however. (Bayes' theorem)
P(B,C|A) • P(A)
P(A|B,C) = ━━━━━━━━
P(B,C)
Furthermore, if B and C are conditionally independent given A, the following is true.
P(A|B,C) ∝ P(B|A) • P(C|A) • P(A)