Disjunctive Normal Form Formula - f#

Im about to use a set-based representation of formulas in disjunctive normal form. And I've found out that since conjunction is commutative, associative and (a ∧ a) is equivalent to a it is convenient to
represent a basic conjunct bc by its set of literals litOf(bc).
Im thinking about representing a disjunctive normal form formula a:
bc1 v . . . v bcn
by the set:
dnfToSet(a) = {litOf(bc1), . . . , litOf(bcn)}
that we will call the dns set of a.
How should i write F# declarations for the functions litOf and dnfToSet ?

I'm not sure I understand the question, but here is what I came up with:
let litOf = Set.ofSeq
let dnfToSet a =
let isNotSuperset bci = a |> Set.forall(fun bcj -> (bci = bcj) || not(Set.isSuperset bci bcj))
a |> Set.filter isNotSuperset
The following illustrates:
type bc = A | B | C | D
let bc1 = litOf [A; B; C]
let bc2 = litOf [B; C; B]
let bc3 = litOf [C; B; A]
let bc4 = litOf [D]
let a = litOf [ bc1; bc2; bc3; bc4 ]
let dnf = dnfToSet a
Putting it all into FSI yields:
type bc =
| A
| B
| C
| D
val bc1 : Set<bc> = set [A; B; C]
val bc2 : Set<bc> = set [B; C]
val bc3 : Set<bc> = set [A; B; C]
val bc4 : Set<bc> = set [D]
val a : Set<Set<bc>> = set [set [A; B; C]; set [B; C]; set [D]]
val dnf : Set<Set<bc>> = set [set [B; C]; set [D]]
((B ∧ C) V (D))
Finally, for the record, here are functions I used to print the formula:
let sprintlit lit =
System.String.Join(" ∧ ", lit |> Seq.map(sprintf "%A") |> Seq.toArray)
|> sprintf "(%s)"
let sprintdnf set =
System.String.Join(" V ", set |> Seq.map sprintlit |> Seq.toArray)
|> sprintf "(%s)"

Related

How to extract discriminated union types from a list without specifying the constructor?

Let's say I have a discriminated union consisting of three cases. Case A and C each takes a constructor to type X and Y respectively. I have a list consisting of different DU types and I want to filter that list down to a single DU type. Currently, I have a list consisting each of A, B and C. Now, if I want to filter the DU list only to type case A, how can I do that without having to pass the constructor to case A? (or pass a default constructor, I don't know how to do that either)
type X = {
Id: string
Name: string
}
type Y = {
Id: int
}
type DU =
| A of a:X
| B
| C of b:Y
let extractDUTypesFromList (listDU: List<DU>) (typ: DU) : List<DU> =
listDU
|> List.filter (fun m -> m = typ)
let a = (A {Id = "1"; Name = "Test"})
let aa = (A {Id = "2"; Name = "Test 2"})
let b = B
let c = (C {Id = 1})
let listDU: List<DU> = [a; b; c; aa]
let filteredDUList: List<DU> = // this list will only contain case A
extractDUTypesFromList listDU (A _) // doesn't work
There's no way to make such a filter generically. What I would do is
let filteredDUList =
listDU |> List.filter (function A _ -> true | _ -> false)
If you want to extract all the Xs, you can do the following instead:
listDU |> List.choose (function A x -> Some(x) | _ -> None)
In order to filter like that we need the opposite of the DU constructor, which is an active recognizer.
Unfortunately you'll have to create them by hand, although I did a suggestion to have the F# compiler derive them automatically, this is a good example of why such suggestion matters.
// Active recognizers (ideally autogenerated)
let (|A|_|) = function | A x -> Some x | _ -> None
let (|B|_|) = function | B -> Some () | _ -> None
let (|C|_|) = function | C x -> Some x | _ -> None
let inline extractDUTypesFromList (listDU: List<DU>) (typ: DU -> Option<'t>) : List<DU> =
listDU
|> List.choose (fun x -> typ x |> Option.map (fun _ -> x))
let a = (A {Id = "1"; Name = "Test"})
let aa = (A {Id = "2"; Name = "Test 2"})
let b = B
let c = (C {Id = 1})
let listDU: List<DU> = [a; b; c; aa]
let filteredDUList: List<DU> = // this list will only contain case A
extractDUTypesFromList listDU (|A|_|)
results in
val filteredDUList : List<DU> = [A { Id = "1"
Name = "Test" }; A { Id = "2"
Name = "Test 2" }]
No need to say that you can make normal functions instead of active recognizers, since in this usage alone we're not using pattern matching at all, I mean you can name the function tryA as suggested, instead of (|A|_|).
another version: in contrast to #brianberns solution (which i think is a good one) this does not use reflection. it requires the creation of dummy values to define the filter criteria as in the op.
this and all other solutions are not really nice f# code, there should be a better way for what you want to accomplish.
type X = {
Id: string
Name: string
}
with static member Empty = { Id=""; Name="" }
type Y = {
Id: int
}
with static member Empty = { Id=0 }
type DU =
| A of a:X
| B
| C of b:Y
with
static member IsSameCase a b =
match a, b with
| A _, A _ -> true
| B, B -> true
| C _, C _ -> true
| _ -> false
let extractDUTypesFromList (listDU: List<DU>) (case: DU) : List<DU> =
listDU
|> List.filter (DU.IsSameCase case)
let a = (A {Id = "1"; Name = "Test"})
let aa = (A {Id = "2"; Name = "Test 2"})
let b = B
let c = (C {Id = 1})
let listDU: List<DU> = [a; b; c; aa]
let filteredDUList: List<DU> = // this list will only contain case A
extractDUTypesFromList listDU ((A (X.Empty)))
extractDUTypesFromList listDU ((A (X.Empty)))
extractDUTypesFromList listDU B
extractDUTypesFromList listDU (C (Y.Empty))
#torbonde's suggestion is good if you know the union case you want to filter by at compile time, but if you want a general solution that works for any union case, I think you'll need F# reflection:
open FSharp.Reflection
let extractDUTypesFromList (listDU: List<DU>) (unionCaseName : string) : List<DU> =
listDU
|> List.filter (fun m ->
let unionCase, _ = FSharpValue.GetUnionFields(m, typeof<DU>)
unionCase.Name = unionCaseName)
let filteredDUList: List<DU> = // this list will only contain case A
extractDUTypesFromList listDU "A"
Note that you'll pay a runtime cost and lose some of the type-checking benefits of the compiler (e.g. the code will silently break if case A's name is subsequently modified), but it will do what you want.

How Reflection MakeUnion three level type?

I want to try to reflect all types of combinations,
I am using a recursive function
Working at two level
But it won't work at the third level.
open Microsoft.FSharp.Reflection
let rec getAll<'A> (c : UnionCaseInfo) : obj [] =
match c.GetFields() |> List.ofSeq with
| [ x ] when FSharpType.IsUnion x.PropertyType ->
FSharpType.GetUnionCases(x.PropertyType)
|> Array.map (fun uc ->
FSharpValue.MakeUnion(c, getAll(uc)))
|> Array.ofSeq
| _ ->
[| FSharpValue.MakeUnion(c, Array.empty) |]
type C = | C1 | C2
//type B = | B1 | B2
type B = | B1 of C | B2
type A =
| A1
| A2toB of B
| A3
static member GetAll =
FSharpType.GetUnionCases(typeof<A>)
|> Seq.collect getAll<A>
|> Seq.cast<A>
|> Array.ofSeq
(A2toB (B1 C1)).ToString() |> printfn "%A"
A.GetAll |> Array.map (fun t -> t.ToString() |> printfn "%A")
"A2toB (B1 C1)"
Unhandled Exception: System.Reflection.TargetParameterCountException: Parameter count mismatch.
when only use two levels
type B = | B1 | B2
Correct return
"A1"
"A2toB B1"
"A2toB B2"
"A3"
The reason you're getting the exception is that when you call getAll in the recursive case for B1, the field type is C, and C has two cases, C1 | C2, so you get back an array of two elements. Then, that array is passed to the MakeUnion call for B1, which expects only one element (a single instance of C). The call fails because there's an unexpected extra C passed in the array.
You can make this work for your example case by adding something like Array.take 1 to your recursive call to getAll, but it won't work in the general case. I'm not entirely sure what you're trying to accomplish, so providing a general solution is currently a little tricky. If you can clarify your requirements, we can probably provide a better solution.
Here's a version that works for your specific example (though as I said, this is not a good general solution):
let rec getAll<'A> (c : UnionCaseInfo) : obj [] =
match c.GetFields() |> List.ofSeq with
| [ x ] when FSharpType.IsUnion x.PropertyType ->
FSharpType.GetUnionCases(x.PropertyType)
|> Array.map (fun uc ->
FSharpValue.MakeUnion(c, getAll(uc) |> Array.take 1))
|> Array.ofSeq
| _ ->
[| FSharpValue.MakeUnion(c, Array.empty) |]
Here's the output:
"A1"
"A2toB (B1 C1)"
"A2toB B2"
"A3"
thanks Aaron M. Eshbach for found my recursive error, I fix my code
let rec getAll<'A> (c: UnionCaseInfo): obj [] =
match c.GetFields() |> List.ofSeq with
| [ x ] when FSharpType.IsUnion x.PropertyType ->
FSharpType.GetUnionCases(x.PropertyType)
|> Array.map (fun uc ->
let t = uc.Name
getAll (uc) |> Array.map (fun a ->
FSharpValue.MakeUnion(c, [| a |]))
)
|> Array.concat
|> Array.ofSeq
| _ ->
let t = c.Name
[| FSharpValue.MakeUnion(c, Array.empty) |]
I think your code can be simplified. Let us reduce the level of nesting by one; utilize an array sequence expression for generation; and also, let's recurse on System.Type instead of on the unwieldy UnionCaseInfo.
The type parameter, removed below, could have been used at run-time only for unboxing the outermost union type. The type of the other generated cases is necessarily obj, also demonstrating the somewhat limited utility of dynamically generated union cases.
let rec getCases t = [|
for ucinfo in FSharpType.GetUnionCases t do
match ucinfo.GetFields() with
| [|pinfo|] when FSharpType.IsUnion pinfo.PropertyType ->
for x in getCases pinfo.PropertyType ->
FSharpValue.MakeUnion(ucinfo, [|x|])
| _ -> yield FSharpValue.MakeUnion(ucinfo, [||]) |]
// val getCases : t:System.Type -> obj []
type A = A1 | A2toB of B | A3
and B = B1 of C | B2
and C = C1 | C2
getCases typeof<A>
// val it : obj [] = [|A1; A2toB (B1 C1); A2toB (B1 C2); A2toB B2; A3|]

F# - How to conveniently apply elements in a list to a curried function's parameters?

Supposed there is a list:
let lst = [1;2;3]
And a curried function:
let addAll a b c =
a + b + c
How can I input the parameters for the curried function coveniently using the elements in list lst?
One way of doing this is:
addAll (lst |> List.item 0) (lst |> List.item 1) (lst |> List.item 2)
But this doesn't scale very well! Also, it's boring.
It is hard to say from the limited example what your actual use case is. Lists are designed to contain a varying number of items and functions take constant number of items, so the two do not match well. It might make more sense to use a tuple rather than a list:
let tup = (1,2,3)
let addAll (a, b, c) =
a + b + c
addAll tup
Tuples contain fixed number of items, but they can be easily constructed and deconstructed and allow you to pass all parameters to your function at once.
You can also do what you asked about using reflection, but this may break in future versions of F# and it is almost never a good design for a simple case like this. It is also slow and as you can see from the number of downcasts and boxing, it is also not very safe:
let lst = [1;2;3]
let addAll a b c =
a + b + c
let addAllVal = addAll
let f = addAllVal.GetType().GetMethod("Invoke", [| typeof<int>; typeof<int>; typeof<int> |])
let res = f.Invoke(addAllVal, Array.map box (Array.ofList lst)) :?> int
Another option is to use pattern matching:
let lst = [1;2;3]
match lst with [ a ; b; c] -> addAll a b c |_-> 0
returns 6.
If lst does not have exactly 3 elements then it returns 0 but you can change it to handle other cases:
let callAddAll lst =
match lst with
| [ ] -> 0
| [ a ] -> addAll a 0 0
| [ a ; b ] -> addAll a b 0
| [ a ; b ; c ] -> addAll a b c
| a :: b :: c :: rest -> addAll a b c // ignore rest
[ ] |> callAddAll |> printfn "lst = %d" // = 0
[1 ] |> callAddAll |> printfn "lst = %d" // = 1
[1;2 ] |> callAddAll |> printfn "lst = %d" // = 3
[1;2;3 ] |> callAddAll |> printfn "lst = %d" // = 6
[1;2;3;4] |> callAddAll |> printfn "lst = %d" // = 6

get polynom representation for lagrangian interpolation

How can I represent an incomplete mathematical function?
I need to do something like (x - constant) then
(x - constant)*(x - another) => (x^2 - x * constant - x * another + constant * another)
and so on.
I'm trying to make a program to do Lagrangian interpolation (finding a function for some points)
so I need to make a function that I can see (print, or something), from a set of known values.
sorry if confusing.
In case you want to implement the Lagrange Interpolation as discussed here
getting a function that interpolates values:
then this is the direct translation into F#:
let LagrangeInterpol (points : (Double*Double)[]) x =
let indizes = [0..points.Length-1]
let p j =
indizes
|> List.map (fun k ->
if k <> j
then (x - fst points.[k])
/ (fst points.[j] - fst points.[k])
else 1.0)
|> List.fold (*) 1.0
indizes |> List.sumBy (fun j -> p j * snd points.[j])
examples
Here is a simple test-session:
> let points = [|0.0,0.0; 1.0,2.0; 2.0,3.0|];;
val points : (float * float) [] = [|(0.0, 0.0); (1.0, 2.0); (2.0, 3.0)|]
> let f = LagrangeInterpol points;;
val f : (Double -> float)
> f 0.0;;
val it : float = 0.0
> f 1.0;;
val it : float = 2.0
> f 2.0;;
val it : float = 3.0
So I hope I did not make any major mistake.
Please note that I made no efford to do any performance optimizations here - this should be sufficent to draw a graph or get a few values in between.
getting a representation of the polynom
This is a bit more trickier - you can either try to come up with the combinatorical formulas for the coefficients or (like me here) be mathematical lazy and just implement a Polynom-Type with just enough operators:
type Polynom =
Poly of float list with
override p.ToString () =
match p with
| Poly coefs ->
System.String.Join (" + ", coefs |> List.mapi (fun i c -> sprintf "%AX^%d" c i))
static member Const c = Poly [c]
static member Zero = Polynom.Const 0.0
static member One = Polynom.Const 1.0
static member X = Poly [0.0; 1.0]
static member (+) (Poly cs1, Poly cs2) =
let m = max (List.length cs1) (List.length cs2)
List.zip (ofLen m cs1) (ofLen m cs2)
|> List.map (fun (a,b) -> a+b)
|> Poly
static member (-) (Poly cs1, Poly cs2) =
let m = max (List.length cs1) (List.length cs2)
List.zip (ofLen m cs1) (ofLen m cs2)
|> List.map (fun (a,b) -> a-b)
|> Poly
static member (*) (f : float, Poly cs2) : Polynom =
cs2
|> List.map (fun c -> f * c)
|> Poly
static member private shift n (Poly cs) =
List.replicate n 0.0 # cs |> Poly
static member (*) (Poly cs1, p2 : Polynom) : Polynom =
cs1
|> List.mapi (fun i c -> Polynom.shift i (c * p2))
|> List.sum
static member (/) (Poly cs1, f : float) : Polynom =
cs1
|> List.map (fun c -> c / f)
|> Poly
Here I just use a list of floats to represent the coefficients of a polynom (so X^2 + 2X + 3 is Poly [3.0; 2.0; 1.0] note that the ith coefficient is the one at X^i.
Having this we can use almost the same function as before:
let getPolynom (points : (float * float)[]) =
let indizes = [0..points.Length-1]
let p j =
indizes
|> List.map (fun k ->
if k <> j
then (Polynom.X - Polynom.Const (fst points.[k]))
/ (fst points.[j] - fst points.[k])
else Polynom.One)
|> List.fold (*) Polynom.One
indizes |> List.sumBy (fun j -> Polynom.Const (snd points.[j]) * p j)
As you can see I used the same function and only replaces the argument x with Polynom.X and wrapped the constants approbiatley.
examples
and here are two examples (compare them to the Wiki-Page they should be right):
> LagrangeInterpolation.getPolynom
[|(1.0, 1.0); (2.0, 4.0); (3.0, 9.0)|] |> string;;
val it : string = "0.0X^0 + 0.0X^1 + 1.0X^2"
> LagrangeInterpolation.getPolynom
[| 1.0,1.0; 2.0,8.0; 3.0,27.0 |] |> string;;
val it : string = "6.0X^0 + -11.0X^1 + 6.0X^2"
complete code with helpers
the complete code for this inside a module is:
module LagrangeInterpolation =
let private ofLen n cs =
let l = List.length cs
if l < n
then cs # List.replicate (n-l) 0.0
else cs
type Polynom =
Poly of float list with
override p.ToString () =
match p with
| Poly coefs ->
System.String.Join (" + ", coefs |> List.mapi (fun i c -> sprintf "%AX^%d" c i))
static member Const c = Poly [c]
static member Zero = Polynom.Const 0.0
static member One = Polynom.Const 1.0
static member X = Poly [0.0; 1.0]
static member (+) (Poly cs1, Poly cs2) =
let m = max (List.length cs1) (List.length cs2)
List.zip (ofLen m cs1) (ofLen m cs2)
|> List.map (fun (a,b) -> a+b)
|> Poly
static member (-) (Poly cs1, Poly cs2) =
let m = max (List.length cs1) (List.length cs2)
List.zip (ofLen m cs1) (ofLen m cs2)
|> List.map (fun (a,b) -> a-b)
|> Poly
static member (*) (f : float, Poly cs2) : Polynom =
cs2
|> List.map (fun c -> f * c)
|> Poly
static member private shift n (Poly cs) =
List.replicate n 0.0 # cs |> Poly
static member (*) (Poly cs1, p2 : Polynom) : Polynom =
cs1
|> List.mapi (fun i c -> Polynom.shift i (c * p2))
|> List.sum
static member (/) (Poly cs1, f : float) : Polynom =
cs1
|> List.map (fun c -> c / f)
|> Poly
let getPolynom (points : (float * float)[]) =
let indizes = [0..points.Length-1]
let p j =
indizes
|> List.map (fun k ->
if k <> j
then (Polynom.X - Polynom.Const (fst points.[k]))
/ (fst points.[j] - fst points.[k])
else Polynom.One)
|> List.fold (*) Polynom.One
indizes |> List.sumBy (fun j -> Polynom.Const (snd points.[j]) * p j)
remarks
For better output you should probably add some simplifications (for example Poly [1.0;0.0] -> Poly [1.0]) and improve the ToString method but I'm sure you can handle ;)
If you mean a function that is partial, i.e. it is undefined on some of its inputs, then there are generally two ways to deal with this. One option is to use option<'T> type and wrap the correct result in Some or return None when the value is undefined. For example:
let safeDivide a b =
if b = 0 then None else Some(a / b)
The caller than has to pattern match on the result (or use something like the Maybe computation builder) which makes calling the function harder, but you have full control over how the error is handled.
The other option is to throw an exception. This happens automatically for integer division, but you could write something like this:
let safeDivide a b =
if b = 0 then invalidArg "b" "Division by zero!"
a / b
This is a bit easier to write, but you need to be aware of the behavior and handle the exceptions correctly.

How to retrieve the head and tail of a tuple in F#

How do I retrieve the head and tail of a tuple in F#?
For example Conj (a, b), the head is Conj, tail is (a, b).
I want to recursively run buildtree function on each parameters, put the head as Node's element, where is the map in F#?
let rec getparams = map List.head (List.tail getparams);
type Elem = Prop
type Tree = E | T of Elem * Tree * Tree
let rec buildtree vars = function
| E = head vars
| T = buildtree (getparams vars)
After updated:
open System
open Microsoft.FSharp.Reflection
// Learn more about F# at http://fsharp.net
//type Prop = {a: string; b: string}
//let Prop a b = (a, b)
type Op = Prop
type tree = E | T of Op * tree * tree
let tree x y z = (x, y, z)
type binOp = Conj | Disj | Impl
type expr =
| Prop of string
| BinOp of binOp * expr * expr
| Conj of expr * expr
| Disj of expr * expr
| Impl of expr * expr
type Prop = {a: string}
let Prop a = (a)
//type Conj = {a : Prop; b : Prop}
let Conj a b = (a, b)
//type Conj_Int = {a : Prop; b : Prop}
let Conj_Int a b = Conj a b
//type Conj_Elmin1 = {a : Conj}
let Conj_Elmin1 a = fst a
//type Conj_Elmin2 = {a : Conj}
let Conj_Elmin2 a = snd a
//type Impl = {a : Prop; b : Prop}
let Impl a b = (a b)
//type Impl_Int = {assume : Prop; b : Prop}
let Impl_Int assume b = Impl assume b
//type Impl_Elmin = {a :string; b : Impl}
let Impl_Elmin a b = if a = fst b then snd b
type Neg = {a : Prop;}
let Neg a = (a)
//type Double_Neg_Int = {a : Prop;}
let Double_Neg_Int a = Neg(Neg(a))
//type Double_Neg_Elmin = {a : Prop}
let Double_Neg_Elmin a = fst(fst(a))
//type Disj = {a : Prop; b : Prop}
let Disj a b = (a,b)
//type Disj_Int1 = {a : Prop; b : Prop}
let Disj_Int1 a b = (a b)
//type Disj_Int2 = {a : Prop; b : Prop}
let Disj_Int2 a b = (a b)
//type Disj_Elmin1 = {a : Disj}
let Disj_Elmin1 a = fst(a)
//type Disj_Elmin2 = {a : Disj}
let Disj_Elmin2 a = snd(a)
type TupleSplitter = static member splitTuple (a,b,c) = (a,(b,c))
let tupleToList t = if Microsoft.FSharp.Reflection.FSharpType.IsTuple(t.GetType()) then Some (Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields t |> Array.toList) else None
let operation x = List.head(List.ofSeq(FSharpValue.GetTupleFields(x)))
let parameters x = List.tail(List.ofSeq(FSharpValue.GetTupleFields(x)))
let rec map f = function | Prop _ as t -> f t | BinOp(op, a, b) -> f(BinOp(op, map f a, map f b))
(*
let rec map f = function
| Prop _ as t -> f t | Conj(a, b) -> f(Conj(map f a, map f b))
| Disj(a, b) -> f(Disj(map f a, map f b))
| Impl(a, b) -> f(Impl(map f a, map f b))
*)
let buildtree vars expr = map (function Prop v -> Map.find v vars | expr -> expr) expr
let t = buildtree(Conj("a","b"))
how to have two type of expression Op*Tree*Tree and Op*Tree?
As Ankur said, you can't get head and tail of a tuple - these operations are designed for processing functional lists that have arbitrary length and cannot be define for tuples that have a length known at compile time. If you want data with arbitrary length, you should probably use tuples and pattern matching (or List.head and List.tail).
If you really need to process tuples dynamically, you can use F# reflection:
open Microsoft.FSharp.Reflection
(1,2,3)
|> FSharpValue.GetTupleFields // Get fields of tuple as an array
|> List.ofSeq // Convert array to a list
|> List.tail // Now you can process list using head/tail
However, note that reflection is generally a bit slow and it should only be used when you need it (i.e. when writing some code that is dynamic and can't be written in any other way).
You seem to be trying to replicate Haskell syntax and semantics in F#. Don't do that. Look at existing ML code and learn how to solve your problem idiomatically. In other words, your question is an XY problem: you're asking the wrong question.
Without knowing what problem you are trying to solve, it is difficult to answer your question but my best guess is:
type Expr =
| Prop of string
| Conj of Expr * Expr
| Disj of Expr * Expr
| Impl of Expr * Expr
let deConj = function
| Conj(a, b) -> a, b
| _ -> invalidArg "expr" "deConj"
Perhaps you want to write a map over your expr type:
let rec map f = function
| Prop _ as t -> f t
| Conj(a, b) -> f(Conj(map f a, map f b))
| Disj(a, b) -> f(Disj(map f a, map f b))
| Impl(a, b) -> f(Impl(map f a, map f b))
Another solution is to rewrite your type to factor out the operators:
type binOp = Conj | Disj | Impl
type expr =
| Prop of string
| BinOp of binOp * expr * expr
let rec map f = function
| Prop _ as t -> f t
| BinOp(op, a, b) -> f(BinOp(op, map f a, map f b))
EDIT
I am not sure what your buildtree function is supposed to do but if it is evaluating expressions then perhaps you want something like this:
let buildtree vars expr =
map (function Proj v -> Map.find v vars | expr -> expr) expr
This will map one expression to another, replacing Proj v with the corresponding expression (i.e. value of the variable v) given by vars.
A tuple is defined as (exp1,exp2, ... ,expn) for example (1,"2",'3').
I can't see this pattern in your code.
If you use (exp1 exp2) it means function application (apply exp2 as first argument to function exp1).
The error you see on your code is because you defined Conj as a function accepting a function as first paramenter and you passed a string ("a") instead of a function.
If your question is how to split a tuple in head and tail you can go for the dynamic approach Tomas just explained, it will work for any n-tuple but you'll lose type information.
Otherwise the strong type solution is simply based on pattern matching:
let splitTuple (a,b,c) = (a,(b,c))
// Usage
let (head,tail) = splitTuple (1,"2",'3')
And if you want to make it work for n-tuples you'll have to define one overload for each n:
type TupleSplitter =
static member splitTuple (a,b,c) = (a,(b,c))
static member splitTuple (a,b,c,d) = (a,(b,c,d))
static member splitTuple (a,b,c,d,e) = (a,(b,c,d,e))
// ... more overloads, as much as you need
// Usage
let (head,tail) = TupleSplitter.splitTuple (1,"2",'3',4.0)
// val tail : string * char * float = ("2", '3', 4.0)
// val head : int = 1

Resources