Logic and Program modes

Imandra has two modes: logic mode and program mode. When we launch Imandra Terminal (or an Imandra Jupyter Notebook session), we start off in logic mode.

In the terminal, we can identify that Imandra is in logic mode by the pound sign prompt (#).

In a notebook, we can inspect Imandra's current mode using the #config directive.

In [1]:
#config 1
Out[1]:
----------------------------------------------------------------------------
Configuration component: Mode
----------------------------------------------------------------------------
Value: Logic
Help topics: program, logic
----------------------------------------------------------------------------

While in logic mode, we have access to Imandra's reasoning tools, such as verify and theorem.

In [2]:
let succ n = n + 1
Out[2]:
val succ : Z.t -> Z.t = <fun>
In [3]:
verify (fun n -> succ n > n)
Out[3]:
- : int -> bool = <fun>
Proved
proof
ground_instances0
definitions0
inductions0
search_time
0.019s
details
Expand
smt_stats
rlimit count12
mk bool var5
num allocs1399890465
memory16.060000
max memory18.750000
Expand
  • start[0.019s] (:var_0: + 1) > :var_0:
  • simplify

    into
    true
    expansions
    []
    rewrite_steps
      forward_chaining
      • unsat

        (mp (asserted (not true)) (rewrite (= (not true) false)) false)

      A future notebook will summarize the various reasoning tools, and explain when to use which one.

      In logic mode, all definitions -- types, values and functions -- are entered into the logic. We can see all previous events in logic mode by inspecting the #history (aliased to #h).

      In [4]:
      #h;;
      
      Out[4]:
      
      

      All events in session

      • 0. Fun: succ
      • 1. Verify: <expr>

      dependency graph
      In [5]:
      #h succ
      
      Out[5]:
      
      

      Fun: succ

      iml
      definition
      fun (n : int) -> n + 1
      def
      namesucc
      typeint -> int
      recursivefalse
      call signaturesucc (n : int)
      validatedin 0.000s
      locationjupyter cell 2:1,0--18
      hashes
      succb1de521a2f1a2fe90b72c75dd66ebec3307ff71f798199a0ea1bef1cb2865b94

      While in logic mode, we are restricted to a purely functional subset of OCaml, and our recursive functions must terminate.

      If we try to define a non-termating function, for example, Imandra will reject it.

      In [6]:
      let rec bad_repeat x = x :: bad_repeat x
      
      Out[6]:
      val bad_repeat : 'a -> 'a list = <fun>
      File "jupyter cell 6", line 1, characters 0-40:
      Error: Validate: no measure provided, and Imandra cannot guess any.
      Are you sure this function is actually terminating?
      
      In [7]:
      #show bad_repeat
      
      Out[7]:
      Unknown element.
      

      For more complex recursive functions, we may need to convince Imandra that the function terminates, for example by defining a "measure". See the notebook Proving Program Termination with Imandra for more details.

      Our logic-mode definitions are allowed to call other definitions only if those other definitions have been admitted into the logic.

      In [8]:
      let say_hi () = print_endline "Hello!"
      
      Out[8]:
      val say_hi : unit -> unit = <fun>
      Error: No function Pervasives/0.print_endline was defined in logic-mode
      

      In order to define such a side-effecting function, we switch to program mode. We do this using the #program directive.

      In [9]:
      #program;;
      
      #config 1;;
      
      Out[9]:
      ----------------------------------------------------------------------------
      Configuration component: Mode
      ----------------------------------------------------------------------------
      Value: Program
      Help topics: program, logic
      ----------------------------------------------------------------------------
      

      In the terminal, we can identify that Imandra is in program mode by the angle bracket prompt (>).

      Now that we are in program mode, we have the full power of OCaml at our fingertips!

      In [10]:
      let say_hi () = print_endline "Hello!"
      
      Out[10]:
      val say_hi : unit -> unit = <fun>
      
      In [11]:
      say_hi ()
      
      Out[11]:
      Hello!
      - : unit = ()
      

      When we switch back to logic mode (using the #logic directive), we can still refer to our program-mode definitions at the top level.

      In [12]:
      #logic;;
      
      say_hi ()
      
      Out[12]:
      Hello!
      - : unit = ()
      

      But we are forbidden from using them in our logic-mode definitions.

      In [13]:
      let say_hi_from_logic_mode () = say_hi ()
      
      Out[13]:
      val say_hi_from_logic_mode : unit -> unit = <fun>
      Error: No function say_hi/7239 was defined in logic-mode
      

      Often, we want to define a type in logic mode and then a related function in program mode, for example a pretty-printer. For this case we can use the [@@program] annotation to define a one-off program-mode function while in logic mode.

      In [14]:
      type person = { name : string; favorite_color : string };;
      
      let print_person (person : person) : string =
        Printf.sprintf "%s prefers %s things" person.name person.favorite_color
      [@@program];;
      
      print_person { name = "Matt"; favorite_color = "green" };;
      
      Out[14]:
      type person = { name : string; favorite_color : string; }
      val print_person : person -> string = <fun>
      - : string = "Matt prefers green things"
      

      There is also the [@@logic] annotation, which allows admitting a definition into the logic while in program mode.

      That concludes our overview of Imandra's logic and program modes!