anchor.diff:  
   [<AutoOpen>]
   module internal Util =
  -  type IDictionary<'k, 'v> with
  -    member this.TryGet k =
  -      match this.TryGetValue k with
  -       | (false, _) -> None
  -       | (_, v) -> Some v
  -    member this.Add (kvs: seq<'k * 'v>) =
  -      kvs |> Seq.iter this.Add
  -  module Dictionary =
  -    let create kvs =
  -      let d = Dictionary<_, _>()
  -      d.Add kvs
  -      d
  -
     let setupWhen (onEvent: IEvent<_, _>) =
       let mainCtx = ivar ()
       let handler =
  
positive.code:     Async.setMain ctx
      onEvent.RemoveHandler handler
  
  type Item = {
      IsCompleted: Stream.Var<bool>
      Header: Stream.Var<string>
    }
  
  [<STAThread; EntryPoint>]
  let main argv =
  
negative.code:         |> Stream.mapJob (fun (bn, pred) -> onMain {
             [main.FilterAll; main.FilterActive; main.FilterCompleted]
             |> Seq.iter (fun bn' -> bn'.IsEnabled <- bn <> bn')
             return pred })      
  
        let newControl id item =
          let control = ToDo.UI.ItemControl ()
  
          control.IsCompleted.IsChecked <- Nullable<bool> item.IsCompleted
          control.Header.Text <- item.Header
          control.Header.FontStyle <-
            if item.IsCompleted then FontStyles.Italic else FontStyles.Normal
  
          Stream.ofObservableOnMain control.IsCompleted.Click
          |> Stream.subscribeJob (fun _ -> onMain {
            let isCompleted = control.IsCompleted.IsChecked.Value
            return! Stream.Var.updateFun modelVar
                      << updateItem id <| fun item ->
                          {item with IsCompleted = isCompleted} })
  
          Stream.ofObservableOnMain control.Header.MouseDoubleClick
          |> Stream.subscribeJob (fun _ -> onMain {
            control.Header.Focusable <- true
            if control.Header.Focus () then
              control.Header.SelectAll () })
          Stream.ofObservableOnMain control.Header.KeyUp
          |> Stream.filterFun (fun evt -> evt.Key = Key.Enter)
          |> Stream.mapIgnore
          |> Stream.merge (Stream.ofObservableOnMain control.Header.LostFocus
                          |> Stream.mapIgnore)
          |> Stream.subscribeJob (fun () -> onMain {
            Keyboard.ClearFocus ()
            control.Header.Focusable <- false
            match control.Header.Text.Trim () with
              | "" -> control.Header.Text <- item.Header
              | header ->
                return! Stream.Var.updateFun modelVar << updateItem id <| fun item ->
                        {item with Header = header} })
  
          Stream.merge
            (Stream.ofObservableOnMain control.MouseEnter |> Stream.mapConst +1)
            (Stream.ofObservableOnMain control.MouseLeave |> Stream.mapConst -1)
          |> Stream.scanFromFun 0 (+)
          |> Stream.subscribeJob (fun n -> onMain {
            control.Remove.Visibility <-
              if 0 < n then Visibility.Visible else Visibility.Hidden })
  
          Stream.ofObservableOnMain control.Remove.Click
          |> Stream.subscribeJob (fun _ ->
            Stream.Var.updateFun modelVar <| Map.remove id)
  
          control
  
        Stream.Var.tap modelVar
        |> Stream.combineLatest filter
        |> Stream.subscribeJob (fun (filter, model) -> onMain {
           main.Items.Children.Clear ()
           model
           |> Map.iter (fun id item ->
              if filter item then
                newControl id item
                |> main.Items.Children.Add
                |> ignore) })
  
        Stream.Var.tap modelVar
        |> Stream.subscribeJob (fun model -> onMain {
           let n = Map.toSeq model |> Seq.filter (snd >> isActive) |> Seq.length
           main.NumberOfItems.Text <- sprintf "%d items left" n })
  
        Stream.Var.tap modelVar
        |> Stream.subscribeJob (fun model -> onMain {
           let n = Map.toSeq model |> Seq.filter (snd >> isCompleted) |> Seq.length
           main.ClearCompleted.Visibility <-
             if n=0 then Visibility.Hidden else Visibility.Visible
           main.ClearCompleted.Content <- sprintf "Clear completed %d" n })
  
        Stream.ofObservableOnMain main.ClearCompleted.Click
        |> Stream.subscribeJob (fun _ ->
           Stream.Var.updateFun modelVar << Map.filter <| fun _ -> isActive)
  
        Stream.Var.tap modelVar
        |> Stream.mapFun (fun model ->
           not (Map.isEmpty model) && Map.forall (fun _ -> isCompleted) model)
        |> Stream.subscribeJob (fun allCompleted -> onMain {
           main.CompleteAll.IsChecked <- Nullable<bool> allCompleted })
  
        Stream.ofObservableOnMain main.CompleteAll.Click
        |> Stream.subscribeJob (fun _ ->
           onMain { return main.CompleteAll.IsChecked.Value } >>= fun isCompleted ->
           Stream.Var.updateFun modelVar << Map.map <| fun _ item ->
           {item with IsCompleted = isCompleted})
  
    |> queue
  
  