include URL for trfrac.sty and mathpartir in LaTeX output
[coq-hetmet.git] / src / Extraction.v
1 (* need this or the Haskell extraction fails *)
2 Set Printing Width 1300000.
3
4 Require Import Coq.Lists.List.
5 Require Import Coq.Strings.Ascii.
6 Require Import Coq.Strings.String.
7
8 Require Import Preamble.
9 Require Import General.
10
11 Require Import NaturalDeduction.
12 Require Import NaturalDeductionToLatex.
13
14 Require Import HaskKinds.
15 Require Import HaskCoreLiterals.
16 Require Import HaskCoreVars.
17 Require Import HaskCoreTypes.
18 Require Import HaskCore.
19 Require Import HaskWeakVars.
20 Require Import HaskWeakTypes.
21 Require Import HaskWeak.
22 Require Import HaskStrongTypes.
23 Require Import HaskStrong.
24 Require Import HaskProof.
25 Require Import HaskCoreToWeak.
26 Require Import HaskWeakToStrong.
27 Require Import HaskStrongToProof.
28 Require Import HaskProofToStrong.
29 Require Import HaskProofToLatex.
30 Require Import HaskStrongToWeak.
31 Require Import HaskWeakToCore.
32
33 Open Scope string_scope.
34 Extraction Language Haskell.
35
36 (* I try to reuse Haskell types mostly to get the "deriving Show" aspect *)
37 Extract Inductive option => "Prelude.Maybe" [ "Prelude.Just" "Prelude.Nothing" ].
38 Extract Inductive list   => "([])" [ "([])" "(:)" ].
39 (*Extract Inductive vec    => "([])" [ "([])" "(:)" ].*)
40 (*Extract Inductive Tree   => "([])" [ "([])" "(:)" ].*)
41 Extract Inlined Constant map => "Prelude.map".
42 Extract Inductive string => "Prelude.String" [ "([])" "(:)" ].
43 Extract Inductive prod   => "(,)" [ "(,)" ].
44 Extract Inductive sum    => "Prelude.Either" [ "Prelude.Left" "Prelude.Right" ].
45 Extract Inductive sumbool => "Prelude.Bool" [ "Prelude.True" "Prelude.False" ].
46 Extract Inductive bool    => "Prelude.Bool" [ "Prelude.True" "Prelude.False" ].
47 Extract Inductive unit    => "()" [ "()" ].
48 Extract Inlined Constant string_dec => "(==)".
49 Extract Inlined Constant ascii_dec => "(==)".
50 Extract Inductive string => "Prelude.String" [ "[]" "(:)" ].
51
52 (* adapted from ExtrOcamlString.v *)
53 Extract Inductive ascii => "Prelude.Char" [ "bin2ascii" ] "bin2ascii'".
54 Extract Constant zero   => "'\000'".
55 Extract Constant one    => "'\001'".
56 Extract Constant shift  => "shiftAscii".
57
58 Unset Extraction Optimize.
59 Unset Extraction AutoInline.
60
61 Variable Prelude_error : forall {A}, string -> A.   Extract Inlined Constant Prelude_error => "Prelude.error".
62
63 Section core2proof.
64   Context (ce:@CoreExpr CoreVar).
65
66   Definition Γ : TypeEnv := nil.
67
68   Definition Δ : CoercionEnv Γ := nil.
69
70   Definition φ : TyVarResolver Γ :=
71     fun cv => Error ("unbound tyvar: " +++ (cv:CoreVar)).
72     (*fun tv => error ("tried to get the representative of an unbound tyvar:" +++ (getCoreVarOccString tv)).*)
73
74   Definition ψ : CoVarResolver Γ Δ :=
75     fun cv => Error ("tried to get the representative of an unbound covar!" (*+++ (getTypeVarOccString cv)*)).
76
77   (* We need to be able to resolve unbound exprvars, but we can be sure their types will have no
78    * free tyvars in them *)
79   Definition ξ (cv:CoreVar) : LeveledHaskType Γ ★ :=
80     match coreVarToWeakVar cv with
81       | WExprVar wev => match weakTypeToType'' φ wev ★ with
82                           | Error s => Prelude_error ("Error in top-level xi: " +++ s)
83                           | OK    t => t @@ nil
84                         end
85       | WTypeVar _   => Prelude_error "top-level xi got a type variable"
86       | WCoerVar _   => Prelude_error "top-level xi got a coercion variable"
87     end.
88
89   Definition header : string :=
90     "\documentclass[9pt]{article}"+++eol+++
91     "\usepackage{amsmath}"+++eol+++
92     "\usepackage{amssymb}"+++eol+++
93     "\usepackage{proof}"+++eol+++
94     "\usepackage{mathpartir}   % http://cristal.inria.fr/~remy/latex/"+++eol+++
95     "\usepackage{trfrac}       % http://www.utdallas.edu/~hamlen/trfrac.sty"+++eol+++
96     "\def\code#1#2{\Box_{#1} #2}"+++eol+++
97     "\usepackage[paperwidth=200in,centering]{geometry}"+++eol+++
98     "\usepackage[displaymath,tightpage,active]{preview}"+++eol+++
99     "\begin{document}"+++eol+++
100     "\begin{preview}"+++eol.
101
102   Definition footer : string :=
103     eol+++"\end{preview}"+++
104     eol+++"\end{document}"+++
105     eol.
106
107   Definition handleExpr' (ce:@CoreExpr CoreVar) : ???string :=
108     addErrorMessage ("input CoreSyn: " +++ ce)
109     (addErrorMessage ("input CoreType: " +++ coreTypeOfCoreExpr ce) (
110       coreExprToWeakExpr ce >>= fun we =>
111         addErrorMessage ("WeakExpr: " +++ we)
112           ((addErrorMessage ("CoreType of WeakExpr: " +++ coreTypeOfCoreExpr (weakExprToCoreExpr we))
113             ((weakTypeOfWeakExpr we) >>= fun t =>
114               (addErrorMessage ("WeakType: " +++ t)
115                 ((weakTypeToType'' φ t ★) >>= fun τ =>
116                   addErrorMessage ("HaskType: " +++ τ)
117                   ((weakExprToStrongExpr Γ Δ φ ψ ξ τ nil we) >>= fun e =>
118                     OK (eol+++"$$"+++ nd_ml_toLatex (@expr2proof _ _ _ _ _ _ e)+++"$$"+++eol)
119                   )))))))).
120
121   Definition handleExpr (ce:@CoreExpr CoreVar) : string :=
122     match handleExpr' ce with
123       | OK x => x
124       | Error s => Prelude_error s
125     end.
126
127   Definition handleBind (bind:@CoreBind CoreVar) : string :=
128     match bind with
129       | CoreNonRec _ e => handleExpr e
130       | CoreRec    lbe => fold_left (fun x y => x+++eol+++eol+++y) (map (fun x => handleExpr (snd x)) lbe) ""
131     end.
132
133   Definition coqPassCoreToString (lbinds:list (@CoreBind CoreVar)) : string :=
134     header +++
135     (fold_left (fun x y => x+++eol+++eol+++y) (map handleBind lbinds) "")
136     +++ footer.
137
138   Definition coqPassCoreToCore (lbinds:list (@CoreBind CoreVar)) : list (@CoreBind CoreVar) :=
139     lbinds.
140
141 End core2proof.
142
143 Extraction "Extraction.hs" coqPassCoreToString coqPassCoreToCore.
144