#2318 Fantom is Declarative or Imperative Programming

lean Mon 21 Jul 2014

Hey everyone,

Does Fantom is Declarative or Imperative Programming? I thought that it is an imperative programming, because there is statements, etc. (like any other standard language..), but I read in "Why Fantom" that it is a declarative programming.. Why?!

Thanks!!

SlimerDude Mon 21 Jul 2014

These terms are very academic, and as I never did Computer Science, I'm afraid I can't help you with their definitions and how applicable they are to Fantom.

ttmrichter Mon 21 Jul 2014

"If" statements have nothing to do with imperative or declarative. Haskell is declarative. It has "if" expressions. Mercury is declarative. It has "if" expressions. SQL is declarative. It has "IF" expressions. SNOBOL4, on the other hand, is imperative. It does not have "if" statements.

What distinguishes imperative from declarative programming is control flow. If your language is concerned with control flow when expressing the logic of a computation, it is imperative. If it is not concerned with control flow, it is declarative.

That being said, imperative/declarative is not bipolar. It is a spectrum. Mercury and Haskell, for example, are on the very declarative side of the spectrum. Prolog is mostly declarative but has imperative elements (cuts and failure loops). Most popular languages are almost militantly imperative, but increasingly declarative notions are starting to leak through.

Fantom is imperative. It has some tooling in place to assist in declarative styles of programming in narrow circumstances but it is chiefly imperative.

Jens Mon 21 Jul 2014

Fantom is imperative just like Java, C and most other mainstream languages. It is imperative in that modification of fields and variables and other side effecting statements are central to the language and not restricted in any way.

There is no universal, exact meaning of the term declarative programming. Fantom has a declarative element in that there is syntax for creating data structures such as lists, maps, custom objects and other values. Using these features you can create data structures by specifying their structure, which can be said to be a declarative way of programming.

This can be contrasted to how you create data structures in an imperative way, where you create the data structure piece by piece and use side effecting statements to make it into its final shape.

Consider these two ways of creating a GUI window.

Imperative style:

w = Window.make()
w.setTitle("FWT Demo")
w.setSize(new Size(1000, 600))

c = EdgePane.make()

p = TabPane.make()
t1 = Tab.make()
t1.setText("Buttons")
t1.add( ... )
p.add(t1)

t2 = Tab.make()
t2.setText("Labels")
t2.add( ... )
p.add(t2)

c.setCenter(p)
w.setContent(c)

Declarative style:

w = Window
{
  title = "FWT Demo"
  size = Size(1000, 600)
  content = EdgePane
  {
    center = TabPane
    {
      Tab { text = "Buttons"; ... },
      Tab { text = "Labels";  ... },
    }
  }
}

In the first example a window is first created, then modified step by step by issuing commands to it. This is the imperative way. It is easy to see exactly what happens and in what order, but the structure of the code has no direct connection to how the final window looks.

In the second example a declarative style is used. Here the structure of the code matches the structure of the window, which makes it easier to read. But it is harder to see exactly what happens and in what order. This style resembles how you build GUI:s using XML or HTML.

In most languages you can program in a more or less declarative way, but Fantom makes this style more useful and convenient than most languages. This is one of the nice things about it.

SlimerDude Mon 21 Jul 2014

Wow! I learn new things everyday!

Cheers!

lean Tue 22 Jul 2014

Thanks to all of you!!!! You help me a lot! :)

tomcl Tue 22 Jul 2014

Declarative programming is, as said above, a continuum, the idea is that you use (typically) function composition and recursion instead of sequences of changes to variable values, so that semantics is declarative "what you see is what you have" rather than operational "work out what you have after all those iterative changes have done something".

Iteration + variable modification in general is less easy to reason about and understand because the order in which variables are modified matters, although there are many cases where the way local variables are changed iteratively is equivalent to a pure functional recursion.

Fantom has map on lists which together with convenient closures makes some declarative (functional) constructions easier.

More generally the fact that you can define closures so easily, and that they work properly (binding local variable values) supports writing programs that use functions as data objects. This is not specifically declarative, but tends to support declarative programming because functions as data tend not to be mutated and are very powerful. Look at the extensive use of it-blocks in Fantom GUI code. Warning - Fantom's closures can write to bound variables, which are treated as references, and that feature, while powerful, goes against the spirit of declarative programming.

The fact you are encouraged to think about and use immutable data structures encourages declarative programming, obviously.

Currying - ability to bind parameter(s) to a multi-parameter function to make a more specific function. Fantom lets you do this using Func.bind. This is not as clean syntactically or semantically as true currying, but is perfectly usable.

Fantom does not have some of the other stuff you find in full-blooded declarative languages:

Polymorphic typing for high order functions - this allows useful static typing on functions designed to operate on functions - which if you use functions as data in a comprehensive way is useful, especially because these "high order functions" tend to be very general tools that can be reused.

Logical programming with unification built in to the language. In reality few "normal" programming languages bother with this - but it is key to logical languages using logic statements to represent program - and these are 100% declarative. Personally I don't think logical programming mixes nicely with normal programming and have never myself found it generally useful though there are domains where it is lovely. Fantom does not do it.

To summarise: Declarative = functional and/or logical Fantom has about 30% of the functional support, and none of the logical. But that 30% functional support is very valuable. The real killer for Fantom in occupying a declarative language space is lack of user-defined generics AKA polymorphic typing.

Login or Signup to reply.