Library ConsumeProduce

Require Export SemiconcreteStates.

Consumption and production as relations between semiconcrete states


Inductive consume: state -> asn -> state -> Prop :=
| consume_BAsn b s h:
  beval s b = true ->
  consume (s, h) (BAsn b) (s, h)
| consume_PAsn p es xs s h vs:
  length vs = length xs ->
  consume
    (s, heap_add (singleton_heap (Chunk p (map (eval s) es ++ vs))) h)
    (PAsn p es xs)
    (store_updates s xs vs, h)
| consume_IfAsn_true b a1 a2 s h st:
  beval s b = true ->
  consume (s, h) a1 st ->
  consume (s, h) (IfAsn b a1 a2) st
| consume_IfAsn_false b a1 a2 s h st:
  beval s b = false ->
  consume (s, h) a2 st ->
  consume (s, h) (IfAsn b a1 a2) st
| consume_SepAsn a1 a2 st st' st'':
  consume st a1 st' ->
  consume st' a2 st'' ->
  consume st (SepAsn a1 a2) st''.

Lemma consume_local:
  forall a s h s' h' h0,
  consume (s, h) a (s', h') ->
  consume (s, heap_add h h0) a (s', heap_add h' h0).

Lemma consume_mono:
  forall a s h s' h',
  consume (s, h) a (s', h') ->
  exists h'', h = heap_add h'' h' /\ consume (s, h'') a (s', empty_heap).

Inductive produce: state -> asn -> state -> Prop :=
| produce_BAsn b s h:
  beval s b = true ->
  produce (s, h) (BAsn b) (s, h)
| produce_PAsn p es xs s h vs:
  length vs = length xs ->
  produce
    (s, h)
    (PAsn p es xs)
    (store_updates s xs vs, heap_add (singleton_heap (Chunk p (map (eval s) es ++ vs))) h)
| produce_IfAsn_true b a1 a2 s h st:
  beval s b = true ->
  produce (s, h) a1 st ->
  produce (s, h) (IfAsn b a1 a2) st
| produce_IfAsn_false b a1 a2 s h st:
  beval s b = false ->
  produce (s, h) a2 st ->
  produce (s, h) (IfAsn b a1 a2) st
| produce_SepAsn a1 a2 st st' st'':
  produce st a1 st' ->
  produce st' a2 st'' ->
  produce st (SepAsn a1 a2) st''.

Definition sat0 h s a s' := consume (s, h) a (s', empty_heap).
Inductive sat h s a: Prop :=
| sat_intro s': sat0 h s a s' -> sat h s a.

Theorem consume_produce:
  forall a h s s' h',
  sat0 h s a s' ->
  produce (s, h') a (s', heap_add h h').

Corollary consume_produce_cancel a s h s' h':
  consume (s, h) a (s', h') ->
  produce (s, h') a (s', h).

Heap refinement


Section PredDefs.

Variable pred_defs: pred_table.

Inductive refines: heap -> heap -> Prop :=
| refines_refl h:
  refines h h
| refines_add h1 h2 h1' h2':
  refines h1 h1' ->
  refines h2 h2' ->
  refines (heap_add h1 h2) (heap_add h1' h2')
| refines_pred h0 h p xs vs a:
  refines h0 h ->
  pred_defs p = Some (PredDef xs a) ->
  length vs = length xs ->
  sat h (store_updates store0 xs vs) a ->
  refines h0 (singleton_heap (Chunk p vs))
.

Definition is_concrete_heap h :=
  forall h' p vs,
  h = heap_add (singleton_heap (Chunk p vs)) h' ->
  pred_defs p = None.

Lemma is_concrete_heap_add_l h1 h2:
  is_concrete_heap (heap_add h1 h2) ->
  is_concrete_heap h1.

Lemma is_concrete_heap_add h1 h2:
  is_concrete_heap (heap_add h1 h2) ->
  is_concrete_heap h1 /\ is_concrete_heap h2.

Theorem open_theorem h0 h1:
  refines h0 h1 ->
  forall h p xs vs a,
  h1 = heap_add (singleton_heap (Chunk p vs)) h ->
  is_concrete_heap h0 ->
  pred_defs p = Some (PredDef xs a) ->
  length vs = length xs ->
  exists h',
  sat h' (store_updates store0 xs vs) a /\
  refines h0 (heap_add h' h).

Lemma refines_empty:
  forall h0 h1,
  refines h0 h1 ->
  h1 = empty_heap ->
  h0 = empty_heap.

Lemma refines_heap_add:
  forall h0 h1,
  refines h0 h1 ->
  forall h1a h1b, h1 = heap_add h1a h1b ->
  exists h0a, exists h0b,
  h0 = heap_add h0a h0b /\
  refines h0a h1a /\ refines h0b h1b.

Theorem close_theorem:
  forall h0 h1 h h' p xs a vs,
  refines h0 h1 ->
  h1 = heap_add h h' ->
  pred_defs p = Some (PredDef xs a) ->
  length vs = length xs ->
  sat h (store_updates store0 xs vs) a ->
  refines h0 (heap_add (singleton_heap (Chunk p vs)) h').

Lemma refines_concrete h0 h1: refines h0 h1 -> is_concrete_heap h1 -> h0 = h1.

End PredDefs.