Skip to content

Commit

Permalink
Use quotations for extract_as argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
gebner authored and mtzguido committed Sep 26, 2024
1 parent a8c5846 commit 11b40bf
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 32 deletions.
2 changes: 1 addition & 1 deletion ocaml/fstar-lib/generated/FStar_Parser_Const.ml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions ocaml/fstar-lib/generated/FStar_TypeChecker_Normalize.ml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/parser/FStar.Parser.Const.fst
Original file line number Diff line number Diff line change
Expand Up @@ -598,5 +598,5 @@ let tref_lid = p2l ["FStar"; "Stubs"; "Tactics"; "Types"; "tref"]
let document_lid = p2l ["FStar"; "Stubs"; "Pprint"; "document"]
let issue_lid = p2l ["FStar"; "Issue"; "issue"]

let extract_as_lid = p2l ["FStar"; "Pervasives"; "extract_as"]
let extract_as_lid = p2l ["FStar"; "ExtractAs"; "extract_as"]
let extract_as_impure_effect_lid = p2l ["FStar"; "Pervasives"; "extract_as_impure_effect"]
7 changes: 4 additions & 3 deletions src/typechecker/FStar.TypeChecker.Normalize.fst
Original file line number Diff line number Diff line change
Expand Up @@ -866,9 +866,10 @@ let is_forall_const cfg (phi : term) : option term =
let is_extract_as_attr (attr: attribute) : option term =
let head, args = head_and_args attr in
match (Subst.compress head).n, args with
| Tm_uinst({n=Tm_fvar fv}, [u]), [_; (t, _)]
when Syntax.fv_eq_lid fv PC.extract_as_lid ->
Some t
| Tm_fvar fv, [t, _] when Syntax.fv_eq_lid fv PC.extract_as_lid ->
(match (Subst.compress t).n with
| Tm_quoted(impl, _) -> Some impl
| _ -> None)
| _ -> None

let has_extract_as_attr (g: Env.env) (lid: I.lid) : option term =
Expand Down
5 changes: 3 additions & 2 deletions tests/extraction/ExtractAs.fst
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module ExtractAs
open FStar.ExtractAs
let fail_unless (b: bool) = if b then "ok" else magic ()

// Test that extract_as works when extracting the definition itself.

[@@extract_as (fun x -> x + 10)]
[@@extract_as (`(fun (x: nat) -> x + 10))]
let frob y = 2 + y

let _ = fail_unless (frob 1 = 11)

// Test that extract_as works when inlining the definition.

inline_for_extraction noextract [@@extract_as (fun x -> x + 10)]
inline_for_extraction noextract [@@extract_as (`(fun (x: nat) -> x + 10))]
let bar_2 y = 2 + y
let bar z = bar_2 z

Expand Down
35 changes: 35 additions & 0 deletions ulib/FStar.ExtractAs.fst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(*
Copyright 2024 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.
*)

module FStar.ExtractAs
open FStar.Stubs.Reflection.Types

(** Replaces the annotated definition
by the specified implementation during extraction.
There are no checks whether the implementation
has the same semantics, or even the same type.
For example, if you have:
[@@extract_as (`(fun (x: nat) -> "not a number"))]
let add_one (x: nat) : nat = x + 42
Then `add_one` will extract to `let add_one x = "not a number"`,
and most likely cause the extracted program to crash.
Note that the argument needs to be a literal quotation.
*)
let extract_as (impl: term) = ()
2 changes: 0 additions & 2 deletions ulib/FStar.Pervasives.fst
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ let primitive_extraction = ()

let extract_as_impure_effect = ()

let extract_as _ = ()

let strictly_positive = ()

let unused = ()
Expand Down
14 changes: 0 additions & 14 deletions ulib/FStar.Pervasives.fsti
Original file line number Diff line number Diff line change
Expand Up @@ -1146,20 +1146,6 @@ val primitive_extraction : unit
*)
val extract_as_impure_effect : unit

(** Replaces the annotated definition
by the specified implementation during extraction.
There are no checks whether the implementation
has the same semantics, or even the same type.
For example, if you have:
[@@extract_as (fun (x: nat) -> "not a number")]
let add_one (x: nat) : nat = x + 42
Then `add_one` will extract to `let add_one x = "not a number"`,
and most likely cause the extracted program to crash.
*)
val extract_as (#t: Type u#a) (impl: t) : unit

(** A binder in a definition/declaration may optionally be annotated as strictly_positive
When the let definition is used in a data constructor type in an inductive
Expand Down

0 comments on commit 11b40bf

Please sign in to comment.