The file imp1.ml has been created by extraction.
The file imp1.mli has been created by extraction.
Controlling Extraction of Specific Types
如果不做任何处理的话…生成的 ml 里的 nat 则都会是 Church Numeral…
We can tell Coq how to extract certain Inductive definitions to specific OCaml types.
we must say:
how the Coq type itself should be represented in OCaml
how each constructor should be translated
1
ExtractInductivebool⇒"bool"["true""false"].
also, for non-enumeration types (where the constructors take arguments),
we give an OCaml expression that can be used as a “recursor” over elements of the type. (Think Church numerals.)
1
2
3
4
ExtractInductivenat⇒"int"["0""(fun x → x + 1)"]"(fun zero succ n →
if n=0 then zero () else succ (n-1))".
e → e'
------------- (work inside constructor)
C(e) -> C(e')
e → e'
------------------------------- (work on the expr match against)
case e of ... → case e' of ...
----------------------------------------------- (match Left constructor, substitute)
case L(v) of L(x) => e1 | R(y) => e2 → e1 [v/x]
----------------------------------------------- (match Right constructor, substitute)
case R(v) of L(x) => e1 | R(y) => e2 → e1 [v/x]
可以发现 case 表达式可以理解为一种特殊的 application,会将其 argument 根据某种 tag (这里为构造函数) apply 到对应的 case body 上,
每个 case body 都是和 lambda abstraction 同构的一种 binder:
1
2
3
4
L(x) => e1 === λx.e1
R(x) => e2 === λx.e2
case v e1|e2 === (λx.e1|e2) v -- `e1` or `e2` depends on the _tag_ wrapped on `v`
这个角度也解释了 Haskell/SML 在申明函数时直接对参数写 pattern match 的理论合理性.