Functional Hybrid Modeling from an Object-Oriented Perspective - - PowerPoint PPT Presentation

functional hybrid modeling from an object oriented
SMART_READER_LITE
LIVE PREVIEW

Functional Hybrid Modeling from an Object-Oriented Perspective - - PowerPoint PPT Presentation

Functional Hybrid Modeling from an Object-Oriented Perspective Henrik Nilsson (University of Nottingham), John Peterson (Western State College), and Paul Hudak (Yale University) Functional Hybrid Modeling from an Object-Oriented Perspective


slide-1
SLIDE 1

Functional Hybrid Modeling from an Object-Oriented Perspective

Henrik Nilsson (University of Nottingham), John Peterson (Western State College), and Paul Hudak (Yale University)

Functional Hybrid Modeling from an Object-Oriented Perspective – p.1/27

slide-2
SLIDE 2

Background (1)

  • Functional Reactive Programming (FRP)

integrates notions suitable for causal hybrid modelling with functional programming.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.2/27

slide-3
SLIDE 3

Background (1)

  • Functional Reactive Programming (FRP)

integrates notions suitable for causal hybrid modelling with functional programming.

  • Yampa is an instance of FRP embedded in

Haskell.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.2/27

slide-4
SLIDE 4

Background (1)

  • Functional Reactive Programming (FRP)

integrates notions suitable for causal hybrid modelling with functional programming.

  • Yampa is an instance of FRP embedded in

Haskell.

  • One central idea: first-class reactive

components (or models):

  • enables highly structurally dynamic

systems to be described declaratively;

  • opens up for meta-modelling without

additional language layers.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.2/27

slide-5
SLIDE 5

Background (2)

  • Additional interesting aspects:
  • full power of a modern functional language

available;

  • polymorphic type system;
  • well-understood underlying semantics.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.3/27

slide-6
SLIDE 6

Functional Hybrid Modelling (1)

  • Our goal with Functional Hybrid Modelling

(FHM) is to combine an FRP-approach with non-causal modelling yielding:

Functional Hybrid Modeling from an Object-Oriented Perspective – p.4/27

slide-7
SLIDE 7

Functional Hybrid Modelling (1)

  • Our goal with Functional Hybrid Modelling

(FHM) is to combine an FRP-approach with non-causal modelling yielding:

  • a powerful, fully-declarative, non-causal

modelling language supporting highly structurally dynamic systems;

Functional Hybrid Modeling from an Object-Oriented Perspective – p.4/27

slide-8
SLIDE 8

Functional Hybrid Modelling (1)

  • Our goal with Functional Hybrid Modelling

(FHM) is to combine an FRP-approach with non-causal modelling yielding:

  • a powerful, fully-declarative, non-causal

modelling language supporting highly structurally dynamic systems;

  • a semantic framework for studying

modelling and simulation languages supporting structural dynamism.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.4/27

slide-9
SLIDE 9

Functional Hybrid Modelling (2)

  • The idea of FHM goes back a few years

(PADL 2003). UK research funding (EPSRC) secured very recently. Thus still work in very early stages.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.5/27

slide-10
SLIDE 10

The Rest of the Talk

  • A brief introduction to FRP/Yampa as a

background.

  • Sketch the key ideas of how this may be

generalized to a non-causal setting.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.6/27

slide-11
SLIDE 11

Signal functions

Key concept: functions on signals (first class).

Functional Hybrid Modeling from an Object-Oriented Perspective – p.7/27

slide-12
SLIDE 12

Signal functions

Key concept: functions on signals (first class). Intuition: Signal α ≈ Time → α x :: Signal T1 y :: Signal T2 SF α β ≈ Signal α → Signal β f :: SF T1 T2

Functional Hybrid Modeling from an Object-Oriented Perspective – p.7/27

slide-13
SLIDE 13

Signal functions

Key concept: functions on signals (first class). Intuition: Signal α ≈ Time → α x :: Signal T1 y :: Signal T2 SF α β ≈ Signal α → Signal β f :: SF T1 T2 Additionally, causality required: output at time t must be determined by input on interval [0, t].

Functional Hybrid Modeling from an Object-Oriented Perspective – p.7/27

slide-14
SLIDE 14

Signal functions and state

Alternative view:

Functional Hybrid Modeling from an Object-Oriented Perspective – p.8/27

slide-15
SLIDE 15

Signal functions and state

Alternative view: Signal functions can encapsulate state. state(t) summarizes input history x(t′), t′ ∈ [0, t].

Functional Hybrid Modeling from an Object-Oriented Perspective – p.8/27

slide-16
SLIDE 16

Signal functions and state

Alternative view: Signal functions can encapsulate state. state(t) summarizes input history x(t′), t′ ∈ [0, t]. From this perspective, signal functions are:

  • stateful if y(t) depends on x(t) and state(t)
  • stateless if y(t) depends only on x(t)

Integral is an example of a stateful signal function.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.8/27

slide-17
SLIDE 17

Programming with signal functions

In Yampa, systems are described by combining signal functions (forming new signal functions).

Functional Hybrid Modeling from an Object-Oriented Perspective – p.9/27

slide-18
SLIDE 18

Programming with signal functions

In Yampa, systems are described by combining signal functions (forming new signal functions). For example, serial composition:

Functional Hybrid Modeling from an Object-Oriented Perspective – p.9/27

slide-19
SLIDE 19

Programming with signal functions

In Yampa, systems are described by combining signal functions (forming new signal functions). For example, serial composition: A combinator can be defined that captures this: (≫) :: SF a b → SF b c → SF a c Note: plain function operating on first-class signal function.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.9/27

slide-20
SLIDE 20

The Arrow framework (1)

These diagrams convey the general idea: arrf ≫ firstf loopf first :: SF a b → SF (a, c) (b, c) loop :: SF (a, c) (b, c) → SF a b

Functional Hybrid Modeling from an Object-Oriented Perspective – p.10/27

slide-21
SLIDE 21

The Arrow framework (2)

Some derived combinators: secondf f ∗ ∗ ∗ g f& & &g

Functional Hybrid Modeling from an Object-Oriented Perspective – p.11/27

slide-22
SLIDE 22

Example: Constructing a network

Functional Hybrid Modeling from an Object-Oriented Perspective – p.12/27

slide-23
SLIDE 23

Example: Constructing a network

Functional Hybrid Modeling from an Object-Oriented Perspective – p.12/27

slide-24
SLIDE 24

Example: Constructing a network

loop (arr (λ(x, y) → ((x, y), x)) ≫ (fst f ≫ (arr (λ(x, y) → (x, (x, y))) ≫ (g ∗ ∗ ∗ h))))

Functional Hybrid Modeling from an Object-Oriented Perspective – p.12/27

slide-25
SLIDE 25

The Arrow notation

Functional Hybrid Modeling from an Object-Oriented Perspective – p.13/27

slide-26
SLIDE 26

The Arrow notation

Functional Hybrid Modeling from an Object-Oriented Perspective – p.13/27

slide-27
SLIDE 27

The Arrow notation

proc x → do rec u ← f − ≺ (x, v) y ← g− ≺ u v ← h− ≺ (u, x) returnA− ≺ y

Functional Hybrid Modeling from an Object-Oriented Perspective – p.13/27

slide-28
SLIDE 28

Switching

Some switching combinators:

  • switch :: SF a (b, Event c) → (c → SF a b)

→ SF a b

  • pSwitchB :: Functor col ⇒

col (SF a b) → SF (a, col b) (Event c) → (col (SF a b) → c → SF a (col b)) → SF a (col b)

Functional Hybrid Modeling from an Object-Oriented Perspective – p.14/27

slide-29
SLIDE 29

What makes Yampa different?

  • First class reactive components (signal

functions).

Functional Hybrid Modeling from an Object-Oriented Perspective – p.15/27

slide-30
SLIDE 30

What makes Yampa different?

  • First class reactive components (signal

functions).

  • Supports hybrid (mixed continuous and

discrete time) systems: option type Event represents discrete-time signals.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.15/27

slide-31
SLIDE 31

What makes Yampa different?

  • First class reactive components (signal

functions).

  • Supports hybrid (mixed continuous and

discrete time) systems: option type Event represents discrete-time signals.

  • Supports dynamic system structure through

switching combinators:

Functional Hybrid Modeling from an Object-Oriented Perspective – p.15/27

slide-32
SLIDE 32

Example: Space Invaders

Functional Hybrid Modeling from an Object-Oriented Perspective – p.16/27

slide-33
SLIDE 33

Functional Hybrid Modeling

Same conceptual structure as Yampa, but:

Functional Hybrid Modeling from an Object-Oriented Perspective – p.17/27

slide-34
SLIDE 34

Functional Hybrid Modeling

Same conceptual structure as Yampa, but:

  • First-class relations on signals instead of

functions on signals to enable non-causal modeling.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.17/27

slide-35
SLIDE 35

Functional Hybrid Modeling

Same conceptual structure as Yampa, but:

  • First-class relations on signals instead of

functions on signals to enable non-causal modeling.

  • Employ state-of-the-art symbolic and

numerical methods for sound and efficient simulation.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.17/27

slide-36
SLIDE 36

Functional Hybrid Modeling

Same conceptual structure as Yampa, but:

  • First-class relations on signals instead of

functions on signals to enable non-causal modeling.

  • Employ state-of-the-art symbolic and

numerical methods for sound and efficient simulation.

  • Adapted switch constructs.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.17/27

slide-37
SLIDE 37

First class signal relations

The type for a relation on a signal of type Signal α: SR α Specific relations use a more refined type; e.g. the derivative relation: der :: SR (Real, Real) Since a signal carrying pairs is isomorphic to a pair of signals, der can be understood as a binary relation on two signals.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.18/27

slide-38
SLIDE 38

Defining relations

The following tentative construct denotes a signal relation: sigrel pattern where equations The pattern introduces signal variables which at each point in time are going to be bound to to a “sample” of the corresponding signal. Given p :: t, we have: sigrel p where . . . :: SR t

Functional Hybrid Modeling from an Object-Oriented Perspective – p.19/27

slide-39
SLIDE 39

Equations

Let ei :: ti be non-relational expressions possibly introducing new signal variables. Point-wise equality; the equality must hold for all points in time: e1 = e2 Relation “application”; the relation must hold for all points in time: sr ⋄ e3 Here, sr is an expression having type SR t3.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.20/27

slide-40
SLIDE 40

Equations: examples

Consider a differential equation like x′ = f(x, y). This equation could be written: der ⋄ (x, f(x, y)) For convenience, syntactic sugar closer to standard mathematical notation could be considered: der(x) = f(x, y) Here, der is not a pure function operating only

  • n instantaneous signal values since it depends
  • n the history of the signal.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.21/27

slide-41
SLIDE 41

Modeling electrical components (1)

The type Pin is assumed to be a record type describing an electrical connection. It has fields v for voltage and i for current. twoPin :: SR (Pin, Pin, Voltage) twoPin = sigrel (p, n, v) where v = p.v − n.v p.i + n.i = 0

Functional Hybrid Modeling from an Object-Oriented Perspective – p.22/27

slide-42
SLIDE 42

Modeling electrical components (2)

resistor :: Resistance → SR (Pin, Pin) resistor(r) = sigrel (p, n) where twoPin ⋄ (p, n, v) r · p.i = v inductor :: Inductance → SR (Pin, Pin) inductor(l) = sigrel (p, n) where twoPin ⋄ (p, n, v) l · der(p.i) = v

Functional Hybrid Modeling from an Object-Oriented Perspective – p.23/27

slide-43
SLIDE 43

Modeling electrical components (3)

capacitor :: Capacitance → SR (Pin, Pin) capacitor(c) = sigrel (p, n) where twoPin ⋄ (p, n, v) c · der(v) = p.i

Functional Hybrid Modeling from an Object-Oriented Perspective – p.24/27

slide-44
SLIDE 44

Modeling an electrical circuit (1)

simpleCircuit :: SR Current simpleCircuit = sigrel i where resistor(1000) ⋄ (r1p, r1n) resistor(2200) ⋄ (r2p, r2n) capacitor(0.00047) ⋄ (cp, cn) inductor(0.01) ⋄ (lp, ln) vSourceAC(12) ⋄ (acp, acn) ground ⋄ gp . . .

Functional Hybrid Modeling from an Object-Oriented Perspective – p.25/27

slide-45
SLIDE 45

Modeling an electrical circuit (2)

. . . connect acp, r1p, r2p connect r1n, cp connect r2n, lp connect acn, cn, ln, gp i = r1p.i + r2p.i

Functional Hybrid Modeling from an Object-Oriented Perspective – p.26/27

slide-46
SLIDE 46

Central Research Questions

  • Adaptating Yampa’s switching constructs,

including handling initialization issues.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.27/27

slide-47
SLIDE 47

Central Research Questions

  • Adaptating Yampa’s switching constructs,

including handling initialization issues.

  • Adapting non-causal modelling and simulation

methods to a setting with first class signal relations: causality analysis, symbolic processing , code generation after each switch.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.27/27

slide-48
SLIDE 48

Central Research Questions

  • Adaptating Yampa’s switching constructs,

including handling initialization issues.

  • Adapting non-causal modelling and simulation

methods to a setting with first class signal relations: causality analysis, symbolic processing , code generation after each switch.

  • Guaranteeing compositional correctness

statically through the type system to the extent possible; e.g. employing dependent types to keep track of variable/equation balance.

Functional Hybrid Modeling from an Object-Oriented Perspective – p.27/27