1 (*********************************************************************************************************************************)
2 (* HaskWeakVars: types and variables for HaskWeak *)
3 (*********************************************************************************************************************************)
5 Generalizable All Variables.
6 Require Import Preamble.
7 Require Import General.
8 Require Import Coq.Strings.String.
9 Require Import Coq.Lists.List.
10 Require Import HaskKinds.
11 Require Import HaskCoreLiterals.
12 Require Import HaskCoreVars.
13 Require Import HaskCoreTypes.
14 Require Import HaskWeakTypes.
16 (* TO DO: finish this *)
17 Inductive WeakCoercion : Type := weakCoercion : Kind -> WeakType -> WeakType -> CoreCoercion -> WeakCoercion.
19 (* a WeakCoerVar just wraps a CoreVar and tags it with the pair of types amongst which it coerces *)
20 Inductive WeakCoerVar := weakCoerVar : CoreVar -> Kind -> WeakType -> WeakType -> WeakCoerVar.
22 (* a WeakExprVar just wraps a CoreVar and tags it with the type of its value *)
23 Inductive WeakExprVar := weakExprVar : CoreVar -> WeakType -> WeakExprVar.
25 (* a WeakVar is one of the three sorts *)
26 Inductive WeakVar : Type :=
27 | WExprVar : WeakExprVar -> WeakVar
28 | WTypeVar : WeakTypeVar -> WeakVar
29 | WCoerVar : WeakCoerVar -> WeakVar.
30 Coercion WExprVar : WeakExprVar >-> WeakVar.
31 Coercion WTypeVar : WeakTypeVar >-> WeakVar.
32 Coercion WCoerVar : WeakCoerVar >-> WeakVar.
34 Definition weakTypeVarToKind (tv:WeakTypeVar) : Kind :=
35 match tv with weakTypeVar _ k => k end.
36 Coercion weakTypeVarToKind : WeakTypeVar >-> Kind.
38 Definition weakVarToCoreVar (wv:WeakVar) : CoreVar :=
40 | WExprVar (weakExprVar v _ ) => v
41 | WTypeVar (weakTypeVar v _ ) => v
42 | WCoerVar (weakCoerVar v _ _ _) => v
44 Coercion weakVarToCoreVar : WeakVar >-> CoreVar.
46 Definition haskLiteralToWeakType lit : WeakType :=
47 WTyCon (haskLiteralToTyCon lit).
48 Coercion haskLiteralToWeakType : HaskLiteral >-> WeakType.
50 Variable coreVarToWeakVar : CoreVar -> WeakVar. Extract Inlined Constant coreVarToWeakVar => "coreVarToWeakVar".
51 Variable getTyConTyVars_ : CoreTyCon -> list CoreVar. Extract Inlined Constant getTyConTyVars_ => "getTyConTyVars".
52 Definition tyConTyVars (tc:CoreTyCon) :=
53 General.filter (map (fun x => match coreVarToWeakVar x with WTypeVar v => Some v | _ => None end) (getTyConTyVars_ tc)).
55 Definition tyConKind (tc:TyCon) : list Kind :=
56 map (fun (x:WeakTypeVar) => x:Kind) (tyConTyVars tc).
57 Variable tyFunResultKind : CoreTyCon -> Kind. Extract Inlined Constant tyFunResultKind => "tyFunResultKind".
58 Definition tyFunKind (tc:TyFun) : ((list Kind) * Kind) :=
59 ((map (fun (x:WeakTypeVar) => x:Kind) (tyConTyVars tc)) , (tyFunResultKind tc)).
62 (* EqDecidable instances for all of the above *)
63 Instance WeakCoerVarEqDecidable : EqDecidable WeakCoerVar.
64 apply Build_EqDecidable.
66 destruct v1 as [cv1 t1a t1b].
67 destruct v2 as [cv2 t2a t2b].
68 destruct (eqd_dec cv1 cv2); subst.
69 destruct (eqd_dec t1a t2a); subst.
70 destruct (eqd_dec t1b t2b); subst.
72 right; intro; apply n; inversion H; subst; auto.
73 right; intro; apply n; inversion H; subst; auto.
74 right; intro; apply n; inversion H; subst; auto.
77 Instance WeakExprVarEqDecidable : EqDecidable WeakExprVar.
78 apply Build_EqDecidable.
80 destruct v1 as [cv1 k1].
81 destruct v2 as [cv2 k2].
82 destruct (eqd_dec cv1 cv2); subst.
83 destruct (eqd_dec k1 k2); subst.
85 right; intro; apply n; inversion H; subst; auto.
86 right; intro; apply n; inversion H; subst; auto.
89 Instance WeakVarEqDecidable : EqDecidable WeakVar.
90 apply Build_EqDecidable.
91 induction v1; destruct v2; try (right; intro q; inversion q; fail) ; auto;
92 destruct (eqd_dec w w0); subst.
94 right; intro X; apply n; inversion X; auto.
96 right; intro X; apply n; inversion X; auto.
98 right; intro X; apply n; inversion X; auto.
102 Instance WeakVarToString : ToString WeakVar :=
103 { toString := fun x => toString (weakVarToCoreVar x) }.