190 likes | 333 Vues
This document delves into the formalization of an oscilloscope using a high-level, implementation-independent specification framework. The focus is on employing the Larch algebraic formal specification language to achieve precision, consistency, and completeness in describing waveforms. It introduces formal semantics through typed set theory and first-order logic, discussing key concepts like sets, functions, and logical connectives, leading to a deeper understanding of waveform representation and display. The culmination is an exploration of practical aspects of oscilloscope functionalities.
E N D
On Formalism • High-level (implementation-independent) specification • Recall: Larch – An Algebraic Formal Spec. Lang. • Why formal? Precise, consistent, and complete • Formal semantics: Formal = grammar, e.g., syllogism All persons die. Adam is a person ------------------------ Adam dies. Semantics?
Introduction to Z • Based on typed set theory and first order logic • Sets: • oneTwoThree == {1, 2, 3} • Person == {Adam, Eve} S: P X == 2 X--- S is a set of X’s powerset, i.e., the set of all subsets of X • oneTwoThreeSet == P oneTwoThree == P {1, 2, 3} == ? • personSet == P person == P {Adam, Eve} == ? • |P X| == ?
Introduction to Z • Sets (cont’d) • x memberOf S ? 1 memberOf {1, 2, 3} ? 1 memberof P {1, 2, 3} ? {1} memberof P {1, 2, 3} ? Adam memberOf P Person ? Adam memberOf Person ? {Adam, Eve} memberOf P Person
Introduction to Z • Sets (cont’d) • S subsetOf S’ ? 1 subsetOf {1, 2, 3} ? {1, 2} subsetOf P {1, 2, 3} ? {{1, 2}} subsetOf P {1, 2, 3} ? Adam subsetOf P Person ? Adam subsetOf Person ? Person subsetOf Person ? {Person} subsetOf P Person
Introduction to Z • Sets (cont’d) • S X S’ (cross/cartesian product) oneTwoThree X person == {1, 2, 3} X {Adam, Eve} == {{1, Adam}, {1, Eve}, {2, Adam}, {2, Eve}, {3, Adam}, {3, Eve}} ? {1, 2} subsetOf {1, 2} X {1, 2} • S U S’, S intersect S’, S\S’, etc. (skip)
Introduction to Z • Functions • dom f --- The set of values x for which f(x) is defined f(x) = x 2 , dom f = {n memberOf N| 1 <= n <= 5} • ran f --- The set of values yielded by f(x), where x memberOf dom f ran f = ? • f: X -> Y --- f is a total function from X to Y i.e., f is defined for all x memberOf dom(f), i.e., dom(f) = X • f: X -|-> Y --- f is a partial function from X to Y i.e., f is defined for some values in X if f(x) = 1/x, ? dom(f) = Z ? spouse: Person -> Person
Introduction to Z • Functions (cont’d) • (lambda x: T . t) returns the value of the term t (lambda x: N . X 2 ) 5 == 25 (lambda x: N . (X 2 , 1/x) == ? (lambda x, y: N . (X 2 + y, y -1/x) 5 1 == ?
Introduction to Z • First Order Logic • Logical connectives: AND, OR, NOT, =>, <=> • Quantifiers ? Exists n: N . n = n 2 ? Exists p: Person . P == father (Adam) ? Forall i: N . I 2 >= I ? Forall I, j: N . I > j => I 2 > j 2 ? Forall x, y: Person, x == spouse(y) <=> y == spouse(x)
Introduction to Z • Schemas A schema consists of a set of declarations of variables and a predicate constraining these variables (i.e., state space and operations) ----- BirthdayBook ---------------------------------------- | known: P Person | birthday: Person -|-> Date ----------------------------------------------------------------- | known = dom birthday ----------------------------------------------------------------- One possible state: known = {Adam, Caine, Eve} birthday = {Adam |-> Apr/01, Eve |-> Apr/01}
A Simple Oscilloscope ? What is a waveform? - Engineer 0: a graph - Engineer1: a 1-kbyte array of 8-bit samples - Engineer3: a set of voltage values - Engineer4: a function from time to volts
A Simple Oscilloscope • Overview “display Pat’s ECG from 1:01pm to 1:02pm” - Ultimately displaying a trace: mapping time to a horizontal distance across the screen voltage to a vertical offset on the screen - Scale: both horizontal (seconds/meter) and vertical (volts/meter) scaling to convert a “voltage versus time” signal to a point-on-screen versus time” display of the trace - Translate: the trace on the display by horizontal and vertical offsets - Clip: the trace to fit on the screen W -> T Clip waveform (translated) trace Clipped trace trace Scale Translate
waveforms, segments, coordinates, traces • A waveform can be modeled as a partial function of time Waveform == Time -|-> Voltage, where Voltage == Z X {Volt} /* e.g., (1, Volt), (2, Volt) Time == N X {Second} /* e.g., (0, Second), (1, Sec E.g., wf1 == {1 Sec |-> 1 Volt, 2 Sec |-> 3 Volt, 3 Sec |-> 2 Volt} wf2 == {1 Sec |-> 1 Volt, 3 Sec |-> 5 Volt, 4 Sec |-> 6 Volt} wf3 == {25 |-> 5, 26 |-> 6, 27 |-> 8, 28 |-> 10, 29 |-> 11, 30 |-> 13} • A segment corresponds to a waveform over a contiguous time interval ? wf1 ? wf2 ? wf3 ? wf3 (25) ? Wf3 (29)
waveforms, segments, coordinates, traces • A coordinate can be represented by a real and a unit of distance: Coord == R X {Meter} /* (1, Meter), (3.5, Meter) where Voltage == Z X {Volt} /* e.g., (1, Volt), (2, Volt) • A point on the screen by a pair of coordinates: Point == Coord X Coord /* e.g., ((1, Metr), (3, Metre)), (1, 3) • A trace is a mapping from time to points: Trace == Time -|-> Point ? {(0, 2.5), (1, 3), (2, 4), (3, 5), (4, 5.5), (5, 6.5)} memberOf Trace ?{25 |-> 5, 26 |-> 6, 27 |-> 8, 28 |-> 10, 29 |-> 11, 30 |-> 13} memberOf Trace ? {25 |-> (0, 2.5), 26 |-> (1, 3), 27 |-> (2, 4), 28 |-> (3, 5), 29 |-> (4, 5.5), 30 |-> (5, 6.5)}
Scale • takes a segment and scales it both horizontally and vertically • adjusts it s.t. the start of the segment corresponds to a horizontal offset of zero • The horizontal scale factor converts the units from seconds to metres • The vertical scale factor converts from a voltage to metres ------- Scale --------------------------------------------------------------------------------- | segment: Segment /* e.g., {25 |-> 5, 26 |-> 6, 27 |-> 8 28 |-> 10, 29 |-> 11, 30 |-> 13} | HScale: R X {Second/Metre} /* e.g., 1 Second/Metre | VScale: R X {Volt/Metre} /* e.g., 2 Volt/Metre | scaled: Trace ------------------------------------------------------------------------------------------------- | scaled = (lambda t: dom segment . (t – min (dom segment) / HScale, | segment (t) / VScale) ) ------------------------------------------------------------------------------------------------- ? scaled =
Translate ---- Translate ------------------------------------------------------------------------------------- | scaled: Trace /* e.g., {25 |-> (0, 2.5), 26 |-> (1, 3), 27 |-> (2, 4), 28 |-> (3, 5), 29 |-> (4, 5.5), 30 |-> (5, 6.5)} | HOffset, VOffset: Coord /* e.g., (1, 1) | moved: Trace ------------------------------------------------------------------------------------------------------ | moved = (lambda t: dom scaled . (first (scaled (t)) + HOffset, | second (scaled (t)) + VOffset) ) ------------------------------------------------------------------------------------------------------ ? moved =
Clip ----- Clip ---------------------------------------------------------------------------------------- | moved: Trace /* e.g.,{25 |-> (1, 3.5), 26 |-> (2, 4), 27 |-> (3, 5), 28 |-> (4, 6), 29 |-> (5, 6.5), 30 |-> (6, 7.5)} | HMax: R X {Meter} /* e.g., 4 Metre | VMax: R X {Metre} /* e.g., 6 Metre | clipped: Trace --------------------------------------------------------------------------------------------------- | let screen == {(x, y): Coord | 0 < x < HMax ^ -VMax < y < VMax} . | clipped = moved screen --------------------------------------------------------------------------------------------------- • Local definitions within predicates are introduced by the keyword “let” • The operator is for range restriction: R S == {a |-> b | (a |-> b memberOf R) ^ (b memberOf S)} ? Clipped =
Trace Display • Schema conjunction: the three schemas are combined using schema conjunction (Scale ^ Translate ^ Clip) • Displaying only clipped trace: hide scaled and moved traces, as they are only used as intermediate links – keep all the constraints, but omit intermediate variables. DisplayTrace == (Scale ^ Translate ^ Clip) \ (scaled, moved) ? fully expanded declaration =
DisplayKnobs, DisplaySegments • (skip) ----- DisplayKnobs -------------------------------------------------------------- | HScale: R+ X {Second/Metre} | VScale: R+ X {Volt/Metre} | HOffset, VOffset: Coord ---------------------------------------------------------------------------------------- ----- DisplaySegments ---------------------------------------------------------- | segment: Segment | DisplayKnobs | display: P Trace ---------------------------------------------------------------------------------------- | display = {DisplayTrace | dom DisplayTrace = dom Segment . clipped} -----------------------------------------------------------------------------------------