file_name
stringlengths 5
52
| name
stringlengths 4
95
| original_source_type
stringlengths 0
23k
| source_type
stringlengths 9
23k
| source_definition
stringlengths 9
57.9k
| source
dict | source_range
dict | file_context
stringlengths 0
721k
| dependencies
dict | opens_and_abbrevs
listlengths 2
94
| vconfig
dict | interleaved
bool 1
class | verbose_type
stringlengths 1
7.42k
| effect
stringclasses 118
values | effect_flags
sequencelengths 0
2
| mutual_with
sequencelengths 0
11
| ideal_premises
sequencelengths 0
236
| proof_features
sequencelengths 0
1
| is_simple_lemma
bool 2
classes | is_div
bool 2
classes | is_proof
bool 2
classes | is_simply_typed
bool 2
classes | is_type
bool 2
classes | partial_definition
stringlengths 5
3.99k
| completed_definiton
stringlengths 1
1.63M
| isa_cross_project_example
bool 1
class |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.m_spec | val m_spec : Type0 | let m_spec = Core.m_spec | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 24,
"end_line": 51,
"start_col": 0,
"start_line": 51
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Hacl.Impl.Blake2.Core.m_spec"
] | [] | false | false | false | true | true | let m_spec =
| Core.m_spec | false |
|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.t | val t : a: Hacl.Streaming.Blake2.Common.alg -> Type0 | let t (a : alg) = Spec.state a | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 30,
"end_line": 58,
"start_col": 0,
"start_line": 58
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Spec.Blake2.Definitions.state"
] | [] | false | false | false | true | true | let t (a: alg) =
| Spec.state a | false |
|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.s | val s : a: Hacl.Streaming.Blake2.Common.alg -> m: Hacl.Streaming.Blake2.Common.m_spec -> Type0 | let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m) | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 63,
"end_line": 55,
"start_col": 0,
"start_line": 55
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg -> m: Hacl.Streaming.Blake2.Common.m_spec -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.m_spec",
"FStar.Pervasives.Native.tuple2",
"Hacl.Impl.Blake2.Core.state_p"
] | [] | false | false | false | true | true | let s (a: alg) (m: m_spec) =
| let open Core in state_p a m & state_p a m | false |
|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.s_v | val s_v (#a: alg) (#m: m_spec) (h: HS.mem) (s: s a m) : GTot (t a) | val s_v (#a: alg) (#m: m_spec) (h: HS.mem) (s: s a m) : GTot (t a) | let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 13,
"end_line": 79,
"start_col": 0,
"start_line": 78
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | h: FStar.Monotonic.HyperStack.mem -> s: Hacl.Streaming.Blake2.Common.s a m
-> Prims.GTot (Hacl.Streaming.Blake2.Common.t a) | Prims.GTot | [
"sometrivial"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.m_spec",
"FStar.Monotonic.HyperStack.mem",
"Hacl.Streaming.Blake2.Common.s",
"Hacl.Streaming.Blake2.Common.state_v",
"Hacl.Streaming.Blake2.Common.t"
] | [] | false | false | false | false | false | let s_v (#a: alg) (#m: m_spec) (h: HS.mem) (s: s a m) : GTot (t a) =
| state_v h s | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.key_size_t | val key_size_t : a: Hacl.Streaming.Blake2.Common.alg -> Type0 | let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a} | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 50,
"end_line": 144,
"start_col": 0,
"start_line": 143
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a} | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"FStar.UInt32.t",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"FStar.UInt32.v",
"Spec.Blake2.Definitions.max_key"
] | [] | false | false | false | true | true | let key_size_t (a: alg) =
| key_size: U32.t{U32.v key_size <= Spec.max_key a} | false |
|
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.key_size | val key_size : a: Hacl.Streaming.Blake2.Common.alg -> Type0 | let key_size (a : alg) = kk:nat{kk <= Spec.max_key a} | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 53,
"end_line": 140,
"start_col": 0,
"start_line": 140
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============ | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Spec.Blake2.Definitions.max_key"
] | [] | false | false | false | true | true | let key_size (a: alg) =
| kk: nat{kk <= Spec.max_key a} | false |
|
Hacl.Bignum.MontExponentiation.fst | Hacl.Bignum.MontExponentiation.bn_exp_mont_consttime | val bn_exp_mont_consttime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len | val bn_exp_mont_consttime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len | let bn_exp_mont_consttime #t k n mu r2 aM bBits b resM =
if bBits <. size S.bn_exp_mont_consttime_threshold then
bn_exp_mont_bm_consttime k n mu r2 aM bBits b resM
else
bn_exp_mont_fw_consttime k 4ul n mu r2 aM bBits b resM | {
"file_name": "code/bignum/Hacl.Bignum.MontExponentiation.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 58,
"end_line": 300,
"start_col": 0,
"start_line": 296
} | module Hacl.Bignum.MontExponentiation
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BD = Hacl.Spec.Bignum.Definitions
module SN = Hacl.Spec.Bignum
module SM = Hacl.Spec.Bignum.Montgomery
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module LE = Lib.Exponentiation
module BE = Hacl.Impl.Exponentiation
module E = Hacl.Spec.Exponentiation.Lemmas
module S = Hacl.Spec.Bignum.MontExponentiation
#reset-options "--z3rlimit 50 --fuel 0 --ifuel 0"
// All operations are performed in the Montgomery domain!
inline_for_extraction noextract
let a_spec (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len{0 < BD.bn_v n}) =
Lib.NatMod.nat_mod (BD.bn_v n)
inline_for_extraction noextract
let linv (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len) : Type0 =
BD.bn_v a < BD.bn_v n
inline_for_extraction noextract
let refl (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len{linv n a}) : a_spec n =
BD.bn_v a
inline_for_extraction noextract
let linv_ctx (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (ctx:BD.lbignum t (len + len)) : Type0 =
let ctx_n = LSeq.sub ctx 0 len in
let ctx_r2 = LSeq.sub ctx len len in
ctx_n == n /\
0 < BD.bn_v n /\ BD.bn_v ctx_r2 = pow2 (2 * bits t * len) % BD.bn_v n
inline_for_extraction noextract
let mk_to_nat_mont_ll_comm_monoid
(t:limb_t)
(len:BN.meta_len t)
(n:BD.lbignum t (v len))
(mu:limb t{SM.bn_mont_pre n mu})
: BE.to_comm_monoid t len (len +! len) =
{
BE.a_spec = a_spec n;
BE.comm_monoid = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (BD.bn_v n) (v mu);
BE.linv_ctx = linv_ctx n;
BE.linv = linv n;
BE.refl = refl n;
}
inline_for_extraction noextract
val bn_mont_one:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lone_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_one #t k n mu ctx oneM =
[@inline_let] let len = k.BM.bn.BN.len in
let ctx_n = sub ctx 0ul len in
let ctx_r2 = sub ctx len len in
let h0 = ST.get () in
SM.bn_mont_one_lemma n mu (as_seq h0 ctx_r2);
BM.bn_mont_one len k.BM.from ctx_n mu ctx_r2 oneM
inline_for_extraction noextract
val bn_mont_mul:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lmul_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_mul #t k n mu ctx aM bM resM =
let h0 = ST.get () in
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 bM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.mul ctx_n mu aM bM resM
inline_for_extraction noextract
val bn_mont_sqr:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lsqr_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_sqr #t k n mu ctx aM resM =
let h0 = ST.get () in
SM.bn_mont_sqr_lemma n mu (as_seq h0 aM);
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 aM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.sqr ctx_n mu aM resM
inline_for_extraction noextract
let mk_bn_mont_concrete_ops
(t:limb_t)
(k:BM.mont t)
(n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len)))
(mu:limb t{SM.bn_mont_pre n mu}) :
BE.concrete_ops t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len) =
{
BE.to = mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu;
BE.lone = bn_mont_one k n mu;
BE.lmul = bn_mont_mul k n mu;
BE.lsqr = bn_mont_sqr k n mu;
}
///////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
val mk_ctx:
#t:limb_t
-> len:BN.meta_len t
-> n:lbignum t len
-> r2:lbignum t len
-> ctx:lbignum t (len +! len) ->
Stack unit
(requires fun h ->
live h n /\ live h r2 /\ live h ctx /\
disjoint n ctx /\ disjoint r2 ctx /\
0 < bn_v h n /\ bn_v h r2 == pow2 (2 * bits t * v len) % bn_v h n)
(ensures fun h0 _ h1 -> modifies (loc ctx) h0 h1 /\
linv_ctx (as_seq h0 n) (as_seq h1 ctx))
let mk_ctx #t len n r2 ctx =
let h0 = ST.get () in
update_sub ctx 0ul len n;
let h1 = ST.get () in
assert (LSeq.sub (as_seq h1 ctx) 0 (v len) == as_seq h0 n);
update_sub ctx len len r2;
let h2 = ST.get () in
LSeq.eq_intro
(LSeq.sub (as_seq h2 ctx) 0 (v len))
(LSeq.sub (as_seq h1 ctx) 0 (v len));
assert (LSeq.sub (as_seq h2 ctx) 0 (v len) == as_seq h0 n);
assert (LSeq.sub (as_seq h2 ctx) (v len) (v len) == as_seq h0 r2)
noextract
let bn_exp_mont_pre
(#t:limb_t)
(#len:SN.bn_len t)
(n:BD.lbignum t len)
(mu:limb t)
(r2:BD.lbignum t len)
(aM:BD.lbignum t len)
(bBits:size_nat)
(b:BD.lbignum t (BD.blocks0 bBits (bits t)))
=
SM.bn_mont_pre n mu /\
BD.bn_v r2 == pow2 (2 * bits t * len) % BD.bn_v n /\
BD.bn_v b < pow2 bBits /\
BD.bn_v aM < BD.bn_v n
inline_for_extraction noextract
let bn_exp_mont_st (t:limb_t) (len:BN.meta_len t) =
n:lbignum t len
-> mu:limb t
-> r2:lbignum t len
-> aM:lbignum t len
-> bBits:size_t
-> b:lbignum t (blocks0 bBits (size (bits t)))
-> resM:lbignum t len ->
Stack unit
(requires fun h ->
live h n /\ live h aM /\ live h b /\ live h resM /\ live h r2 /\
disjoint resM aM /\ disjoint resM b /\ disjoint resM n /\ disjoint n aM /\
disjoint resM r2 /\ disjoint aM r2 /\ disjoint n r2 /\ disjoint aM b /\
bn_exp_mont_pre (as_seq h n) mu (as_seq h r2) (as_seq h aM) (v bBits) (as_seq h b))
(ensures fun h0 _ h1 -> modifies (loc aM |+| loc resM) h0 h1 /\
(let k1 = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu) in
bn_v h1 resM == LE.pow k1 (bn_v h0 aM) (bn_v h0 b)))
// This function is *NOT* constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_bm_vartime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_bm_vartime #t k n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
[@inline_let] let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_rl_vartime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) ctx aM bLen bBits b resM;
LE.exp_rl_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame ()
// This function is constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_bm_consttime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_bm_consttime #t k n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
[@inline_let] let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_mont_ladder_swap_consttime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) ctx aM bLen bBits b resM;
LE.exp_mont_ladder_swap_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
LE.exp_mont_ladder_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame ()
// This function is *NOT* constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_fw_vartime:
#t:limb_t
-> k:BM.mont t
-> l:size_t{0 < v l /\ v l < bits U32 /\ pow2 (v l) * v k.BM.bn.BN.len <= max_size_t} ->
bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_fw_vartime #t k l n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_fw_vartime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) l ctx aM bLen bBits b resM;
LE.exp_fw_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b) (v l);
pop_frame ()
// This function is constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_fw_consttime:
#t:limb_t
-> k:BM.mont t
-> l:size_t{0 < v l /\ v l < bits U32 /\ pow2 (v l) * v k.BM.bn.BN.len <= max_size_t} ->
bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_fw_consttime #t k l n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_fw_consttime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) l ctx aM bLen bBits b resM;
LE.exp_fw_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b) (v l);
pop_frame ()
///////////////////////////////////////////////
// This function is *NOT* constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_vartime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_vartime #t k n mu r2 aM bBits b resM =
if bBits <. size S.bn_exp_mont_vartime_threshold then
bn_exp_mont_bm_vartime k n mu r2 aM bBits b resM
else
bn_exp_mont_fw_vartime k 4ul n mu r2 aM bBits b resM
// This function is constant-time on the exponent b.
inline_for_extraction noextract | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.NatMod.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Exponentiation.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Exponentiation.Lemmas.fst.checked",
"Hacl.Spec.Bignum.Montgomery.fsti.checked",
"Hacl.Spec.Bignum.MontExponentiation.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Spec.Bignum.fsti.checked",
"Hacl.Impl.Exponentiation.fsti.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Bignum.MontExponentiation.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontExponentiation",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Exponentiation.Lemmas",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Lib.Exponentiation",
"short_module": "LE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum",
"short_module": "SN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | k: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontExponentiation.bn_exp_mont_st t (Mkbn?.len (Mkmont?.bn k)) | Prims.Tot | [
"total"
] | [] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Hacl.Bignum.Definitions.limb",
"Lib.IntTypes.size_t",
"Hacl.Bignum.Definitions.blocks0",
"Lib.IntTypes.size",
"Lib.IntTypes.bits",
"Lib.IntTypes.op_Less_Dot",
"Lib.IntTypes.U32",
"Hacl.Spec.Bignum.MontExponentiation.bn_exp_mont_consttime_threshold",
"Hacl.Bignum.MontExponentiation.bn_exp_mont_bm_consttime",
"Prims.unit",
"Prims.bool",
"Hacl.Bignum.MontExponentiation.bn_exp_mont_fw_consttime",
"FStar.UInt32.__uint_to_t"
] | [] | false | false | false | false | false | let bn_exp_mont_consttime #t k n mu r2 aM bBits b resM =
| if bBits <. size S.bn_exp_mont_consttime_threshold
then bn_exp_mont_bm_consttime k n mu r2 aM bBits b resM
else bn_exp_mont_fw_consttime k 4ul n mu r2 aM bBits b resM | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.get_state_p | val get_state_p (#a: alg) (#m: m_spec) (s: s a m) : Tot (Core.state_p a m) | val get_state_p (#a: alg) (#m: m_spec) (s: s a m) : Tot (Core.state_p a m) | let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 24,
"end_line": 69,
"start_col": 0,
"start_line": 68
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: Hacl.Streaming.Blake2.Common.s a m -> Hacl.Impl.Blake2.Core.state_p a m | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.m_spec",
"Hacl.Streaming.Blake2.Common.s",
"Hacl.Impl.Blake2.Core.state_p"
] | [] | false | false | false | false | false | let get_state_p (#a: alg) (#m: m_spec) (s: s a m) : Tot (Core.state_p a m) =
| match s with | _, p -> p | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.k | val k : a: Hacl.Streaming.Blake2.Common.alg -> kk: Hacl.Streaming.Blake2.Common.key_size a
-> Hacl.Streaming.Interface.stateful Prims.unit | let k = stateful_key | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 20,
"end_line": 215,
"start_col": 0,
"start_line": 215
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg -> kk: Hacl.Streaming.Blake2.Common.key_size a
-> Hacl.Streaming.Interface.stateful Prims.unit | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.stateful_key"
] | [] | false | false | false | false | false | let k =
| stateful_key | false |
|
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.mem_memP | val mem_memP (#a: eqtype) (x: a) (l: list a)
: Lemma (ensures (mem x l <==> memP x l)) [SMTPat (mem x l); SMTPat (memP x l)] | val mem_memP (#a: eqtype) (x: a) (l: list a)
: Lemma (ensures (mem x l <==> memP x l)) [SMTPat (mem x l); SMTPat (memP x l)] | let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 26,
"end_line": 42,
"start_col": 0,
"start_line": 34
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> l: Prims.list a
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.mem x l <==> FStar.List.Tot.Base.memP x l)
[SMTPat (FStar.List.Tot.Base.mem x l); SMTPat (FStar.List.Tot.Base.memP x l)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.mem_memP",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"Prims.b2t",
"FStar.List.Tot.Base.mem",
"FStar.List.Tot.Base.memP",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.bool",
"Prims.Nil"
] | [
"recursion"
] | false | false | true | false | false | let rec mem_memP (#a: eqtype) (x: a) (l: list a)
: Lemma (ensures (mem x l <==> memP x l)) [SMTPat (mem x l); SMTPat (memP x l)] =
| match l with
| [] -> ()
| a :: q -> mem_memP x q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.llist | val llist : a: Type -> n: Prims.nat -> Type | let llist a (n:nat) = l:list a {length l = n} | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 45,
"end_line": 26,
"start_col": 0,
"start_line": 26
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Type -> n: Prims.nat -> Type | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.list",
"Prims.b2t",
"Prims.op_Equality",
"FStar.List.Tot.Base.length"
] | [] | false | false | false | true | true | let llist a (n: nat) =
| l: list a {length l = n} | false |
|
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.memP_map_intro | val memP_map_intro (#a #b: Type) (f: (a -> Tot b)) (x: a) (l: list a)
: Lemma (requires True) (ensures (memP x l ==> memP (f x) (map f l))) (decreases l) | val memP_map_intro (#a #b: Type) (f: (a -> Tot b)) (x: a) (l: list a)
: Lemma (requires True) (ensures (memP x l ==> memP (f x) (map f l))) (decreases l) | let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 79,
"start_col": 0,
"start_line": 68
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> b) -> x: a -> l: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.memP x l ==>
FStar.List.Tot.Base.memP (f x) (FStar.List.Tot.Base.map f l)) (decreases l) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.memP_map_intro",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"FStar.List.Tot.Base.memP",
"FStar.List.Tot.Base.map",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec memP_map_intro (#a #b: Type) (f: (a -> Tot b)) (x: a) (l: list a)
: Lemma (requires True) (ensures (memP x l ==> memP (f x) (map f l))) (decreases l) =
| match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.mem_existsb | val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs)))) | val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs)))) | let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 107,
"start_col": 0,
"start_line": 104
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a -> | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> Prims.bool) -> xs: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.existsb f xs <==>
(exists (x: a). f x = true /\ FStar.List.Tot.Base.mem x xs)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.bool",
"Prims.list",
"FStar.List.Tot.Properties.mem_existsb",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec mem_existsb #a f xs =
| match xs with
| [] -> ()
| hd :: tl -> mem_existsb f tl | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.memP_existsb | val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs)))) | val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs)))) | let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 66,
"start_col": 0,
"start_line": 63
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a -> | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> Prims.bool) -> xs: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.existsb f xs <==>
(exists (x: a). f x = true /\ FStar.List.Tot.Base.memP x xs)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.bool",
"Prims.list",
"FStar.List.Tot.Properties.memP_existsb",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec memP_existsb #a f xs =
| match xs with
| [] -> ()
| hd :: tl -> memP_existsb f tl | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.memP_map_elim | val memP_map_elim (#a #b: Type) (f: (a -> Tot b)) (y: b) (l: list a)
: Lemma (requires True)
(ensures (memP y (map f l) ==> (exists (x: a). memP x l /\ f x == y)))
(decreases l) | val memP_map_elim (#a #b: Type) (f: (a -> Tot b)) (y: b) (l: list a)
: Lemma (requires True)
(ensures (memP y (map f l) ==> (exists (x: a). memP x l /\ f x == y)))
(decreases l) | let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 92,
"start_col": 0,
"start_line": 81
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> b) -> y: b -> l: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.memP y (FStar.List.Tot.Base.map f l) ==>
(exists (x: a). FStar.List.Tot.Base.memP x l /\ f x == y)) (decreases l) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.memP_map_elim",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_imp",
"FStar.List.Tot.Base.memP",
"FStar.List.Tot.Base.map",
"Prims.l_Exists",
"Prims.l_and",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec memP_map_elim (#a #b: Type) (f: (a -> Tot b)) (y: b) (l: list a)
: Lemma (requires True)
(ensures (memP y (map f l) ==> (exists (x: a). memP x l /\ f x == y)))
(decreases l) =
| match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_index_memP | val lemma_index_memP (#t: Type) (l: list t) (i: nat{i < length l})
: Lemma (ensures ((index l i) `memP` l)) [SMTPat ((index l i) `memP` l)] | val lemma_index_memP (#t: Type) (l: list t) (i: nat{i < length l})
: Lemma (ensures ((index l i) `memP` l)) [SMTPat ((index l i) `memP` l)] | let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 40,
"end_line": 51,
"start_col": 0,
"start_line": 45
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list t -> i: Prims.nat{i < FStar.List.Tot.Base.length l}
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.memP (FStar.List.Tot.Base.index l i) l)
[SMTPat (FStar.List.Tot.Base.memP (FStar.List.Tot.Base.index l i) l)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.List.Tot.Base.length",
"Prims.int",
"FStar.List.Tot.Properties.lemma_index_memP",
"FStar.List.Tot.Base.tl",
"Prims.op_Subtraction",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"FStar.List.Tot.Base.memP",
"FStar.List.Tot.Base.index",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.Nil"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_index_memP (#t: Type) (l: list t) (i: nat{i < length l})
: Lemma (ensures ((index l i) `memP` l)) [SMTPat ((index l i) `memP` l)] =
| match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1) | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.block_len | val block_len (a: alg) : U32.t | val block_len (a: alg) : U32.t | let block_len (a : alg) : U32.t = Core.size_block a | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 51,
"end_line": 235,
"start_col": 0,
"start_line": 235
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a }) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg -> FStar.UInt32.t | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Impl.Blake2.Core.size_block",
"FStar.UInt32.t"
] | [] | false | false | false | true | false | let block_len (a: alg) : U32.t =
| Core.size_block a | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_acc_memP | val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc))) | val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc))) | let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 43,
"end_line": 138,
"start_col": 0,
"start_line": 136
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list a -> acc: Prims.list a -> x: a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.memP x (FStar.List.Tot.Base.rev_acc l acc) <==>
FStar.List.Tot.Base.memP x l \/ FStar.List.Tot.Base.memP x acc) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.rev_acc_memP",
"Prims.Cons",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec rev_acc_memP #a l acc x =
| match l with
| [] -> ()
| hd :: tl -> rev_acc_memP tl (hd :: acc) x | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_length | val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l)) | val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l)) | let rev_length l = rev_acc_length l [] | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 38,
"end_line": 131,
"start_col": 0,
"start_line": 131
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list 'a
-> FStar.Pervasives.Lemma
(ensures FStar.List.Tot.Base.length (FStar.List.Tot.Base.rev l) = FStar.List.Tot.Base.length l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.rev_acc_length",
"Prims.Nil",
"Prims.unit"
] | [] | true | false | true | false | false | let rev_length l =
| rev_acc_length l [] | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_acc_length | val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc)) | val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc)) | let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 43,
"end_line": 126,
"start_col": 0,
"start_line": 124
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list 'a -> acc: Prims.list 'a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.length (FStar.List.Tot.Base.rev_acc l acc) =
FStar.List.Tot.Base.length l + FStar.List.Tot.Base.length acc) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.rev_acc_length",
"Prims.Cons",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec rev_acc_length l acc =
| match l with
| [] -> ()
| hd :: tl -> rev_acc_length tl (hd :: acc) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_memP | val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l)) | val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l)) | let rev_memP #a l x = rev_acc_memP l [] x | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 144,
"start_col": 0,
"start_line": 144
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list a -> x: a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.memP x (FStar.List.Tot.Base.rev l) <==> FStar.List.Tot.Base.memP x l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.rev_acc_memP",
"Prims.Nil",
"Prims.unit"
] | [] | true | false | true | false | false | let rev_memP #a l x =
| rev_acc_memP l [] x | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_mem | val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l)) | val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l)) | let rev_mem l x = rev_memP l x | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 149,
"start_col": 0,
"start_line": 149
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list a -> x: a
-> FStar.Pervasives.Lemma
(ensures FStar.List.Tot.Base.mem x (FStar.List.Tot.Base.rev l) <==> FStar.List.Tot.Base.mem x l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.rev_memP",
"Prims.unit"
] | [] | true | false | true | false | false | let rev_mem l x =
| rev_memP l x | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.mem_count | val mem_count (#a: eqtype) (l: list a) (x: a) : Lemma (mem x l <==> count x l > 0) | val mem_count (#a: eqtype) (l: list a) (x: a) : Lemma (mem x l <==> count x l > 0) | let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 117,
"start_col": 0,
"start_line": 109
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list a -> x: a
-> FStar.Pervasives.Lemma
(ensures FStar.List.Tot.Base.mem x l <==> FStar.List.Tot.Base.count x l > 0) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.mem_count",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"Prims.b2t",
"FStar.List.Tot.Base.mem",
"Prims.op_GreaterThan",
"FStar.List.Tot.Base.count",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec mem_count (#a: eqtype) (l: list a) (x: a) : Lemma (mem x l <==> count x l > 0) =
| match l with
| [] -> ()
| x' :: l' -> mem_count l' x | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_l_cons | val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl))) | val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl))) | let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl' | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 175,
"start_col": 0,
"start_line": 173
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | hd: 'a -> tl: Prims.list 'a -> l: Prims.list 'a
-> FStar.Pervasives.Lemma (ensures l @ hd :: tl == (l @ [hd]) @ tl) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_l_cons",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_l_cons hd tl l =
| match l with
| [] -> ()
| hd' :: tl' -> append_l_cons hd tl tl' | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_assoc | val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3))) | val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3))) | let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 182,
"start_col": 0,
"start_line": 180
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list 'a -> l2: Prims.list 'a -> l3: Prims.list 'a
-> FStar.Pervasives.Lemma (ensures l1 @ l2 @ l3 == (l1 @ l2) @ l3) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_assoc",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_assoc l1 l2 l3 =
| match l1 with
| [] -> ()
| hd :: tl -> append_assoc tl l2 l3 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_length | val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))] | val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))] | let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 189,
"start_col": 0,
"start_line": 187
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list 'a -> l2: Prims.list 'a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.length (l1 @ l2) =
FStar.List.Tot.Base.length l1 + FStar.List.Tot.Base.length l2)
[SMTPat (FStar.List.Tot.Base.length (l1 @ l2))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_length",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_length l1 l2 =
| match l1 with
| [] -> ()
| hd :: tl -> append_length tl l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_l_nil | val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])] | val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])] | let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 29,
"end_line": 163,
"start_col": 0,
"start_line": 161
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list 'a -> FStar.Pervasives.Lemma (ensures l @ [] == l) [SMTPat (l @ [])] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_l_nil",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_l_nil =
| function
| [] -> ()
| hd :: tl -> append_l_nil tl | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_mem | val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2))) | val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2))) | let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 32,
"end_line": 199,
"start_col": 0,
"start_line": 197
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list t -> l2: Prims.list t -> a: t
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.mem a (l1 @ l2) =
(FStar.List.Tot.Base.mem a l1 || FStar.List.Tot.Base.mem a l2)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.append_mem",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_mem #t l1 l2 a =
| match l1 with
| [] -> ()
| hd :: tl -> append_mem tl l2 a | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_mem_forall | val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2))) | val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2))) | let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 37,
"end_line": 218,
"start_col": 0,
"start_line": 216
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
forall (a: a).
FStar.List.Tot.Base.mem a (l1 @ l2) =
(FStar.List.Tot.Base.mem a l1 || FStar.List.Tot.Base.mem a l2)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.append_mem_forall",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_mem_forall #a l1 l2 =
| match l1 with
| [] -> ()
| hd :: tl -> append_mem_forall tl l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_memP_forall | val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2))) | val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2))) | let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 38,
"end_line": 226,
"start_col": 0,
"start_line": 224
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
forall (a: a).
FStar.List.Tot.Base.memP a (l1 @ l2) <==>
FStar.List.Tot.Base.memP a l1 \/ FStar.List.Tot.Base.memP a l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_memP_forall",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_memP_forall #a l1 l2 =
| match l1 with
| [] -> ()
| hd :: tl -> append_memP_forall tl l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev'T | val rev'T : _: Prims.list _ -> Prims.list _ | let rev'T = rev' | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 16,
"end_line": 339,
"start_col": 0,
"start_line": 339
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> [] | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.list _ -> Prims.list _ | Prims.Tot | [
"total"
] | [] | [
"FStar.List.Tot.Properties.rev'",
"Prims.list"
] | [] | false | false | false | true | false | let rev'T =
| rev' | false |
|
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_memP | val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2))) | val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2))) | let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 33,
"end_line": 209,
"start_col": 0,
"start_line": 207
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list t -> l2: Prims.list t -> a: t
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.memP a (l1 @ l2) <==>
FStar.List.Tot.Base.memP a l1 \/ FStar.List.Tot.Base.memP a l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_memP",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_memP #t l1 l2 a =
| match l1 with
| [] -> ()
| hd :: tl -> append_memP tl l2 a | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_count_forall | val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2))) | val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2))) | let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 39,
"end_line": 245,
"start_col": 0,
"start_line": 243
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
forall (a: a).
FStar.List.Tot.Base.count a (l1 @ l2) =
FStar.List.Tot.Base.count a l1 + FStar.List.Tot.Base.count a l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.append_count_forall",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_count_forall #a l1 l2 =
| match l1 with
| [] -> ()
| hd :: tl -> append_count_forall tl l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_count | val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2))) | val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2))) | let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 236,
"start_col": 0,
"start_line": 234
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list t -> l2: Prims.list t -> a: t
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.count a (l1 @ l2) =
FStar.List.Tot.Base.count a l1 + FStar.List.Tot.Base.count a l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.append_count",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_count #t l1 l2 a =
| match l1 with
| [] -> ()
| hd :: tl -> append_count tl l2 a | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_length_inv_tail | val append_length_inv_tail (#a: Type) (left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2)) | val append_length_inv_tail (#a: Type) (left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2)) | let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 303,
"start_col": 0,
"start_line": 295
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | left1: Prims.list a -> right1: Prims.list a -> left2: Prims.list a -> right2: Prims.list a
-> FStar.Pervasives.Lemma
(requires
left1 @ right1 == left2 @ right2 /\
FStar.List.Tot.Base.length right1 == FStar.List.Tot.Base.length right2)
(ensures left1 == left2 /\ right1 == right2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_length_inv_head",
"Prims.unit",
"FStar.List.Tot.Properties.append_length",
"Prims.l_and",
"Prims.eq2",
"FStar.List.Tot.Base.append",
"Prims.nat",
"FStar.List.Tot.Base.length",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let append_length_inv_tail (#a: Type) (left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2)) =
| append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_inv_tail | val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2)) | val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2)) | let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 8,
"end_line": 281,
"start_col": 0,
"start_line": 267
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list 'a -> l1: Prims.list 'a -> l2: Prims.list 'a
-> FStar.Pervasives.Lemma (requires l1 @ l == l2 @ l) (ensures l1 == l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.Pervasives.Native.Mktuple2",
"FStar.List.Tot.Properties.append_inv_tail",
"Prims.Nil",
"FStar.List.Tot.Base.op_At",
"Prims.Cons",
"Prims.unit",
"FStar.List.Tot.Properties.append_l_cons"
] | [
"recursion"
] | false | false | true | false | false | let rec append_inv_tail l l1 l2 =
| match l1, l2 with
| [], [] -> ()
| hd1 :: tl1, hd2 :: tl2 -> append_inv_tail l tl1 tl2
| [], hd2 :: tl2 ->
(match l with
| [] -> ()
| hd :: tl ->
append_l_cons hd tl tl2;
append_inv_tail tl [] (tl2 @ [hd]))
| hd1 :: tl1, [] ->
(match l with
| [] -> ()
| hd :: tl ->
append_l_cons hd tl tl1;
append_inv_tail tl (tl1 @ [hd]) []) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_length_inv_head | val append_length_inv_head (#a: Type) (left1 right1 left2 right2: list a)
: Lemma (requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1) | val append_length_inv_head (#a: Type) (left1 right1 left2 right2: list a)
: Lemma (requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1) | let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 58,
"end_line": 293,
"start_col": 0,
"start_line": 283
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | left1: Prims.list a -> right1: Prims.list a -> left2: Prims.list a -> right2: Prims.list a
-> FStar.Pervasives.Lemma
(requires
left1 @ right1 == left2 @ right2 /\
FStar.List.Tot.Base.length left1 == FStar.List.Tot.Base.length left2)
(ensures left1 == left2 /\ right1 == right2)
(decreases left1) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_length_inv_head",
"FStar.List.Tot.Base.tl",
"Prims.unit",
"Prims.l_and",
"Prims.eq2",
"FStar.List.Tot.Base.append",
"Prims.nat",
"FStar.List.Tot.Base.length",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec append_length_inv_head (#a: Type) (left1 right1 left2 right2: list a)
: Lemma (requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1) =
| match left1 with
| [] -> ()
| _ :: left1' -> append_length_inv_head left1' right1 (tl left2) right2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_inv_head | val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2)) | val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2)) | let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 40,
"end_line": 262,
"start_col": 0,
"start_line": 260
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list 'a -> l1: Prims.list 'a -> l2: Prims.list 'a
-> FStar.Pervasives.Lemma (requires l @ l1 == l @ l2) (ensures l1 == l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_inv_head",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_inv_head l l1 l2 =
| match l with
| [] -> ()
| hd :: tl -> append_inv_head tl l1 l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev' | val rev': list 'a -> Tot (list 'a) | val rev': list 'a -> Tot (list 'a) | let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd] | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 28,
"end_line": 338,
"start_col": 0,
"start_line": 336
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.list 'a -> Prims.list 'a | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Prims.Nil",
"FStar.List.Tot.Base.op_At",
"FStar.List.Tot.Properties.rev'",
"Prims.Cons"
] | [
"recursion"
] | false | false | false | true | false | let rec rev' =
| function
| [] -> []
| hd :: tl -> (rev' tl) @ [hd] | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_injective | val append_injective (#a: _) (l0 l0' l1 l1': list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\ append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1') | val append_injective (#a: _) (l0 l0' l1 l1': list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\ append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1') | let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1' | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 49,
"end_line": 321,
"start_col": 0,
"start_line": 305
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l0: Prims.list a -> l0': Prims.list a -> l1: Prims.list a -> l1': Prims.list a
-> FStar.Pervasives.Lemma
(ensures
(FStar.List.Tot.Base.length l0 == FStar.List.Tot.Base.length l0' \/
FStar.List.Tot.Base.length l1 == FStar.List.Tot.Base.length l1') /\ l0 @ l1 == l0' @ l1' ==>
l0 == l0' /\ l1 == l1') | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.Classical.Sugar.implies_intro",
"Prims.l_and",
"Prims.l_or",
"Prims.eq2",
"Prims.nat",
"FStar.List.Tot.Base.length",
"FStar.List.Tot.Base.append",
"Prims.squash",
"FStar.Classical.Sugar.or_elim",
"Prims.l_not",
"FStar.List.Tot.Properties.append_length_inv_head",
"FStar.List.Tot.Properties.append_length_inv_tail",
"Prims.unit",
"Prims.l_True",
"Prims.l_imp",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let append_injective #a (l0: list a) (l0': list a) (l1: list a) (l1': list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\ append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1') =
| introduce ((length l0 == length l0' \/ length l1 == length l1') /\ append l0 l1 == append l0' l1') ==> (
l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/ (length l1 == length l1')
returns _
with _ . append_length_inv_head l0 l1 l0' l1'
and _ . append_length_inv_tail l0 l1 l0' l1' | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_rev' | val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l))) | val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l))) | let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 57,
"end_line": 351,
"start_col": 0,
"start_line": 351
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list 'a
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.rev l == FStar.List.Tot.Properties.rev' l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_l_nil",
"FStar.List.Tot.Properties.rev'",
"Prims.unit",
"FStar.List.Tot.Properties.rev_acc_rev'",
"Prims.Nil"
] | [] | true | false | true | false | false | let rev_rev' l =
| rev_acc_rev' l [];
append_l_nil (rev' l) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_acc_rev' | val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc))) | val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc))) | let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 73,
"end_line": 346,
"start_col": 0,
"start_line": 344
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list 'a -> acc: Prims.list 'a
-> FStar.Pervasives.Lemma
(ensures FStar.List.Tot.Base.rev_acc l acc == FStar.List.Tot.Properties.rev' l @ acc) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_l_cons",
"FStar.List.Tot.Properties.rev'",
"Prims.unit",
"FStar.List.Tot.Properties.rev_acc_rev'",
"Prims.Cons"
] | [
"recursion"
] | false | false | true | false | false | let rec rev_acc_rev' l acc =
| match l with
| [] -> ()
| hd :: tl ->
rev_acc_rev' tl (hd :: acc);
append_l_cons hd acc (rev' tl) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev'_involutive | val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l)) | val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l)) | let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 60,
"end_line": 370,
"start_col": 0,
"start_line": 368
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list 'a
-> FStar.Pervasives.Lemma
(ensures FStar.List.Tot.Properties.rev' (FStar.List.Tot.Properties.rev' l) == l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.rev'_involutive",
"Prims.unit",
"FStar.List.Tot.Properties.rev'_append",
"FStar.List.Tot.Properties.rev'",
"Prims.Cons",
"Prims.Nil"
] | [
"recursion"
] | false | false | true | false | false | let rec rev'_involutive =
| function
| [] -> ()
| hd :: tl ->
rev'_append (rev' tl) [hd];
rev'_involutive tl | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_snoc_length | val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1)) | val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1)) | let lemma_snoc_length (l, x) = append_length l [x] | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 50,
"end_line": 382,
"start_col": 0,
"start_line": 382
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lx: (Prims.list 'a * 'a)
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.length (FStar.List.Tot.Base.snoc lx) =
FStar.List.Tot.Base.length (FStar.Pervasives.Native.fst lx) + 1) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"FStar.Pervasives.Native.tuple2",
"Prims.list",
"FStar.List.Tot.Properties.append_length",
"Prims.Cons",
"Prims.Nil",
"Prims.unit"
] | [] | false | false | true | false | false | let lemma_snoc_length (l, x) =
| append_length l [x] | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev'_list_ind | val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l))) | val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l))) | let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 32,
"end_line": 391,
"start_col": 0,
"start_line": 389
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl))))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: (_: Prims.list 'a -> Prims.bool) -> l: Prims.list 'a
-> FStar.Pervasives.Lemma
(requires
p [] /\
(forall (hd: 'a) (tl: Prims.list 'a).
p (FStar.List.Tot.Properties.rev' tl) ==> p (FStar.List.Tot.Properties.rev' (hd :: tl)))
) (ensures p (FStar.List.Tot.Properties.rev' l)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Prims.bool",
"FStar.List.Tot.Properties.rev'_list_ind",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec rev'_list_ind p =
| function
| [] -> ()
| hd :: tl -> rev'_list_ind p tl | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_involutive | val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l)) | val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l)) | let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 71,
"end_line": 375,
"start_col": 0,
"start_line": 375
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list 'a
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.rev (FStar.List.Tot.Base.rev l) == l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.rev'_involutive",
"Prims.unit",
"FStar.List.Tot.Properties.rev_rev'",
"FStar.List.Tot.Properties.rev'"
] | [] | true | false | true | false | false | let rev_involutive l =
| rev_rev' l;
rev_rev' (rev' l);
rev'_involutive l | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev'_append | val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1)))) | val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1)))) | let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd] | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 72,
"end_line": 358,
"start_col": 0,
"start_line": 356
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list 'a -> l2: Prims.list 'a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Properties.rev' (l1 @ l2) ==
FStar.List.Tot.Properties.rev' l2 @ FStar.List.Tot.Properties.rev' l1) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_l_nil",
"FStar.List.Tot.Properties.rev'",
"FStar.List.Tot.Properties.append_assoc",
"Prims.Cons",
"Prims.Nil",
"Prims.unit",
"FStar.List.Tot.Properties.rev'_append"
] | [
"recursion"
] | false | false | true | false | false | let rec rev'_append l1 l2 =
| match l1 with
| [] -> append_l_nil (rev' l2)
| hd :: tl ->
rev'_append tl l2;
append_assoc (rev' l2) (rev' tl) [hd] | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_append | val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1)))) | val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1)))) | let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 84,
"end_line": 363,
"start_col": 0,
"start_line": 363
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list 'a -> l2: Prims.list 'a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.rev (l1 @ l2) == FStar.List.Tot.Base.rev l2 @ FStar.List.Tot.Base.rev l1) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.rev'_append",
"Prims.unit",
"FStar.List.Tot.Properties.rev_rev'",
"FStar.List.Tot.Base.op_At"
] | [] | true | false | true | false | false | let rev_append l1 l2 =
| rev_rev' l1;
rev_rev' l2;
rev_rev' (l1 @ l2);
rev'_append l1 l2 | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.buffer_to_stateful_key_t | val buffer_to_stateful_key_t
(a: alg)
(kk: key_size a {kk > 0})
(k: B.buffer uint8 {B.length k == kk})
: Tot (stateful_key_t a kk) | val buffer_to_stateful_key_t
(a: alg)
(kk: key_size a {kk > 0})
(k: B.buffer uint8 {B.length k == kk})
: Tot (stateful_key_t a kk) | let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 3,
"end_line": 155,
"start_col": 0,
"start_line": 152
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size} | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
a: Hacl.Streaming.Blake2.Common.alg ->
kk: Hacl.Streaming.Blake2.Common.key_size a {kk > 0} ->
k:
LowStar.Buffer.buffer Hacl.Streaming.Blake2.Common.uint8
{LowStar.Monotonic.Buffer.length k == kk}
-> Hacl.Streaming.Blake2.Common.stateful_key_t a kk | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.key_size",
"Prims.b2t",
"Prims.op_GreaterThan",
"LowStar.Buffer.buffer",
"Hacl.Streaming.Blake2.Common.uint8",
"Prims.eq2",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"LowStar.Buffer.trivial_preorder",
"Hacl.Streaming.Blake2.Common.stateful_key_t"
] | [] | false | false | false | false | false | let buffer_to_stateful_key_t
(a: alg)
(kk: key_size a {kk > 0})
(k: B.buffer uint8 {B.length k == kk})
: Tot (stateful_key_t a kk) =
| k | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_append_last | val lemma_append_last (#a: Type) (l1 l2: list a)
: Lemma (requires (length l2 > 0)) (ensures (last (l1 @ l2) == last l2)) | val lemma_append_last (#a: Type) (l1 l2: list a)
: Lemma (requires (length l2 > 0)) (ensures (last (l1 @ l2) == last l2)) | let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 40,
"end_line": 331,
"start_col": 0,
"start_line": 325
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.length l2 > 0)
(ensures FStar.List.Tot.Base.last (l1 @ l2) == FStar.List.Tot.Base.last l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.lemma_append_last",
"Prims.unit",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.List.Tot.Base.length",
"Prims.squash",
"Prims.eq2",
"FStar.List.Tot.Base.last",
"FStar.List.Tot.Base.op_At",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_append_last (#a: Type) (l1 l2: list a)
: Lemma (requires (length l2 > 0)) (ensures (last (l1 @ l2) == last l2)) =
| match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.rev_ind | val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l)) | val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l)) | let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 61,
"end_line": 396,
"start_col": 0,
"start_line": 396
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl])))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: (_: Prims.list 'a -> Prims.bool) -> l: Prims.list 'a
-> FStar.Pervasives.Lemma
(requires p [] /\ (forall (hd: Prims.list 'a) (tl: 'a). p hd ==> p (hd @ [tl]))) (ensures p l) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Prims.bool",
"FStar.List.Tot.Properties.rev'_list_ind",
"FStar.List.Tot.Properties.rev'",
"Prims.unit",
"FStar.List.Tot.Properties.rev'_involutive"
] | [] | true | false | true | false | false | let rev_ind p l =
| rev'_involutive l;
rev'_list_ind p (rev' l) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.subset_reflexive | val subset_reflexive (#a: eqtype) (l: list a) : Lemma (subset l l) [SMTPat (subset l l)] | val subset_reflexive (#a: eqtype) (l: list a) : Lemma (subset l l) [SMTPat (subset l l)] | let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 63,
"end_line": 605,
"start_col": 0,
"start_line": 604
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list a
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.subset l l)
[SMTPat (FStar.List.Tot.Base.subset l l)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.mem_subset",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.b2t",
"FStar.List.Tot.Base.subset",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.bool",
"Prims.Nil"
] | [] | true | false | true | false | false | let subset_reflexive (#a: eqtype) (l: list a) : Lemma (subset l l) [SMTPat (subset l l)] =
| mem_subset l l | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.split_using | val split_using (#t: Type) (l: list t) (x: t{x `memP` l}) : GTot (list t * list t) | val split_using (#t: Type) (l: list t) (x: t{x `memP` l}) : GTot (list t * list t) | let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 496,
"start_col": 0,
"start_line": 486
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list t -> x: t{FStar.List.Tot.Base.memP x l} -> Prims.GTot (Prims.list t * Prims.list t) | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.list",
"FStar.List.Tot.Base.memP",
"FStar.Pervasives.Native.Mktuple2",
"Prims.Nil",
"FStar.StrongExcludedMiddle.strong_excluded_middle",
"Prims.eq2",
"Prims.bool",
"Prims.Cons",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Properties.split_using"
] | [
"recursion"
] | false | false | false | false | false | let rec split_using (#t: Type) (l: list t) (x: t{x `memP` l}) : GTot (list t * list t) =
| match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x)
then ([], l)
else
(let l1', l2' = split_using rest x in
a :: l1', l2') | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_unsnoc_length | val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1)) | val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1)) | let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 30,
"end_line": 445,
"start_col": 0,
"start_line": 444
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list a {FStar.List.Tot.Base.length l > 0}
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.length (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.unsnoc l)) ==
FStar.List.Tot.Base.length l - 1) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.List.Tot.Base.length",
"FStar.List.Tot.Properties.lemma_snoc_length",
"FStar.List.Tot.Base.unsnoc",
"Prims.unit"
] | [] | true | false | true | false | false | let lemma_unsnoc_length #a l =
| lemma_snoc_length (unsnoc l) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.map_lemma | val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)] | val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)] | let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 408,
"start_col": 0,
"start_line": 405
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: 'a -> 'b) -> l: Prims.list 'a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.length (FStar.List.Tot.Base.map f l) = FStar.List.Tot.Base.length l)
[SMTPat (FStar.List.Tot.Base.map f l)] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.map_lemma",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec map_lemma f l =
| match l with
| [] -> ()
| h :: t -> map_lemma f t | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.partition_mem | val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2))) | val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2))) | let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 550,
"start_col": 0,
"start_line": 548
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> Prims.bool) -> l: Prims.list a -> x: a
-> FStar.Pervasives.Lemma
(ensures
(let _ = FStar.List.Tot.Base.partition f l in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ l1 l2 = _ in
FStar.List.Tot.Base.mem x l =
(FStar.List.Tot.Base.mem x l1 || FStar.List.Tot.Base.mem x l2))
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.bool",
"Prims.list",
"FStar.List.Tot.Properties.partition_mem",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec partition_mem #a f l x =
| match l with
| [] -> ()
| hd :: tl -> partition_mem f tl x | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.output_size | val output_size (a: alg) : nat | val output_size (a: alg) : nat | let output_size (a : alg) : nat = Spec.max_output a | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 51,
"end_line": 238,
"start_col": 0,
"start_line": 238
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL
inline_for_extraction noextract
let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a })
inline_for_extraction noextract
let block_len (a : alg) : U32.t = Core.size_block a | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg -> Prims.nat | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Spec.Blake2.Definitions.max_output",
"Prims.nat"
] | [] | false | false | false | true | false | let output_size (a: alg) : nat =
| Spec.max_output a | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.partition_mem_p_forall | val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x)))) | val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x)))) | let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 571,
"start_col": 0,
"start_line": 569
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: (_: a -> Prims.bool) -> l: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
(let _ = FStar.List.Tot.Base.partition p l in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ l1 l2 = _ in
(forall (x: a). FStar.List.Tot.Base.mem x l1 ==> p x) /\
(forall (x: a). FStar.List.Tot.Base.mem x l2 ==> Prims.op_Negation (p x)))
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.bool",
"Prims.list",
"FStar.List.Tot.Properties.partition_mem_p_forall",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec partition_mem_p_forall #a p l =
| match l with
| [] -> ()
| hd :: tl -> partition_mem_p_forall p tl | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_snoc_unsnoc | val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))] | val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))] | let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 438,
"start_col": 0,
"start_line": 434
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx)))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | lx: (Prims.list a * a)
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.unsnoc (FStar.List.Tot.Base.snoc lx) == lx)
(decreases FStar.List.Tot.Base.length (FStar.Pervasives.Native.fst lx))
[SMTPat (FStar.List.Tot.Base.unsnoc (FStar.List.Tot.Base.snoc lx))] | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"FStar.Pervasives.Native.tuple2",
"Prims.list",
"FStar.List.Tot.Properties.lemma_snoc_unsnoc",
"FStar.Pervasives.Native.Mktuple2",
"FStar.List.Tot.Base.tl",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_snoc_unsnoc #a lx =
| let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_unsnoc_is_last | val lemma_unsnoc_is_last (#t: Type) (l: list t)
: Lemma (requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) | val lemma_unsnoc_is_last (#t: Type) (l: list t)
: Lemma (requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) | let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 466,
"start_col": 0,
"start_line": 460
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list t
-> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.length l > 0)
(ensures
FStar.Pervasives.Native.snd (FStar.List.Tot.Base.unsnoc l) == FStar.List.Tot.Base.last l /\
FStar.Pervasives.Native.snd (FStar.List.Tot.Base.unsnoc l) ==
FStar.List.Tot.Base.index l (FStar.List.Tot.Base.length l - 1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.lemma_unsnoc_is_last",
"FStar.List.Tot.Base.tl",
"Prims.unit",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.List.Tot.Base.length",
"Prims.squash",
"Prims.l_and",
"Prims.eq2",
"FStar.Pervasives.Native.snd",
"FStar.List.Tot.Base.unsnoc",
"FStar.List.Tot.Base.last",
"FStar.List.Tot.Base.index",
"Prims.op_Subtraction",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_unsnoc_is_last (#t: Type) (l: list t)
: Lemma (requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
| match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.partition_count | val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l))))) | val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l))))) | let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 36,
"end_line": 583,
"start_col": 0,
"start_line": 581
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> Prims.bool) -> l: Prims.list a -> x: a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.count x l =
FStar.List.Tot.Base.count x (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.partition f l)) +
FStar.List.Tot.Base.count x (FStar.Pervasives.Native.snd (FStar.List.Tot.Base.partition f l))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.bool",
"Prims.list",
"FStar.List.Tot.Properties.partition_count",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec partition_count #a f l x =
| match l with
| [] -> ()
| hd :: tl -> partition_count f tl x | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_unsnoc_append | val lemma_unsnoc_append (#a: Type) (l1 l2: list a)
: Lemma (requires (length l2 > 0))
(ensures
(let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) | val lemma_unsnoc_append (#a: Type) (l1 l2: list a)
: Lemma (requires (length l2 > 0))
(ensures
(let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) | let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 42,
"end_line": 457,
"start_col": 0,
"start_line": 448
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.length l2 > 0)
(ensures
(let _ = FStar.List.Tot.Base.unsnoc (l1 @ l2) in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ al a = _ in
let _ = FStar.List.Tot.Base.unsnoc l2 in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ bl b = _ in
al == l1 @ bl /\ a == b)
<:
Type0)
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.lemma_unsnoc_append",
"Prims.unit",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.List.Tot.Base.length",
"Prims.squash",
"Prims.l_and",
"Prims.eq2",
"FStar.List.Tot.Base.op_At",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Base.unsnoc",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_unsnoc_append (#a: Type) (l1 l2: list a)
: Lemma (requires (length l2 > 0))
(ensures
(let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
| match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_unsnoc_snoc | val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))] | val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))] | let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 7,
"end_line": 426,
"start_col": 0,
"start_line": 417
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list a {FStar.List.Tot.Base.length l > 0}
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.snoc (FStar.List.Tot.Base.unsnoc l) == l)
[SMTPat (FStar.List.Tot.Base.snoc (FStar.List.Tot.Base.unsnoc l))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.List.Tot.Base.length",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.List.Tot.Base.append",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Base.splitAt",
"Prims.op_Subtraction",
"Prims.Nil",
"FStar.Pervasives.pattern",
"Prims.op_Equality",
"Prims.int",
"Prims.bool",
"FStar.List.Tot.Base.tl",
"FStar.List.Tot.Base.lemma_splitAt_snd_length",
"FStar.Pervasives.Native.Mktuple2",
"Prims.Cons",
"FStar.List.Tot.Base.unsnoc"
] | [] | false | false | true | false | false | let lemma_unsnoc_snoc #a l =
| let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
let rec aux (l: list a {length l > 0})
: Lemma
(let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l)
in
aux l | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_unsnoc_index | val lemma_unsnoc_index (#t: Type) (l: list t) (i: nat)
: Lemma (requires (length l > 0 /\ i < length l - 1))
(ensures (i < length (fst (unsnoc l)) /\ index (fst (unsnoc l)) i == index l i)) | val lemma_unsnoc_index (#t: Type) (l: list t) (i: nat)
: Lemma (requires (length l > 0 /\ i < length l - 1))
(ensures (i < length (fst (unsnoc l)) /\ index (fst (unsnoc l)) i == index l i)) | let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 42,
"end_line": 478,
"start_col": 0,
"start_line": 470
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list t -> i: Prims.nat
-> FStar.Pervasives.Lemma
(requires FStar.List.Tot.Base.length l > 0 /\ i < FStar.List.Tot.Base.length l - 1)
(ensures
i < FStar.List.Tot.Base.length (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.unsnoc l)) /\
FStar.List.Tot.Base.index (FStar.Pervasives.Native.fst (FStar.List.Tot.Base.unsnoc l)) i ==
FStar.List.Tot.Base.index l i) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"Prims.nat",
"Prims.int",
"FStar.List.Tot.Properties.lemma_unsnoc_index",
"FStar.List.Tot.Base.tl",
"Prims.op_Subtraction",
"Prims.unit",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.List.Tot.Base.length",
"Prims.op_LessThan",
"Prims.squash",
"FStar.Pervasives.Native.fst",
"FStar.List.Tot.Base.unsnoc",
"Prims.eq2",
"FStar.List.Tot.Base.index",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_unsnoc_index (#t: Type) (l: list t) (i: nat)
: Lemma (requires (length l > 0 /\ i < length l - 1))
(ensures (i < length (fst (unsnoc l)) /\ index (fst (unsnoc l)) i == index l i)) =
| match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1) | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.block | val block : a: Hacl.Streaming.Blake2.Common.alg -> Type0 | let block (a : alg) = (block: S.seq uint8 { S.length block = Spec.size_block a }) | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 81,
"end_line": 232,
"start_col": 0,
"start_line": 232
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1
noextract inline_for_extraction
let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"FStar.Seq.Base.seq",
"Hacl.Streaming.Blake2.Common.uint8",
"Prims.b2t",
"Prims.op_Equality",
"Prims.nat",
"FStar.Seq.Base.length",
"Spec.Blake2.Definitions.size_block"
] | [] | false | false | false | true | true | let block (a: alg) =
| (block: S.seq uint8 {S.length block = Spec.size_block a}) | false |
|
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.mem_subset | val mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb)) (ensures (subset la lb)) | val mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb)) (ensures (subset la lb)) | let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 32,
"end_line": 602,
"start_col": 0,
"start_line": 597
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | la: Prims.list a -> lb: Prims.list a
-> FStar.Pervasives.Lemma
(requires forall (x: a). FStar.List.Tot.Base.mem x la ==> FStar.List.Tot.Base.mem x lb)
(ensures FStar.List.Tot.Base.subset la lb) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.mem_subset",
"Prims.unit",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.b2t",
"FStar.List.Tot.Base.mem",
"Prims.squash",
"FStar.List.Tot.Base.subset",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb)) (ensures (subset la lb)) =
| match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.partition_mem_forall | val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2)))) | val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2)))) | let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 39,
"end_line": 560,
"start_col": 0,
"start_line": 558
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> Prims.bool) -> l: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
(let _ = FStar.List.Tot.Base.partition f l in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ l1 l2 = _ in
forall (x: a).
FStar.List.Tot.Base.mem x l =
(FStar.List.Tot.Base.mem x l1 || FStar.List.Tot.Base.mem x l2))
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.bool",
"Prims.list",
"FStar.List.Tot.Properties.partition_mem_forall",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec partition_mem_forall #a f l =
| match l with
| [] -> ()
| hd :: tl -> partition_mem_forall f tl | false |
Hacl.Bignum.MontExponentiation.fst | Hacl.Bignum.MontExponentiation.bn_exp_mont_fw_vartime | val bn_exp_mont_fw_vartime:
#t:limb_t
-> k:BM.mont t
-> l:size_t{0 < v l /\ v l < bits U32 /\ pow2 (v l) * v k.BM.bn.BN.len <= max_size_t} ->
bn_exp_mont_st t k.BM.bn.BN.len | val bn_exp_mont_fw_vartime:
#t:limb_t
-> k:BM.mont t
-> l:size_t{0 < v l /\ v l < bits U32 /\ pow2 (v l) * v k.BM.bn.BN.len <= max_size_t} ->
bn_exp_mont_st t k.BM.bn.BN.len | let bn_exp_mont_fw_vartime #t k l n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_fw_vartime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) l ctx aM bLen bBits b resM;
LE.exp_fw_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b) (v l);
pop_frame () | {
"file_name": "code/bignum/Hacl.Bignum.MontExponentiation.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 256,
"start_col": 0,
"start_line": 245
} | module Hacl.Bignum.MontExponentiation
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BD = Hacl.Spec.Bignum.Definitions
module SN = Hacl.Spec.Bignum
module SM = Hacl.Spec.Bignum.Montgomery
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module LE = Lib.Exponentiation
module BE = Hacl.Impl.Exponentiation
module E = Hacl.Spec.Exponentiation.Lemmas
module S = Hacl.Spec.Bignum.MontExponentiation
#reset-options "--z3rlimit 50 --fuel 0 --ifuel 0"
// All operations are performed in the Montgomery domain!
inline_for_extraction noextract
let a_spec (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len{0 < BD.bn_v n}) =
Lib.NatMod.nat_mod (BD.bn_v n)
inline_for_extraction noextract
let linv (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len) : Type0 =
BD.bn_v a < BD.bn_v n
inline_for_extraction noextract
let refl (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len{linv n a}) : a_spec n =
BD.bn_v a
inline_for_extraction noextract
let linv_ctx (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (ctx:BD.lbignum t (len + len)) : Type0 =
let ctx_n = LSeq.sub ctx 0 len in
let ctx_r2 = LSeq.sub ctx len len in
ctx_n == n /\
0 < BD.bn_v n /\ BD.bn_v ctx_r2 = pow2 (2 * bits t * len) % BD.bn_v n
inline_for_extraction noextract
let mk_to_nat_mont_ll_comm_monoid
(t:limb_t)
(len:BN.meta_len t)
(n:BD.lbignum t (v len))
(mu:limb t{SM.bn_mont_pre n mu})
: BE.to_comm_monoid t len (len +! len) =
{
BE.a_spec = a_spec n;
BE.comm_monoid = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (BD.bn_v n) (v mu);
BE.linv_ctx = linv_ctx n;
BE.linv = linv n;
BE.refl = refl n;
}
inline_for_extraction noextract
val bn_mont_one:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lone_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_one #t k n mu ctx oneM =
[@inline_let] let len = k.BM.bn.BN.len in
let ctx_n = sub ctx 0ul len in
let ctx_r2 = sub ctx len len in
let h0 = ST.get () in
SM.bn_mont_one_lemma n mu (as_seq h0 ctx_r2);
BM.bn_mont_one len k.BM.from ctx_n mu ctx_r2 oneM
inline_for_extraction noextract
val bn_mont_mul:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lmul_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_mul #t k n mu ctx aM bM resM =
let h0 = ST.get () in
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 bM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.mul ctx_n mu aM bM resM
inline_for_extraction noextract
val bn_mont_sqr:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lsqr_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_sqr #t k n mu ctx aM resM =
let h0 = ST.get () in
SM.bn_mont_sqr_lemma n mu (as_seq h0 aM);
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 aM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.sqr ctx_n mu aM resM
inline_for_extraction noextract
let mk_bn_mont_concrete_ops
(t:limb_t)
(k:BM.mont t)
(n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len)))
(mu:limb t{SM.bn_mont_pre n mu}) :
BE.concrete_ops t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len) =
{
BE.to = mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu;
BE.lone = bn_mont_one k n mu;
BE.lmul = bn_mont_mul k n mu;
BE.lsqr = bn_mont_sqr k n mu;
}
///////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
val mk_ctx:
#t:limb_t
-> len:BN.meta_len t
-> n:lbignum t len
-> r2:lbignum t len
-> ctx:lbignum t (len +! len) ->
Stack unit
(requires fun h ->
live h n /\ live h r2 /\ live h ctx /\
disjoint n ctx /\ disjoint r2 ctx /\
0 < bn_v h n /\ bn_v h r2 == pow2 (2 * bits t * v len) % bn_v h n)
(ensures fun h0 _ h1 -> modifies (loc ctx) h0 h1 /\
linv_ctx (as_seq h0 n) (as_seq h1 ctx))
let mk_ctx #t len n r2 ctx =
let h0 = ST.get () in
update_sub ctx 0ul len n;
let h1 = ST.get () in
assert (LSeq.sub (as_seq h1 ctx) 0 (v len) == as_seq h0 n);
update_sub ctx len len r2;
let h2 = ST.get () in
LSeq.eq_intro
(LSeq.sub (as_seq h2 ctx) 0 (v len))
(LSeq.sub (as_seq h1 ctx) 0 (v len));
assert (LSeq.sub (as_seq h2 ctx) 0 (v len) == as_seq h0 n);
assert (LSeq.sub (as_seq h2 ctx) (v len) (v len) == as_seq h0 r2)
noextract
let bn_exp_mont_pre
(#t:limb_t)
(#len:SN.bn_len t)
(n:BD.lbignum t len)
(mu:limb t)
(r2:BD.lbignum t len)
(aM:BD.lbignum t len)
(bBits:size_nat)
(b:BD.lbignum t (BD.blocks0 bBits (bits t)))
=
SM.bn_mont_pre n mu /\
BD.bn_v r2 == pow2 (2 * bits t * len) % BD.bn_v n /\
BD.bn_v b < pow2 bBits /\
BD.bn_v aM < BD.bn_v n
inline_for_extraction noextract
let bn_exp_mont_st (t:limb_t) (len:BN.meta_len t) =
n:lbignum t len
-> mu:limb t
-> r2:lbignum t len
-> aM:lbignum t len
-> bBits:size_t
-> b:lbignum t (blocks0 bBits (size (bits t)))
-> resM:lbignum t len ->
Stack unit
(requires fun h ->
live h n /\ live h aM /\ live h b /\ live h resM /\ live h r2 /\
disjoint resM aM /\ disjoint resM b /\ disjoint resM n /\ disjoint n aM /\
disjoint resM r2 /\ disjoint aM r2 /\ disjoint n r2 /\ disjoint aM b /\
bn_exp_mont_pre (as_seq h n) mu (as_seq h r2) (as_seq h aM) (v bBits) (as_seq h b))
(ensures fun h0 _ h1 -> modifies (loc aM |+| loc resM) h0 h1 /\
(let k1 = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu) in
bn_v h1 resM == LE.pow k1 (bn_v h0 aM) (bn_v h0 b)))
// This function is *NOT* constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_bm_vartime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_bm_vartime #t k n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
[@inline_let] let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_rl_vartime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) ctx aM bLen bBits b resM;
LE.exp_rl_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame ()
// This function is constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_bm_consttime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_bm_consttime #t k n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
[@inline_let] let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_mont_ladder_swap_consttime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) ctx aM bLen bBits b resM;
LE.exp_mont_ladder_swap_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
LE.exp_mont_ladder_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame ()
// This function is *NOT* constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_fw_vartime:
#t:limb_t
-> k:BM.mont t
-> l:size_t{0 < v l /\ v l < bits U32 /\ pow2 (v l) * v k.BM.bn.BN.len <= max_size_t} ->
bn_exp_mont_st t k.BM.bn.BN.len | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.NatMod.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Exponentiation.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Exponentiation.Lemmas.fst.checked",
"Hacl.Spec.Bignum.Montgomery.fsti.checked",
"Hacl.Spec.Bignum.MontExponentiation.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Spec.Bignum.fsti.checked",
"Hacl.Impl.Exponentiation.fsti.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Bignum.MontExponentiation.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontExponentiation",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Exponentiation.Lemmas",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Lib.Exponentiation",
"short_module": "LE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum",
"short_module": "SN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
k: Hacl.Bignum.Montgomery.mont t ->
l:
Lib.IntTypes.size_t
{ 0 < Lib.IntTypes.v l /\ Lib.IntTypes.v l < Lib.IntTypes.bits Lib.IntTypes.U32 /\
Prims.pow2 (Lib.IntTypes.v l) * Lib.IntTypes.v (Mkbn?.len (Mkmont?.bn k)) <=
Lib.IntTypes.max_size_t }
-> Hacl.Bignum.MontExponentiation.bn_exp_mont_st t (Mkbn?.len (Mkmont?.bn k)) | Prims.Tot | [
"total"
] | [] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.bits",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Prims.pow2",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Lib.IntTypes.max_size_t",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.Definitions.limb",
"Hacl.Bignum.Definitions.blocks0",
"Lib.IntTypes.size",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Lib.Exponentiation.exp_fw_lemma",
"Lib.NatMod.nat_mod",
"Hacl.Bignum.Definitions.bn_v",
"FStar.Ghost.reveal",
"Lib.Exponentiation.Definition.comm_monoid",
"Hacl.Impl.Exponentiation.lexp_fw_vartime",
"Lib.IntTypes.op_Plus_Bang",
"Hacl.Bignum.MontExponentiation.mk_bn_mont_concrete_ops",
"FStar.Ghost.hide",
"Hacl.Spec.Bignum.Definitions.lbignum",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Bignum.MontExponentiation.mk_ctx",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.add",
"Lib.Buffer.create",
"Lib.IntTypes.uint",
"Lib.IntTypes.SEC",
"Lib.Buffer.lbuffer",
"FStar.Ghost.erased",
"Hacl.Spec.Exponentiation.Lemmas.mk_nat_mont_ll_comm_monoid",
"Lib.IntTypes.int_t",
"Prims.eq2",
"Prims.int",
"Prims.l_or",
"Lib.IntTypes.range",
"Prims.op_GreaterThan",
"Prims.op_Subtraction",
"Prims.op_Multiply",
"Lib.IntTypes.mk_int",
"Hacl.Spec.Bignum.Definitions.blocks0",
"Hacl.Bignum.meta_len",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"FStar.HyperStack.ST.push_frame"
] | [] | false | false | false | false | false | let bn_exp_mont_fw_vartime #t k l n mu r2 aM bBits b resM =
| push_frame ();
let h0 = ST.get () in
[@@ inline_let ]let len = k.BM.bn.BN.len in
let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_fw_vartime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) l ctx aM bLen
bBits b resM;
LE.exp_fw_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b) (v l);
pop_frame () | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.index_of | val index_of (#t: Type) (l: list t) (x: t{x `memP` l})
: GTot (i: nat{i < length l /\ index l i == x}) | val index_of (#t: Type) (l: list t) (x: t{x `memP` l})
: GTot (i: nat{i < length l /\ index l i == x}) | let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 5,
"end_line": 536,
"start_col": 0,
"start_line": 527
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l]. | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list t -> x: t{FStar.List.Tot.Base.memP x l}
-> Prims.GTot
(i: Prims.nat{i < FStar.List.Tot.Base.length l /\ FStar.List.Tot.Base.index l i == x}) | Prims.GTot | [
"sometrivial"
] | [] | [
"Prims.list",
"FStar.List.Tot.Base.memP",
"FStar.StrongExcludedMiddle.strong_excluded_middle",
"Prims.eq2",
"Prims.bool",
"Prims.op_Addition",
"FStar.List.Tot.Properties.index_of",
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.List.Tot.Base.length",
"FStar.List.Tot.Base.index"
] | [
"recursion"
] | false | false | false | false | false | let rec index_of (#t: Type) (l: list t) (x: t{x `memP` l})
: GTot (i: nat{i < length l /\ index l i == x}) =
| match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (0) else (1 + index_of rest x) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.partition_count_forall | val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l))))) | val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l))))) | let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 593,
"start_col": 0,
"start_line": 591
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l))))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> Prims.bool) -> l: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
forall (x: a).
FStar.List.Tot.Base.count x l =
FStar.List.Tot.Base.count x
(FStar.Pervasives.Native.fst (FStar.List.Tot.Base.partition f l)) +
FStar.List.Tot.Base.count x
(FStar.Pervasives.Native.snd (FStar.List.Tot.Base.partition f l))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.bool",
"Prims.list",
"FStar.List.Tot.Properties.partition_count_forall",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec partition_count_forall #a f l =
| match l with
| [] -> ()
| hd :: tl -> partition_count_forall f tl | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.state_to_lbuffer | val state_to_lbuffer (#a: alg) (#m: m_spec) (s: Core.state_p a m)
: B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) | val state_to_lbuffer (#a: alg) (#m: m_spec) (s: Core.state_p a m)
: B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) | let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 3,
"end_line": 86,
"start_col": 0,
"start_line": 84
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | s: Hacl.Impl.Blake2.Core.state_p a m
-> LowStar.Buffer.lbuffer (Hacl.Impl.Blake2.Core.element_t a m)
(4 * FStar.UInt32.v (Hacl.Impl.Blake2.Core.row_len a m)) | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.m_spec",
"Hacl.Impl.Blake2.Core.state_p",
"LowStar.Buffer.lbuffer",
"Hacl.Impl.Blake2.Core.element_t",
"FStar.Mul.op_Star",
"FStar.UInt32.v",
"Hacl.Impl.Blake2.Core.row_len"
] | [] | false | false | false | false | false | let state_to_lbuffer (#a: alg) (#m: m_spec) (s: Core.state_p a m)
: B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
| s | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.lemma_split_using | val lemma_split_using (#t: Type) (l: list t) (x: t{x `memP` l})
: Lemma
(ensures
(let l1, l2 = split_using l x in
length l2 > 0 /\ ~(x `memP` l1) /\ hd l2 == x /\ append l1 l2 == l)) | val lemma_split_using (#t: Type) (l: list t) (x: t{x `memP` l})
: Lemma
(ensures
(let l1, l2 = split_using l x in
length l2 > 0 /\ ~(x `memP` l1) /\ hd l2 == x /\ append l1 l2 == l)) | let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 66,
"end_line": 520,
"start_col": 0,
"start_line": 498
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list t -> x: t{FStar.List.Tot.Base.memP x l}
-> FStar.Pervasives.Lemma
(ensures
(let _ = FStar.List.Tot.Properties.split_using l x in
(let FStar.Pervasives.Native.Mktuple2 #_ #_ l1 l2 = _ in
FStar.List.Tot.Base.length l2 > 0 /\ ~(FStar.List.Tot.Base.memP x l1) /\
FStar.List.Tot.Base.hd l2 == x /\ l1 @ l2 == l)
<:
Type0)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Base.memP",
"FStar.Classical.or_elim",
"Prims.eq2",
"Prims.unit",
"Prims.squash",
"FStar.List.Tot.Properties.lemma_split_using",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThan",
"FStar.List.Tot.Base.length",
"Prims.l_not",
"FStar.List.Tot.Base.hd",
"FStar.List.Tot.Base.append",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Properties.split_using",
"Prims.l_True",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec lemma_split_using (#t: Type) (l: list t) (x: t{x `memP` l})
: Lemma
(ensures
(let l1, l2 = split_using l x in
length l2 > 0 /\ ~(x `memP` l1) /\ hd l2 == x /\ append l1 l2 == l)) =
| match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\ ~(x `memP` l1) /\ hd l2 == x /\ append l1 l2 == l
in
FStar.Classical.or_elim #_
#_
#(fun () -> goal)
(fun (_: squash (a == x)) -> ())
(fun (_: squash (x `memP` rest)) -> lemma_split_using rest x) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.append_sorted | val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))] | val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))] | let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 656,
"start_col": 0,
"start_line": 654
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2)))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
f: (_: a -> _: a -> Prims.bool) ->
l1: Prims.list a {FStar.List.Tot.Properties.sorted f l1} ->
l2: Prims.list a {FStar.List.Tot.Properties.sorted f l2} ->
pivot: a
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Properties.total_order f /\
(forall (y: a). FStar.List.Tot.Base.mem y l1 ==> Prims.op_Negation (f pivot y)) /\
(forall (y: a). FStar.List.Tot.Base.mem y l2 ==> f pivot y))
(ensures FStar.List.Tot.Properties.sorted f (l1 @ pivot :: l2))
[SMTPat (FStar.List.Tot.Properties.sorted f (l1 @ pivot :: l2))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.bool",
"Prims.list",
"Prims.b2t",
"FStar.List.Tot.Properties.sorted",
"FStar.List.Tot.Properties.append_sorted",
"Prims.unit"
] | [
"recursion"
] | false | false | true | false | false | let rec append_sorted #a f l1 l2 pivot =
| match l1 with
| [] -> ()
| hd :: tl -> append_sorted f tl l2 pivot | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.sortWith_permutation | val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l)) | val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l)) | let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 65,
"end_line": 624,
"start_col": 0,
"start_line": 616
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> _: a -> Prims.int) -> l: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
forall (x: a).
FStar.List.Tot.Base.count x l =
FStar.List.Tot.Base.count x (FStar.List.Tot.Base.sortWith f l))
(decreases FStar.List.Tot.Base.length l) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.eqtype",
"Prims.int",
"Prims.list",
"FStar.List.Tot.Properties.append_count_forall",
"FStar.List.Tot.Base.sortWith",
"Prims.Cons",
"Prims.unit",
"FStar.List.Tot.Properties.sortWith_permutation",
"FStar.List.Tot.Properties.partition_count_forall",
"FStar.List.Tot.Base.bool_of_compare",
"FStar.List.Tot.Base.partition_length",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Base.partition"
] | [
"recursion"
] | false | false | true | false | false | let rec sortWith_permutation #a f l =
| match l with
| [] -> ()
| pivot :: tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot :: sortWith f hi) | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.max_input_len | val max_input_len (a: alg) : (x: U64.t{U64.v x == max_input_length a}) | val max_input_len (a: alg) : (x: U64.t{U64.v x == max_input_length a}) | let max_input_len (a: alg): (x:U64.t { U64.v x == max_input_length a }) = 0xffffffffffffffffUL | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 94,
"end_line": 229,
"start_col": 0,
"start_line": 229
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// -------------
noextract
let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1 | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg
-> x: FStar.UInt64.t{FStar.UInt64.v x == Hacl.Streaming.Blake2.Common.max_input_length a} | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"FStar.UInt64.__uint_to_t",
"FStar.UInt64.t",
"Prims.eq2",
"Prims.int",
"Prims.l_or",
"FStar.UInt.size",
"FStar.UInt64.n",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThanOrEqual",
"Prims.op_LessThanOrEqual",
"Spec.Blake2.Definitions.max_limb",
"Prims.op_GreaterThan",
"Spec.Blake2.Definitions.size_block",
"FStar.UInt64.v",
"Hacl.Streaming.Blake2.Common.max_input_length"
] | [] | false | false | false | false | false | let max_input_len (a: alg) : (x: U64.t{U64.v x == max_input_length a}) =
| 0xffffffffffffffffuL | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.sorted | val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool | val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool | let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 41,
"end_line": 633,
"start_col": 0,
"start_line": 630
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: 'a -> _: 'a -> Prims.bool) -> _: Prims.list 'a -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.bool",
"Prims.list",
"Prims.op_AmpAmp",
"FStar.List.Tot.Properties.sorted",
"Prims.Cons"
] | [
"recursion"
] | false | false | false | true | false | let rec sorted f =
| function
| [] | [_] -> true
| x :: y :: tl -> f x y && sorted f (y :: tl) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.noRepeats_append_elim | val noRepeats_append_elim (#a: eqtype) (l1 l2: list a)
: Lemma (requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x. mem x l1 ==> ~(mem x l2))))
(decreases l1) | val noRepeats_append_elim (#a: eqtype) (l1 l2: list a)
: Lemma (requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x. mem x l1 ==> ~(mem x l2))))
(decreases l1) | let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 704,
"start_col": 0,
"start_line": 693
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.noRepeats (l1 @ l2))
(ensures
FStar.List.Tot.Base.noRepeats l1 /\ FStar.List.Tot.Base.noRepeats l2 /\
(forall (x: a). FStar.List.Tot.Base.mem x l1 ==> ~(FStar.List.Tot.Base.mem x l2)))
(decreases l1) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.noRepeats_append_elim",
"Prims.unit",
"FStar.List.Tot.Properties.append_mem",
"Prims.b2t",
"FStar.List.Tot.Base.noRepeats",
"FStar.List.Tot.Base.op_At",
"Prims.squash",
"Prims.l_and",
"Prims.l_Forall",
"Prims.l_imp",
"FStar.List.Tot.Base.mem",
"Prims.l_not",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec noRepeats_append_elim (#a: eqtype) (l1 l2: list a)
: Lemma (requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x. mem x l1 ==> ~(mem x l2))))
(decreases l1) =
| match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.noRepeats_append_intro | val noRepeats_append_intro (#a: eqtype) (l1 l2: list a)
: Lemma (requires (noRepeats l1 /\ noRepeats l2 /\ (forall x. mem x l1 ==> ~(mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1) | val noRepeats_append_intro (#a: eqtype) (l1 l2: list a)
: Lemma (requires (noRepeats l1 /\ noRepeats l2 /\ (forall x. mem x l1 ==> ~(mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1) | let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 32,
"end_line": 717,
"start_col": 0,
"start_line": 706
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.noRepeats l1 /\ FStar.List.Tot.Base.noRepeats l2 /\
(forall (x: a). FStar.List.Tot.Base.mem x l1 ==> ~(FStar.List.Tot.Base.mem x l2)))
(ensures FStar.List.Tot.Base.noRepeats (l1 @ l2))
(decreases l1) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.List.Tot.Properties.noRepeats_append_intro",
"Prims.unit",
"FStar.List.Tot.Properties.append_mem",
"Prims.l_and",
"Prims.b2t",
"FStar.List.Tot.Base.noRepeats",
"Prims.l_Forall",
"Prims.l_imp",
"FStar.List.Tot.Base.mem",
"Prims.l_not",
"Prims.squash",
"FStar.List.Tot.Base.op_At",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec noRepeats_append_intro (#a: eqtype) (l1 l2: list a)
: Lemma (requires (noRepeats l1 /\ noRepeats l2 /\ (forall x. mem x l1 ==> ~(mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1) =
| match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2 | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.stateful_key_to_buffer | val stateful_key_to_buffer (#a: alg) (#kk: key_size a) (key: stateful_key_t a kk)
: b: B.buffer uint8 {B.length b = kk} | val stateful_key_to_buffer (#a: alg) (#kk: key_size a) (key: stateful_key_t a kk)
: b: B.buffer uint8 {B.length b = kk} | let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 39,
"end_line": 212,
"start_col": 0,
"start_line": 209
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ()) | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | key: Hacl.Streaming.Blake2.Common.stateful_key_t a kk
-> b:
LowStar.Buffer.buffer Hacl.Streaming.Blake2.Common.uint8
{LowStar.Monotonic.Buffer.length b = kk} | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Hacl.Streaming.Blake2.Common.key_size",
"Hacl.Streaming.Blake2.Common.stateful_key_t",
"Prims.op_Equality",
"Prims.int",
"LowStar.Buffer.null",
"Hacl.Streaming.Blake2.Common.uint8",
"Prims.bool",
"LowStar.Buffer.buffer",
"Prims.b2t",
"Prims.nat",
"LowStar.Monotonic.Buffer.length",
"LowStar.Buffer.trivial_preorder"
] | [] | false | false | false | false | false | let stateful_key_to_buffer (#a: alg) (#kk: key_size a) (key: stateful_key_t a kk)
: b: B.buffer uint8 {B.length b = kk} =
| if kk = 0 then B.null #uint8 else key | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.no_repeats_p_append_elim | val no_repeats_p_append_elim (#a: Type) (l1 l2: list a)
: Lemma (requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x. memP x l1 ==> ~(memP x l2))))
(decreases l1) | val no_repeats_p_append_elim (#a: Type) (l1 l2: list a)
: Lemma (requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x. memP x l1 ==> ~(memP x l2))))
(decreases l1) | let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 34,
"end_line": 746,
"start_col": 0,
"start_line": 735
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.no_repeats_p (l1 @ l2))
(ensures
FStar.List.Tot.Base.no_repeats_p l1 /\ FStar.List.Tot.Base.no_repeats_p l2 /\
(forall (x: a). FStar.List.Tot.Base.memP x l1 ==> ~(FStar.List.Tot.Base.memP x l2)))
(decreases l1) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.no_repeats_p_append_elim",
"Prims.unit",
"FStar.List.Tot.Properties.append_memP",
"FStar.List.Tot.Base.no_repeats_p",
"FStar.List.Tot.Base.append",
"Prims.squash",
"Prims.l_and",
"Prims.l_Forall",
"Prims.l_imp",
"FStar.List.Tot.Base.memP",
"Prims.l_not",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec no_repeats_p_append_elim (#a: Type) (l1 l2: list a)
: Lemma (requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x. memP x l1 ==> ~(memP x l2))))
(decreases l1) =
| match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.no_repeats_p_append | val no_repeats_p_append (#a: Type) (l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==>
((no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x. memP x l1 ==> ~(memP x l2))))) | val no_repeats_p_append (#a: Type) (l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==>
((no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x. memP x l1 ==> ~(memP x l2))))) | let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 64,
"end_line": 769,
"start_col": 0,
"start_line": 761
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.no_repeats_p (l1 @ l2) <==>
FStar.List.Tot.Base.no_repeats_p l1 /\ FStar.List.Tot.Base.no_repeats_p l2 /\
(forall (x: a). FStar.List.Tot.Base.memP x l1 ==> ~(FStar.List.Tot.Base.memP x l2))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.Classical.move_requires",
"FStar.List.Tot.Base.no_repeats_p",
"FStar.List.Tot.Base.append",
"Prims.l_and",
"Prims.l_Forall",
"Prims.l_imp",
"FStar.List.Tot.Base.memP",
"Prims.l_not",
"FStar.List.Tot.Properties.no_repeats_p_append_elim",
"Prims.unit",
"FStar.List.Tot.Properties.no_repeats_p_append_intro",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let no_repeats_p_append (#a: Type) (l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==>
((no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x. memP x l1 ==> ~(memP x l2))))) =
| FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2 | false |
Hacl.Bignum.MontExponentiation.fst | Hacl.Bignum.MontExponentiation.bn_exp_mont_bm_vartime | val bn_exp_mont_bm_vartime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len | val bn_exp_mont_bm_vartime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len | let bn_exp_mont_bm_vartime #t k n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
[@inline_let] let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_rl_vartime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) ctx aM bLen bBits b resM;
LE.exp_rl_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame () | {
"file_name": "code/bignum/Hacl.Bignum.MontExponentiation.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 216,
"start_col": 0,
"start_line": 205
} | module Hacl.Bignum.MontExponentiation
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BD = Hacl.Spec.Bignum.Definitions
module SN = Hacl.Spec.Bignum
module SM = Hacl.Spec.Bignum.Montgomery
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module LE = Lib.Exponentiation
module BE = Hacl.Impl.Exponentiation
module E = Hacl.Spec.Exponentiation.Lemmas
module S = Hacl.Spec.Bignum.MontExponentiation
#reset-options "--z3rlimit 50 --fuel 0 --ifuel 0"
// All operations are performed in the Montgomery domain!
inline_for_extraction noextract
let a_spec (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len{0 < BD.bn_v n}) =
Lib.NatMod.nat_mod (BD.bn_v n)
inline_for_extraction noextract
let linv (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len) : Type0 =
BD.bn_v a < BD.bn_v n
inline_for_extraction noextract
let refl (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len{linv n a}) : a_spec n =
BD.bn_v a
inline_for_extraction noextract
let linv_ctx (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (ctx:BD.lbignum t (len + len)) : Type0 =
let ctx_n = LSeq.sub ctx 0 len in
let ctx_r2 = LSeq.sub ctx len len in
ctx_n == n /\
0 < BD.bn_v n /\ BD.bn_v ctx_r2 = pow2 (2 * bits t * len) % BD.bn_v n
inline_for_extraction noextract
let mk_to_nat_mont_ll_comm_monoid
(t:limb_t)
(len:BN.meta_len t)
(n:BD.lbignum t (v len))
(mu:limb t{SM.bn_mont_pre n mu})
: BE.to_comm_monoid t len (len +! len) =
{
BE.a_spec = a_spec n;
BE.comm_monoid = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (BD.bn_v n) (v mu);
BE.linv_ctx = linv_ctx n;
BE.linv = linv n;
BE.refl = refl n;
}
inline_for_extraction noextract
val bn_mont_one:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lone_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_one #t k n mu ctx oneM =
[@inline_let] let len = k.BM.bn.BN.len in
let ctx_n = sub ctx 0ul len in
let ctx_r2 = sub ctx len len in
let h0 = ST.get () in
SM.bn_mont_one_lemma n mu (as_seq h0 ctx_r2);
BM.bn_mont_one len k.BM.from ctx_n mu ctx_r2 oneM
inline_for_extraction noextract
val bn_mont_mul:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lmul_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_mul #t k n mu ctx aM bM resM =
let h0 = ST.get () in
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 bM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.mul ctx_n mu aM bM resM
inline_for_extraction noextract
val bn_mont_sqr:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lsqr_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_sqr #t k n mu ctx aM resM =
let h0 = ST.get () in
SM.bn_mont_sqr_lemma n mu (as_seq h0 aM);
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 aM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.sqr ctx_n mu aM resM
inline_for_extraction noextract
let mk_bn_mont_concrete_ops
(t:limb_t)
(k:BM.mont t)
(n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len)))
(mu:limb t{SM.bn_mont_pre n mu}) :
BE.concrete_ops t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len) =
{
BE.to = mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu;
BE.lone = bn_mont_one k n mu;
BE.lmul = bn_mont_mul k n mu;
BE.lsqr = bn_mont_sqr k n mu;
}
///////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
val mk_ctx:
#t:limb_t
-> len:BN.meta_len t
-> n:lbignum t len
-> r2:lbignum t len
-> ctx:lbignum t (len +! len) ->
Stack unit
(requires fun h ->
live h n /\ live h r2 /\ live h ctx /\
disjoint n ctx /\ disjoint r2 ctx /\
0 < bn_v h n /\ bn_v h r2 == pow2 (2 * bits t * v len) % bn_v h n)
(ensures fun h0 _ h1 -> modifies (loc ctx) h0 h1 /\
linv_ctx (as_seq h0 n) (as_seq h1 ctx))
let mk_ctx #t len n r2 ctx =
let h0 = ST.get () in
update_sub ctx 0ul len n;
let h1 = ST.get () in
assert (LSeq.sub (as_seq h1 ctx) 0 (v len) == as_seq h0 n);
update_sub ctx len len r2;
let h2 = ST.get () in
LSeq.eq_intro
(LSeq.sub (as_seq h2 ctx) 0 (v len))
(LSeq.sub (as_seq h1 ctx) 0 (v len));
assert (LSeq.sub (as_seq h2 ctx) 0 (v len) == as_seq h0 n);
assert (LSeq.sub (as_seq h2 ctx) (v len) (v len) == as_seq h0 r2)
noextract
let bn_exp_mont_pre
(#t:limb_t)
(#len:SN.bn_len t)
(n:BD.lbignum t len)
(mu:limb t)
(r2:BD.lbignum t len)
(aM:BD.lbignum t len)
(bBits:size_nat)
(b:BD.lbignum t (BD.blocks0 bBits (bits t)))
=
SM.bn_mont_pre n mu /\
BD.bn_v r2 == pow2 (2 * bits t * len) % BD.bn_v n /\
BD.bn_v b < pow2 bBits /\
BD.bn_v aM < BD.bn_v n
inline_for_extraction noextract
let bn_exp_mont_st (t:limb_t) (len:BN.meta_len t) =
n:lbignum t len
-> mu:limb t
-> r2:lbignum t len
-> aM:lbignum t len
-> bBits:size_t
-> b:lbignum t (blocks0 bBits (size (bits t)))
-> resM:lbignum t len ->
Stack unit
(requires fun h ->
live h n /\ live h aM /\ live h b /\ live h resM /\ live h r2 /\
disjoint resM aM /\ disjoint resM b /\ disjoint resM n /\ disjoint n aM /\
disjoint resM r2 /\ disjoint aM r2 /\ disjoint n r2 /\ disjoint aM b /\
bn_exp_mont_pre (as_seq h n) mu (as_seq h r2) (as_seq h aM) (v bBits) (as_seq h b))
(ensures fun h0 _ h1 -> modifies (loc aM |+| loc resM) h0 h1 /\
(let k1 = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu) in
bn_v h1 resM == LE.pow k1 (bn_v h0 aM) (bn_v h0 b)))
// This function is *NOT* constant-time on the exponent b.
inline_for_extraction noextract | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.NatMod.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Exponentiation.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Exponentiation.Lemmas.fst.checked",
"Hacl.Spec.Bignum.Montgomery.fsti.checked",
"Hacl.Spec.Bignum.MontExponentiation.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Spec.Bignum.fsti.checked",
"Hacl.Impl.Exponentiation.fsti.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Bignum.MontExponentiation.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontExponentiation",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Exponentiation.Lemmas",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Lib.Exponentiation",
"short_module": "LE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum",
"short_module": "SN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | k: Hacl.Bignum.Montgomery.mont t
-> Hacl.Bignum.MontExponentiation.bn_exp_mont_st t (Mkbn?.len (Mkmont?.bn k)) | Prims.Tot | [
"total"
] | [] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Hacl.Bignum.Definitions.limb",
"Lib.IntTypes.size_t",
"Hacl.Bignum.Definitions.blocks0",
"Lib.IntTypes.size",
"Lib.IntTypes.bits",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Lib.Exponentiation.exp_rl_lemma",
"Lib.NatMod.nat_mod",
"Hacl.Bignum.Definitions.bn_v",
"FStar.Ghost.reveal",
"Lib.Exponentiation.Definition.comm_monoid",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Hacl.Impl.Exponentiation.lexp_rl_vartime",
"Lib.IntTypes.op_Plus_Bang",
"Hacl.Bignum.MontExponentiation.mk_bn_mont_concrete_ops",
"FStar.Ghost.hide",
"Hacl.Spec.Bignum.Definitions.lbignum",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Bignum.MontExponentiation.mk_ctx",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.add",
"Lib.Buffer.create",
"Lib.IntTypes.uint",
"Lib.IntTypes.SEC",
"Lib.Buffer.lbuffer",
"FStar.Ghost.erased",
"Hacl.Spec.Exponentiation.Lemmas.mk_nat_mont_ll_comm_monoid",
"Lib.IntTypes.int_t",
"Prims.eq2",
"Prims.int",
"Prims.l_or",
"Lib.IntTypes.range",
"Prims.l_and",
"Prims.b2t",
"Prims.op_GreaterThan",
"Prims.op_LessThanOrEqual",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.op_Multiply",
"Lib.IntTypes.mk_int",
"Hacl.Spec.Bignum.Definitions.blocks0",
"Hacl.Bignum.meta_len",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"FStar.HyperStack.ST.push_frame"
] | [] | false | false | false | false | false | let bn_exp_mont_bm_vartime #t k n mu r2 aM bBits b resM =
| push_frame ();
let h0 = ST.get () in
[@@ inline_let ]let len = k.BM.bn.BN.len in
[@@ inline_let ]let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_rl_vartime len
(len +! len)
(mk_bn_mont_concrete_ops t k (as_seq h0 n) mu)
ctx
aM
bLen
bBits
b
resM;
LE.exp_rl_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame () | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.sortWith_sorted | val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l)) | val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l)) | let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 78,
"end_line": 675,
"start_col": 0,
"start_line": 665
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l)))) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> _: a -> Prims.int) -> l: Prims.list a
-> FStar.Pervasives.Lemma
(requires FStar.List.Tot.Properties.total_order (FStar.List.Tot.Base.bool_of_compare f))
(ensures
FStar.List.Tot.Properties.sorted (FStar.List.Tot.Base.bool_of_compare f)
(FStar.List.Tot.Base.sortWith f l) /\
(forall (x: a).
FStar.List.Tot.Base.mem x l =
FStar.List.Tot.Base.mem x (FStar.List.Tot.Base.sortWith f l)))
(decreases FStar.List.Tot.Base.length l) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.eqtype",
"Prims.int",
"Prims.list",
"FStar.List.Tot.Properties.append_sorted",
"FStar.List.Tot.Base.bool_of_compare",
"FStar.List.Tot.Base.sortWith",
"Prims.unit",
"FStar.List.Tot.Properties.append_mem_forall",
"Prims.Cons",
"FStar.List.Tot.Properties.sortWith_sorted",
"FStar.List.Tot.Properties.partition_mem_p_forall",
"FStar.List.Tot.Properties.partition_mem_forall",
"FStar.List.Tot.Base.partition_length",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Base.partition"
] | [
"recursion"
] | false | false | true | false | false | let rec sortWith_sorted #a f l =
| match l with
| [] -> ()
| pivot :: tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot :: sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.no_repeats_p_append_swap | val no_repeats_p_append_swap (#a: Type) (l1 l2: list a)
: Lemma (no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1)) | val no_repeats_p_append_swap (#a: Type) (l1 l2: list a)
: Lemma (no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1)) | let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 27,
"end_line": 777,
"start_col": 0,
"start_line": 771
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.no_repeats_p (l1 @ l2) <==> FStar.List.Tot.Base.no_repeats_p (l2 @ l1)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.no_repeats_p_append",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"FStar.List.Tot.Base.no_repeats_p",
"FStar.List.Tot.Base.append",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let no_repeats_p_append_swap (#a: Type) (l1 l2: list a)
: Lemma (no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1)) =
| no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.no_repeats_p_false_intro | val no_repeats_p_false_intro (#a: Type) (l1 l l2 l3: list a)
: Lemma (requires (Cons? l))
(ensures (~(no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3))))))) | val no_repeats_p_false_intro (#a: Type) (l1 l l2 l3: list a)
: Lemma (requires (Cons? l))
(ensures (~(no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3))))))) | let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 20,
"end_line": 810,
"start_col": 0,
"start_line": 799
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l: Prims.list a -> l2: Prims.list a -> l3: Prims.list a
-> FStar.Pervasives.Lemma (requires Cons? l)
(ensures ~(FStar.List.Tot.Base.no_repeats_p (l1 @ l @ l2 @ l @ l3))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_memP",
"Prims.unit",
"FStar.List.Tot.Base.append",
"FStar.List.Tot.Properties.no_repeats_p_append",
"Prims._assert",
"FStar.List.Tot.Base.memP",
"FStar.List.Tot.Base.hd",
"Prims.b2t",
"Prims.uu___is_Cons",
"Prims.squash",
"Prims.l_not",
"FStar.List.Tot.Base.no_repeats_p",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let no_repeats_p_false_intro (#a: Type) (l1 l l2 l3: list a)
: Lemma (requires (Cons? l))
(ensures (~(no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3))))))) =
| let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.fold_left_invar | val fold_left_invar (#a #b: Type) (f: (a -> b -> Tot a)) (l: list b) (p: (a -> Tot Type0))
: Lemma (requires forall (x: a) (y: b). p x ==> memP y l ==> p (f x y))
(ensures forall (x: a). p x ==> p (fold_left f x l)) | val fold_left_invar (#a #b: Type) (f: (a -> b -> Tot a)) (l: list b) (p: (a -> Tot Type0))
: Lemma (requires forall (x: a) (y: b). p x ==> memP y l ==> p (f x y))
(ensures forall (x: a). p x ==> p (fold_left f x l)) | let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 35,
"end_line": 926,
"start_col": 0,
"start_line": 915
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> _: b -> a) -> l: Prims.list b -> p: (_: a -> Type0)
-> FStar.Pervasives.Lemma
(requires forall (x: a) (y: b). p x ==> FStar.List.Tot.Base.memP y l ==> p (f x y))
(ensures forall (x: a). p x ==> p (FStar.List.Tot.Base.fold_left f x l)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.fold_left_invar",
"Prims.unit",
"Prims.l_Forall",
"Prims.l_imp",
"FStar.List.Tot.Base.memP",
"Prims.squash",
"FStar.List.Tot.Base.fold_left",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec fold_left_invar (#a #b: Type) (f: (a -> b -> Tot a)) (l: list b) (p: (a -> Tot Type0))
: Lemma (requires forall (x: a) (y: b). p x ==> memP y l ==> p (f x y))
(ensures forall (x: a). p x ==> p (fold_left f x l)) =
| match l with
| [] -> ()
| y :: q -> fold_left_invar f q p | false |
Hacl.Streaming.Blake2.Common.fst | Hacl.Streaming.Blake2.Common.max_input_length | val max_input_length (a: alg) : n: nat{n <= Spec.max_limb a /\ n > Spec.size_block a} | val max_input_length (a: alg) : n: nat{n <= Spec.max_limb a /\ n > Spec.size_block a} | let max_input_length (a : alg) : n:nat { n <= Spec.max_limb a /\ n > Spec.size_block a } =
assert_norm (pow2 64 < pow2 128);
pow2 64 - 1 | {
"file_name": "code/streaming/Hacl.Streaming.Blake2.Common.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 13,
"end_line": 226,
"start_col": 0,
"start_line": 224
} | module Hacl.Streaming.Blake2.Common
module HS = FStar.HyperStack
module B = LowStar.Buffer
module S = FStar.Seq
module LS = Lib.Sequence
module U32 = FStar.UInt32
module U64 = FStar.UInt64
module U128 = FStar.UInt128
module I = Hacl.Streaming.Interface
module ST = FStar.HyperStack.ST
open FStar.Mul
module Loops = Lib.LoopCombinators
/// Opening a bunch of modules for Blake2
/// =====================================
inline_for_extraction noextract
let uint8 = Lib.IntTypes.uint8
inline_for_extraction noextract
let uint32 = Lib.IntTypes.uint32
unfold noextract
let size_nat = Lib.IntTypes.size_nat
unfold noextract
let max_key = Spec.Blake2.max_key
unfold noextract
let lbytes = Lib.ByteSequence.lbytes
module Spec = Spec.Blake2
module Core = Hacl.Impl.Blake2.Core
open Hacl.Impl.Blake2.Generic
/// An instance of the stateful type class for blake2
/// =================================================
#set-options "--max_fuel 0 --max_ifuel 0 --z3rlimit 50"
inline_for_extraction noextract
let index = unit
inline_for_extraction noextract
let alg = Spec.alg
inline_for_extraction noextract
let m_spec = Core.m_spec
/// The stateful state: (wv, hash)
inline_for_extraction noextract
let s (a : alg) (m : m_spec) = Core.(state_p a m & state_p a m)
inline_for_extraction noextract
let t (a : alg) = Spec.state a
(* In the internal state, we keep wv, the working vector. It's essentially
temporary scratch space that the Blake2 implementation expects to receive. (Why
is the implementation not performing its own stack allocations? Don't know!) *)
inline_for_extraction noextract
let get_wv (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with wv, _ -> wv
inline_for_extraction noextract
let get_state_p (#a : alg) (#m : m_spec) (s : s a m) : Tot (Core.state_p a m) =
match s with _, p -> p
(* But the working vector is not reflected in the state at all -- it doesn't
have meaningful specification contents. *)
inline_for_extraction noextract
let state_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (Spec.state a) =
Core.state_v h (get_state_p s)
inline_for_extraction noextract
let s_v (#a : alg) (#m : m_spec) (h : HS.mem) (s : s a m) : GTot (t a) =
state_v h s
/// Small helper which facilitates inferencing implicit arguments for buffer
/// operations
inline_for_extraction noextract
let state_to_lbuffer (#a : alg) (#m : m_spec) (s : Core.state_p a m) :
B.lbuffer (Core.element_t a m) (4 * U32.v (Core.row_len a m)) =
s
inline_for_extraction noextract
let stateful_blake2 (a : alg) (m : m_spec) : I.stateful unit =
I.Stateful
(fun () -> s a m) (* s *)
(* footprint *)
(fun #_ _ acc ->
let wv, b = acc in
B.loc_union
(B.loc_addr_of_buffer (state_to_lbuffer wv))
(B.loc_addr_of_buffer (state_to_lbuffer b)))
(* freeable *)
(fun #_ _ acc ->
let wv, b = acc in
B.freeable (state_to_lbuffer wv) /\
B.freeable (state_to_lbuffer b))
(* invariant *)
(fun #_ h acc ->
let wv, b = acc in
B.live h (state_to_lbuffer wv) /\
B.live h (state_to_lbuffer b) /\
B.disjoint (state_to_lbuffer wv) (state_to_lbuffer b))
(fun () -> t a) (* t *)
(fun () h acc -> s_v h acc) (* v *)
(fun #_ h acc -> let wv, b = acc in ()) (* invariant_loc_in_footprint *)
(fun #_ l acc h0 h1 -> let wv, b = acc in ()) (* frame_invariant *)
(fun #_ _ _ _ _ -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
let wv = Core.alloc_state a m in
let b = Core.alloc_state a m in
wv, b)
(* create_in *)
(fun () r ->
let wv = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
let b = B.malloc r (Core.zero_element a m) U32.(4ul *^ Core.row_len a m) in
wv, b)
(* free *)
(fun _ acc ->
match acc with wv, b ->
B.free (state_to_lbuffer wv);
B.free (state_to_lbuffer b))
(* copy *)
(fun _ src dst ->
match src with src_wv, src_b ->
match dst with src_wv, dst_b ->
B.blit (state_to_lbuffer src_b) 0ul (state_to_lbuffer dst_b) 0ul
U32.(4ul *^ Core.row_len a m))
/// Stateful key
/// ============
inline_for_extraction noextract
let key_size (a : alg) = kk:nat{kk <= Spec.max_key a}
inline_for_extraction noextract
let key_size_t (a : alg) =
key_size:U32.t{U32.v key_size <= Spec.max_key a}
/// Defining stateful keys
inline_for_extraction noextract
let stateful_key_t (a : alg) (key_size : key_size a) : Type =
if key_size = 0 then unit else b:B.buffer uint8 { B.length b == key_size}
inline_for_extraction noextract
let buffer_to_stateful_key_t (a : alg) (kk : key_size a{kk > 0})
(k : B.buffer uint8 { B.length k == kk }) :
Tot (stateful_key_t a kk) =
k
inline_for_extraction noextract
let unit_to_stateful_key_t (a : alg) :
Tot (stateful_key_t a 0) =
()
/// The ``has_key`` parameter is meta
/// TODO: this definition could be moved to Hacl.Streaming.Interface, it could
/// be pretty useful in other situations as it generalizes ``stateful_buffer`` in
/// the case the length is zero. Note that rather than being unit, the type could
/// be buffer too (and we would use null whenever needed).
inline_for_extraction noextract
let stateful_key (a : alg) (kk : key_size a) :
I.stateful unit =
I.Stateful
(fun _ -> stateful_key_t a kk)
(* footprint *)
(fun #_ h s -> if kk = 0 then B.loc_none else B.loc_addr_of_buffer (s <: B.buffer uint8))
(* freeable *)
(fun #_ h s -> if kk = 0 then True else B.freeable (s <: B.buffer uint8))
(* invariant *)
(fun #_ h s ->
if kk = 0 then True
else B.live h (s <: B.buffer uint8))
(fun _ -> s:S.seq uint8 { S.length s == kk })
(fun _ h s -> if kk = 0 then Seq.empty else B.as_seq h (s <: B.buffer uint8))
(fun #_ h s -> ()) (* invariant_loc_in_footprint *)
(fun #_ l s h0 h1 -> ()) (* frame_invariant *)
(fun #_ l s h0 h1 -> ()) (* frame_freeable *)
(* alloca *)
(fun () ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.alloca (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* create_in *)
(fun () r ->
if kk > 0 then
buffer_to_stateful_key_t a kk (B.malloc r (Lib.IntTypes.u8 0) (U32.uint_to_t kk))
else unit_to_stateful_key_t a)
(* free *)
(fun _ s -> if kk > 0 then B.free (s <: B.buffer uint8) else ())
(* copy *)
(fun _ s_src s_dst ->
if kk > 0 then
B.blit (s_src <: B.buffer uint8) 0ul
(s_dst <: B.buffer uint8) 0ul (U32.uint_to_t kk)
else ())
inline_for_extraction noextract
let stateful_key_to_buffer (#a : alg) (#kk : key_size a)
(key : stateful_key_t a kk) :
b:B.buffer uint8 { B.length b = kk } =
if kk = 0 then B.null #uint8 else key
inline_for_extraction noextract
let k = stateful_key
/// Actual functor instantiation
/// ============================
/// Small helpers
/// ------------- | {
"checked_file": "/",
"dependencies": [
"Spec.Blake2.Alternative.fsti.checked",
"Spec.Blake2.fst.checked",
"prims.fst.checked",
"LowStar.Buffer.fst.checked",
"Lib.UpdateMulti.fst.checked",
"Lib.Sequence.Lemmas.fsti.checked",
"Lib.Sequence.fsti.checked",
"Lib.LoopCombinators.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.ByteSequence.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Streaming.Interface.fsti.checked",
"Hacl.Impl.Blake2.Generic.fst.checked",
"Hacl.Impl.Blake2.Core.fsti.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.UInt128.fsti.checked",
"FStar.Seq.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Streaming.Blake2.Common.fst"
} | [
{
"abbrev": false,
"full_module": "Hacl.Impl.Blake2.Generic",
"short_module": null
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Blake2.Core",
"short_module": "Core"
},
{
"abbrev": true,
"full_module": "Spec.Blake2",
"short_module": "Spec"
},
{
"abbrev": true,
"full_module": "Lib.LoopCombinators",
"short_module": "Loops"
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": true,
"full_module": "Hacl.Streaming.Interface",
"short_module": "I"
},
{
"abbrev": true,
"full_module": "FStar.UInt128",
"short_module": "U128"
},
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LS"
},
{
"abbrev": true,
"full_module": "FStar.Seq",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "LowStar.Buffer",
"short_module": "B"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Streaming.Blake2",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Hacl.Streaming.Blake2.Common.alg
-> n:
Prims.nat{n <= Spec.Blake2.Definitions.max_limb a /\ n > Spec.Blake2.Definitions.size_block a} | Prims.Tot | [
"total"
] | [] | [
"Hacl.Streaming.Blake2.Common.alg",
"Prims.op_Subtraction",
"Prims.pow2",
"Prims.unit",
"FStar.Pervasives.assert_norm",
"Prims.b2t",
"Prims.op_LessThan",
"Prims.nat",
"Prims.l_and",
"Prims.op_LessThanOrEqual",
"Spec.Blake2.Definitions.max_limb",
"Prims.op_GreaterThan",
"Spec.Blake2.Definitions.size_block"
] | [] | false | false | false | false | false | let max_input_length (a: alg) : n: nat{n <= Spec.max_limb a /\ n > Spec.size_block a} =
| assert_norm (pow2 64 < pow2 128);
pow2 64 - 1 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.no_repeats_p_append_permut | val no_repeats_p_append_permut (#a: Type) (l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==>
no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5))))) | val no_repeats_p_append_permut (#a: Type) (l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==>
no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5))))) | let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5)) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 54,
"end_line": 797,
"start_col": 0,
"start_line": 779
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a -> l3: Prims.list a -> l4: Prims.list a -> l5: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.no_repeats_p (l1 @ l2 @ l3 @ l4 @ l5) <==>
FStar.List.Tot.Base.no_repeats_p (l1 @ l4 @ l3 @ l2 @ l5)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.append_memP_forall",
"FStar.List.Tot.Base.append",
"Prims.unit",
"FStar.List.Tot.Properties.no_repeats_p_append",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"FStar.List.Tot.Base.no_repeats_p",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let no_repeats_p_append_permut (#a: Type) (l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==>
no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5))))) =
| no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5)) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.assoc_append_elim_r | val assoc_append_elim_r (#a: eqtype) (#b: Type) (x: a) (l1 l2: list (a * b))
: Lemma (requires (assoc x l2 == None \/ ~(assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1) | val assoc_append_elim_r (#a: eqtype) (#b: Type) (x: a) (l1 l2: list (a * b))
: Lemma (requires (assoc x l2 == None \/ ~(assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1) | let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 69,
"end_line": 854,
"start_col": 0,
"start_line": 843
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> l1: Prims.list (a * b) -> l2: Prims.list (a * b)
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.assoc x l2 == FStar.Pervasives.Native.None \/
~(FStar.List.Tot.Base.assoc x l1 == FStar.Pervasives.Native.None))
(ensures FStar.List.Tot.Base.assoc x (l1 @ l2) == FStar.List.Tot.Base.assoc x l1)
(decreases l1) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"Prims.op_Equality",
"Prims.bool",
"FStar.List.Tot.Properties.assoc_append_elim_r",
"Prims.unit",
"Prims.l_or",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.List.Tot.Base.assoc",
"FStar.Pervasives.Native.None",
"Prims.l_not",
"Prims.squash",
"FStar.List.Tot.Base.op_At",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec assoc_append_elim_r (#a: eqtype) (#b: Type) (x: a) (l1 l2: list (a * b))
: Lemma (requires (assoc x l2 == None \/ ~(assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1) =
| match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.assoc_append_elim_l | val assoc_append_elim_l (#a: eqtype) (#b: Type) (x: a) (l1 l2: list (a * b))
: Lemma (requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1) | val assoc_append_elim_l (#a: eqtype) (#b: Type) (x: a) (l1 l2: list (a * b))
: Lemma (requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1) | let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 79,
"end_line": 867,
"start_col": 0,
"start_line": 856
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> l1: Prims.list (a * b) -> l2: Prims.list (a * b)
-> FStar.Pervasives.Lemma
(requires FStar.List.Tot.Base.assoc x l1 == FStar.Pervasives.Native.None)
(ensures FStar.List.Tot.Base.assoc x (l1 @ l2) == FStar.List.Tot.Base.assoc x l2)
(decreases l1) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"Prims.op_Equality",
"Prims._assert",
"Prims.l_False",
"Prims.bool",
"FStar.List.Tot.Properties.assoc_append_elim_l",
"Prims.unit",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.List.Tot.Base.assoc",
"FStar.Pervasives.Native.None",
"Prims.squash",
"FStar.List.Tot.Base.op_At",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec assoc_append_elim_l (#a: eqtype) (#b: Type) (x: a) (l1 l2: list (a * b))
: Lemma (requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1) =
| match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.assoc_mem | val assoc_mem (#a: eqtype) (#b: Type) (x: a) (l: list (a * b))
: Lemma (ensures (mem x (map fst l) <==> (exists y. assoc x l == Some y))) | val assoc_mem (#a: eqtype) (#b: Type) (x: a) (l: list (a * b))
: Lemma (ensures (mem x (map fst l) <==> (exists y. assoc x l == Some y))) | let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 26,
"end_line": 911,
"start_col": 0,
"start_line": 896
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> l: Prims.list (a * b)
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.mem x (FStar.List.Tot.Base.map FStar.Pervasives.Native.fst l) <==>
(exists (y: b). FStar.List.Tot.Base.assoc x l == FStar.Pervasives.Native.Some y)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"FStar.List.Tot.Base.assoc",
"FStar.List.Tot.Properties.memP_map_elim",
"FStar.Pervasives.Native.fst",
"Prims.unit",
"FStar.List.Tot.Properties.mem_memP",
"FStar.List.Tot.Base.map",
"FStar.List.Tot.Properties.assoc_memP_none",
"FStar.List.Tot.Properties.memP_map_intro",
"FStar.Pervasives.Native.Mktuple2",
"FStar.List.Tot.Properties.assoc_memP_some",
"Prims.l_True",
"Prims.squash",
"Prims.l_iff",
"Prims.b2t",
"FStar.List.Tot.Base.mem",
"Prims.l_Exists",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.Some",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let assoc_mem (#a: eqtype) (#b: Type) (x: a) (l: list (a * b))
: Lemma (ensures (mem x (map fst l) <==> (exists y. assoc x l == Some y))) =
| match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.no_repeats_p_append_intro | val no_repeats_p_append_intro (#a: Type) (l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x. memP x l1 ==> ~(memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1) | val no_repeats_p_append_intro (#a: Type) (l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x. memP x l1 ==> ~(memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1) | let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 35,
"end_line": 759,
"start_col": 0,
"start_line": 748
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.no_repeats_p l1 /\ FStar.List.Tot.Base.no_repeats_p l2 /\
(forall (x: a). FStar.List.Tot.Base.memP x l1 ==> ~(FStar.List.Tot.Base.memP x l2)))
(ensures FStar.List.Tot.Base.no_repeats_p (l1 @ l2))
(decreases l1) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.no_repeats_p_append_intro",
"Prims.unit",
"FStar.List.Tot.Properties.append_memP",
"Prims.l_and",
"FStar.List.Tot.Base.no_repeats_p",
"Prims.l_Forall",
"Prims.l_imp",
"FStar.List.Tot.Base.memP",
"Prims.l_not",
"Prims.squash",
"FStar.List.Tot.Base.append",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec no_repeats_p_append_intro (#a: Type) (l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x. memP x l1 ==> ~(memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1) =
| match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.assoc_memP_none | val assoc_memP_none (#a: eqtype) (#b: Type) (x: a) (l: list (a * b))
: Lemma (requires (assoc x l == None)) (ensures (forall y. ~(memP (x, y) l))) (decreases l) | val assoc_memP_none (#a: eqtype) (#b: Type) (x: a) (l: list (a * b))
: Lemma (requires (assoc x l == None)) (ensures (forall y. ~(memP (x, y) l))) (decreases l) | let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 72,
"end_line": 894,
"start_col": 0,
"start_line": 883
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> l: Prims.list (a * b)
-> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.assoc x l == FStar.Pervasives.Native.None)
(ensures forall (y: b). ~(FStar.List.Tot.Base.memP (x, y) l))
(decreases l) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"Prims.op_Equality",
"Prims._assert",
"Prims.l_False",
"Prims.bool",
"FStar.List.Tot.Properties.assoc_memP_none",
"Prims.unit",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.List.Tot.Base.assoc",
"FStar.Pervasives.Native.None",
"Prims.squash",
"Prims.l_Forall",
"Prims.l_not",
"FStar.List.Tot.Base.memP",
"FStar.Pervasives.Native.Mktuple2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec assoc_memP_none (#a: eqtype) (#b: Type) (x: a) (l: list (a * b))
: Lemma (requires (assoc x l == None)) (ensures (forall y. ~(memP (x, y) l))) (decreases l) =
| match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.index_extensionality | val index_extensionality (#a: Type) (l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\ (forall (i: nat). i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2)) | val index_extensionality (#a: Type) (l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\ (forall (i: nat). i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2)) | let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ()) | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 49,
"end_line": 1023,
"start_col": 0,
"start_line": 1015
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> () | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(requires
FStar.List.Tot.Base.length l1 == FStar.List.Tot.Base.length l2 /\
(forall (i: Prims.nat).
i < FStar.List.Tot.Base.length l1 ==>
FStar.List.Tot.Base.index l1 i == FStar.List.Tot.Base.index l2 i)) (ensures l1 == l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.index_extensionality_aux",
"Prims.nat",
"Prims.b2t",
"Prims.op_LessThan",
"FStar.List.Tot.Base.length",
"Prims.unit",
"Prims.eq2",
"FStar.List.Tot.Base.index",
"Prims.l_and",
"Prims.l_Forall",
"Prims.l_imp",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let index_extensionality (#a: Type) (l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\ (forall (i: nat). i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2)) =
| index_extensionality_aux l1 l2 () (fun i -> ()) | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.assoc_memP_some | val assoc_memP_some (#a: eqtype) (#b: Type) (x: a) (y: b) (l: list (a * b))
: Lemma (requires (assoc x l == Some y)) (ensures (memP (x, y) l)) (decreases l) | val assoc_memP_some (#a: eqtype) (#b: Type) (x: a) (y: b) (l: list (a * b))
: Lemma (requires (assoc x l == Some y)) (ensures (memP (x, y) l)) (decreases l) | let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 64,
"end_line": 881,
"start_col": 0,
"start_line": 869
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> y: b -> l: Prims.list (a * b)
-> FStar.Pervasives.Lemma
(requires FStar.List.Tot.Base.assoc x l == FStar.Pervasives.Native.Some y)
(ensures FStar.List.Tot.Base.memP (x, y) l)
(decreases l) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"Prims.op_Equality",
"Prims.bool",
"FStar.List.Tot.Properties.assoc_memP_some",
"Prims.unit",
"Prims.eq2",
"FStar.Pervasives.Native.option",
"FStar.List.Tot.Base.assoc",
"FStar.Pervasives.Native.Some",
"Prims.squash",
"FStar.List.Tot.Base.memP",
"FStar.Pervasives.Native.Mktuple2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec assoc_memP_some (#a: eqtype) (#b: Type) (x: a) (y: b) (l: list (a * b))
: Lemma (requires (assoc x l == Some y)) (ensures (memP (x, y) l)) (decreases l) =
| match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q | false |
Hacl.Bignum.MontExponentiation.fst | Hacl.Bignum.MontExponentiation.bn_exp_mont_fw_consttime | val bn_exp_mont_fw_consttime:
#t:limb_t
-> k:BM.mont t
-> l:size_t{0 < v l /\ v l < bits U32 /\ pow2 (v l) * v k.BM.bn.BN.len <= max_size_t} ->
bn_exp_mont_st t k.BM.bn.BN.len | val bn_exp_mont_fw_consttime:
#t:limb_t
-> k:BM.mont t
-> l:size_t{0 < v l /\ v l < bits U32 /\ pow2 (v l) * v k.BM.bn.BN.len <= max_size_t} ->
bn_exp_mont_st t k.BM.bn.BN.len | let bn_exp_mont_fw_consttime #t k l n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_fw_consttime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) l ctx aM bLen bBits b resM;
LE.exp_fw_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b) (v l);
pop_frame () | {
"file_name": "code/bignum/Hacl.Bignum.MontExponentiation.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 14,
"end_line": 278,
"start_col": 0,
"start_line": 267
} | module Hacl.Bignum.MontExponentiation
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.Mul
open Lib.IntTypes
open Lib.Buffer
open Hacl.Bignum.Definitions
module ST = FStar.HyperStack.ST
module LSeq = Lib.Sequence
module BD = Hacl.Spec.Bignum.Definitions
module SN = Hacl.Spec.Bignum
module SM = Hacl.Spec.Bignum.Montgomery
module BN = Hacl.Bignum
module BM = Hacl.Bignum.Montgomery
module LE = Lib.Exponentiation
module BE = Hacl.Impl.Exponentiation
module E = Hacl.Spec.Exponentiation.Lemmas
module S = Hacl.Spec.Bignum.MontExponentiation
#reset-options "--z3rlimit 50 --fuel 0 --ifuel 0"
// All operations are performed in the Montgomery domain!
inline_for_extraction noextract
let a_spec (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len{0 < BD.bn_v n}) =
Lib.NatMod.nat_mod (BD.bn_v n)
inline_for_extraction noextract
let linv (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len) : Type0 =
BD.bn_v a < BD.bn_v n
inline_for_extraction noextract
let refl (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (a:BD.lbignum t len{linv n a}) : a_spec n =
BD.bn_v a
inline_for_extraction noextract
let linv_ctx (#t:limb_t) (#len:SN.bn_len t) (n:BD.lbignum t len) (ctx:BD.lbignum t (len + len)) : Type0 =
let ctx_n = LSeq.sub ctx 0 len in
let ctx_r2 = LSeq.sub ctx len len in
ctx_n == n /\
0 < BD.bn_v n /\ BD.bn_v ctx_r2 = pow2 (2 * bits t * len) % BD.bn_v n
inline_for_extraction noextract
let mk_to_nat_mont_ll_comm_monoid
(t:limb_t)
(len:BN.meta_len t)
(n:BD.lbignum t (v len))
(mu:limb t{SM.bn_mont_pre n mu})
: BE.to_comm_monoid t len (len +! len) =
{
BE.a_spec = a_spec n;
BE.comm_monoid = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (BD.bn_v n) (v mu);
BE.linv_ctx = linv_ctx n;
BE.linv = linv n;
BE.refl = refl n;
}
inline_for_extraction noextract
val bn_mont_one:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lone_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_one #t k n mu ctx oneM =
[@inline_let] let len = k.BM.bn.BN.len in
let ctx_n = sub ctx 0ul len in
let ctx_r2 = sub ctx len len in
let h0 = ST.get () in
SM.bn_mont_one_lemma n mu (as_seq h0 ctx_r2);
BM.bn_mont_one len k.BM.from ctx_n mu ctx_r2 oneM
inline_for_extraction noextract
val bn_mont_mul:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lmul_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_mul #t k n mu ctx aM bM resM =
let h0 = ST.get () in
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 bM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.mul ctx_n mu aM bM resM
inline_for_extraction noextract
val bn_mont_sqr:
#t:limb_t
-> k:BM.mont t
-> n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len))
-> mu:limb t{SM.bn_mont_pre n mu} ->
BE.lsqr_st t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len)
(mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu)
let bn_mont_sqr #t k n mu ctx aM resM =
let h0 = ST.get () in
SM.bn_mont_sqr_lemma n mu (as_seq h0 aM);
SM.bn_mont_mul_lemma n mu (as_seq h0 aM) (as_seq h0 aM);
let ctx_n = sub ctx 0ul k.BM.bn.BN.len in
k.BM.sqr ctx_n mu aM resM
inline_for_extraction noextract
let mk_bn_mont_concrete_ops
(t:limb_t)
(k:BM.mont t)
(n:Ghost.erased (BD.lbignum t (v k.BM.bn.BN.len)))
(mu:limb t{SM.bn_mont_pre n mu}) :
BE.concrete_ops t k.BM.bn.BN.len (k.BM.bn.BN.len +! k.BM.bn.BN.len) =
{
BE.to = mk_to_nat_mont_ll_comm_monoid t k.BM.bn.BN.len n mu;
BE.lone = bn_mont_one k n mu;
BE.lmul = bn_mont_mul k n mu;
BE.lsqr = bn_mont_sqr k n mu;
}
///////////////////////////////////////////////////////////////////////
inline_for_extraction noextract
val mk_ctx:
#t:limb_t
-> len:BN.meta_len t
-> n:lbignum t len
-> r2:lbignum t len
-> ctx:lbignum t (len +! len) ->
Stack unit
(requires fun h ->
live h n /\ live h r2 /\ live h ctx /\
disjoint n ctx /\ disjoint r2 ctx /\
0 < bn_v h n /\ bn_v h r2 == pow2 (2 * bits t * v len) % bn_v h n)
(ensures fun h0 _ h1 -> modifies (loc ctx) h0 h1 /\
linv_ctx (as_seq h0 n) (as_seq h1 ctx))
let mk_ctx #t len n r2 ctx =
let h0 = ST.get () in
update_sub ctx 0ul len n;
let h1 = ST.get () in
assert (LSeq.sub (as_seq h1 ctx) 0 (v len) == as_seq h0 n);
update_sub ctx len len r2;
let h2 = ST.get () in
LSeq.eq_intro
(LSeq.sub (as_seq h2 ctx) 0 (v len))
(LSeq.sub (as_seq h1 ctx) 0 (v len));
assert (LSeq.sub (as_seq h2 ctx) 0 (v len) == as_seq h0 n);
assert (LSeq.sub (as_seq h2 ctx) (v len) (v len) == as_seq h0 r2)
noextract
let bn_exp_mont_pre
(#t:limb_t)
(#len:SN.bn_len t)
(n:BD.lbignum t len)
(mu:limb t)
(r2:BD.lbignum t len)
(aM:BD.lbignum t len)
(bBits:size_nat)
(b:BD.lbignum t (BD.blocks0 bBits (bits t)))
=
SM.bn_mont_pre n mu /\
BD.bn_v r2 == pow2 (2 * bits t * len) % BD.bn_v n /\
BD.bn_v b < pow2 bBits /\
BD.bn_v aM < BD.bn_v n
inline_for_extraction noextract
let bn_exp_mont_st (t:limb_t) (len:BN.meta_len t) =
n:lbignum t len
-> mu:limb t
-> r2:lbignum t len
-> aM:lbignum t len
-> bBits:size_t
-> b:lbignum t (blocks0 bBits (size (bits t)))
-> resM:lbignum t len ->
Stack unit
(requires fun h ->
live h n /\ live h aM /\ live h b /\ live h resM /\ live h r2 /\
disjoint resM aM /\ disjoint resM b /\ disjoint resM n /\ disjoint n aM /\
disjoint resM r2 /\ disjoint aM r2 /\ disjoint n r2 /\ disjoint aM b /\
bn_exp_mont_pre (as_seq h n) mu (as_seq h r2) (as_seq h aM) (v bBits) (as_seq h b))
(ensures fun h0 _ h1 -> modifies (loc aM |+| loc resM) h0 h1 /\
(let k1 = E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu) in
bn_v h1 resM == LE.pow k1 (bn_v h0 aM) (bn_v h0 b)))
// This function is *NOT* constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_bm_vartime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_bm_vartime #t k n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
[@inline_let] let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_rl_vartime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) ctx aM bLen bBits b resM;
LE.exp_rl_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame ()
// This function is constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_bm_consttime: #t:limb_t -> k:BM.mont t -> bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_bm_consttime #t k n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
[@inline_let] let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_mont_ladder_swap_consttime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) ctx aM bLen bBits b resM;
LE.exp_mont_ladder_swap_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
LE.exp_mont_ladder_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b);
pop_frame ()
// This function is *NOT* constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_fw_vartime:
#t:limb_t
-> k:BM.mont t
-> l:size_t{0 < v l /\ v l < bits U32 /\ pow2 (v l) * v k.BM.bn.BN.len <= max_size_t} ->
bn_exp_mont_st t k.BM.bn.BN.len
let bn_exp_mont_fw_vartime #t k l n mu r2 aM bBits b resM =
push_frame ();
let h0 = ST.get () in
[@inline_let] let len = k.BM.bn.BN.len in
let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_fw_vartime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) l ctx aM bLen bBits b resM;
LE.exp_fw_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b) (v l);
pop_frame ()
// This function is constant-time on the exponent b.
inline_for_extraction noextract
val bn_exp_mont_fw_consttime:
#t:limb_t
-> k:BM.mont t
-> l:size_t{0 < v l /\ v l < bits U32 /\ pow2 (v l) * v k.BM.bn.BN.len <= max_size_t} ->
bn_exp_mont_st t k.BM.bn.BN.len | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"Lib.Sequence.fsti.checked",
"Lib.NatMod.fsti.checked",
"Lib.IntTypes.fsti.checked",
"Lib.Exponentiation.fsti.checked",
"Lib.Buffer.fsti.checked",
"Hacl.Spec.Exponentiation.Lemmas.fst.checked",
"Hacl.Spec.Bignum.Montgomery.fsti.checked",
"Hacl.Spec.Bignum.MontExponentiation.fst.checked",
"Hacl.Spec.Bignum.Definitions.fst.checked",
"Hacl.Spec.Bignum.fsti.checked",
"Hacl.Impl.Exponentiation.fsti.checked",
"Hacl.Bignum.Montgomery.fsti.checked",
"Hacl.Bignum.Definitions.fst.checked",
"Hacl.Bignum.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked",
"FStar.Ghost.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Bignum.MontExponentiation.fst"
} | [
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.MontExponentiation",
"short_module": "S"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Exponentiation.Lemmas",
"short_module": "E"
},
{
"abbrev": true,
"full_module": "Hacl.Impl.Exponentiation",
"short_module": "BE"
},
{
"abbrev": true,
"full_module": "Lib.Exponentiation",
"short_module": "LE"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum.Montgomery",
"short_module": "BM"
},
{
"abbrev": true,
"full_module": "Hacl.Bignum",
"short_module": "BN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Montgomery",
"short_module": "SM"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum",
"short_module": "SN"
},
{
"abbrev": true,
"full_module": "Hacl.Spec.Bignum.Definitions",
"short_module": "BD"
},
{
"abbrev": true,
"full_module": "Lib.Sequence",
"short_module": "LSeq"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "ST"
},
{
"abbrev": false,
"full_module": "Hacl.Bignum.Definitions",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.Buffer",
"short_module": null
},
{
"abbrev": false,
"full_module": "Lib.IntTypes",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack.ST",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.HyperStack",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Bignum",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
k: Hacl.Bignum.Montgomery.mont t ->
l:
Lib.IntTypes.size_t
{ 0 < Lib.IntTypes.v l /\ Lib.IntTypes.v l < Lib.IntTypes.bits Lib.IntTypes.U32 /\
Prims.pow2 (Lib.IntTypes.v l) * Lib.IntTypes.v (Mkbn?.len (Mkmont?.bn k)) <=
Lib.IntTypes.max_size_t }
-> Hacl.Bignum.MontExponentiation.bn_exp_mont_st t (Mkbn?.len (Mkmont?.bn k)) | Prims.Tot | [
"total"
] | [] | [
"Hacl.Bignum.Definitions.limb_t",
"Hacl.Bignum.Montgomery.mont",
"Lib.IntTypes.size_t",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThan",
"Lib.IntTypes.v",
"Lib.IntTypes.U32",
"Lib.IntTypes.PUB",
"Lib.IntTypes.bits",
"Prims.op_LessThanOrEqual",
"FStar.Mul.op_Star",
"Prims.pow2",
"Hacl.Bignum.__proj__Mkbn__item__len",
"Hacl.Bignum.Montgomery.__proj__Mkmont__item__bn",
"Lib.IntTypes.max_size_t",
"Hacl.Bignum.Definitions.lbignum",
"Hacl.Bignum.Definitions.limb",
"Hacl.Bignum.Definitions.blocks0",
"Lib.IntTypes.size",
"FStar.HyperStack.ST.pop_frame",
"Prims.unit",
"Lib.Exponentiation.exp_fw_lemma",
"Lib.NatMod.nat_mod",
"Hacl.Bignum.Definitions.bn_v",
"FStar.Ghost.reveal",
"Lib.Exponentiation.Definition.comm_monoid",
"Hacl.Impl.Exponentiation.lexp_fw_consttime",
"Lib.IntTypes.op_Plus_Bang",
"Hacl.Bignum.MontExponentiation.mk_bn_mont_concrete_ops",
"FStar.Ghost.hide",
"Hacl.Spec.Bignum.Definitions.lbignum",
"Lib.Buffer.as_seq",
"Lib.Buffer.MUT",
"Hacl.Bignum.MontExponentiation.mk_ctx",
"Lib.Buffer.lbuffer_t",
"Lib.IntTypes.add",
"Lib.Buffer.create",
"Lib.IntTypes.uint",
"Lib.IntTypes.SEC",
"Lib.Buffer.lbuffer",
"FStar.Ghost.erased",
"Hacl.Spec.Exponentiation.Lemmas.mk_nat_mont_ll_comm_monoid",
"Lib.IntTypes.int_t",
"Prims.eq2",
"Prims.int",
"Prims.l_or",
"Lib.IntTypes.range",
"Prims.op_GreaterThan",
"Prims.op_Subtraction",
"Prims.op_Multiply",
"Lib.IntTypes.mk_int",
"Hacl.Spec.Bignum.Definitions.blocks0",
"Hacl.Bignum.meta_len",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"FStar.HyperStack.ST.push_frame"
] | [] | false | false | false | false | false | let bn_exp_mont_fw_consttime #t k l n mu r2 aM bBits b resM =
| push_frame ();
let h0 = ST.get () in
[@@ inline_let ]let len = k.BM.bn.BN.len in
let bLen = blocks0 bBits (size (bits t)) in
let k1 = Ghost.hide (E.mk_nat_mont_ll_comm_monoid (bits t) (v len) (bn_v h0 n) (v mu)) in
let ctx = create (len +! len) (uint #t #SEC 0) in
mk_ctx #t len n r2 ctx;
BE.lexp_fw_consttime len (len +! len) (mk_bn_mont_concrete_ops t k (as_seq h0 n) mu) l ctx aM bLen
bBits b resM;
LE.exp_fw_lemma k1 (bn_v h0 aM) (v bBits) (bn_v h0 b) (v l);
pop_frame () | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.strict_suffix_of_nil | val strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma (requires True) (ensures (strict_suffix_of [] (x :: l))) (decreases l) | val strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma (requires True) (ensures (strict_suffix_of [] (x :: l))) (decreases l) | let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma
(requires True)
(ensures (strict_suffix_of [] (x::l)))
(decreases l)
= match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 40,
"end_line": 1034,
"start_col": 0,
"start_line": 1027
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q
let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2
(* Properties of [index] *)
private let rec index_extensionality_aux
(#a: Type)
(l1 l2: list a)
(l_len: (l_len: unit { length l1 == length l2 } ))
(l_index: (i: (i: nat {i < length l1})) -> Tot (l_index: unit {index l1 i == index l2 i}))
: Lemma
(ensures (l1 == l2))
= match (l1, l2) with
| (a1::q1, a2::q2) ->
let a_eq : (a_eq : unit {a1 == a2}) = l_index 0 in
let q_len : (q_len: unit {length q1 == length q2}) = () in
let q_index (i: (i: nat {i < length q1})) : Tot (q_index: unit {index q1 i == index q2 i}) =
l_index (i + 1) in
let q_eq : (q_eq : unit {l1 == l2}) = index_extensionality_aux q1 q2 q_len q_index in
()
| _ -> ()
let index_extensionality
(#a: Type)
(l1 l2: list a)
: Lemma
(requires
(length l1 == length l2 /\
(forall (i: nat) . i < length l1 ==> index l1 i == index l2 i)))
(ensures (l1 == l2))
= index_extensionality_aux l1 l2 () (fun i -> ())
(** Properties of [strict_suffix_of] *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> l: Prims.list a
-> FStar.Pervasives.Lemma (ensures FStar.List.Tot.Base.strict_suffix_of [] (x :: l)) (decreases l) | FStar.Pervasives.Lemma | [
"lemma",
""
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.strict_suffix_of_nil",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"FStar.List.Tot.Base.strict_suffix_of",
"Prims.Nil",
"Prims.Cons",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec strict_suffix_of_nil (#a: Type) (x: a) (l: list a)
: Lemma (requires True) (ensures (strict_suffix_of [] (x :: l))) (decreases l) =
| match l with
| [] -> ()
| a' :: q -> strict_suffix_of_nil a' q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.fold_left_append_monoid | val fold_left_append_monoid (#a: Type) (f: (a -> a -> Tot a)) (z: a) (l1 l2: list a)
: Lemma
(requires
(forall u v w. f u (f v w) == f (f u v) w) /\ (forall x. f x z == x) /\
(forall x. f z x == x))
(ensures fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2)) | val fold_left_append_monoid (#a: Type) (f: (a -> a -> Tot a)) (z: a) (l1 l2: list a)
: Lemma
(requires
(forall u v w. f u (f v w) == f (f u v) w) /\ (forall x. f x z == x) /\
(forall x. f z x == x))
(ensures fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2)) | let fold_left_append_monoid
(#a: Type)
(f: (a -> a -> Tot a))
(z: a)
(l1 l2: list a)
: Lemma
(requires
(forall u v w . f u (f v w) == f (f u v) w) /\
(forall x . f x z == x) /\
(forall x . f z x == x))
(ensures
fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2))
= fold_left_append f l1 l2;
fold_left_monoid f z l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 25,
"end_line": 994,
"start_col": 0,
"start_line": 981
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2
let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> _: a -> a) -> z: a -> l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(requires
(forall (u905: a) (v: a) (w: a). f u905 (f v w) == f (f u905 v) w) /\
(forall (x: a). f x z == x) /\ (forall (x: a). f z x == x))
(ensures
FStar.List.Tot.Base.fold_left f z (l1 @ l2) ==
f (FStar.List.Tot.Base.fold_left f z l1) (FStar.List.Tot.Base.fold_left f z l2)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.fold_left_monoid",
"Prims.unit",
"FStar.List.Tot.Properties.fold_left_append",
"Prims.l_and",
"Prims.l_Forall",
"Prims.eq2",
"Prims.squash",
"FStar.List.Tot.Base.fold_left",
"FStar.List.Tot.Base.op_At",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let fold_left_append_monoid (#a: Type) (f: (a -> a -> Tot a)) (z: a) (l1 l2: list a)
: Lemma
(requires
(forall u v w. f u (f v w) == f (f u v) w) /\ (forall x. f x z == x) /\
(forall x. f z x == x))
(ensures fold_left f z (l1 @ l2) == f (fold_left f z l1) (fold_left f z l2)) =
| fold_left_append f l1 l2;
fold_left_monoid f z l2 | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.fold_left_monoid | val fold_left_monoid (#a: Type) (opA: (a -> a -> Tot a)) (zeroA: a) (l: list a)
: Lemma
(requires
(forall u v w. (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x. (x `opA` zeroA) == x) /\ (forall x. (zeroA `opA` x) == x))
(ensures forall x. (fold_left opA x l) == (x `opA` (fold_left opA zeroA l))) | val fold_left_monoid (#a: Type) (opA: (a -> a -> Tot a)) (zeroA: a) (l: list a)
: Lemma
(requires
(forall u v w. (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x. (x `opA` zeroA) == x) /\ (forall x. (zeroA `opA` x) == x))
(ensures forall x. (fold_left opA x l) == (x `opA` (fold_left opA zeroA l))) | let rec fold_left_monoid
(#a: Type)
(opA: (a -> a -> Tot a))
(zeroA: a)
(l: list a)
: Lemma
(requires
(forall u v w . (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x . (x `opA` zeroA) == x) /\
(forall x . (zeroA `opA` x) == x))
(ensures
forall x .
(fold_left opA x l) == (x `opA` (fold_left opA zeroA l)))
= match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 42,
"end_line": 979,
"start_col": 0,
"start_line": 964
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q
let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2
let rec fold_left_append
(#a #b: Type)
(f: a -> b -> Tot a)
(l1 l2: list b)
: Lemma
(ensures forall x . fold_left f x (l1 @ l2) == fold_left f (fold_left f x l1) l2)
= match l1 with
| [] -> ()
| x :: q -> fold_left_append f q l2 | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | opA: (_: a -> _: a -> a) -> zeroA: a -> l: Prims.list a
-> FStar.Pervasives.Lemma
(requires
(forall (u890: a) (v: a) (w: a). opA u890 (opA v w) == opA (opA u890 v) w) /\
(forall (x: a). opA x zeroA == x) /\ (forall (x: a). opA zeroA x == x))
(ensures
forall (x: a).
FStar.List.Tot.Base.fold_left opA x l == opA x (FStar.List.Tot.Base.fold_left opA zeroA l)
) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.fold_left_monoid",
"Prims.unit",
"Prims.l_and",
"Prims.l_Forall",
"Prims.eq2",
"Prims.squash",
"FStar.List.Tot.Base.fold_left",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec fold_left_monoid (#a: Type) (opA: (a -> a -> Tot a)) (zeroA: a) (l: list a)
: Lemma
(requires
(forall u v w. (u `opA` (v `opA` w)) == ((u `opA` v) `opA` w)) /\
(forall x. (x `opA` zeroA) == x) /\ (forall x. (zeroA `opA` x) == x))
(ensures forall x. (fold_left opA x l) == (x `opA` (fold_left opA zeroA l))) =
| match l with
| [] -> ()
| x :: q -> fold_left_monoid opA zeroA q | false |
FStar.List.Tot.Properties.fst | FStar.List.Tot.Properties.map_append | val map_append (#a #b: Type) (f: (a -> Tot b)) (l1 l2: list a)
: Lemma (ensures map f (l1 @ l2) == map f l1 @ map f l2) | val map_append (#a #b: Type) (f: (a -> Tot b)) (l1 l2: list a)
: Lemma (ensures map f (l1 @ l2) == map f l1 @ map f l2) | let rec map_append
(#a #b: Type)
(f: a -> Tot b)
(l1 l2: list a)
:
Lemma
(ensures map f (l1 @ l2) == map f l1 @ map f l2)
=
match l1 with
| [] -> ()
| x :: q -> map_append f q l2 | {
"file_name": "ulib/FStar.List.Tot.Properties.fst",
"git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3",
"git_url": "https://github.com/FStarLang/FStar.git",
"project_name": "FStar"
} | {
"end_col": 31,
"end_line": 952,
"start_col": 0,
"start_line": 942
} | (*
Copyright 2008-2014 Nikhil Swamy and Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
(**
This module states and proves some properties about pure and total
operations on lists.
@summary Properties of pure total operations on lists
*)
module FStar.List.Tot.Properties
open FStar.List.Tot.Base
(** A list indexed by its length **)
let llist a (n:nat) = l:list a {length l = n}
(** Properties about mem **)
(** Correctness of [mem] for types with decidable equality. TODO:
replace [mem] with [memP] in relevant lemmas and define the right
SMTPat to automatically recover lemmas about [mem] for types with
decidable equality *)
let rec mem_memP
(#a: eqtype)
(x: a)
(l: list a)
: Lemma (ensures (mem x l <==> memP x l))
[SMTPat (mem x l); SMTPat (memP x l)]
= match l with
| [] -> ()
| a :: q -> mem_memP x q
(** If an element can be [index]ed, then it is a [memP] of the list. *)
let rec lemma_index_memP (#t:Type) (l:list t) (i:nat{i < length l}) :
Lemma
(ensures (index l i `memP` l))
[SMTPat (index l i `memP` l)] =
match i with
| 0 -> ()
| _ -> lemma_index_memP (tl l) (i - 1)
(** The empty list has no elements. *)
val memP_empty : #a: Type -> x:a ->
Lemma (requires (memP x []))
(ensures False)
let memP_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val memP_existsb: #a: Type -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ memP x xs))))
let rec memP_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> memP_existsb f tl
let rec memP_map_intro
(#a #b: Type)
(f: a -> Tot b)
(x: a)
(l: list a)
: Lemma
(requires True)
(ensures (memP x l ==> memP (f x) (map f l)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_intro f x q (* NOTE: would fail if [requires memP x l] instead of [ ==> ] *)
let rec memP_map_elim
(#a #b: Type)
(f: a -> Tot b)
(y: b)
(l: list a)
: Lemma
(requires True)
(ensures (memP y (map f l) ==> (exists (x : a) . memP x l /\ f x == y)))
(decreases l)
= match l with
| [] -> ()
| _ :: q -> memP_map_elim f y q
(** The empty list has no elements *)
val mem_empty : #a:eqtype -> x:a ->
Lemma (requires (mem x []))
(ensures False)
let mem_empty #a x = ()
(** Full specification for [existsb]: [existsb f xs] holds if, and
only if, there exists an element [x] of [xs] such that [f x] holds. *)
val mem_existsb: #a:eqtype -> f:(a -> Tot bool) -> xs:list a ->
Lemma(ensures (existsb f xs <==> (exists (x:a). (f x = true /\ mem x xs))))
let rec mem_existsb #a f xs =
match xs with
| [] -> ()
| hd::tl -> mem_existsb f tl
let rec mem_count
(#a: eqtype)
(l: list a)
(x: a)
: Lemma
(mem x l <==> count x l > 0)
= match l with
| [] -> ()
| x' :: l' -> mem_count l' x
(** Properties about rev **)
val rev_acc_length : l:list 'a -> acc:list 'a ->
Lemma (requires True)
(ensures (length (rev_acc l acc) = length l + length acc))
let rec rev_acc_length l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_length tl (hd::acc)
val rev_length : l:list 'a ->
Lemma (requires True)
(ensures (length (rev l) = length l))
let rev_length l = rev_acc_length l []
val rev_acc_memP : #a:Type -> l:list a -> acc:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev_acc l acc) <==> (memP x l \/ memP x acc)))
let rec rev_acc_memP #a l acc x = match l with
| [] -> ()
| hd::tl -> rev_acc_memP tl (hd::acc) x
(** A list and its reversed have the same elements *)
val rev_memP : #a:Type -> l:list a -> x:a ->
Lemma (requires True)
(ensures (memP x (rev l) <==> memP x l))
let rev_memP #a l x = rev_acc_memP l [] x
val rev_mem : #a:eqtype -> l:list a -> x:a ->
Lemma (requires True)
(ensures (mem x (rev l) <==> mem x l))
let rev_mem l x = rev_memP l x
(** Properties about append **)
val append_nil_l: l:list 'a ->
Lemma (requires True)
(ensures ([]@l == l))
let append_nil_l l = ()
val append_l_nil: l:list 'a ->
Lemma (requires True)
(ensures (l@[] == l)) [SMTPat (l@[])]
let rec append_l_nil = function
| [] -> ()
| hd::tl -> append_l_nil tl
val append_cons_l: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures (((hd::tl)@l) == (hd::(tl@l))))
let append_cons_l hd tl l = ()
val append_l_cons: hd:'a -> tl:list 'a -> l:list 'a ->
Lemma (requires True)
(ensures ((l@(hd::tl)) == ((l@[hd])@tl)))
let rec append_l_cons hd tl l = match l with
| [] -> ()
| hd'::tl' -> append_l_cons hd tl tl'
val append_assoc: l1:list 'a -> l2:list 'a -> l3:list 'a ->
Lemma (requires True)
(ensures ((l1@(l2@l3)) == ((l1@l2)@l3)))
let rec append_assoc l1 l2 l3 = match l1 with
| [] -> ()
| hd::tl -> append_assoc tl l2 l3
val append_length: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures (length (l1@l2) = length l1 + length l2)) [SMTPat (length (l1 @ l2))]
let rec append_length l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_length tl l2
val append_mem: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (mem a (l1@l2) = (mem a l1 || mem a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_mem #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_mem tl l2 a
val append_memP: #t:Type -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
(* [SMTPat (mem a (l1@l2))] *)
let rec append_memP #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_memP tl l2 a
val append_mem_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. mem a (l1@l2) = (mem a l1 || mem a l2)))
let rec append_mem_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_mem_forall tl l2
val append_memP_forall: #a:Type -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. memP a (l1 `append` l2) <==> (memP a l1 \/ memP a l2)))
let rec append_memP_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_memP_forall tl l2
val append_count: #t:eqtype -> l1:list t
-> l2:list t
-> a:t
-> Lemma (requires True)
(ensures (count a (l1@l2) = (count a l1 + count a l2)))
let rec append_count #t l1 l2 a = match l1 with
| [] -> ()
| hd::tl -> append_count tl l2 a
val append_count_forall: #a:eqtype -> l1:list a
-> l2:list a
-> Lemma (requires True)
(ensures (forall a. count a (l1@l2) = (count a l1 + count a l2)))
(* [SMTPat (l1@l2)] *)
let rec append_count_forall #a l1 l2 = match l1 with
| [] -> ()
| hd::tl -> append_count_forall tl l2
val append_eq_nil: l1:list 'a -> l2:list 'a ->
Lemma (requires (l1@l2 == []))
(ensures (l1 == [] /\ l2 == []))
let append_eq_nil l1 l2 = ()
val append_eq_singl: l1:list 'a -> l2:list 'a -> x:'a ->
Lemma (requires (l1@l2 == [x]))
(ensures ((l1 == [x] /\ l2 == []) \/ (l1 == [] /\ l2 == [x])))
let append_eq_singl l1 l2 x = ()
val append_inv_head: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l@l1) == (l@l2)))
(ensures (l1 == l2))
let rec append_inv_head l l1 l2 = match l with
| [] -> ()
| hd::tl -> append_inv_head tl l1 l2
val append_inv_tail: l:list 'a -> l1:list 'a -> l2:list 'a ->
Lemma (requires ((l1@l) == (l2@l)))
(ensures (l1 == l2))
let rec append_inv_tail l l1 l2 = match l1, l2 with
| [], [] -> ()
| hd1::tl1, hd2::tl2 -> append_inv_tail l tl1 tl2
| [], hd2::tl2 ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl2; append_inv_tail tl [] (tl2@[hd])
(* We can here apply the induction hypothesis thanks to termination on a lexicographical ordering of the arguments! *)
)
| hd1::tl1, [] ->
(match l with
| [] -> ()
| hd::tl -> append_l_cons hd tl tl1; append_inv_tail tl (tl1@[hd]) []
(* Idem *)
)
let rec append_length_inv_head
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length left1 == length left2))
(ensures (left1 == left2 /\ right1 == right2))
(decreases left1)
= match left1 with
| [] -> ()
| _ :: left1' ->
append_length_inv_head left1' right1 (tl left2) right2
let append_length_inv_tail
(#a: Type)
(left1 right1 left2 right2: list a)
: Lemma
(requires (append left1 right1 == append left2 right2 /\ length right1 == length right2))
(ensures (left1 == left2 /\ right1 == right2))
= append_length left1 right1;
append_length left2 right2;
append_length_inv_head left1 right1 left2 right2
let append_injective #a (l0 l0':list a)
(l1 l1':list a)
: Lemma
(ensures
(length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1' ==>
l0 == l0' /\ l1 == l1')
= introduce
((length l0 == length l0' \/ length l1 == length l1') /\
append l0 l1 == append l0' l1')
==>
(l0 == l0' /\ l1 == l1')
with _. eliminate (length l0 == length l0') \/
(length l1 == length l1')
returns _
with _. append_length_inv_head l0 l1 l0' l1'
and _. append_length_inv_tail l0 l1 l0' l1'
(** The [last] element of a list remains the same, even after that list is
[append]ed to another list. *)
let rec lemma_append_last (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0))
(ensures (last (l1 @ l2) == last l2)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_append_last l1' l2
(** Properties mixing rev and append **)
val rev': list 'a -> Tot (list 'a)
let rec rev' = function
| [] -> []
| hd::tl -> (rev' tl)@[hd]
let rev'T = rev'
val rev_acc_rev': l:list 'a -> acc:list 'a ->
Lemma (requires (True))
(ensures ((rev_acc l acc) == ((rev' l)@acc)))
let rec rev_acc_rev' l acc = match l with
| [] -> ()
| hd::tl -> rev_acc_rev' tl (hd::acc); append_l_cons hd acc (rev' tl)
val rev_rev': l:list 'a ->
Lemma (requires True)
(ensures ((rev l) == (rev' l)))
let rev_rev' l = rev_acc_rev' l []; append_l_nil (rev' l)
val rev'_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev' (l1@l2)) == ((rev' l2)@(rev' l1))))
let rec rev'_append l1 l2 = match l1 with
| [] -> append_l_nil (rev' l2)
| hd::tl -> rev'_append tl l2; append_assoc (rev' l2) (rev' tl) [hd]
val rev_append: l1:list 'a -> l2:list 'a ->
Lemma (requires True)
(ensures ((rev (l1@l2)) == ((rev l2)@(rev l1))))
let rev_append l1 l2 = rev_rev' l1; rev_rev' l2; rev_rev' (l1@l2); rev'_append l1 l2
val rev'_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev' (rev' l) == l))
let rec rev'_involutive = function
| [] -> ()
| hd::tl -> rev'_append (rev' tl) [hd]; rev'_involutive tl
val rev_involutive : l:list 'a ->
Lemma (requires True)
(ensures (rev (rev l) == l))
let rev_involutive l = rev_rev' l; rev_rev' (rev' l); rev'_involutive l
(** Properties about snoc *)
val lemma_snoc_length : (lx:(list 'a * 'a)) ->
Lemma (requires True)
(ensures (length (snoc lx) = length (fst lx) + 1))
let lemma_snoc_length (l, x) = append_length l [x]
(** Reverse induction principle **)
val rev'_list_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p (rev' tl) ==> p (rev' (hd::tl)))))
(ensures (p (rev' l)))
let rec rev'_list_ind p = function
| [] -> ()
| hd::tl -> rev'_list_ind p tl
val rev_ind: p:(list 'a -> Tot bool) -> l:list 'a ->
Lemma (requires ((p []) /\ (forall hd tl. p hd ==> p (hd@[tl]))))
(ensures (p l))
let rev_ind p l = rev'_involutive l; rev'_list_ind p (rev' l)
(** Properties about iterators **)
val map_lemma: f:('a -> Tot 'b)
-> l:(list 'a)
-> Lemma (requires True)
(ensures (length (map f l)) = length l)
[SMTPat (map f l)]
let rec map_lemma f l =
match l with
| [] -> ()
| h::t -> map_lemma f t
(** Properties about unsnoc *)
(** [unsnoc] is the inverse of [snoc] *)
val lemma_unsnoc_snoc: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (snoc (unsnoc l) == l))
[SMTPat (snoc (unsnoc l))]
let lemma_unsnoc_snoc #a l =
let l', x = unsnoc l in
let l1, l2 = l', [x] in
lemma_splitAt_snd_length (length l - 1) l;
// assert ((l1, l2) == splitAt (length l - 1) l);
let rec aux (l:list a{length l > 0}) :
Lemma (let l1, l2 = splitAt (length l - 1) l in
append l1 l2 == l) =
if length l = 1 then () else aux (tl l) in
aux l
(** [snoc] is the inverse of [unsnoc] *)
val lemma_snoc_unsnoc: #a:Type -> lx:(list a * a) ->
Lemma (requires True)
(ensures (unsnoc (snoc lx) == lx))
(decreases (length (fst (lx))))
[SMTPat (unsnoc (snoc lx))]
let rec lemma_snoc_unsnoc #a lx =
let l, x = lx in
match l with
| [] -> ()
| _ -> lemma_snoc_unsnoc (tl l, x)
(** Doing an [unsnoc] gives us a list that is shorter in length by 1 *)
val lemma_unsnoc_length: #a:Type -> l:list a{length l > 0} ->
Lemma (requires True)
(ensures (length (fst (unsnoc l)) == length l - 1))
let lemma_unsnoc_length #a l =
lemma_snoc_length (unsnoc l)
(** [unsnoc] followed by [append] can be connected to the same vice-versa. *)
let rec lemma_unsnoc_append (#a:Type) (l1 l2:list a) :
Lemma
(requires (length l2 > 0)) // the [length l2 = 0] is trivial
(ensures (
let al, a = unsnoc (l1 @ l2) in
let bl, b = unsnoc l2 in
al == l1 @ bl /\ a == b)) =
match l1 with
| [] -> ()
| _ :: l1' -> lemma_unsnoc_append l1' l2
(** [unsnoc] gives you [last] element, which is [index]ed at [length l - 1] *)
let rec lemma_unsnoc_is_last (#t:Type) (l:list t) :
Lemma
(requires (length l > 0))
(ensures (snd (unsnoc l) == last l /\ snd (unsnoc l) == index l (length l - 1))) =
match l with
| [_] -> ()
| _ -> lemma_unsnoc_is_last (tl l)
(** [index]ing on the left part of an [unsnoc]d list is the same as indexing
the original list. *)
let rec lemma_unsnoc_index (#t:Type) (l:list t) (i:nat) :
Lemma
(requires (length l > 0 /\ i < length l - 1))
(ensures (
i < length (fst (unsnoc l)) /\
index (fst (unsnoc l)) i == index l i)) =
match i with
| 0 -> ()
| _ -> lemma_unsnoc_index (tl l) (i - 1)
(** Definition and properties about [split_using] *)
(** [split_using] splits a list at the first instance of finding an
element in it.
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (list t * list t) =
match l with
| [_] -> [], l
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
[], l
) else (
let l1', l2' = split_using rest x in
a :: l1', l2'
)
let rec lemma_split_using (#t:Type) (l:list t) (x:t{x `memP` l}) :
Lemma
(ensures (
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l)) =
match l with
| [_] -> ()
| a :: rest ->
let goal =
let l1, l2 = split_using l x in
length l2 > 0 /\
~(x `memP` l1) /\
hd l2 == x /\
append l1 l2 == l
in
FStar.Classical.or_elim
#_ #_
#(fun () -> goal)
(fun (_:squash (a == x)) -> ())
(fun (_:squash (x `memP` rest)) -> lemma_split_using rest x)
(** Definition of [index_of] *)
(** [index_of l x] gives the index of the leftmost [x] in [l].
NOTE: Uses [strong_excluded_middle] axiom. *)
let rec index_of (#t:Type) (l:list t) (x:t{x `memP` l}) :
GTot (i:nat{i < length l /\ index l i == x}) =
match l with
| [_] -> 0
| a :: rest ->
if FStar.StrongExcludedMiddle.strong_excluded_middle (a == x) then (
0
) else (
1 + index_of rest x
)
(** Properties about partition **)
(** If [partition f l = (l1, l2)], then for any [x], [x] is in [l] if
and only if [x] is in either one of [l1] or [l2] *)
val partition_mem: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
mem x l = (mem x l1 || mem x l2)))
let rec partition_mem #a f l x = match l with
| [] -> ()
| hd::tl -> partition_mem f tl x
(** Same as [partition_mem], but using [forall] *)
val partition_mem_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition f l in
(forall x. mem x l = (mem x l1 || mem x l2))))
let rec partition_mem_forall #a f l = match l with
| [] -> ()
| hd::tl -> partition_mem_forall f tl
(** If [partition f l = (l1, l2)], then for any [x], if [x] is in [l1]
(resp. [l2]), then [f x] holds (resp. does not hold) *)
val partition_mem_p_forall: #a:eqtype -> p:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (let l1, l2 = partition p l in
(forall x. mem x l1 ==> p x) /\ (forall x. mem x l2 ==> not (p x))))
let rec partition_mem_p_forall #a p l = match l with
| [] -> ()
| hd::tl -> partition_mem_p_forall p tl
(** If [partition f l = (l1, l2)], then the number of occurrences of
any [x] in [l] is the same as the sum of the number of occurrences in
[l1] and [l2]. *)
val partition_count: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> x:a
-> Lemma (requires True)
(ensures (count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
let rec partition_count #a f l x = match l with
| [] -> ()
| hd::tl -> partition_count f tl x
(** Same as [partition_count], but using [forall] *)
val partition_count_forall: #a:eqtype -> f:(a -> Tot bool)
-> l:list a
-> Lemma (requires True)
(ensures (forall x. count x l = (count x (fst (partition f l)) + count x (snd (partition f l)))))
(* [SMTPat (partitionT f l)] *)
let rec partition_count_forall #a f l= match l with
| [] -> ()
| hd::tl -> partition_count_forall f tl
(** Properties about subset **)
let rec mem_subset (#a: eqtype) (la lb: list a)
: Lemma (requires (forall x. mem x la ==> mem x lb))
(ensures (subset la lb)) =
match la with
| [] -> ()
| hd :: tl -> mem_subset tl lb
let subset_reflexive (#a: eqtype) (l: list a)
: Lemma (subset l l) [SMTPat (subset l l)] = mem_subset l l
(** Correctness of quicksort **)
(** Correctness of [sortWith], part 1/2: the number of occurrences of
any [x] in [sortWith f l] is the same as the number of occurrences in
[l]. *)
val sortWith_permutation: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires True)
(ensures (forall x. count x l = count x (sortWith f l)))
(decreases (length l))
let rec sortWith_permutation #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_count_forall (bool_of_compare f pivot) tl;
sortWith_permutation f lo;
sortWith_permutation f hi;
append_count_forall (sortWith f lo) (pivot::sortWith f hi)
(** [sorted f l] holds if, and only if, any two consecutive elements
[x], [y] of [l] are such that [f x y] holds
*)
val sorted: ('a -> 'a -> Tot bool) -> list 'a -> Tot bool
let rec sorted f = function
| []
| [_] -> true
| x::y::tl -> f x y && sorted f (y::tl)
(** [f] is a total order if, and only if, it is reflexive,
anti-symmetric, transitive and total. *)
type total_order (#a:Type) (f: (a -> a -> Tot bool)) =
(forall a. f a a) (* reflexivity *)
/\ (forall a1 a2. f a1 a2 /\ f a2 a1 ==> a1 == a2) (* anti-symmetry *)
/\ (forall a1 a2 a3. f a1 a2 /\ f a2 a3 ==> f a1 a3) (* transitivity *)
/\ (forall a1 a2. f a1 a2 \/ f a2 a1) (* totality *)
(** Correctness of the merging of two sorted lists around a pivot. *)
val append_sorted: #a:eqtype
-> f:(a -> a -> Tot bool)
-> l1:list a{sorted f l1}
-> l2:list a{sorted f l2}
-> pivot:a
-> Lemma (requires (total_order #a f
/\ (forall y. mem y l1 ==> not(f pivot y))
/\ (forall y. mem y l2 ==> f pivot y)))
(ensures (sorted f (l1@(pivot::l2))))
[SMTPat (sorted f (l1@(pivot::l2)))]
let rec append_sorted #a f l1 l2 pivot = match l1 with
| [] -> ()
| hd::tl -> append_sorted f tl l2 pivot
(** Correctness of [sortWith], part 2/2: the elements of [sortWith f
l] are sorted according to comparison function [f], and the elements
of [sortWith f l] are the elements of [l]. *)
val sortWith_sorted: #a:eqtype -> f:(a -> a -> Tot int) -> l:list a ->
Lemma (requires (total_order #a (bool_of_compare f)))
(ensures ((sorted (bool_of_compare f) (sortWith f l)) /\ (forall x. mem x l = mem x (sortWith f l))))
(decreases (length l))
let rec sortWith_sorted #a f l = match l with
| [] -> ()
| pivot::tl ->
let hi, lo = partition (bool_of_compare f pivot) tl in
partition_length (bool_of_compare f pivot) tl;
partition_mem_forall (bool_of_compare f pivot) tl;
partition_mem_p_forall (bool_of_compare f pivot) tl;
sortWith_sorted f lo;
sortWith_sorted f hi;
append_mem_forall (sortWith f lo) (pivot::sortWith f hi);
append_sorted (bool_of_compare f) (sortWith f lo) (sortWith f hi) pivot
(** Properties of [noRepeats] *)
let noRepeats_nil
(#a: eqtype)
: Lemma
(ensures (noRepeats #a []))
= ()
let noRepeats_cons
(#a: eqtype)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (mem h tl)) /\ noRepeats tl))
(ensures (noRepeats #a (h::tl)))
= ()
let rec noRepeats_append_elim
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats (l1 @ l2)))
(ensures (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_elim q1 l2
let rec noRepeats_append_intro
(#a: eqtype)
(l1 l2: list a)
: Lemma
(requires (noRepeats l1 /\ noRepeats l2 /\ (forall x . mem x l1 ==> ~ (mem x l2))))
(ensures (noRepeats (l1 @ l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_mem q1 l2 x;
noRepeats_append_intro q1 l2
(** Properties of [no_repeats_p] *)
let no_repeats_p_nil
(#a: Type)
: Lemma
(ensures (no_repeats_p #a []))
= ()
let no_repeats_p_cons
(#a: Type)
(h: a)
(tl: list a)
: Lemma
(requires ((~ (memP h tl)) /\ no_repeats_p tl))
(ensures (no_repeats_p #a (h::tl)))
= ()
let rec no_repeats_p_append_elim
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p (l1 `append` l2)))
(ensures (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_elim q1 l2
let rec no_repeats_p_append_intro
(#a: Type)
(l1 l2: list a)
: Lemma
(requires (no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2))))
(ensures (no_repeats_p (l1 `append` l2)))
(decreases l1)
= match l1 with
| [] -> ()
| x :: q1 ->
append_memP q1 l2 x;
no_repeats_p_append_intro q1 l2
let no_repeats_p_append
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> (
(no_repeats_p l1 /\ no_repeats_p l2 /\ (forall x . memP x l1 ==> ~ (memP x l2)))
))
= FStar.Classical.move_requires (no_repeats_p_append_intro l1) l2;
FStar.Classical.move_requires (no_repeats_p_append_elim l1) l2
let no_repeats_p_append_swap
(#a: Type)
(l1 l2: list a)
: Lemma
(no_repeats_p (l1 `append` l2) <==> no_repeats_p (l2 `append` l1))
= no_repeats_p_append l1 l2;
no_repeats_p_append l2 l1
let no_repeats_p_append_permut
(#a: Type)
(l1 l2 l3 l4 l5: list a)
: Lemma
((no_repeats_p (l1 `append` (l2 `append` (l3 `append` (l4 `append` l5))))) <==> no_repeats_p (l1 `append` (l4 `append` (l3 `append` (l2 `append` l5)))))
= no_repeats_p_append l1 (l2 `append` (l3 `append` (l4 `append` l5)));
append_memP_forall l2 (l3 `append` (l4 `append` l5));
append_memP_forall l3 (l4 `append` l5);
append_memP_forall l4 l5;
no_repeats_p_append l2 (l3 `append` (l4 `append` l5));
no_repeats_p_append l3 (l4 `append` l5);
no_repeats_p_append l4 l5;
no_repeats_p_append l2 l5;
no_repeats_p_append l3 (l2 `append` l5);
append_memP_forall l2 l5;
no_repeats_p_append l4 (l3 `append` (l2 `append` l5));
append_memP_forall l3 (l2 `append` l5);
no_repeats_p_append l1 (l4 `append` (l3 `append` (l2 `append` l5)));
append_memP_forall l4 (l3 `append` (l2 `append` l5))
let no_repeats_p_false_intro
(#a: Type)
(l1 l l2 l3: list a)
: Lemma
(requires (Cons? l))
(ensures (~ (no_repeats_p (l1 `append` (l `append` (l2 `append` (l `append` l3)))))))
= let x = hd l in
assert (memP x l);
no_repeats_p_append l1 (l `append` (l2 `append` (l `append` l3)));
no_repeats_p_append l (l2 `append` (l `append` l3));
append_memP l2 (l `append` l3) x;
append_memP l l3 x
(** Properties of [assoc] *)
let assoc_nil
(#a: eqtype)
(#b: Type)
(x: a)
: Lemma
(ensures (assoc #a #b x [] == None))
= ()
let assoc_cons_eq
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(q: list (a * b))
: Lemma
(ensures (assoc x ((x, y) :: q) == Some y))
= ()
let assoc_cons_not_eq
(#a: eqtype)
(#b: Type)
(x x': a)
(y: b)
(q: list (a * b))
: Lemma
(requires (x <> x'))
(ensures (assoc x' ((x, y) :: q) == assoc x' q))
= ()
let rec assoc_append_elim_r
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l2 == None \/ ~ (assoc x l1 == None)))
(ensures (assoc x (l1 @ l2) == assoc x l1))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_append_elim_r x q l2
let rec assoc_append_elim_l
(#a: eqtype)
(#b: Type)
(x: a)
(l1 l2: list (a * b))
: Lemma
(requires (assoc x l1 == None))
(ensures (assoc x (l1 @ l2) == assoc x l2))
(decreases l1)
= match l1 with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_append_elim_l x q l2
let rec assoc_memP_some
(#a: eqtype)
(#b: Type)
(x: a)
(y: b)
(l: list (a * b))
: Lemma
(requires (assoc x l == Some y))
(ensures (memP (x, y) l))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then () else assoc_memP_some x y q
let rec assoc_memP_none
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(requires (assoc x l == None))
(ensures (forall y . ~ (memP (x, y) l)))
(decreases l)
= match l with
| [] -> ()
| (x', _) :: q -> if x = x' then assert False else assoc_memP_none x q
let assoc_mem
(#a: eqtype)
(#b: Type)
(x: a)
(l: list (a * b))
: Lemma
(ensures (mem x (map fst l) <==> (exists y . assoc x l == Some y)))
= match assoc x l with
| None ->
assoc_memP_none x l;
mem_memP x (map fst l);
memP_map_elim fst x l
| Some y ->
assoc_memP_some x y l;
memP_map_intro fst (x, y) l;
mem_memP x (map fst l)
(** Properties of [fold_left] *)
let rec fold_left_invar
(#a #b: Type)
(f: (a -> b -> Tot a))
(l: list b)
(p: (a -> Tot Type0))
: Lemma
(requires forall (x: a) (y: b) . p x ==> memP y l ==> p (f x y) )
(ensures forall (x: a) . p x ==> p (fold_left f x l))
=
match l with
| [] -> ()
| y :: q -> fold_left_invar f q p
let rec fold_left_map
(#a #b #c: Type)
(f_aba: a -> b -> Tot a)
(f_bc: b -> Tot c)
(f_aca: a -> c -> Tot a)
(l: list b)
: Lemma
(requires forall (x: a) (y: b) . f_aba x y == f_aca x (f_bc y) )
(ensures forall (x : a) . fold_left f_aba x l == fold_left f_aca x (map f_bc l) )
=
match l with
| [] -> ()
| y :: q -> fold_left_map f_aba f_bc f_aca q | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.StrongExcludedMiddle.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.Classical.Sugar.fsti.checked",
"FStar.Classical.fsti.checked"
],
"interface_file": false,
"source_file": "FStar.List.Tot.Properties.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.List.Tot.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.List.Tot",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> b) -> l1: Prims.list a -> l2: Prims.list a
-> FStar.Pervasives.Lemma
(ensures
FStar.List.Tot.Base.map f (l1 @ l2) ==
FStar.List.Tot.Base.map f l1 @ FStar.List.Tot.Base.map f l2) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.list",
"FStar.List.Tot.Properties.map_append",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"FStar.List.Tot.Base.map",
"FStar.List.Tot.Base.op_At",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [
"recursion"
] | false | false | true | false | false | let rec map_append (#a #b: Type) (f: (a -> Tot b)) (l1 l2: list a)
: Lemma (ensures map f (l1 @ l2) == map f l1 @ map f l2) =
| match l1 with
| [] -> ()
| x :: q -> map_append f q l2 | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.